Skip to content

Commit 1a71a8f

Browse files
committed
Improve testability
1 parent 801999e commit 1a71a8f

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

Sources/DefaultsAdapter.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,20 @@ public struct DefaultsAdapter<KeyStore: DefaultsKeyStore> {
4848

4949
#if !os(Linux) && !os(watchOS)
5050
internal let syncer: DefaultsSyncer
51+
52+
internal init(defaults: UserDefaults, keyStore: KeyStore, iCloudStore: ICloudStore) {
53+
self.defaults = defaults
54+
self.keyStore = keyStore
55+
self.syncer = DefaultsSyncer(defaults: defaults, iCloudStore: iCloudStore)
56+
}
5157
#endif
5258

5359
public init(defaults: UserDefaults, keyStore: KeyStore) {
5460
self.defaults = defaults
5561
self.keyStore = keyStore
5662

5763
#if !os(Linux) && !os(watchOS)
58-
self.syncer = DefaultsSyncer(defaults: defaults)
64+
self.syncer = DefaultsSyncer(defaults: defaults, iCloudStore: NSUbiquitousKeyValueStore.default)
5965
#endif
6066
}
6167

Sources/DefaultsSyncer.swift

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,29 @@
2525

2626
import Foundation
2727

28+
internal protocol ICloudStore {
29+
var dictionaryRepresentation: [String : Any] { get }
30+
31+
func object(forKey defaultName: String) -> Any?
32+
func set(_ anObject: Any?, forKey aKey: String)
33+
@discardableResult func synchronize() -> Bool
34+
}
35+
36+
extension NSUbiquitousKeyValueStore: ICloudStore {
37+
}
38+
39+
2840
internal class DefaultsSyncer {
2941

30-
let defaults: UserDefaults
42+
private let defaults: UserDefaults
43+
private let iCloudStore: ICloudStore
3144

3245
var syncedKeys = Set<String>()
3346

34-
init(defaults: UserDefaults) {
47+
init(defaults: UserDefaults, iCloudStore: ICloudStore) {
3548
self.defaults = defaults
49+
self.iCloudStore = iCloudStore
50+
3651
NotificationCenter.default.addSafeObserver(self,
3752
selector: #selector(iCloudDefaultsDidUpdate),
3853
name: NSUbiquitousKeyValueStore.didChangeExternallyNotification)
@@ -61,10 +76,10 @@ internal class DefaultsSyncer {
6176
}
6277

6378
// Implementation
64-
let allICloudKeys = Set(NSUbiquitousKeyValueStore.default.dictionaryRepresentation.keys)
79+
let allICloudKeys = Set(iCloudStore.dictionaryRepresentation.keys)
6580
let updatedSyncedKeys = allICloudKeys.filter { syncedKeys.contains($0) }
6681
updatedSyncedKeys.forEach { key in
67-
let iCloudValue = NSUbiquitousKeyValueStore.default.object(forKey: key)
82+
let iCloudValue = iCloudStore.object(forKey: key)
6883
defaults.set(iCloudValue, forKey: key)
6984
}
7085
}
@@ -74,10 +89,10 @@ internal class DefaultsSyncer {
7489
guard !syncedKeys.isEmpty else { return }
7590
syncedKeys.forEach { key in
7691
let localValue = defaults.object(forKey: key)
77-
NSUbiquitousKeyValueStore.default.set(localValue, forKey: key)
92+
iCloudStore.set(localValue, forKey: key)
7893
}
7994
// request upload to ICloud
80-
NSUbiquitousKeyValueStore.default.synchronize()
95+
iCloudStore.synchronize()
8196
}
8297

8398
deinit {

0 commit comments

Comments
 (0)