Skip to content

Commit

Permalink
Add ability to use custom NSUserDefaults for DIDefaults
Browse files Browse the repository at this point in the history
  • Loading branch information
k06a committed May 29, 2016
1 parent 50709ba commit d6d43ef
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
2 changes: 1 addition & 1 deletion DeluxeInjection.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "DeluxeInjection"
s.version = "0.3.1"
s.version = "0.3.2"
s.summary = "Simplest Objective-C Dependency Injection (DI:syringe:) implementation ever"

s.description = <<-DESC
Expand Down
14 changes: 13 additions & 1 deletion DeluxeInjection/Classes/DIDefaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ NS_ASSUME_NONNULL_BEGIN
*/
typedef NSString * _Nullable (^DIDefaultsKeyBlock)(Class targetClass, NSString *propertyName, Class propertyClass, NSSet<Protocol *> *propertyProtocols);

/**
* Block to define custom NSUserDefaults to use
*
* @param targetClass Class to be injected/rejected
* @param propertyName Property name to be injected/rejected
* @param propertyClass Class of property to be injected/rejected, at least \c NSObject
* @param propertyProtocols Set of property protocols including all superprotocols
*
* @return NSUserDefaults instance for propertyName of \c targetClass or \c nil to use \c [NSUserDefaults \c standardUserDefaults]
*/
typedef NSUserDefaults * _Nullable (^DIUserDefaultsBlock)(Class targetClass, NSString *propertyName, Class propertyClass, NSSet<Protocol *> *propertyProtocols);

@interface DeluxeInjection (DIDefaults)

/**
Expand All @@ -64,7 +76,7 @@ typedef NSString * _Nullable (^DIDefaultsKeyBlock)(Class targetClass, NSString *
* Inject properties marked with \c <DIDefaults> and \c <DIDefaultsSync> protocol
* using NSUserDefaults access with custom key provided by block
*/
+ (void)injectDefaultsWithKey:(DIDefaultsKeyBlock)keyBlock;
+ (void)injectDefaultsWithKey:(DIDefaultsKeyBlock)keyBlock defaults:(DIUserDefaultsBlock)defaultsBlock;

/**
* Reject all injections marked explicitly with \c <DIDefaults> and \c <DIDefaultsSync> protocol.
Expand Down
30 changes: 21 additions & 9 deletions DeluxeInjection/Classes/DIDefaults.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,20 @@ @implementation DeluxeInjection (DIDefaults)

#pragma mark - Private

+ (void)injectDefaultsWithKey:(DIDefaultsKeyBlock)keyBlock forProtocol:(Protocol *)protocol withSync:(BOOL)withSync withArchive:(BOOL)withArchive {
+ (void)injectDefaultsWithKey:(DIDefaultsKeyBlock)keyBlock
defaults:(DIUserDefaultsBlock)defaultsBlock
forProtocol:(Protocol *)protocol
withSync:(BOOL)withSync
withArchive:(BOOL)withArchive {

[self inject:^NSArray *(Class targetClass, SEL getter, SEL setter, NSString *propertyName, Class propertyClass, NSSet<Protocol *> *propertyProtocols) {
NSString *key = keyBlock(targetClass, propertyName, propertyClass, propertyProtocols) ?: propertyName;
NSUserDefaults *defaults = defaultsBlock(targetClass, propertyName, propertyClass, propertyProtocols) ?: [NSUserDefaults standardUserDefaults];
return @[DIGetterMake(^id _Nullable(id _Nonnull target, id _Nullable __autoreleasing * _Nonnull ivar) {
if (withSync) {
[[NSUserDefaults standardUserDefaults] synchronize];
[defaults synchronize];
}
id value = [[NSUserDefaults standardUserDefaults] objectForKey:key];
id value = [defaults objectForKey:key];
if (withArchive && value) {
return [NSKeyedUnarchiver unarchiveObjectWithData:value];
}
Expand All @@ -45,9 +51,9 @@ + (void)injectDefaultsWithKey:(DIDefaultsKeyBlock)keyBlock forProtocol:(Protocol
if (withArchive && value) {
value = [NSKeyedArchiver archivedDataWithRootObject:value];
}
[[NSUserDefaults standardUserDefaults] setObject:value forKey:key];
[defaults setObject:value forKey:key];
if (withSync) {
[[NSUserDefaults standardUserDefaults] synchronize];
[defaults synchronize];
}
if (originalSetter) {
originalSetter(target, setter, value);
Expand All @@ -65,10 +71,16 @@ + (void)injectDefaults {
}

+ (void)injectDefaultsWithKey:(DIDefaultsKeyBlock)keyBlock {
[self injectDefaultsWithKey:keyBlock forProtocol:@protocol(DIDefaults) withSync:NO withArchive:NO];
[self injectDefaultsWithKey:keyBlock forProtocol:@protocol(DIDefaultsSync) withSync:YES withArchive:NO];
[self injectDefaultsWithKey:keyBlock forProtocol:@protocol(DIDefaultsArchived) withSync:NO withArchive:YES];
[self injectDefaultsWithKey:keyBlock forProtocol:@protocol(DIDefaultsArchivedSync) withSync:YES withArchive:YES];
[self injectDefaultsWithKey:keyBlock defaults:^NSUserDefaults *(Class targetClass, NSString *propertyName, Class propertyClass, NSSet<Protocol *> *propertyProtocols) {
return nil;
}];
}

+ (void)injectDefaultsWithKey:(DIDefaultsKeyBlock)keyBlock defaults:(DIUserDefaultsBlock)defaultsBlock {
[self injectDefaultsWithKey:keyBlock defaults:defaultsBlock forProtocol:@protocol(DIDefaults) withSync:NO withArchive:NO];
[self injectDefaultsWithKey:keyBlock defaults:defaultsBlock forProtocol:@protocol(DIDefaultsSync) withSync:YES withArchive:NO];
[self injectDefaultsWithKey:keyBlock defaults:defaultsBlock forProtocol:@protocol(DIDefaultsArchived) withSync:NO withArchive:YES];
[self injectDefaultsWithKey:keyBlock defaults:defaultsBlock forProtocol:@protocol(DIDefaultsArchivedSync) withSync:YES withArchive:YES];
}

+ (void)rejectDefaults {
Expand Down

0 comments on commit d6d43ef

Please sign in to comment.