diff --git a/DeluxeInjection.podspec b/DeluxeInjection.podspec index f54fa34..769ea66 100644 --- a/DeluxeInjection.podspec +++ b/DeluxeInjection.podspec @@ -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 diff --git a/DeluxeInjection/Classes/DIDefaults.h b/DeluxeInjection/Classes/DIDefaults.h index 3e28c21..3e82807 100644 --- a/DeluxeInjection/Classes/DIDefaults.h +++ b/DeluxeInjection/Classes/DIDefaults.h @@ -53,6 +53,18 @@ NS_ASSUME_NONNULL_BEGIN */ typedef NSString * _Nullable (^DIDefaultsKeyBlock)(Class targetClass, NSString *propertyName, Class propertyClass, NSSet *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 *propertyProtocols); + @interface DeluxeInjection (DIDefaults) /** @@ -64,7 +76,7 @@ typedef NSString * _Nullable (^DIDefaultsKeyBlock)(Class targetClass, NSString * * Inject properties marked with \c and \c 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 and \c protocol. diff --git a/DeluxeInjection/Classes/DIDefaults.m b/DeluxeInjection/Classes/DIDefaults.m index efe926e..df56be1 100644 --- a/DeluxeInjection/Classes/DIDefaults.m +++ b/DeluxeInjection/Classes/DIDefaults.m @@ -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 *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]; } @@ -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); @@ -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 *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 {