Skip to content

Commit 5a7fdf2

Browse files
committed
Improve testability
1 parent 801999e commit 5a7fdf2

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-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: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,28 @@
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+
2839
internal class DefaultsSyncer {
2940

30-
let defaults: UserDefaults
41+
private let defaults: UserDefaults
42+
private let iCloudStore: ICloudStore
3143

3244
var syncedKeys = Set<String>()
3345

34-
init(defaults: UserDefaults) {
46+
init(defaults: UserDefaults, iCloudStore: ICloudStore) {
3547
self.defaults = defaults
48+
self.iCloudStore = iCloudStore
49+
3650
NotificationCenter.default.addSafeObserver(self,
3751
selector: #selector(iCloudDefaultsDidUpdate),
3852
name: NSUbiquitousKeyValueStore.didChangeExternallyNotification)
@@ -61,10 +75,10 @@ internal class DefaultsSyncer {
6175
}
6276

6377
// Implementation
64-
let allICloudKeys = Set(NSUbiquitousKeyValueStore.default.dictionaryRepresentation.keys)
78+
let allICloudKeys = Set(iCloudStore.dictionaryRepresentation.keys)
6579
let updatedSyncedKeys = allICloudKeys.filter { syncedKeys.contains($0) }
6680
updatedSyncedKeys.forEach { key in
67-
let iCloudValue = NSUbiquitousKeyValueStore.default.object(forKey: key)
81+
let iCloudValue = iCloudStore.object(forKey: key)
6882
defaults.set(iCloudValue, forKey: key)
6983
}
7084
}
@@ -74,10 +88,10 @@ internal class DefaultsSyncer {
7488
guard !syncedKeys.isEmpty else { return }
7589
syncedKeys.forEach { key in
7690
let localValue = defaults.object(forKey: key)
77-
NSUbiquitousKeyValueStore.default.set(localValue, forKey: key)
91+
iCloudStore.set(localValue, forKey: key)
7892
}
7993
// request upload to ICloud
80-
NSUbiquitousKeyValueStore.default.synchronize()
94+
iCloudStore.synchronize()
8195
}
8296

8397
deinit {

0 commit comments

Comments
 (0)