Skip to content
This repository has been archived by the owner on Aug 29, 2024. It is now read-only.

Commit

Permalink
Updated code and podspec for Swift 4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtSabintsev committed Mar 29, 2018
2 parents e43256e + 1d62c5c commit bbe650e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

![Swift Support](https://img.shields.io/badge/Swift-3.1%2C%203.2%2C%204.0-orange.svg)

[![Platform](https://img.shields.io/badge/Platforms-iOS%20%7c%20tvOS%20%7c%20watchOS-lightgray.svg?style=flat)](http://cocoadocs.org/docsets/Zephyr)
[![Platform](https://img.shields.io/badge/Platforms-iOS%20%7c%20tvOS-lightgray.svg?style=flat)](http://cocoadocs.org/docsets/Zephyr)

[![CocoaPods](https://img.shields.io/cocoapods/v/Zephyr.svg)]() [![Carthage Compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)]() [![SwiftPM Compatible](https://img.shields.io/badge/SwiftPM-Compatible-brightgreen.svg)](https://swift.org/package-manager/) [![CocoaPods](https://img.shields.io/cocoapods/dt/Zephyr.svg)](https://cocoapods.org/pods/Zephyr) [![CocoaPods](https://img.shields.io/cocoapods/dm/Zephyr.svg)](https://cocoapods.org/pods/Zephyr)
---
Expand Down
61 changes: 46 additions & 15 deletions Sources/Zephyr.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,17 @@ public class Zephyr: NSObject {

/// A session-persisted variable to directly access all of the NSUserDefaults elements.
private var zephyrLocalStoreDictionary: [String: Any] {
return UserDefaults.standard.dictionaryRepresentation()
return userDefaults.dictionaryRepresentation()
}

/// A session-persisted variable to directly access all of the NSUbiquitousKeyValueStore elements.
private var zephyrRemoteStoreDictionary: [String: Any] {
return NSUbiquitousKeyValueStore.default.dictionaryRepresentation
}

// The `UserDefaults` object to sync with `NSUbiquitousKeyValueStore/iCloud`.
private var userDefaults: UserDefaults = UserDefaults.standard

/// Zephyr's initialization method.
///
/// Do not call this method directly.
Expand All @@ -72,7 +75,7 @@ public class Zephyr: NSObject {
deinit {
zephyrQueue.sync {
for key in registeredObservationKeys {
UserDefaults.standard.removeObserver(self, forKeyPath: key)
userDefaults.removeObserver(self, forKeyPath: key)
}
}
}
Expand All @@ -86,6 +89,7 @@ public class Zephyr: NSObject {
/// - Parameters:
/// - keys: If you pass a one or more keys, only those key will be synchronized. If no keys are passed, than all NSUserDefaults will be synchronized with NSUbiquitousKeyValueStore.
public static func sync(keys: String...) {

if !keys.isEmpty {
sync(keys: keys)
return
Expand All @@ -111,9 +115,10 @@ public class Zephyr: NSObject {
///
/// This method will synchronize an array of keys between NSUserDefaults and NSUbiquitousKeyValueStore.
///
/// - Parameters:
/// - Parameters:
/// - keys: An array of keys that should be synchronized between NSUserDefaults and NSUbiquitousKeyValueStore.
public static func sync(keys: [String]) {

switch shared.dataStoreWithLatestData() {
case .local:
printGeneralSyncStatus(finished: false, destination: .remote)
Expand All @@ -130,6 +135,32 @@ public class Zephyr: NSObject {
}
}

/// Overloaded version of Zephyr's synchronization method, **sync(keys:)**.
///
/// If a custom UserDefaults object is passed in, Zephyr will synchronize that rather than UserDefaults.standard
///
/// - Parameters:
/// - userDefaults: The UserDefaults object that should be synchronized with UbiquitousKeyValueStore.default
/// default value is UserDefaults.standard
/// - keys: If you pass a one or more keys, only those key will be synchronized. If no keys are passed, than all NSUserDefaults will be synchronized with NSUbiquitousKeyValueStore.
public static func sync(keys: String..., userDefaults: UserDefaults = UserDefaults.standard) {
shared.userDefaults = userDefaults
sync(keys: keys)
}

/// Overloaded version of Zephyr's synchronization method, **sync(keys:)**.
///
/// If a custom UserDefaults object is passed in, Zephyr will synchronize that rather than UserDefaults.standard
///
/// - Parameters:
/// - userDefaults: The UserDefaults object that should be synchronized with UbiquitousKeyValueStore.default
/// default value is UserDefaults.standard
/// - keys: An array of keys that should be synchronized between NSUserDefaults and NSUbiquitousKeyValueStore.
public static func sync(keys: [String], userDefaults: UserDefaults = UserDefaults.standard) {
shared.userDefaults = userDefaults
sync(keys: keys)
}

/// Add specific keys to be monitored in the background. Monitored keys will automatically
/// be synchronized between both data stores whenever a change is detected
///
Expand Down Expand Up @@ -178,7 +209,7 @@ public class Zephyr: NSObject {
///
/// Remove specific keys from being monitored in the background.
///
/// - Parameters:
/// - Parameters:
/// - keys: Pass one or more keys that you would like to stop monitoring.
public static func removeKeysFromBeingMonitored(keys: String...) {
removeKeysFromBeingMonitored(keys: keys)
Expand Down Expand Up @@ -227,8 +258,8 @@ private extension Zephyr {

private extension Zephyr {
/// Synchronizes specific keys to/from NSUbiquitousKeyValueStore and NSUserDefaults.
///
/// - Parameters:
///
/// - Parameters:
/// - keys: Array of keys to synchronize.
/// - dataStore: Signifies if keys should be synchronized to/from iCloud.
func syncSpecificKeys(keys: [String], dataStore: ZephyrDataStore) {
Expand All @@ -247,8 +278,8 @@ private extension Zephyr {
/// Synchronizes all NSUserDefaults to NSUbiquitousKeyValueStore.
///
/// If a key is passed, only that key will be synchronized.
///
/// - Parameters:
///
/// - Parameters:
/// - key: If you pass a key, only that key will be updated in NSUbiquitousKeyValueStore.
/// - value: The value that will be synchronized. Must be passed with a key, otherwise, nothing will happen.
func syncToCloud(key: String? = nil, value: Any? = nil) {
Expand Down Expand Up @@ -295,7 +326,7 @@ private extension Zephyr {
/// - key: If you pass a key, only that key will updated in NSUserDefaults.
/// - value: The value that will be synchronized. Must be passed with a key, otherwise, nothing will happen.
func syncFromCloud(key: String? = nil, value: Any? = nil) {
let defaults = UserDefaults.standard
let defaults = userDefaults
defaults.set(Date(), forKey: ZephyrSyncKey)

// Sync all defaults from iCloud if key is nil, otherwise sync only the specific key/value pair.
Expand Down Expand Up @@ -331,7 +362,7 @@ extension Zephyr {

/// Adds key-value observation after synchronization of a specific key.
///
/// - Parameters:
/// - Parameters:
/// - key: The key that should be added and monitored.
private func registerObserver(key: String) {
if key == ZephyrSyncKey {
Expand All @@ -340,7 +371,7 @@ extension Zephyr {

if !registeredObservationKeys.contains(key) {

UserDefaults.standard.addObserver(self, forKeyPath: key, options: .new, context: nil)
userDefaults.addObserver(self, forKeyPath: key, options: .new, context: nil)
registeredObservationKeys.append(key)

}
Expand All @@ -359,7 +390,7 @@ extension Zephyr {

if let index = registeredObservationKeys.index(of: key) {

UserDefaults.standard.removeObserver(self, forKeyPath: key, context: nil)
userDefaults.removeObserver(self, forKeyPath: key, context: nil)
registeredObservationKeys.remove(at: index)

}
Expand All @@ -376,7 +407,7 @@ extension Zephyr {
zephyrQueue.async {
if self.registeredObservationKeys.contains(keyPath) {
if object is UserDefaults {
UserDefaults.standard.set(Date(), forKey: self.ZephyrSyncKey)
self.userDefaults.set(Date(), forKey: self.ZephyrSyncKey)
}

self.syncSpecificKeys(keys: [keyPath], dataStore: .local)
Expand Down Expand Up @@ -458,7 +489,7 @@ private extension Zephyr {

/// Prints the subscription state for a specific key if debugEnabled == true
///
/// - Parameters:
/// - Parameters:
/// - key: The key being synchronized.
/// - subscribed: The subscription status of the key.
static func printObservationStatus(key: String, subscribed: Bool) {
Expand All @@ -473,7 +504,7 @@ private extension Zephyr {

/// Prints a status to the console if
///
/// - Parameters:
/// - Parameters:
/// - debugEnabled == true
/// - status: The string that should be printed to the console.
static func printStatus(status: String) {
Expand Down
26 changes: 14 additions & 12 deletions Zephyr.podspec
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
Pod::Spec.new do |s|
s.name = "Zephyr"
s.version = "2.2.4"
s.summary = "Effortlessly synchronize UserDefaults over iCloud"
s.swift_version = "4.1"
# Version
s.version = "3.1.0"
s.swift_version = '4.1'

# Meta
s.name = "Zephyr"
s.summary = "Effortlessly synchronize UserDefaults over iCloud."
s.homepage = "https://github.com/ArtSabintsev/Zephyr"
s.license = "MIT"
s.authors = { "Arthur Ariel Sabintsev" => "[email protected]"}
s.description = <<-DESC
Effortlessly synchronize UserDefaults over iCloud.
Effortlessly synchronize your UserDefaults over iCloud.
DESC

s.ios.deployment_target = '9.0'
s.tvos.deployment_target = '9.0'
s.watchos.deployment_target = '3.0'
# Deployment
s.ios.deployment_target = '9.0'
s.tvos.deployment_target = '9.0'

s.homepage = "https://github.com/ArtSabintsev/Zephyr"
s.license = "MIT"
s.authors = { "Arthur Ariel Sabintsev" => "[email protected]"}
s.platform = :ios, "8.0"
# Sources
s.source = { :git => "https://github.com/ArtSabintsev/Zephyr.git", :tag => s.version.to_s }
s.source_files = 'Sources/*.swift'
s.requires_arc = true
Expand Down

0 comments on commit bbe650e

Please sign in to comment.