nativescript-preferences
Version:
Allows your app to have native settings support
71 lines • 2.9 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
var frame_1 = require("tns-core-modules/ui/frame");
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).commit();
}
else if (pref instanceof java.lang.Boolean) {
this.getPreferences().edit().putBoolean(key, value).commit();
}
else if (typeof pref === "number") {
this.getPreferences().edit().putInt(key, value).commit();
}
};
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().commit();
};
Preferences.prototype.openSettings = function () {
var frame = frame_1.Frame.topmost();
if (frame) {
var activity = frame.android.activity;
var intent = new android.content.Intent(activity, com.sitefinitysteve.nativescriptsettings.NativescriptSettingsActivity.class);
activity.startActivity(intent);
}
else {
console.log("UNABLE TO FIND Frame.topmost()");
}
};
Preferences.prototype.getPreferences = function () {
var context = getContext();
return android.preference.PreferenceManager.getDefaultSharedPreferences(context);
};
return Preferences;
}(preferences_common_1.Common));
exports.Preferences = Preferences;
function getContext() {
var ctx = java.lang.Class.forName("android.app.AppGlobals").getMethod("getInitialApplication", null).invoke(null, null);
if (ctx) {
return ctx;
}
return java.lang.Class.forName("android.app.ActivityThread").getMethod("currentApplication", null).invoke(null, null);
}
//# sourceMappingURL=preferences.android.js.map
;