UNPKG

@nativescript-community/preferences

Version:

Common API to allow users to use native (familiar) app settings screens instead of having to craft custom UIs

96 lines 3.92 kB
import { Common } from './index.common'; import { Application } from '@nativescript/core'; var ObserverClass = /** @class */ (function (_super) { __extends(ObserverClass, _super); function ObserverClass() { return _super !== null && _super.apply(this, arguments) || this; } // NOTE: Refactor this - use Typescript property instead of strings.... ObserverClass.prototype.observeValueForKeyPathOfObjectChangeContext = function (path) { var owner = this.owner; if (owner) { owner.notify({ eventName: 'key:' + path, object: owner }); } }; return ObserverClass; }(NSObject)); export class Preferences extends Common { constructor() { super(); this.userDefaults = NSUserDefaults.standardUserDefaults; this.mNativeObservers = {}; this.registered = false; this.registerDefaultsFromSettingsBundle(); Application.ios.addNotificationObserver(NSUserDefaultsDidChangeNotification, () => { this.notify({ eventName: 'change', object: this }); }); } setValue(key, value) { this.userDefaults.setValueForKey(value, key); } getValue(key) { const standardUserDefaults = NSUserDefaults.standardUserDefaults; const us = standardUserDefaults.objectForKey(key); if (us == null) { this.registerDefaultsFromSettingsBundle(); } return this.userDefaults.objectForKey(key); } onListenerAdded(eventName, count) { if (eventName.startsWith('key:') && count === 1) { const key = eventName.replace('key:', ''); if (!this.mNativeObservers[key]) { this.mNativeObservers[key] = ObserverClass.alloc().init(); this.mNativeObservers[key]['owner'] = this; } this.userDefaults.addObserverForKeyPathOptionsContext(this.mNativeObservers[key], key, 1 /* NSKeyValueObservingOptions.New */, null); } } onListenerRemoved(eventName, count) { if (eventName.startsWith('key:') && count === 0) { const key = eventName.replace('key:', ''); if (this.mNativeObservers[key]) { this.userDefaults.removeObserverForKeyPath(this.mNativeObservers[key], key); this.mNativeObservers[key] = null; delete this.mNativeObservers[key]; } } } openSettings() { UIApplication.sharedApplication.openURL(NSURL.URLWithString(UIApplicationOpenSettingsURLString)); } clear() { } //https://stackoverflow.com/questions/6291477/how-to-retrieve-values-from-settings-bundle-in-objective-c registerDefaultsFromSettingsBundle() { if (this.registered) { return; } this.registered = true; const settingsPath = NSBundle.mainBundle.pathForResourceOfType('Settings', 'bundle'); if (!settingsPath) { return; } const settingsBundle = NSString.stringWithString(settingsPath); const rootPath = settingsBundle.stringByAppendingPathComponent('Root.plist'); const settings = NSDictionary.dictionaryWithContentsOfFile(rootPath); const preferences = settings.objectForKey('PreferenceSpecifiers'); const prefs = preferences.count; const defaultsToRegister = NSMutableDictionary.alloc().initWithCapacity(prefs); for (let i = 0; i < prefs; i++) { const prefSpecification = preferences.objectAtIndex(i); const key = prefSpecification.objectForKey('Key'); if (key) { defaultsToRegister.setObjectForKey('', key); } } this.userDefaults.registerDefaults(defaultsToRegister); this.userDefaults.synchronize(); } } //# sourceMappingURL=index.ios.js.map