2015-05-05 12:34:38 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import "RCTSettingsManager.h"
|
|
|
|
|
|
|
|
#import "RCTBridge.h"
|
|
|
|
#import "RCTConvert.h"
|
|
|
|
#import "RCTEventDispatcher.h"
|
2015-05-06 07:11:31 +00:00
|
|
|
#import "RCTUtils.h"
|
2015-05-05 12:34:38 +00:00
|
|
|
|
|
|
|
@implementation RCTSettingsManager
|
|
|
|
{
|
|
|
|
BOOL _ignoringUpdates;
|
|
|
|
NSUserDefaults *_defaults;
|
|
|
|
}
|
|
|
|
|
|
|
|
@synthesize bridge = _bridge;
|
|
|
|
|
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
|
2015-11-25 11:09:00 +00:00
|
|
|
- (instancetype)initWithUserDefaults:(NSUserDefaults *)defaults
|
2015-05-05 12:34:38 +00:00
|
|
|
{
|
2015-11-25 11:09:00 +00:00
|
|
|
if ((self = [self init])) {
|
|
|
|
_defaults = defaults;
|
|
|
|
}
|
|
|
|
return self;
|
2015-05-05 12:34:38 +00:00
|
|
|
}
|
|
|
|
|
2015-11-25 11:09:00 +00:00
|
|
|
- (void)setBridge:(RCTBridge *)bridge
|
2015-05-05 12:34:38 +00:00
|
|
|
{
|
2015-11-25 11:09:00 +00:00
|
|
|
_bridge = bridge;
|
2015-05-05 12:34:38 +00:00
|
|
|
|
2015-11-25 11:09:00 +00:00
|
|
|
if (!_defaults) {
|
|
|
|
_defaults = [NSUserDefaults standardUserDefaults];
|
2015-05-05 12:34:38 +00:00
|
|
|
}
|
|
|
|
|
2015-11-25 11:09:00 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(userDefaultsDidChange:)
|
|
|
|
name:NSUserDefaultsDidChangeNotification
|
|
|
|
object:_defaults];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSDictionary<NSString *, id> *)constantsToExport
|
|
|
|
{
|
|
|
|
return @{@"settings": RCTJSONClean([_defaults dictionaryRepresentation])};
|
2015-05-05 12:34:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)userDefaultsDidChange:(NSNotification *)note
|
|
|
|
{
|
|
|
|
if (_ignoringUpdates) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-23 16:08:51 +00:00
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
2015-05-06 07:11:31 +00:00
|
|
|
[_bridge.eventDispatcher
|
|
|
|
sendDeviceEventWithName:@"settingsUpdated"
|
|
|
|
body:RCTJSONClean([_defaults dictionaryRepresentation])];
|
2016-05-23 16:08:51 +00:00
|
|
|
#pragma clang diagnostic pop
|
2015-05-05 12:34:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set one or more values in the settings.
|
|
|
|
* TODO: would it be useful to have a callback for when this has completed?
|
|
|
|
*/
|
|
|
|
RCT_EXPORT_METHOD(setValues:(NSDictionary *)values)
|
|
|
|
{
|
|
|
|
_ignoringUpdates = YES;
|
|
|
|
[values enumerateKeysAndObjectsUsingBlock:^(NSString *key, id json, BOOL *stop) {
|
|
|
|
id plist = [RCTConvert NSPropertyList:json];
|
|
|
|
if (plist) {
|
2016-07-07 19:36:56 +00:00
|
|
|
[self->_defaults setObject:plist forKey:key];
|
2015-05-05 12:34:38 +00:00
|
|
|
} else {
|
2016-07-07 19:36:56 +00:00
|
|
|
[self->_defaults removeObjectForKey:key];
|
2015-05-05 12:34:38 +00:00
|
|
|
}
|
|
|
|
}];
|
|
|
|
|
|
|
|
[_defaults synchronize];
|
|
|
|
_ignoringUpdates = NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove some values from the settings.
|
|
|
|
*/
|
2015-12-10 18:09:04 +00:00
|
|
|
RCT_EXPORT_METHOD(deleteValues:(NSArray<NSString *> *)keys)
|
2015-05-05 12:34:38 +00:00
|
|
|
{
|
|
|
|
_ignoringUpdates = YES;
|
|
|
|
for (NSString *key in keys) {
|
|
|
|
[_defaults removeObjectForKey:key];
|
|
|
|
}
|
|
|
|
|
|
|
|
[_defaults synchronize];
|
|
|
|
_ignoringUpdates = NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|