Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add iCloud syncing #229

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
### Next
* Added support for iCloud syncing via `Defaults.syncKeys([])`. [@StevenMagdy](https://github.com/StevenMagdy)

### 5.3.0 (2021-02-24)
* Renamed `OptionalType.empty` to `OptionalType.__swifty_empty`. Also removed the `OptionalType.wrapped` since it wasn't used in the framework anymore. Please note that this still shouldn't be something you rely on tho, we're gonna explore ways to remove the public `OptionalType` in a future releases. [@sunshinejr](https://github.com/sunshinejr)
Expand Down
49 changes: 49 additions & 0 deletions Sources/Defaults+Syncing.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// SwiftyUserDefaults
//
// Copyright (c) 2015-present Radosław Pietruszewski, Łukasz Mróz
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//

#if !os(Linux) && !os(watchOS)

public extension DefaultsAdapter {

func startSyncing(for keyPaths: PartialKeyPath<KeyStore>...) {
let rawKeys = keyPaths.map { keyStore[keyPath: $0] }.compactMap { $0 as? RawKeyRepresentable }.map { $0._key }
syncer.syncedKeys.formUnion(rawKeys)
}

func stopSyncing(for keyPaths: PartialKeyPath<KeyStore>...) {
let rawKeys = keyPaths.map { keyStore[keyPath: $0] }.compactMap { $0 as? RawKeyRepresentable }.map { $0._key }
syncer.syncedKeys.subtract(rawKeys)
}

func forceSync() {
syncer.forceSync()
}

func stopSyncingAll() {
syncer.syncedKeys = []
}

}

#endif
31 changes: 31 additions & 0 deletions Sources/Defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import Foundation

public var Defaults = DefaultsAdapter<DefaultsKeys>(defaults: .standard, keyStore: .init())

// MARK: - UserDefaults

public extension UserDefaults {

/// Returns `true` if `key` exists
Expand Down Expand Up @@ -80,3 +82,32 @@ internal extension UserDefaults {
}
}
}

#if !os(Linux) && !os(watchOS)

// MARK: - NSUbiquitousKeyValueStore

public extension NSUbiquitousKeyValueStore {

/// Returns `true` if `key` exists
func hasKey<T>(_ key: DefaultsKey<T>) -> Bool {
return object(forKey: key._key) != nil
}

/// Removes value for `key`
func remove<T>(_ key: DefaultsKey<T>) {
removeObject(forKey: key._key)
synchronize()
}

/// Removes all keys and values from iCloud
/// Use with caution!
func removeAll() {
for (key, _) in dictionaryRepresentation {
removeObject(forKey: key)
}
synchronize()
}
}

#endif
14 changes: 14 additions & 0 deletions Sources/DefaultsAdapter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,23 @@ public struct DefaultsAdapter<KeyStore: DefaultsKeyStore> {
public let defaults: UserDefaults
public let keyStore: KeyStore

#if !os(Linux) && !os(watchOS)
internal let syncer: DefaultsSyncer

internal init(defaults: UserDefaults, keyStore: KeyStore, remoteStore: RemoteStore) {
self.defaults = defaults
self.keyStore = keyStore
self.syncer = DefaultsSyncer(defaults: defaults, remoteStore: remoteStore)
}
#endif

public init(defaults: UserDefaults, keyStore: KeyStore) {
self.defaults = defaults
self.keyStore = keyStore

#if !os(Linux) && !os(watchOS)
self.syncer = DefaultsSyncer(defaults: defaults, remoteStore: NSUbiquitousKeyValueStore.default)
#endif
}

@available(*, unavailable)
Expand Down
6 changes: 5 additions & 1 deletion Sources/DefaultsKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@

import Foundation

internal protocol RawKeyRepresentable {
var _key: String { get }
}

// MARK: - Static keys

/// Specialize with value type
/// and pass key name to the initializer to create a key.
public struct DefaultsKey<ValueType: DefaultsSerializable> {
public struct DefaultsKey<ValueType: DefaultsSerializable>: RawKeyRepresentable {

public let _key: String
public let defaultValue: ValueType.T?
Expand Down
114 changes: 114 additions & 0 deletions Sources/DefaultsSyncer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
//
// SwiftyUserDefaults
//
// Copyright (c) 2015-present Radosław Pietruszewski, Łukasz Mróz
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#if !os(Linux) && !os(watchOS)

import Foundation

internal protocol RemoteStore {
var dictionaryRepresentation: [String: Any] { get }

func object(forKey defaultName: String) -> Any?
func set(_ anObject: Any?, forKey aKey: String)
@discardableResult func synchronize() -> Bool
}

extension NSUbiquitousKeyValueStore: RemoteStore {
}

internal class DefaultsSyncer {

private let defaults: UserDefaults
private let remoteStore: RemoteStore

var syncedKeys = Set<String>()

init(defaults: UserDefaults, remoteStore: RemoteStore) {
self.defaults = defaults
self.remoteStore = remoteStore

NotificationCenter.default.addSafeObserver(self,
selector: #selector(iCloudDefaultsDidUpdate),
name: NSUbiquitousKeyValueStore.didChangeExternallyNotification)
NotificationCenter.default.addSafeObserver(self,
selector: #selector(localDefaultsDidUpdate),
name: UserDefaults.didChangeNotification)
}

internal func forceSync() {
iCloudDefaultsDidUpdate()
localDefaultsDidUpdate()
}

@objc
private func iCloudDefaultsDidUpdate() {
guard !syncedKeys.isEmpty else { return }

// HouseKeeping
NotificationCenter.default.removeObserver(self,
name: UserDefaults.didChangeNotification,
object: nil)
defer {
NotificationCenter.default.addSafeObserver(self,
selector: #selector(localDefaultsDidUpdate),
name: UserDefaults.didChangeNotification)
}

// Implementation
let allICloudKeys = Set(remoteStore.dictionaryRepresentation.keys)
let updatedSyncedKeys = allICloudKeys.filter { syncedKeys.contains($0) }
updatedSyncedKeys.forEach { key in
let iCloudValue = remoteStore.object(forKey: key)
defaults.set(iCloudValue, forKey: key)
}
}

@objc
private func localDefaultsDidUpdate() {
guard !syncedKeys.isEmpty else { return }
syncedKeys.forEach { key in
let localValue = defaults.object(forKey: key)
remoteStore.set(localValue, forKey: key)
}
// request upload to ICloud
remoteStore.synchronize()
}

deinit {
NotificationCenter.default.removeObserver(self,
name: UserDefaults.didChangeNotification,
object: nil)
NotificationCenter.default.removeObserver(self,
name: NSUbiquitousKeyValueStore.didChangeExternallyNotification,
object: nil)
}
}

private extension NotificationCenter {
func addSafeObserver(_ observer: Any, selector aSelector: Selector, name aName: NSNotification.Name, object anObject: Any? = nil) {
removeObserver(observer, name: aName, object: anObject)
addObserver(observer, selector: aSelector, name: aName, object: anObject)
}
}

#endif
10 changes: 10 additions & 0 deletions SwiftyUserDefaults.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
9FA427F1D84703CE8EC6CD38 /* Defaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE155E06F856DFEF78FD63F7 /* Defaults.swift */; };
A3CAE6734DDE8FD952CE2CF2 /* Defaults+Subscripts.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF92C9F82ABF7807D4D4DDDD /* Defaults+Subscripts.swift */; };
ACE14E08E7B3EECD55B932B3 /* Defaults+Dictionary.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3D8980553F1615D4ACB4AA43 /* Defaults+Dictionary.swift */; };
B01ABE9223E4798200E5E003 /* DefaultsSyncer.swift in Sources */ = {isa = PBXBuildFile; fileRef = B01ABE9123E4798200E5E003 /* DefaultsSyncer.swift */; };
B056341D23E3C5DD006D08EE /* Defaults+Syncing.swift in Sources */ = {isa = PBXBuildFile; fileRef = B056341C23E3C5DD006D08EE /* Defaults+Syncing.swift */; };
B31C4E974609611EF7360E51 /* Defaults+Double.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8790CE5B53F91DAD51975EDC /* Defaults+Double.swift */; };
C1EF5E679553F49B4C163561 /* SwiftyUserDefaults.h in Headers */ = {isa = PBXBuildFile; fileRef = B134969522555B348C06FD6C /* SwiftyUserDefaults.h */; settings = {ATTRIBUTES = (Public, ); }; };
CC4CF6A63F6AA1B932D6757E /* DefaultsBridges.swift in Sources */ = {isa = PBXBuildFile; fileRef = 20EBA39F1E25782FDC1CFC36 /* DefaultsBridges.swift */; };
Expand Down Expand Up @@ -69,6 +71,8 @@
9701991125BC1345631E831E /* DefaultsObserver.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = DefaultsObserver.swift; path = Sources/DefaultsObserver.swift; sourceTree = "<group>"; };
A574FF49CBCEBCCEAE9BD42E /* Defaults+String.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = "Defaults+String.swift"; path = "Tests/SwiftyUserDefaultsTests/Built-ins/Defaults+String.swift"; sourceTree = "<group>"; };
A5A5BEEC51B2A01157F9965F /* Defaults+BestFroggiesEnum.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = "Defaults+BestFroggiesEnum.swift"; path = "Tests/SwiftyUserDefaultsTests/External types/Defaults+BestFroggiesEnum.swift"; sourceTree = "<group>"; };
B01ABE9123E4798200E5E003 /* DefaultsSyncer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = DefaultsSyncer.swift; path = Sources/DefaultsSyncer.swift; sourceTree = "<group>"; };
B056341C23E3C5DD006D08EE /* Defaults+Syncing.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = "Defaults+Syncing.swift"; path = "Sources/Defaults+Syncing.swift"; sourceTree = "<group>"; };
B134969522555B348C06FD6C /* SwiftyUserDefaults.h */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.h; name = SwiftyUserDefaults.h; path = Sources/SwiftyUserDefaults.h; sourceTree = "<group>"; };
BF92C9F82ABF7807D4D4DDDD /* Defaults+Subscripts.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = "Defaults+Subscripts.swift"; path = "Sources/Defaults+Subscripts.swift"; sourceTree = "<group>"; };
CB55DC75BF69CEA7DB03750E /* BuiltIns.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = BuiltIns.swift; path = Sources/BuiltIns.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -131,6 +135,8 @@
85096B65E07A3BC7F856F0D1 /* OptionalType.swift */,
858E15CF231FEC2F00DC1418 /* PropertyWrappers.swift */,
B134969522555B348C06FD6C /* SwiftyUserDefaults.h */,
B056341C23E3C5DD006D08EE /* Defaults+Syncing.swift */,
B01ABE9123E4798200E5E003 /* DefaultsSyncer.swift */,
);
name = Sources;
sourceTree = "<group>";
Expand Down Expand Up @@ -165,7 +171,9 @@
1A0E1BA11455E9E44F192EAA /* Sources */,
C14BA19AA073B0F4B6ED9A3D /* Tests */,
);
indentWidth = 4;
sourceTree = "<group>";
tabWidth = 4;
};
5BF9A4C7B374362259AAD1B0 /* Built-ins */ = {
isa = PBXGroup;
Expand Down Expand Up @@ -357,8 +365,10 @@
buildActionMask = 2147483647;
files = (
858E15D0231FEC2F00DC1418 /* PropertyWrappers.swift in Sources */,
B01ABE9223E4798200E5E003 /* DefaultsSyncer.swift in Sources */,
D102C1011A4314AFF773DCD5 /* Defaults+StringToBool.swift in Sources */,
037280C1C6C68BDB8E3759E6 /* DefaultsKey.swift in Sources */,
B056341D23E3C5DD006D08EE /* Defaults+Syncing.swift in Sources */,
CC4CF6A63F6AA1B932D6757E /* DefaultsBridges.swift in Sources */,
ED39909122B0204E0046F502 /* DefaultsKeys.swift in Sources */,
A3CAE6734DDE8FD952CE2CF2 /* Defaults+Subscripts.swift in Sources */,
Expand Down