/* * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ #import #import #import #import #import #import #import "RCTSettingsPlugins.h" @interface RCTSettingsManager() @end @implementation RCTSettingsManager { BOOL _ignoringUpdates; NSUserDefaults *_defaults; } @synthesize bridge = _bridge; RCT_EXPORT_MODULE() + (BOOL)requiresMainQueueSetup { return NO; } - (instancetype)init { return [self initWithUserDefaults:[NSUserDefaults standardUserDefaults]]; } - (instancetype)initWithUserDefaults:(NSUserDefaults *)defaults { if ((self = [super init])) { _defaults = defaults; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userDefaultsDidChange:) name:NSUserDefaultsDidChangeNotification object:_defaults]; } return self; } - (facebook::react::ModuleConstants)constantsToExport { return (facebook::react::ModuleConstants)[self getConstants]; } - (facebook::react::ModuleConstants)getConstants { return facebook::react::typedConstants({ .settings = RCTJSONClean([_defaults dictionaryRepresentation]) }); } - (void)userDefaultsDidChange:(NSNotification *)note { if (_ignoringUpdates) { return; } #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" [_bridge.eventDispatcher sendDeviceEventWithName:@"settingsUpdated" body:RCTJSONClean([_defaults dictionaryRepresentation])]; #pragma clang diagnostic pop } /** * 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) { [self->_defaults setObject:plist forKey:key]; } else { [self->_defaults removeObjectForKey:key]; } }]; [_defaults synchronize]; _ignoringUpdates = NO; } /** * Remove some values from the settings. */ RCT_EXPORT_METHOD(deleteValues:(NSArray *)keys) { _ignoringUpdates = YES; for (NSString *key in keys) { [_defaults removeObjectForKey:key]; } [_defaults synchronize]; _ignoringUpdates = NO; } - (std::shared_ptr) getTurboModuleWithJsInvoker:(std::shared_ptr)jsInvoker nativeInvoker:(std::shared_ptr)nativeInvoker perfLogger:(id)perfLogger { return std::make_shared(self, jsInvoker, nativeInvoker, perfLogger); } @end Class RCTSettingsManagerCls(void) { return RCTSettingsManager.class; }