Skip to content

Commit 9c4ff76

Browse files
committed
Extract Array, Math, NotificationCenter, and IncrementalDelay to WordPressShared
Move the Array and Math extensions and NotificationCenter.observeOnce into WordPressShared, and split IncrementalDelay out of Delay.swift (leaving the GCD DispatchDelayedAction/DelayStateWrapper helpers in the app). Relocate their tests into WordPressSharedTests and add the WordPressShared import to the remaining callers.
1 parent 5da83b5 commit 9c4ff76

17 files changed

Lines changed: 49 additions & 39 deletions

File tree

File renamed without changes.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import Foundation
2+
3+
/// Provides a sequence of incremental delays, repeating the last one
4+
/// indefinitely.
5+
///
6+
public struct IncrementalDelay<Element> {
7+
public var current: Element
8+
9+
private let delaySequence: AnySequence<Element>
10+
private var iterator: AnyIterator<Element>
11+
12+
public init(_ sequence: [Element]) {
13+
precondition(!sequence.isEmpty, "IncrementalDelay sequence can't be empty")
14+
delaySequence = sequence.repeatingLast()
15+
iterator = delaySequence.makeIterator()
16+
current = iterator.next()!
17+
}
18+
19+
public mutating func increment() {
20+
current = iterator.next()!
21+
}
22+
23+
public mutating func reset() {
24+
iterator = delaySequence.makeIterator()
25+
current = iterator.next()!
26+
}
27+
}

WordPress/Classes/Extensions/Math.swift renamed to Modules/Sources/WordPressShared/Utility/Math.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ extension Int {
1010
/// - Precondition: divisor must be > 0
1111
/// - returns: an Int rounded to the nearest integer that's a multiple of the argument.
1212
///
13-
func round(_ divisor: UInt) -> Int {
13+
public func round(_ divisor: UInt) -> Int {
1414
assert(divisor > 0)
1515
if self == 0 || divisor == 0 {
1616
return self
@@ -30,39 +30,39 @@ extension Comparable {
3030
/// - max if self > max
3131
/// - otherwise it returns self
3232
///
33-
func clamp(min minValue: Self, max maxValue: Self) -> Self {
33+
public func clamp(min minValue: Self, max maxValue: Self) -> Self {
3434
return Swift.min(Swift.max(self, minValue), maxValue)
3535
}
3636
}
3737

3838
extension CGSize {
39-
func clamp(min minValue: CGSize, max maxValue: CGSize) -> CGSize {
39+
public func clamp(min minValue: CGSize, max maxValue: CGSize) -> CGSize {
4040
let width = self.width.clamp(min: minValue.width, max: maxValue.width)
4141
let height = self.height.clamp(min: minValue.height, max: maxValue.height)
4242
return CGSize(width: width, height: height)
4343
}
4444

45-
func clamp(min minValue: CGFloat, max maxValue: CGFloat) -> CGSize {
45+
public func clamp(min minValue: CGFloat, max maxValue: CGFloat) -> CGSize {
4646
let minSize = CGSize(width: minValue, height: minValue)
4747
let maxSize = CGSize(width: maxValue, height: maxValue)
4848
return clamp(min: minSize, max: maxSize)
4949
}
5050

51-
func clamp(min minValue: Int, max maxValue: Int) -> CGSize {
51+
public func clamp(min minValue: Int, max maxValue: Int) -> CGSize {
5252
return clamp(min: CGFloat(minValue), max: CGFloat(maxValue))
5353
}
5454

55-
func scaled(by scale: CGFloat) -> CGSize {
55+
public func scaled(by scale: CGFloat) -> CGSize {
5656
CGSize(width: width * scale, height: height * scale)
5757
}
5858

59-
func rounded() -> CGSize {
59+
public func rounded() -> CGSize {
6060
CGSize(width: width.rounded(), height: height.rounded())
6161
}
6262
}
6363

6464
extension CGFloat {
65-
func zeroIfNaN() -> CGFloat {
65+
public func zeroIfNaN() -> CGFloat {
6666
return self.isNaN ? 0.0 : self
6767
}
6868
}

WordPress/Classes/Extensions/NotificationCenter+ObserveOnce.swift renamed to Modules/Sources/WordPressShared/Utility/NotificationCenter+ObserveOnce.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ extension NotificationCenter {
2626
///
2727
@discardableResult
2828
@objc
29-
func observeOnce(forName name: NSNotification.Name?, object: Any?, queue: OperationQueue?, using block: @escaping (Foundation.Notification) -> Swift.Void, filter: ((Foundation.Notification) -> Bool)? = nil) -> NSObjectProtocol {
29+
public func observeOnce(forName name: NSNotification.Name?, object: Any?, queue: OperationQueue?, using block: @escaping (Foundation.Notification) -> Swift.Void, filter: ((Foundation.Notification) -> Bool)? = nil) -> NSObjectProtocol {
3030
let oneTimeObserver = OneTimeObserver(action: block)
3131

3232
let observer = NotificationCenter.default.addObserver(

Tests/KeystoneTests/Tests/Extensions/ArrayTests.swift renamed to Modules/Tests/WordPressSharedTests/ArrayTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import XCTest
2-
import WordPress
2+
@testable import WordPressShared
33

44
class ArrayTests: XCTestCase {
55
func testRepeatLast() {

Tests/KeystoneTests/Tests/Utility/DelayTests.swift renamed to Modules/Tests/WordPressSharedTests/DelayTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import XCTest
2-
import WordPress
2+
@testable import WordPressShared
33

44
class DelayTests: XCTestCase {
55
func testIncrementalDelay() {

Tests/KeystoneTests/Tests/Extensions/MathTest.swift renamed to Modules/Tests/WordPressSharedTests/MathTest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import XCTest
2-
@testable import WordPress
2+
@testable import WordPressShared
33

44
class MathTest: XCTestCase {
55

Tests/KeystoneTests/Tests/Extensions/NotificationCenterObserveOnceTests.swift renamed to Modules/Tests/WordPressSharedTests/NotificationCenterObserveOnceTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import XCTest
2-
@testable import WordPress
2+
@testable import WordPressShared
33

44
private let counterKey = "counter"
55

Tests/KeystoneTests/Tests/Models/MediaImageServiceTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import XCTest
2+
import WordPressShared
23
import OHHTTPStubs
34
import OHHTTPStubsSwift
45
import AsyncImageKit

WordPress/Classes/Utility/Delay.swift

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Foundation
2+
import WordPressShared
23

34
/// A delayed action implemented using GCD
45
///
@@ -61,32 +62,6 @@ struct DispatchDelayedAction {
6162
}
6263
}
6364

64-
/// Provides a sequence of incremental delays, repeating the last one
65-
/// indefinitely.
66-
///
67-
public struct IncrementalDelay<Element> {
68-
public var current: Element
69-
70-
private let delaySequence: AnySequence<Element>
71-
private var iterator: AnyIterator<Element>
72-
73-
public init(_ sequence: [Element]) {
74-
precondition(!sequence.isEmpty, "IncrementalDelay sequence can't be empty")
75-
delaySequence = sequence.repeatingLast()
76-
iterator = delaySequence.makeIterator()
77-
current = iterator.next()!
78-
}
79-
80-
public mutating func increment() {
81-
current = iterator.next()!
82-
}
83-
84-
public mutating func reset() {
85-
iterator = delaySequence.makeIterator()
86-
current = iterator.next()!
87-
}
88-
}
89-
9065
/// A helper struct that encapsulates an IncrementalDelay and DispatchDelayedAction, keeping track of current retryAttempt.
9166
///
9267
struct DelayStateWrapper {

0 commit comments

Comments
 (0)