UNPKG

nativescript-akylas-preferences

Version:

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

105 lines 4.07 kB
Object.defineProperty(exports, "__esModule", { value: true }); exports.Preferences = void 0; var app = require("@nativescript/core/application"); var preferences_common_1 = require("./preferences.common"); var Preferences = (function (_super) { __extends(Preferences, _super); function Preferences() { return _super !== null && _super.apply(this, arguments) || this; } Preferences.prototype.setValue = function (key, value) { var allPrefs = this.getPreferences().getAll(); var pref = allPrefs.get(key); if (typeof pref === 'string') { this.getPreferences() .edit() .putString(key, value) .apply(); } else if (pref instanceof java.lang.Boolean) { this.getPreferences() .edit() .putBoolean(key, value) .apply(); } else if (typeof pref === 'number') { this.getPreferences() .edit() .putInt(key, value) .apply(); } }; Preferences.prototype.getValue = function (key, defaultValue) { var allPrefs = this.getPreferences().getAll(); var pref = allPrefs.get(key); if (typeof pref === 'string') { if (!defaultValue) defaultValue = ''; return this.getPreferences().getString(key, defaultValue); } else if (pref instanceof java.lang.Boolean) { if (!defaultValue) defaultValue = false; return this.getPreferences().getBoolean(key, defaultValue); } else if (typeof pref === 'number') { if (!defaultValue) defaultValue = 0; return this.getPreferences().getInt(key, defaultValue); } return null; }; Preferences.prototype.clear = function () { this.getPreferences() .edit() .clear() .apply(); }; Preferences.prototype.onListenerAdded = function (eventName, count) { this.getPreferences(); }; Preferences.prototype.openSettings = function () { var ID = 5836; var activity = app.android.foregroundActivity || app.android.startActivity; return new Promise(function (resolve, reject) { var onActivityResultHandler = function (data) { if (data.requestCode === ID) { app.android.off(app.AndroidApplication.activityResultEvent, onActivityResultHandler); resolve(); } }; app.android.on(app.AndroidApplication.activityResultEvent, onActivityResultHandler); try { activity.startActivityForResult(new android.content.Intent(activity, com.nativescript.preferences.NativescriptSettingsActivity.class), ID); } catch (err) { console.log(err); reject(err); } }); }; Preferences.prototype.getPreferences = function () { var _this = this; if (!this.sharedPreferences) { this.sharedPreferences = app.getNativeApplication().getApplicationContext().getSharedPreferences('prefs.db', 0); this.listener = new android.content.SharedPreferences.OnSharedPreferenceChangeListener({ onSharedPreferenceChanged: function (pref, key) { _this.notify({ eventName: 'change', object: _this, key: key }); _this.notify({ eventName: 'key:' + key, object: _this }); } }); this.sharedPreferences.registerOnSharedPreferenceChangeListener(this.listener); } return this.sharedPreferences; }; return Preferences; }(preferences_common_1.Common)); exports.Preferences = Preferences; //# sourceMappingURL=preferences.android.js.map