Skip to content

Commit 324f9fe

Browse files
committed
Added URL session. Added sorts. Added transforms.
1 parent c7dc54f commit 324f9fe

File tree

5 files changed

+123
-95
lines changed

5 files changed

+123
-95
lines changed

Diff for: Sources/SwiftBoost/CoreGraphics/Extensions/CGAffineTransformExtension.swift

+12
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,17 @@ public extension CGAffineTransform {
66
init(scale: CGFloat) {
77
self.init(scaleX: scale, y: scale)
88
}
9+
10+
init(rotationDegress: CGFloat) {
11+
self.init(rotationAngle: CGFloat(rotationDegress * .pi / 180))
12+
}
13+
14+
func scaledBy(xy: CGFloat) -> CGAffineTransform {
15+
return self.scaledBy(x: xy, y: xy)
16+
}
17+
18+
func rotated(degress: CGFloat) -> CGAffineTransform {
19+
return self.rotated(by: CGFloat(degress * .pi / 180))
20+
}
921
}
1022
#endif

Diff for: Sources/SwiftBoost/Foundation/Extensions/File.swift

-95
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import Foundation
2+
3+
public extension MutableCollection where Self: RandomAccessCollection {
4+
5+
func sorted<Value>(
6+
by keyPath: KeyPath<Element, Value>,
7+
using valuesAreInIncreasingOrder: (Value, Value) throws -> Bool
8+
) rethrows -> [Element] {
9+
try sorted {
10+
try valuesAreInIncreasingOrder($0[keyPath: keyPath], $1[keyPath: keyPath])
11+
}
12+
}
13+
14+
func sorted<Value: Comparable>(
15+
by keyPath: KeyPath<Element, Value>,
16+
order: MutableCollectionOrder<Value>
17+
) -> [Element] {
18+
sorted(by: keyPath, using: order.operator)
19+
}
20+
}
21+
22+
public enum MutableCollectionOrder<Value: Comparable> {
23+
24+
// MARK: - Cases
25+
26+
/// Represents ascending order. In this case, the associated operator function is `<`.
27+
case ascending
28+
/// Represents descending order. In this case, the associated operator function is `>`.
29+
case descending
30+
31+
// MARK: - Properties
32+
33+
public var `operator`: (Value, Value) -> Bool {
34+
switch self {
35+
case .ascending:
36+
return (<)
37+
case .descending:
38+
return (>)
39+
}
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import Foundation
2+
3+
public extension URL {
4+
5+
static var empty: URL {
6+
return URL(string: "https://apple.com")!
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import Foundation
2+
3+
public extension URLSession {
4+
5+
enum AppError: Error {
6+
7+
case invalidURL(String)
8+
case networkError(Error)
9+
case noResponse
10+
case decodingError(Error)
11+
12+
public func errorMessage() -> String {
13+
switch self {
14+
case .invalidURL(let str):
15+
return "badURL: \(str)"
16+
case .networkError(let error):
17+
return "networkError: \(error)"
18+
case .noResponse:
19+
return "no network response"
20+
case .decodingError(let error):
21+
return "decoding error: \(error)"
22+
}
23+
}
24+
}
25+
26+
enum HTTPMethod {
27+
28+
case get
29+
30+
var id: String {
31+
switch self {
32+
case .get: return "get"
33+
}
34+
}
35+
}
36+
37+
static func request(
38+
url: String,
39+
method: HTTPMethod,
40+
completion: @escaping (AppError?, Data?, HTTPURLResponse?) ->Void)
41+
{
42+
guard let url = URL(string: url) else {
43+
completion(AppError.invalidURL(url), nil, nil)
44+
return
45+
}
46+
47+
var request = URLRequest(url: url)
48+
request.httpMethod = method.id
49+
URLSession.shared.dataTask(with: request) { (data, response, error) in
50+
guard let response = response as? HTTPURLResponse else {
51+
completion(AppError.noResponse, nil, nil)
52+
return
53+
}
54+
55+
if let error = error {
56+
completion(AppError.networkError(error), nil, response)
57+
} else if let data = data {
58+
completion(nil, data, response)
59+
}
60+
}.resume()
61+
}
62+
}

0 commit comments

Comments
 (0)