From 324f9feb4cd84cf6735af2f34afc4f40f2ca736b Mon Sep 17 00:00:00 2001 From: Ivan Vorobei Date: Fri, 5 Jan 2024 14:43:41 +0300 Subject: [PATCH] Added URL session. Added sorts. Added transforms. --- .../CGAffineTransformExtension.swift | 12 +++ .../Foundation/Extensions/File.swift | 95 ------------------- .../MutableCollectionExtension.swift | 41 ++++++++ .../Foundation/Extensions/URLExtension.swift | 8 ++ .../Extensions/URLSessionExtension.swift | 62 ++++++++++++ 5 files changed, 123 insertions(+), 95 deletions(-) delete mode 100644 Sources/SwiftBoost/Foundation/Extensions/File.swift create mode 100644 Sources/SwiftBoost/Foundation/Extensions/MutableCollectionExtension.swift create mode 100644 Sources/SwiftBoost/Foundation/Extensions/URLExtension.swift create mode 100644 Sources/SwiftBoost/Foundation/Extensions/URLSessionExtension.swift diff --git a/Sources/SwiftBoost/CoreGraphics/Extensions/CGAffineTransformExtension.swift b/Sources/SwiftBoost/CoreGraphics/Extensions/CGAffineTransformExtension.swift index 2d7caa3..25060b9 100644 --- a/Sources/SwiftBoost/CoreGraphics/Extensions/CGAffineTransformExtension.swift +++ b/Sources/SwiftBoost/CoreGraphics/Extensions/CGAffineTransformExtension.swift @@ -6,5 +6,17 @@ public extension CGAffineTransform { init(scale: CGFloat) { self.init(scaleX: scale, y: scale) } + + init(rotationDegress: CGFloat) { + self.init(rotationAngle: CGFloat(rotationDegress * .pi / 180)) + } + + func scaledBy(xy: CGFloat) -> CGAffineTransform { + return self.scaledBy(x: xy, y: xy) + } + + func rotated(degress: CGFloat) -> CGAffineTransform { + return self.rotated(by: CGFloat(degress * .pi / 180)) + } } #endif diff --git a/Sources/SwiftBoost/Foundation/Extensions/File.swift b/Sources/SwiftBoost/Foundation/Extensions/File.swift deleted file mode 100644 index a5ad91c..0000000 --- a/Sources/SwiftBoost/Foundation/Extensions/File.swift +++ /dev/null @@ -1,95 +0,0 @@ -import Foundation - -/// This extension provides additional sorting capabilities for mutable collections that also conform to `RandomAccessCollection`. -/// -/// These extension methods allow for easy sorting of collections where the elements have Comparable properties. -/// -/// Example: -/// -/// ```swift -/// struct Person { -/// let name: String -/// let age: Int -/// } -/// -/// var people = [ -/// Person(name: "Alice", age: 30), -/// Person(name: "Bob", age: 20), -/// Person(name: "Charlie", age: 25) -/// ] -/// -/// // Sort by name in ascending order. -/// let sortedByName = people.sorted(by: \.name, order: .ascending) -/// -/// // Sort by age in descending order. -/// let sortedByAge = people.sorted(by: \.age, order: .descending) -/// -/// // Custom sort: sort by name length. -/// let sortedByNameLength = people.sorted(by: \.name) { name1, name2 in -/// return name1.count < name2.count -/// } -/// ``` -/// -/// - Note: These functions use Swift's `keyPath` feature to determine which property to sort by. -public extension MutableCollection where Self: RandomAccessCollection { - - /// Sorts the elements of the collection in the order specified by a closure that compares elements by a key path. - /// - /// - Parameters: - /// - keyPath: A key path to a comparable value on which to sort the collection's elements. - /// - valuesAreInIncreasingOrder: A closure that takes two arguments and returns `true` if the first value should appear before the second in the sorted sequence, and `false` otherwise. - /// - Returns: An array of the collection's elements sorted by the provided key path and comparison closure. - func sorted( - by keyPath: KeyPath, - using valuesAreInIncreasingOrder: (Value, Value) throws -> Bool - ) rethrows -> [Element] { - try sorted { - try valuesAreInIncreasingOrder($0[keyPath: keyPath], $1[keyPath: keyPath]) - } - } - - /// Sorts the elements of the collection in the order specified by a `MutableCollectionOrder` instance and a key path. - /// - /// - Parameters: - /// - keyPath: A key path to a comparable value on which to sort the collection's elements. - /// - order: A `MutableCollectionOrder` instance specifying the order in which to sort the elements. - /// - Returns: An array of the collection's elements sorted by the provided key path and order. - func sorted( - by keyPath: KeyPath, - order: MutableCollectionOrder - ) -> [Element] { - sorted(by: keyPath, using: order.operator) - } -} - -// MARK: - Helper Utlity Type - -/// `MutableCollectionOrder` is an enumeration representing the ordering options for collections of `Comparable` values. -/// -/// This enum has two cases: `.ascending` and `.descending`, representing ascending and descending order respectively. -/// -/// - Note: -/// You can retrieve the associated comparison operator function for each case by accessing the `.operator` property. -/// This operator function can be used to compare `Value` instances in a way that corresponds to the specified order. -public enum MutableCollectionOrder { - - // MARK: - Cases - - /// Represents ascending order. In this case, the associated operator function is `<`. - case ascending - /// Represents descending order. In this case, the associated operator function is `>`. - case descending - - // MARK: - Properties - - /// Returns a comparison operator function corresponding to the order represented by the case. - /// For `.ascending`, this function is `<`. For `.descending`, it's `>`. - public var `operator`: (Value, Value) -> Bool { - switch self { - case .ascending: - return (<) - case .descending: - return (>) - } - } -} diff --git a/Sources/SwiftBoost/Foundation/Extensions/MutableCollectionExtension.swift b/Sources/SwiftBoost/Foundation/Extensions/MutableCollectionExtension.swift new file mode 100644 index 0000000..017479a --- /dev/null +++ b/Sources/SwiftBoost/Foundation/Extensions/MutableCollectionExtension.swift @@ -0,0 +1,41 @@ +import Foundation + +public extension MutableCollection where Self: RandomAccessCollection { + + func sorted( + by keyPath: KeyPath, + using valuesAreInIncreasingOrder: (Value, Value) throws -> Bool + ) rethrows -> [Element] { + try sorted { + try valuesAreInIncreasingOrder($0[keyPath: keyPath], $1[keyPath: keyPath]) + } + } + + func sorted( + by keyPath: KeyPath, + order: MutableCollectionOrder + ) -> [Element] { + sorted(by: keyPath, using: order.operator) + } +} + +public enum MutableCollectionOrder { + + // MARK: - Cases + + /// Represents ascending order. In this case, the associated operator function is `<`. + case ascending + /// Represents descending order. In this case, the associated operator function is `>`. + case descending + + // MARK: - Properties + + public var `operator`: (Value, Value) -> Bool { + switch self { + case .ascending: + return (<) + case .descending: + return (>) + } + } +} diff --git a/Sources/SwiftBoost/Foundation/Extensions/URLExtension.swift b/Sources/SwiftBoost/Foundation/Extensions/URLExtension.swift new file mode 100644 index 0000000..b413425 --- /dev/null +++ b/Sources/SwiftBoost/Foundation/Extensions/URLExtension.swift @@ -0,0 +1,8 @@ +import Foundation + +public extension URL { + + static var empty: URL { + return URL(string: "https://apple.com")! + } +} diff --git a/Sources/SwiftBoost/Foundation/Extensions/URLSessionExtension.swift b/Sources/SwiftBoost/Foundation/Extensions/URLSessionExtension.swift new file mode 100644 index 0000000..12fac9c --- /dev/null +++ b/Sources/SwiftBoost/Foundation/Extensions/URLSessionExtension.swift @@ -0,0 +1,62 @@ +import Foundation + +public extension URLSession { + + enum AppError: Error { + + case invalidURL(String) + case networkError(Error) + case noResponse + case decodingError(Error) + + public func errorMessage() -> String { + switch self { + case .invalidURL(let str): + return "badURL: \(str)" + case .networkError(let error): + return "networkError: \(error)" + case .noResponse: + return "no network response" + case .decodingError(let error): + return "decoding error: \(error)" + } + } + } + + enum HTTPMethod { + + case get + + var id: String { + switch self { + case .get: return "get" + } + } + } + + static func request( + url: String, + method: HTTPMethod, + completion: @escaping (AppError?, Data?, HTTPURLResponse?) ->Void) + { + guard let url = URL(string: url) else { + completion(AppError.invalidURL(url), nil, nil) + return + } + + var request = URLRequest(url: url) + request.httpMethod = method.id + URLSession.shared.dataTask(with: request) { (data, response, error) in + guard let response = response as? HTTPURLResponse else { + completion(AppError.noResponse, nil, nil) + return + } + + if let error = error { + completion(AppError.networkError(error), nil, response) + } else if let data = data { + completion(nil, data, response) + } + }.resume() + } +}