Extend FKUserDefaultsPlugin to support app suite user defaults.
Summary: `FKUserDefaultsPlugin` is initialized with an `appSuiteName` but doesn't use this to monitor the app suite user defaults. This adds that capability. In Flipper, "App Suite UserDefaults" defaults will show up in the drop down selector for user defaults. Reviewed By: priteshrnandgaonkar Differential Revision: D14922399 fbshipit-source-id: 3876976ccd0968fdec35f7605107202e1f9d7cee
This commit is contained in:
committed by
Facebook Github Bot
parent
d9d1346c12
commit
7bbb8c10c4
@@ -11,50 +11,63 @@
|
||||
#import <FlipperKit/FlipperResponder.h>
|
||||
#import "FKUserDefaultsSwizzleUtility.h"
|
||||
|
||||
static NSString *const kStandardUserDefaultsName = @"Standard UserDefaults";
|
||||
static NSString *const kAppSuiteUserDefaultsName = @"App Suite UserDefaults";
|
||||
|
||||
@interface FKUserDefaultsPlugin ()
|
||||
@property (nonatomic, strong) id<FlipperConnection> flipperConnection;
|
||||
@property (nonatomic, strong) NSUserDefaults *userDefaults;
|
||||
@property (nonatomic, strong) NSUserDefaults *standardUserDefaults;
|
||||
@property (nonatomic, strong) NSUserDefaults *appSuiteUserDefaults;
|
||||
@property (nonatomic, copy) NSString *key;
|
||||
@property (nonatomic, copy) NSString *suiteName;
|
||||
@end
|
||||
|
||||
@implementation FKUserDefaultsPlugin
|
||||
|
||||
- (instancetype)init {
|
||||
if (self = [super init]) {
|
||||
_standardUserDefaults = [NSUserDefaults standardUserDefaults];
|
||||
__weak typeof(self) weakSelf = self;
|
||||
[FKUserDefaultsSwizzleUtility swizzleSelector:@selector(setObject:forKey:) class:[NSUserDefaults class] block:^(NSInvocation * _Nonnull invocation) {
|
||||
__unsafe_unretained id firstArg = nil;
|
||||
__unsafe_unretained id secondArg = nil;
|
||||
[invocation getArgument:&firstArg atIndex:2];
|
||||
[invocation getArgument:&secondArg atIndex:3];
|
||||
[invocation invoke];
|
||||
[weakSelf userDefaults:([invocation.target isKindOfClass:[NSUserDefaults class]] ? invocation.target : nil)
|
||||
changedWithValue:firstArg
|
||||
key:secondArg];
|
||||
}];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (instancetype)initWithSuiteName:(NSString *)suiteName {
|
||||
if (self = [super init]) {
|
||||
_userDefaults = [NSUserDefaults standardUserDefaults];
|
||||
_suiteName = suiteName;
|
||||
__weak typeof(self) weakSelf = self;
|
||||
[FKUserDefaultsSwizzleUtility swizzleSelector:@selector(setObject:forKey:) class:[NSUserDefaults class] block:^(NSInvocation * _Nonnull invocation) {
|
||||
__unsafe_unretained id firstArg = nil;
|
||||
__unsafe_unretained id secondArg = nil;
|
||||
[invocation getArgument:&firstArg atIndex:2];
|
||||
[invocation getArgument:&secondArg atIndex:3];
|
||||
[invocation invoke];
|
||||
[weakSelf userDefaultsChangedWithValue:firstArg key:secondArg];
|
||||
}];
|
||||
if (self = [self init]) {
|
||||
_suiteName = suiteName;
|
||||
if (_suiteName) {
|
||||
_appSuiteUserDefaults = [[NSUserDefaults alloc] initWithSuiteName:_suiteName];
|
||||
}
|
||||
}
|
||||
return self;
|
||||
|
||||
}
|
||||
|
||||
- (void)didConnect:(id<FlipperConnection>)connection {
|
||||
self.flipperConnection = connection;
|
||||
[connection receive:@"getAllSharedPreferences" withBlock:^(NSDictionary *params, id<FlipperResponder> responder) {
|
||||
NSDictionary *userDefaults = @{
|
||||
@"Standard UserDefaults": [self.userDefaults dictionaryRepresentation]
|
||||
};
|
||||
[responder success: userDefaults];
|
||||
}];
|
||||
|
||||
[connection receive:@"getSharedPreferences" withBlock:^(NSDictionary *params, id<FlipperResponder> responder) {
|
||||
[responder success:[self.userDefaults dictionaryRepresentation]];
|
||||
NSMutableDictionary *userDefaults = [NSMutableDictionary new];
|
||||
userDefaults[kStandardUserDefaultsName] = [self.standardUserDefaults dictionaryRepresentation];
|
||||
if (self.appSuiteUserDefaults) {
|
||||
userDefaults[kAppSuiteUserDefaultsName] = [self.appSuiteUserDefaults dictionaryRepresentation];
|
||||
}
|
||||
[responder success:userDefaults];
|
||||
}];
|
||||
|
||||
[connection receive:@"setSharedPreference" withBlock:^(NSDictionary *params , id<FlipperResponder> responder) {
|
||||
NSUserDefaults *sharedPreferences = [self sharedPreferencesForParams:params];
|
||||
NSString *preferenceName = params[@"preferenceName"];
|
||||
[self.userDefaults setObject:params[@"preferenceValue"] forKey:preferenceName];
|
||||
[responder success:[self.userDefaults dictionaryRepresentation]];
|
||||
[sharedPreferences setObject:params[@"preferenceValue"] forKey:preferenceName];
|
||||
[responder success:[sharedPreferences dictionaryRepresentation]];
|
||||
}];
|
||||
}
|
||||
|
||||
@@ -68,7 +81,19 @@
|
||||
|
||||
#pragma mark - Private methods
|
||||
|
||||
- (void)userDefaultsChangedWithValue:(id)value key:(NSString *)key {
|
||||
- (NSUserDefaults *)sharedPreferencesForParams:(NSDictionary *)params {
|
||||
NSString *const sharedPreferencesNameKey = @"sharedPreferencesName";
|
||||
if (![params[sharedPreferencesNameKey] isKindOfClass:[NSString class]]) {
|
||||
return _standardUserDefaults;
|
||||
}
|
||||
|
||||
NSString *sharedPreferencesName = params[sharedPreferencesNameKey];
|
||||
return ([sharedPreferencesName isEqualToString:kAppSuiteUserDefaultsName]
|
||||
? _appSuiteUserDefaults
|
||||
: _standardUserDefaults);
|
||||
}
|
||||
|
||||
- (void)userDefaults:(NSUserDefaults *)userDefaults changedWithValue:(id)value key:(NSString *)key {
|
||||
NSTimeInterval interval = [[NSDate date] timeIntervalSince1970] * 1000;
|
||||
NSString *intervalStr = [NSString stringWithFormat:@"%f", interval];
|
||||
NSMutableDictionary *params = [@{@"name":key,
|
||||
@@ -80,7 +105,11 @@
|
||||
} else {
|
||||
[params setObject:value forKey:@"value"];
|
||||
}
|
||||
[params setObject:@"Standard UserDefaults" forKey:@"preferences"];
|
||||
|
||||
NSString *sharedPreferencesName = (userDefaults == _standardUserDefaults
|
||||
? kStandardUserDefaultsName
|
||||
: kAppSuiteUserDefaultsName);
|
||||
[params setObject:sharedPreferencesName forKey:@"preferences"];
|
||||
[self.flipperConnection send:@"sharedPreferencesChange" withParams:[params copy]];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user