Skip to content

Commit

Permalink
More comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
borut-t committed Nov 13, 2024
1 parent 6118828 commit 71e97db
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 12 deletions.
18 changes: 15 additions & 3 deletions Sources/Core/Extensions/Foundation/Collection+PovioKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,27 @@ import Foundation

public extension Collection {
/// Returns the element at the specified `index` if it is within bounds, otherwise `nil`.
subscript (safe index: Index) -> Element? {
/// Returns the element at the specified `index` if it is within bounds, otherwise `nil`.
///
/// This subscript safely accesses an element in the collection at the given index. If the index is out of bounds, it returns `nil` instead of causing a runtime error.
///
/// - Parameter index: The index of the element to retrieve.
/// - Returns: The element at the specified index if within bounds, otherwise `nil`.
///
/// ## Example:
/// ```swift
/// let array = [1, 2, 3, 4]
/// let element = array[safe: 2] // returns 3
/// let outOfBounds = array[safe: 10] // returns nil
/// ```
subscript(safe index: Index) -> Element? {
if startIndex <= index && index < endIndex { self[index] } else { nil }
}
}

public extension Collection {
/// Returns the count of elements in the collection that satisfy the given predicate.
///
/// https://forums.swift.org/t/refresh-review-se-0220-count-where/66235/4
/// Check for [Reference](https://forums.swift.org/t/refresh-review-se-0220-count-where/66235/4).
///
/// This method applies the provided `predicate` closure to each element in the collection and counts how many elements satisfy the condition.
///
Expand Down
43 changes: 39 additions & 4 deletions Sources/Core/Extensions/Foundation/Data+PovioKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,53 @@
import Foundation

public extension Data {
/// Returns hexadecimal string representation of data object
/// Usefull for e.g. printing out push notifications `deviceToken`
/// Returns a hexadecimal string representation of the data object.
///
/// This property converts each byte of the data object into a two-character hexadecimal string. It is useful for tasks such as displaying raw binary data (e.g., a device token for push notifications) in a human-readable format.
///
/// Useful for printing out device tokens, hashing results, or any scenario where raw data needs to be displayed as a hexadecimal string.
///
/// - Returns: A string containing the hexadecimal representation of the data.
///
/// ## Example
/// ```swift
/// let data = Data([0x01, 0xAB, 0xFF])
/// Logger.debug(data.hexadecimalString) // "01abff"
/// ```
var hexadecialString: String {
map { String(format: "%02x", $0) }.joined()
}

/// Returns a UTF-8 encoded string.
/// Returns a UTF-8 encoded string from the data object.
///
/// This property attempts to convert the `Data` object into a string using UTF-8 encoding. If the data is not valid UTF-8, it returns `nil`.
///
/// - Returns: A UTF-8 encoded string if the conversion is successful, otherwise `nil`.
///
/// ## Example
/// ```swift
/// let data = Data([0x48, 0x65, 0x6c, 0x6c, 0x6f]) // "Hello" in UTF-8
/// if let string = data.utf8 {
/// Logger.debug(string) // "Hello"
/// }
/// ```
var utf8: String? {
String(data: self, encoding: .utf8)
}

/// Returns a UTF-16 encoded string.
/// Returns a UTF-16 encoded string from the data object.
///
/// This property attempts to convert the `Data` object into a string using UTF-16 encoding. If the data is not valid UTF-16, it returns `nil`.
///
/// - Returns: A UTF-16 encoded string if the conversion is successful, otherwise `nil`.
///
/// - Example:
/// ```swift
/// let data = Data([0x00, 0x48, 0x00, 0x65, 0x00, 0x6c, 0x00, 0x6c, 0x00, 0x6f]) // "Hello" in UTF-16
/// if let string = data.utf16 {
/// Logger.debug(string) // "Hello"
/// }
/// ```
var utf16: String? {
String(data: self, encoding: .utf16)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import StoreKit
public extension SKStoreReviewController {
/// Request a review popup on the current scene.
///
/// Example: `SKStoreReviewController.requestReviewInCurrentScene()`
/// ## Example
/// ```swift
/// SKStoreReviewController.requestReviewInCurrentScene()
/// ```
static func requestReviewInCurrentScene() {
if #available(iOS 14.0, *) {
(UIApplication
Expand Down
12 changes: 12 additions & 0 deletions Sources/Core/Extensions/UIKit/CGSize+PovioKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@
import UIKit

public extension CGSize {
/// Initializes a `CGSize` with equal width and height.
///
/// This initializer creates a `CGSize` instance where both the width and height are set to the provided value. It can be useful for creating square sizes.
///
/// - Parameter size: The value to set for both the width and height of the `CGSize`.
/// - Returns: A `CGSize` instance with both dimensions equal to the specified `size`.
///
/// ## Example
/// ```swift
/// let squareSize = CGSize(size: 50)
/// Logger.debug(squareSize) // (50.0, 50.0)
/// ```
init(size: CGFloat) {
self.init(width: size, height: size)
}
Expand Down
9 changes: 9 additions & 0 deletions Sources/Core/Extensions/UIKit/UIColor+PovioKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
import UIKit

public extension UIColor {
/// Initializes a `UIColor` using integer values for red, green, and blue components.
///
/// This convenience initializer allows you to create a `UIColor` using integer values for the RGB components, where each component is expected to be in the range 0 to 255. It also allows an optional alpha component (opacity), with a default value of 1 (fully opaque).
///
/// - Parameters:
/// - red: The red component of the color, ranging from 0 to 255.
/// - green: The green component of the color, ranging from 0 to 255.
/// - blue: The blue component of the color, ranging from 0 to 255.
/// - alpha: The alpha (opacity) of the color, ranging from 0 (fully transparent) to 1 (fully opaque). Defaults to 1.
convenience init(red: Int, green: Int, blue: Int, alpha: CGFloat = 1) {
self.init(red: CGFloat(red) / 255, green: CGFloat(green) / 255, blue: CGFloat(blue) / 255, alpha: alpha)
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/Core/Extensions/UIKit/UIResponder+PovioKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ public extension UIResponder {
/// Finds and returns first `UIResponder` up in the chain of given `type`.
///
/// If you want to quickly find certain view type in the view hierachy:
/// ```
///
/// ## Example
/// ```swift
/// MainViewController
/// --> ContentView
/// --> ButtonsContainerView
///
/// let firstResponder = buttonsContainerView.firstResponder(ofType: MainViewController)
/// ```
///
func firstResponder<T: AnyObject>(ofType type: T.Type) -> T? {
var current: UIResponder? = self
while current != nil {
Expand All @@ -32,5 +33,4 @@ public extension UIResponder {
return nil
}
}

#endif
1 change: 0 additions & 1 deletion Sources/Core/Extensions/UIKit/UIWindow+PovioKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,4 @@ public extension UIWindow {
return topViewController(with: rootViewController)
}
}

#endif

0 comments on commit 71e97db

Please sign in to comment.