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
117 lines
4.3 KiB
Objective-C
117 lines
4.3 KiB
Objective-C
//
|
|
// FKUserDefaultsPlugin.m
|
|
// Sample
|
|
//
|
|
// Created by Marc Terns on 9/30/18.
|
|
// Copyright © 2018 Facebook. All rights reserved.
|
|
//
|
|
|
|
#import "FKUserDefaultsPlugin.h"
|
|
#import <FlipperKit/FlipperConnection.h>
|
|
#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 *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 = [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) {
|
|
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"];
|
|
[sharedPreferences setObject:params[@"preferenceValue"] forKey:preferenceName];
|
|
[responder success:[sharedPreferences dictionaryRepresentation]];
|
|
}];
|
|
}
|
|
|
|
- (void)didDisconnect {
|
|
self.flipperConnection = nil;
|
|
}
|
|
|
|
- (NSString *)identifier {
|
|
return @"Preferences";
|
|
}
|
|
|
|
#pragma mark - Private methods
|
|
|
|
- (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,
|
|
@"time":intervalStr
|
|
} mutableCopy];
|
|
|
|
if (!value) {
|
|
[params setObject:@"YES" forKey:@"deleted"];
|
|
} else {
|
|
[params setObject:value forKey:@"value"];
|
|
}
|
|
|
|
NSString *sharedPreferencesName = (userDefaults == _standardUserDefaults
|
|
? kStandardUserDefaultsName
|
|
: kAppSuiteUserDefaultsName);
|
|
[params setObject:sharedPreferencesName forKey:@"preferences"];
|
|
[self.flipperConnection send:@"sharedPreferencesChange" withParams:[params copy]];
|
|
}
|
|
|
|
@end
|