This repository has been archived by the owner on Mar 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Added functionality to simplify the initialisation of the wrapper. #11
Open
pkclsoft
wants to merge
2
commits into
sdarlington:master
Choose a base branch
from
pkclsoft:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,37 @@ @implementation SDCloudUserDefaults | |
static NSString *suiteName; | ||
static NSUserDefaults *userDefaults; | ||
|
||
+ (void) setiCloudEnabled:(BOOL)iCloudEnabled { | ||
[[NSUserDefaults standardUserDefaults] setBool:iCloudEnabled forKey:ICLOUD_DATA_ENABLED_KEY]; | ||
[[NSUserDefaults standardUserDefaults] synchronize]; | ||
|
||
if (iCloudEnabled == YES) { | ||
[SDCloudUserDefaults registerForNotifications]; | ||
} else { | ||
[SDCloudUserDefaults removeNotifications]; | ||
} | ||
} | ||
|
||
+ (BOOL) isiCloudEnabled { | ||
return [[NSUserDefaults standardUserDefaults] boolForKey:ICLOUD_DATA_ENABLED_KEY]; | ||
} | ||
|
||
+ (void) initialize { | ||
[super initialize]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way that this can be implemented that doesn't require changing the the API for existing users? |
||
|
||
id keyValue = [SDCloudUserDefaults objectForKey:ICLOUD_DATA_ENABLED_KEY]; | ||
|
||
if (keyValue != nil) { | ||
BOOL iCloudEnabled = [SDCloudUserDefaults boolForKey:ICLOUD_DATA_ENABLED_KEY]; | ||
|
||
if (iCloudEnabled == YES) { | ||
[SDCloudUserDefaults registerForNotifications]; | ||
} else { | ||
[SDCloudUserDefaults removeNotifications]; | ||
} | ||
} | ||
} | ||
|
||
+(NSUserDefaults *) _standardUserDefaults { | ||
if (userDefaults == nil) { | ||
userDefaults = [NSUserDefaults standardUserDefaults]; | ||
|
@@ -62,13 +93,19 @@ +(BOOL)boolForKey:(NSString*)aKey { | |
} | ||
|
||
+(id)objectForKey:(NSString*)aKey { | ||
NSUbiquitousKeyValueStore* cloud = [NSUbiquitousKeyValueStore defaultStore]; | ||
id retv = [cloud objectForKey:aKey]; | ||
if (!retv) { | ||
retv = [[self _standardUserDefaults] objectForKey:aKey]; | ||
[cloud setObject:retv forKey:aKey]; | ||
if ([SDCloudUserDefaults isiCloudEnabled] == YES) { | ||
NSUbiquitousKeyValueStore* cloud = [NSUbiquitousKeyValueStore defaultStore]; | ||
id retv = [cloud objectForKey:aKey]; | ||
|
||
if (!retv) { | ||
retv = [[self _standardUserDefaults] objectForKey:aKey]; | ||
[cloud setObject:retv forKey:aKey]; | ||
} | ||
|
||
return retv; | ||
} else { | ||
return [[self _standardUserDefaults] objectForKey:aKey]; | ||
} | ||
return retv; | ||
} | ||
|
||
+(NSInteger)integerForKey:(NSString*)aKey { | ||
|
@@ -88,8 +125,13 @@ +(void)setBool:(BOOL)aBool forKey:(NSString*)aKey { | |
} | ||
|
||
+(void)setObject:(id)anObject forKey:(NSString*)aKey { | ||
[[NSUbiquitousKeyValueStore defaultStore] setObject:anObject forKey:aKey]; | ||
if ([SDCloudUserDefaults isiCloudEnabled] == YES) { | ||
[[NSUbiquitousKeyValueStore defaultStore] setObject:anObject forKey:aKey]; | ||
} | ||
|
||
[[self _standardUserDefaults] setObject:anObject forKey:aKey]; | ||
|
||
[SDCloudUserDefaults synchronize]; | ||
} | ||
|
||
+(void)setInteger:(NSInteger)anInteger forKey:(NSString*)aKey { | ||
|
@@ -103,12 +145,20 @@ +(void)setFloat:(float)aFloat forKey:(NSString *)aKey { | |
} | ||
|
||
+(void)removeObjectForKey:(NSString*)aKey { | ||
[[NSUbiquitousKeyValueStore defaultStore] removeObjectForKey:aKey]; | ||
if ([SDCloudUserDefaults isiCloudEnabled] == YES) { | ||
[[NSUbiquitousKeyValueStore defaultStore] removeObjectForKey:aKey]; | ||
} | ||
|
||
[[self _standardUserDefaults] removeObjectForKey:aKey]; | ||
|
||
[SDCloudUserDefaults synchronize]; | ||
} | ||
|
||
+(void)synchronize { | ||
[[NSUbiquitousKeyValueStore defaultStore] synchronize]; | ||
if ([SDCloudUserDefaults isiCloudEnabled] == YES) { | ||
[[NSUbiquitousKeyValueStore defaultStore] synchronize]; | ||
} | ||
|
||
[[self _standardUserDefaults] synchronize]; | ||
} | ||
|
||
|
@@ -118,36 +168,39 @@ +(void)registerForNotifications { | |
return; | ||
} | ||
|
||
notificationObserver = [[NSNotificationCenter defaultCenter] addObserverForName:@"NSUbiquitousKeyValueStoreDidChangeExternallyNotification" | ||
object:[NSUbiquitousKeyValueStore defaultStore] | ||
queue:nil | ||
usingBlock:^(NSNotification* notification) { | ||
|
||
NSDictionary* userInfo = [notification userInfo]; | ||
NSNumber* reasonForChange = [userInfo objectForKey:NSUbiquitousKeyValueStoreChangeReasonKey]; | ||
|
||
// If a reason could not be determined, do not update anything. | ||
if (!reasonForChange) | ||
return; | ||
|
||
// Update only for changes from the server. | ||
NSInteger reason = [reasonForChange integerValue]; | ||
if ((reason == NSUbiquitousKeyValueStoreServerChange) || | ||
(reason == NSUbiquitousKeyValueStoreInitialSyncChange)) { | ||
NSUserDefaults* defaults = [self _standardUserDefaults]; | ||
NSUbiquitousKeyValueStore* cloud = [NSUbiquitousKeyValueStore defaultStore]; | ||
NSArray* changedKeys = [userInfo objectForKey:NSUbiquitousKeyValueStoreChangedKeysKey]; | ||
for (NSString* key in changedKeys) { | ||
[defaults setObject:[cloud objectForKey:key] forKey:key]; | ||
|
||
[[NSNotificationCenter defaultCenter] postNotificationName:SDCloudValueUpdatedNotification | ||
object:self | ||
userInfo:@{key:[cloud objectForKey:key]}]; | ||
} | ||
|
||
|
||
} | ||
}]; | ||
notificationObserver = [[NSNotificationCenter defaultCenter] | ||
addObserverForName:NSUbiquitousKeyValueStoreDidChangeExternallyNotification | ||
object:[NSUbiquitousKeyValueStore defaultStore] | ||
queue:nil | ||
usingBlock:^(NSNotification* notification) { | ||
|
||
NSDictionary* userInfo = [notification userInfo]; | ||
NSNumber* reasonForChange = [userInfo objectForKey:NSUbiquitousKeyValueStoreChangeReasonKey]; | ||
|
||
// If a reason could not be determined, do not update anything. | ||
if (!reasonForChange) | ||
return; | ||
|
||
// Update only for changes from the server. | ||
NSInteger reason = [reasonForChange integerValue]; | ||
if ((reason == NSUbiquitousKeyValueStoreServerChange) || | ||
(reason == NSUbiquitousKeyValueStoreInitialSyncChange)) { | ||
|
||
NSUserDefaults* defaults = [self _standardUserDefaults]; | ||
NSUbiquitousKeyValueStore* cloud = [NSUbiquitousKeyValueStore defaultStore]; | ||
NSArray* changedKeys = [userInfo objectForKey:NSUbiquitousKeyValueStoreChangedKeysKey]; | ||
|
||
for (NSString* key in changedKeys) { | ||
[defaults setObject:[cloud objectForKey:key] forKey:key]; | ||
|
||
[[NSNotificationCenter defaultCenter] postNotificationName:SDCloudValueUpdatedNotification | ||
object:self | ||
userInfo:@{key:[cloud objectForKey:key]}]; | ||
} | ||
|
||
|
||
} | ||
}]; | ||
} | ||
|
||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer constants to be defined using the
extern NSString* const
syntax, so the actual value can be hidden in the .m file. (See here for an example.)