Skip to content

Commit 0d2534b

Browse files
committed
2 parents 38dff7c + ab4c053 commit 0d2534b

File tree

9 files changed

+195
-218
lines changed

9 files changed

+195
-218
lines changed

Sources/FZSwiftUtils/Extensions/Collections/Dictionary+.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ public extension Dictionary {
2525
- Note: The collection of transformed keys must not contain duplicates.
2626
*/
2727
func mapKeys<Transformed>(_ transform: (Key) throws -> Transformed) rethrows -> [Transformed: Value] {
28-
try .init(
29-
uniqueKeysWithValues: map { try (transform($0.key), $0.value) }
30-
)
28+
try .init(uniqueKeysWithValues: map { try (transform($0.key), $0.value) })
3129
}
3230

3331
/**

Sources/FZSwiftUtils/Extensions/Networking/URLComponents+.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ extension URLComponents {
6767
return self
6868
}
6969

70+
/// Sets the query items for the URL in the order in which they appear in the original query string.
71+
@discardableResult
72+
public mutating func queryItems(_ items: [String:String?]) -> Self {
73+
self.queryItems = items.map({ URLQueryItem(name: $0, value: $1) })
74+
return self
75+
}
76+
7077
/// Sets the query items for the URL in the order in which they appear in the original query string.
7178
@discardableResult
7279
public mutating func queryItems(@Builder _ items: () -> [URLQueryItem]) -> Self {
@@ -154,6 +161,10 @@ extension URLComponents {
154161
public static func buildExpression(_ expression: URLQueryItem) -> [URLQueryItem] {
155162
[expression]
156163
}
164+
165+
public static func buildExpression(_ expression: [String : String]) -> [URLQueryItem] {
166+
expression.map({ URLQueryItem(name: $0, value: $1) })
167+
}
157168

158169
public static func buildExpression(_ expression: [URLQueryItem]) -> [URLQueryItem] {
159170
expression

Sources/FZSwiftUtils/Extensions/Networking/URLRequest+.swift

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -114,25 +114,9 @@ public extension URLRequest {
114114
}
115115

116116
/// A dictionary containing all of the HTTP header fields for a request.
117-
var allHTTPHeaderFieldsMapped: [HTTPRequestHeaderFieldKey: String] {
118-
get {
119-
guard let allHTTPHeaderFields = allHTTPHeaderFields else { return [:] }
120-
var mapped: [HTTPRequestHeaderFieldKey: String] = [:]
121-
for key in allHTTPHeaderFields.keys.compactMap({ HTTPRequestHeaderFieldKey(rawValue: $0) }) {
122-
mapped[key] = allHTTPHeaderFields[key.rawValue]
123-
}
124-
return mapped
125-
}
126-
set {
127-
guard !newValue.isEmpty else {
128-
allHTTPHeaderFields = nil
129-
return
130-
}
131-
allHTTPHeaderFields = [:]
132-
for key in newValue.keys {
133-
allHTTPHeaderFields?[key.rawValue] = newValue[key]
134-
}
135-
}
117+
var httpHeaderFields: [HTTPRequestHeaderFieldKey: String] {
118+
get { allHTTPHeaderFields?.mapKeys({ HTTPRequestHeaderFieldKey(rawValue: $0) }) ?? [:] }
119+
set { allHTTPHeaderFields = newValue.mapKeys({$0.rawValue}) }
136120
}
137121
}
138122

@@ -171,13 +155,9 @@ public enum HTTPRequestHeaderFieldKey: Hashable, CaseIterable, RawRepresentable,
171155
case via
172156
case warning
173157
case custom(String)
174-
158+
175159
public init(stringLiteral value: String) {
176-
if let first = Self.allCases.first(where: { $0.rawValue == value }) {
177-
self = first
178-
} else {
179-
self = .custom(value)
180-
}
160+
self = .init(rawValue: value)
181161
}
182162

183163
public init(rawValue: String) {

Sources/FZSwiftUtils/Extensions/Networking/URLSessionConfiguration+.swift

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,13 @@ import Foundation
99

1010
public extension URLSessionConfiguration {
1111
/// A dictionary of additional headers to send with requests.
12-
var allHTTPHeaderFieldsMapped: [HTTPRequestHeaderFieldKey: Any] {
12+
var httpAdditionalHeadersMapped: [HTTPRequestHeaderFieldKey: Any] {
1313
get {
14-
guard let allHTTPHeaderFields = httpAdditionalHeaders else { return [:] }
15-
var mapped: [HTTPRequestHeaderFieldKey: Any] = [:]
16-
for value in allHTTPHeaderFields {
17-
if let rawValue = value.key as? String {
18-
let key = HTTPRequestHeaderFieldKey(rawValue: rawValue)
19-
mapped[key] = allHTTPHeaderFields[value.key]
20-
}
21-
}
22-
return mapped
23-
}
24-
set {
25-
guard !newValue.isEmpty else {
26-
httpAdditionalHeaders = nil
27-
return
28-
}
29-
httpAdditionalHeaders = [:]
30-
for key in newValue.keys {
31-
httpAdditionalHeaders?[key.rawValue] = newValue[key]
32-
}
14+
httpAdditionalHeaders?.reduce(into: [HTTPRequestHeaderFieldKey:Any](), { partialResult, value in
15+
guard let key = value.key as? String else { return }
16+
partialResult[HTTPRequestHeaderFieldKey(rawValue: key)] = value.value
17+
}) ?? [:]
3318
}
19+
set { httpAdditionalHeaders = newValue.mapKeys({ $0.rawValue as AnyHashable }) }
3420
}
3521
}

Sources/FZSwiftUtils/Utils/AsyncOperation.swift

Lines changed: 27 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,18 @@ open class AsyncOperation: Operation, Pausable {
1313
public enum State: String, Hashable {
1414
/// The operation is waiting to start.
1515
case waiting = "isWaiting"
16-
1716
/// The operation is ready to start.
1817
case ready = "isReady"
19-
2018
/// The operation is executing.
2119
case executing = "isExecuting"
22-
2320
/// The operation is finished.
2421
case finished = "isFinished"
25-
2622
/// The operation is cancelled.
2723
case cancelled = "isCancelled"
28-
2924
/// The operation is paused.
3025
case paused = "isPaused"
3126
}
3227

33-
/// The handler that gets called when the state changes.
34-
open var onStateChange: ((State) -> Void)?
35-
3628
/// The error, if the operation failed.
3729
open var error: Error?
3830

@@ -45,96 +37,69 @@ open class AsyncOperation: Operation, Pausable {
4537
willChangeValue(forKey: State.cancelled.rawValue)
4638
}
4739
didSet {
48-
switch self.state {
40+
switch state {
4941
case .waiting:
50-
assert(oldValue == .waiting, "Invalid change from \(oldValue) to \(self.state)")
42+
assert(oldValue == .waiting, "Invalid change from \(oldValue) to \(state)")
5143
case .ready:
52-
assert(oldValue == .waiting, "Invalid change from \(oldValue) to \(self.state)")
44+
assert(oldValue == .waiting, "Invalid change from \(oldValue) to \(state)")
5345
case .executing:
54-
assert(
55-
oldValue == .ready || oldValue == .waiting || oldValue == .paused,
56-
"Invalid change from \(oldValue) to \(self.state)"
57-
)
46+
assert( oldValue == .ready || oldValue == .waiting || oldValue == .paused, "Invalid change from \(oldValue) to \(state)")
5847
case .finished:
59-
assert(oldValue != .cancelled, "Invalid change from \(oldValue) to \(self.state)")
48+
assert(oldValue != .cancelled, "Invalid change from \(oldValue) to \(state)")
6049
case .cancelled:
6150
break
6251
case .paused:
63-
assert(oldValue == .executing, "Invalid change from \(oldValue) to \(self.state)")
52+
assert(oldValue == .executing, "Invalid change from \(oldValue) to \(state)")
6453
}
65-
6654
didChangeValue(forKey: State.cancelled.rawValue)
6755
didChangeValue(forKey: State.finished.rawValue)
6856
didChangeValue(forKey: State.executing.rawValue)
6957
didChangeValue(forKey: State.ready.rawValue)
70-
if oldValue != self.state {
71-
self.onStateChange?(self.state)
72-
}
7358
}
7459
}
7560

7661
override open var isReady: Bool {
77-
if self.state == .waiting {
78-
return super.isReady
79-
} else {
80-
return self.state == .ready
81-
}
62+
state == .waiting ? super.isReady : state == .ready
8263
}
8364

8465
override open var isExecuting: Bool {
85-
if self.state == .waiting {
86-
return super.isExecuting
87-
} else {
88-
return self.state == .executing || self.state == .paused
89-
}
66+
state == .waiting ? super.isExecuting : state == .executing || state == .paused
9067
}
9168

9269
override open var isFinished: Bool {
93-
if self.state == .waiting {
94-
return super.isFinished
95-
} else {
96-
return self.state == .finished
97-
}
70+
state == .waiting ? super.isFinished : state == .finished
9871
}
9972

10073
override open var isCancelled: Bool {
101-
if self.state == .waiting {
102-
return super.isCancelled
103-
} else {
104-
return self.state == .cancelled
105-
}
74+
state == .waiting ? super.isCancelled : state == .cancelled
10675
}
10776

10877
/// A Boolean value indicating whether the operation has been paused.
10978
open var isPaused: Bool {
110-
self.state == .paused
79+
state == .paused
11180
}
11281

11382
/// Resumes the operation, if it's paused.
11483
open func resume() {
115-
if isExecuting, state == .paused {
116-
state = .executing
117-
}
84+
guard isExecuting, state == .paused else { return }
85+
state = .executing
11886
}
11987

12088
/// Pauses the operation.
12189
open func pause() {
122-
if isExecuting, state != .paused {
123-
state = .paused
124-
}
90+
guard isExecuting, state != .paused else { return }
91+
state = .paused
12592
}
12693

12794
/// Finishes executing the operation.
12895
open func finish() {
129-
if isExecuting {
130-
state = .finished
131-
}
96+
guard isExecuting else { return }
97+
state = .finished
13298
}
13399

134100
override open func cancel() {
135-
if isExecuting {
136-
state = .cancelled
137-
}
101+
guard isExecuting else { return }
102+
state = .cancelled
138103
}
139104

140105
override open var isAsynchronous: Bool {
@@ -159,7 +124,14 @@ open class AsyncBlockOperation: AsyncOperation {
159124

160125
override open func start() {
161126
super.start()
162-
guard isExecuting else { return }
127+
guard isExecuting, !isPaused else { return }
163128
closure(self)
129+
state = .finished
130+
}
131+
132+
open override func resume() {
133+
super.resume()
134+
guard isExecuting, !isPaused else { return }
135+
start()
164136
}
165137
}

0 commit comments

Comments
 (0)