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,19 +11,22 @@
|
|||||||
#import <FlipperKit/FlipperResponder.h>
|
#import <FlipperKit/FlipperResponder.h>
|
||||||
#import "FKUserDefaultsSwizzleUtility.h"
|
#import "FKUserDefaultsSwizzleUtility.h"
|
||||||
|
|
||||||
|
static NSString *const kStandardUserDefaultsName = @"Standard UserDefaults";
|
||||||
|
static NSString *const kAppSuiteUserDefaultsName = @"App Suite UserDefaults";
|
||||||
|
|
||||||
@interface FKUserDefaultsPlugin ()
|
@interface FKUserDefaultsPlugin ()
|
||||||
@property (nonatomic, strong) id<FlipperConnection> flipperConnection;
|
@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 *key;
|
||||||
@property (nonatomic, copy) NSString *suiteName;
|
@property (nonatomic, copy) NSString *suiteName;
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@implementation FKUserDefaultsPlugin
|
@implementation FKUserDefaultsPlugin
|
||||||
|
|
||||||
- (instancetype)initWithSuiteName:(NSString *)suiteName {
|
- (instancetype)init {
|
||||||
if (self = [super init]) {
|
if (self = [super init]) {
|
||||||
_userDefaults = [NSUserDefaults standardUserDefaults];
|
_standardUserDefaults = [NSUserDefaults standardUserDefaults];
|
||||||
_suiteName = suiteName;
|
|
||||||
__weak typeof(self) weakSelf = self;
|
__weak typeof(self) weakSelf = self;
|
||||||
[FKUserDefaultsSwizzleUtility swizzleSelector:@selector(setObject:forKey:) class:[NSUserDefaults class] block:^(NSInvocation * _Nonnull invocation) {
|
[FKUserDefaultsSwizzleUtility swizzleSelector:@selector(setObject:forKey:) class:[NSUserDefaults class] block:^(NSInvocation * _Nonnull invocation) {
|
||||||
__unsafe_unretained id firstArg = nil;
|
__unsafe_unretained id firstArg = nil;
|
||||||
@@ -31,30 +34,40 @@
|
|||||||
[invocation getArgument:&firstArg atIndex:2];
|
[invocation getArgument:&firstArg atIndex:2];
|
||||||
[invocation getArgument:&secondArg atIndex:3];
|
[invocation getArgument:&secondArg atIndex:3];
|
||||||
[invocation invoke];
|
[invocation invoke];
|
||||||
[weakSelf userDefaultsChangedWithValue:firstArg key:secondArg];
|
[weakSelf userDefaults:([invocation.target isKindOfClass:[NSUserDefaults class]] ? invocation.target : nil)
|
||||||
|
changedWithValue:firstArg
|
||||||
|
key:secondArg];
|
||||||
}];
|
}];
|
||||||
}
|
}
|
||||||
return self;
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (instancetype)initWithSuiteName:(NSString *)suiteName {
|
||||||
|
if (self = [self init]) {
|
||||||
|
_suiteName = suiteName;
|
||||||
|
if (_suiteName) {
|
||||||
|
_appSuiteUserDefaults = [[NSUserDefaults alloc] initWithSuiteName:_suiteName];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)didConnect:(id<FlipperConnection>)connection {
|
- (void)didConnect:(id<FlipperConnection>)connection {
|
||||||
self.flipperConnection = connection;
|
self.flipperConnection = connection;
|
||||||
[connection receive:@"getAllSharedPreferences" withBlock:^(NSDictionary *params, id<FlipperResponder> responder) {
|
[connection receive:@"getAllSharedPreferences" withBlock:^(NSDictionary *params, id<FlipperResponder> responder) {
|
||||||
NSDictionary *userDefaults = @{
|
NSMutableDictionary *userDefaults = [NSMutableDictionary new];
|
||||||
@"Standard UserDefaults": [self.userDefaults dictionaryRepresentation]
|
userDefaults[kStandardUserDefaultsName] = [self.standardUserDefaults dictionaryRepresentation];
|
||||||
};
|
if (self.appSuiteUserDefaults) {
|
||||||
[responder success: userDefaults];
|
userDefaults[kAppSuiteUserDefaultsName] = [self.appSuiteUserDefaults dictionaryRepresentation];
|
||||||
}];
|
}
|
||||||
|
[responder success:userDefaults];
|
||||||
[connection receive:@"getSharedPreferences" withBlock:^(NSDictionary *params, id<FlipperResponder> responder) {
|
|
||||||
[responder success:[self.userDefaults dictionaryRepresentation]];
|
|
||||||
}];
|
}];
|
||||||
|
|
||||||
[connection receive:@"setSharedPreference" withBlock:^(NSDictionary *params , id<FlipperResponder> responder) {
|
[connection receive:@"setSharedPreference" withBlock:^(NSDictionary *params , id<FlipperResponder> responder) {
|
||||||
|
NSUserDefaults *sharedPreferences = [self sharedPreferencesForParams:params];
|
||||||
NSString *preferenceName = params[@"preferenceName"];
|
NSString *preferenceName = params[@"preferenceName"];
|
||||||
[self.userDefaults setObject:params[@"preferenceValue"] forKey:preferenceName];
|
[sharedPreferences setObject:params[@"preferenceValue"] forKey:preferenceName];
|
||||||
[responder success:[self.userDefaults dictionaryRepresentation]];
|
[responder success:[sharedPreferences dictionaryRepresentation]];
|
||||||
}];
|
}];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,7 +81,19 @@
|
|||||||
|
|
||||||
#pragma mark - Private methods
|
#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;
|
NSTimeInterval interval = [[NSDate date] timeIntervalSince1970] * 1000;
|
||||||
NSString *intervalStr = [NSString stringWithFormat:@"%f", interval];
|
NSString *intervalStr = [NSString stringWithFormat:@"%f", interval];
|
||||||
NSMutableDictionary *params = [@{@"name":key,
|
NSMutableDictionary *params = [@{@"name":key,
|
||||||
@@ -80,7 +105,11 @@
|
|||||||
} else {
|
} else {
|
||||||
[params setObject:value forKey:@"value"];
|
[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]];
|
[self.flipperConnection send:@"sharedPreferencesChange" withParams:[params copy]];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user