mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-17 04:41:36 +08:00
updated user preferences and application
This commit is contained in:
@ -54,10 +54,10 @@ class iOSApplication {
|
|||||||
this.window.backgroundColor = UIKit.UIColor.whiteColor();
|
this.window.backgroundColor = UIKit.UIColor.whiteColor();
|
||||||
this.window.makeKeyAndVisible();
|
this.window.makeKeyAndVisible();
|
||||||
|
|
||||||
if (appModule.onLaunch) {
|
if (exports.onLaunch) {
|
||||||
this.window.rootViewController = appModule.onLaunch();
|
this.window.rootViewController = exports.onLaunch();
|
||||||
} else {
|
} else {
|
||||||
log("Missing TK.UI.Application.current.onLaunch");
|
log("Missing Application.onLaunch");
|
||||||
}
|
}
|
||||||
|
|
||||||
log("applicationDidFinishLaunchingWithOptions finished.");
|
log("applicationDidFinishLaunchingWithOptions finished.");
|
||||||
@ -66,8 +66,8 @@ class iOSApplication {
|
|||||||
|
|
||||||
applicationDidBecomeActive: function (application) {
|
applicationDidBecomeActive: function (application) {
|
||||||
log("applicationDidBecomeActive: " + application);
|
log("applicationDidBecomeActive: " + application);
|
||||||
if (appModule.onResume) {
|
if (exports.onResume) {
|
||||||
appModule.onResume();
|
exports.onResume();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -77,8 +77,8 @@ class iOSApplication {
|
|||||||
|
|
||||||
applicationDidEnterBackground: function (application) {
|
applicationDidEnterBackground: function (application) {
|
||||||
log("applicationDidEnterBackground: " + application);
|
log("applicationDidEnterBackground: " + application);
|
||||||
if (appModule.onSuspend) {
|
if (exports.onSuspend) {
|
||||||
appModule.onSuspend();
|
exports.onSuspend();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -88,15 +88,15 @@ class iOSApplication {
|
|||||||
|
|
||||||
applicationWillTerminate: function (application) {
|
applicationWillTerminate: function (application) {
|
||||||
log("applicationWillTerminate: " + application);
|
log("applicationWillTerminate: " + application);
|
||||||
if (appModule.onExit) {
|
if (exports.onExit) {
|
||||||
appModule.onExit();
|
exports.onExit();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
applicationDidReceiveMemoryWarning: function (application) {
|
applicationDidReceiveMemoryWarning: function (application) {
|
||||||
log("applicationDidReceiveMemoryWarning: " + application);
|
log("applicationDidReceiveMemoryWarning: " + application);
|
||||||
if (appModule.onLowMemory) {
|
if (exports.onLowMemory) {
|
||||||
appModule.onLowMemory();
|
exports.onLowMemory();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,19 +1,14 @@
|
|||||||
require("globals");
|
require("globals");
|
||||||
|
|
||||||
export var onLaunch = function (): any {
|
export var onLaunch: () => any = undefined;
|
||||||
}
|
|
||||||
|
|
||||||
export var onSuspend = function (): void {
|
export var onSuspend: () => any = undefined;
|
||||||
}
|
|
||||||
|
|
||||||
export var onResume = function (): void {
|
export var onResume: () => any = undefined;
|
||||||
}
|
|
||||||
|
|
||||||
export var onExit = function (): void {
|
export var onExit: () => any = undefined;
|
||||||
}
|
|
||||||
|
|
||||||
export var onLowMemory = function (): void {
|
export var onLowMemory: () => any = undefined;
|
||||||
}
|
|
||||||
|
|
||||||
export var android = undefined;
|
export var android = undefined;
|
||||||
|
|
||||||
|
@ -174,12 +174,16 @@
|
|||||||
<TypeScriptCompile Include="timer\timer.android.ts">
|
<TypeScriptCompile Include="timer\timer.android.ts">
|
||||||
<DependentUpon>timer.d.ts</DependentUpon>
|
<DependentUpon>timer.d.ts</DependentUpon>
|
||||||
</TypeScriptCompile>
|
</TypeScriptCompile>
|
||||||
|
<TypeScriptCompile Include="UserPreferences\user_preferences_common.ts">
|
||||||
|
<DependentUpon>user_preferences.d.ts</DependentUpon>
|
||||||
|
</TypeScriptCompile>
|
||||||
|
<Content Include="_references.ts" />
|
||||||
|
<TypeScriptCompile Include="Console\console.d.ts" />
|
||||||
<TypeScriptCompile Include="timer\timer.d.ts" />
|
<TypeScriptCompile Include="timer\timer.d.ts" />
|
||||||
<TypeScriptCompile Include="timer\timer.ios.ts">
|
<TypeScriptCompile Include="timer\timer.ios.ts">
|
||||||
<DependentUpon>timer.d.ts</DependentUpon>
|
<DependentUpon>timer.d.ts</DependentUpon>
|
||||||
</TypeScriptCompile>
|
</TypeScriptCompile>
|
||||||
<Content Include="_references.ts" />
|
<Content Include="_references.ts" />
|
||||||
<TypeScriptCompile Include="Console\console.d.ts" />
|
|
||||||
<Content Include="Image\Readme.md" />
|
<Content Include="Image\Readme.md" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
@ -1,97 +1,117 @@
|
|||||||
import utils_module = require("Utils/utils_android");
|
import appModule = require("Application/application");
|
||||||
import appModule = require("Application/application");
|
import Common = require("UserPreferences/user_preferences_common");
|
||||||
|
|
||||||
export class UserPreferences {
|
var sharedPreferences = appModule.android.context.getSharedPreferences("prefs.db", 0);
|
||||||
private sharedPreferences: any;
|
|
||||||
|
|
||||||
constructor() {
|
export var hasKey = function (key: string): boolean {
|
||||||
this.sharedPreferences = appModule.android.context.getSharedPreferences("prefs.db", 0);
|
Common.checkKey(key);
|
||||||
}
|
return sharedPreferences.contains(key);
|
||||||
|
|
||||||
public containsKey(key: string): boolean {
|
|
||||||
return this.sharedPreferences.contains(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
public getBoolean(key: string, defaultValue?: boolean): boolean {
|
|
||||||
if ("undefined" == typeof defaultValue) {
|
|
||||||
defaultValue = false;
|
|
||||||
}
|
|
||||||
return this.sharedPreferences.getBoolean(key, defaultValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
public getDouble(key: string, defaultValue?: number): number {
|
|
||||||
if ("undefined" == typeof defaultValue) {
|
|
||||||
defaultValue = 0.0;
|
|
||||||
}
|
|
||||||
Log('getting double for key ' + key + ' with default ' + defaultValue + ' result: ' + this.sharedPreferences.getFloat(key, defaultValue));
|
|
||||||
return this.sharedPreferences.getFloat(key, defaultValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
public getInt(key: string, defaultValue?: number): number {
|
|
||||||
if ("undefined" == typeof defaultValue) {
|
|
||||||
defaultValue = 0;
|
|
||||||
}
|
|
||||||
return this.sharedPreferences.getInt(key, defaultValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
public getLong(key: string, defaultValue?: number): number {
|
|
||||||
if ("undefined" == typeof defaultValue) {
|
|
||||||
defaultValue = 0;
|
|
||||||
}
|
|
||||||
return this.sharedPreferences.getLong(key, defaultValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
public getString(key: string, defaultValue?: string): string {
|
|
||||||
if ("undefined" == typeof defaultValue) {
|
|
||||||
defaultValue = null; // is this ok?
|
|
||||||
}
|
|
||||||
return this.sharedPreferences.getString(key, defaultValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
public getStrings(key: string, defaultValue?: string[]): string[] {
|
|
||||||
if ("undefined" == typeof defaultValue) {
|
|
||||||
defaultValue = [];
|
|
||||||
}
|
|
||||||
var hashSet = utils_module.Collections.stringArrayToStringSet(defaultValue);
|
|
||||||
var res = this.sharedPreferences.getStringSet(key, hashSet);
|
|
||||||
return utils_module.Collections.stringSetToStringArray(res);
|
|
||||||
}
|
|
||||||
|
|
||||||
public setBoolean(key: string, value: boolean) {
|
|
||||||
var editor = this.sharedPreferences.edit();
|
|
||||||
editor.putBoolean(key, value);
|
|
||||||
editor.commit();
|
|
||||||
}
|
|
||||||
|
|
||||||
public setDouble(key: string, value: number) {
|
|
||||||
var editor = this.sharedPreferences.edit();
|
|
||||||
Log('setting double for key ' + key + ' with value ' + value);
|
|
||||||
editor.putFloat(key, float(value));
|
|
||||||
editor.commit();
|
|
||||||
}
|
|
||||||
|
|
||||||
public setInt(key: string, value: number) {
|
|
||||||
var editor = this.sharedPreferences.edit();
|
|
||||||
editor.putInt(key, value);
|
|
||||||
editor.commit();
|
|
||||||
}
|
|
||||||
|
|
||||||
public setLong(key: string, value: number) {
|
|
||||||
var editor = this.sharedPreferences.edit();
|
|
||||||
editor.putLong(key, long(value));
|
|
||||||
editor.commit();
|
|
||||||
}
|
|
||||||
|
|
||||||
public setString(key: string, value: string) {
|
|
||||||
var editor = this.sharedPreferences.edit();
|
|
||||||
editor.putString(key, value);
|
|
||||||
editor.commit();
|
|
||||||
}
|
|
||||||
|
|
||||||
public setStrings(key: string, values: string[]) {
|
|
||||||
var editor = this.sharedPreferences.edit();
|
|
||||||
var hashSet = utils_module.Collections.stringArrayToStringSet(values);
|
|
||||||
editor.putStringSet(key, hashSet);
|
|
||||||
editor.commit();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getters
|
||||||
|
export var getBoolean = function (key: string, defaultValue?: boolean): boolean {
|
||||||
|
Common.checkKey(key);
|
||||||
|
if (hasKey(key)) {
|
||||||
|
return sharedPreferences.getBoolean(key, false);
|
||||||
|
}
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
export var getString = function(key: string, defaultValue?: string): string {
|
||||||
|
Common.checkKey(key);
|
||||||
|
if (hasKey(key)) {
|
||||||
|
return sharedPreferences.getString(key, "");
|
||||||
|
}
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
export var getNumber = function(key: string, defaultValue?: number): number {
|
||||||
|
Common.checkKey(key);
|
||||||
|
if (hasKey(key)) {
|
||||||
|
return sharedPreferences.getFloat(key, float(0.0));
|
||||||
|
}
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// setters
|
||||||
|
export var setBoolean = function(key: string, value: boolean): void {
|
||||||
|
Common.checkKey(key);
|
||||||
|
Common.ensureValidValue(value, "boolean");
|
||||||
|
var editor = sharedPreferences.edit();
|
||||||
|
editor.putBoolean(key, value);
|
||||||
|
editor.commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
export var setString = function(key: string, value: string): void {
|
||||||
|
Common.checkKey(key);
|
||||||
|
Common.ensureValidValue(value, "string");
|
||||||
|
var editor = sharedPreferences.edit();
|
||||||
|
editor.putString(key, value);
|
||||||
|
editor.commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
export var setNumber = function(key: string, value: number): void {
|
||||||
|
Common.checkKey(key);
|
||||||
|
Common.ensureValidValue(value, "number");
|
||||||
|
var editor = sharedPreferences.edit();
|
||||||
|
editor.putFloat(key, float(value));
|
||||||
|
editor.commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
these are commented out to be used only if requested by users or otherwise needed
|
||||||
|
|
||||||
|
import utils_module = require("Utils/utils_android");
|
||||||
|
|
||||||
|
export var getStringArray = function (key: string, defaultValue?: string[]): string[]{
|
||||||
|
Common.checkKey(key);
|
||||||
|
if (!hasKey(key)) {
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
if (!defaultValue) {
|
||||||
|
defaultValue = [];
|
||||||
|
}
|
||||||
|
var hashSet = utils_module.Collections.stringArrayToStringSet(defaultValue);
|
||||||
|
var res = sharedPreferences.getStringSet(key, hashSet);
|
||||||
|
return utils_module.Collections.stringSetToStringArray(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
export var getInt = function (key: string, defaultValue?: number): number {
|
||||||
|
Common.checkKey(key);
|
||||||
|
if (hasKey(key)) {
|
||||||
|
return sharedPreferences.getInt(key, 0);
|
||||||
|
}
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
export var getLong = function (key: string, defaultValue?: number): number {
|
||||||
|
Common.checkKey(key);
|
||||||
|
if (hasKey(key)) {
|
||||||
|
return sharedPreferences.getLong(key, long(0));
|
||||||
|
}
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
export var setStringArray = function (key: string, values: string[]): void {
|
||||||
|
Common.checkKey(key);
|
||||||
|
var editor = sharedPreferences.edit();
|
||||||
|
var hashSet = utils_module.Collections.stringArrayToStringSet(values);
|
||||||
|
editor.putStringSet(key, hashSet);
|
||||||
|
editor.commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
export var setInt = function (key: string, value: number): void {
|
||||||
|
Common.checkKey(key);
|
||||||
|
var editor = sharedPreferences.edit();
|
||||||
|
editor.putInt(key, value);
|
||||||
|
editor.commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
export var setLong = function (key: string, value: number): void {
|
||||||
|
Common.checkKey(key);
|
||||||
|
var editor = sharedPreferences.edit();
|
||||||
|
editor.putLong(key, long(value));
|
||||||
|
editor.commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
*/
|
69
UserPreferences/user_preferences.d.ts
vendored
69
UserPreferences/user_preferences.d.ts
vendored
@ -13,3 +13,72 @@
|
|||||||
setString(key: string, value: string);
|
setString(key: string, value: string);
|
||||||
setStrings(key: string, value: string[]);
|
setStrings(key: string, value: string[]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* report does such key exist
|
||||||
|
*/
|
||||||
|
export declare var hasKey: (key: string) => boolean;
|
||||||
|
|
||||||
|
// getters
|
||||||
|
|
||||||
|
/**
|
||||||
|
* gets value of the key as boolean, user can provide default value in case there is no value for the key
|
||||||
|
*/
|
||||||
|
export declare var getBoolean: (key: string, defaultValue?: boolean) => boolean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* gets value of the key as string, user can provide default value in case there is no value for the key
|
||||||
|
*/
|
||||||
|
export declare var getString: (key: string, defaultValue?: string) => string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* gets value of the key as string array, user can provide default value in case there is no value for the key
|
||||||
|
*/
|
||||||
|
export declare var getStringArray: (key: string, defaultValue?: string[]) => string[];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* gets value of the key as number (double), user can provide default value in case there is no value for the key
|
||||||
|
*/
|
||||||
|
export declare var getNumber: (key: string, defaultValue?: number) => number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* gets value of the key as integer, user can provide default value in case there is no value for the key
|
||||||
|
*/
|
||||||
|
export declare var getInt: (key: string, defaultValue?: number) => number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* gets value of the key as long integer (not fully supported by JS), user can provide default value in case there is no value for the key
|
||||||
|
*/
|
||||||
|
export declare var getLong: (key: string, defaultValue?: number) => number;
|
||||||
|
|
||||||
|
// setters
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sets value for a key as boolean
|
||||||
|
*/
|
||||||
|
export declare var setBoolean: (key: string, value: boolean) => void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sets value for a key as string
|
||||||
|
*/
|
||||||
|
export declare var setString: (key: string, value: string) => void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sets value for a key as string array
|
||||||
|
*/
|
||||||
|
export declare var setStringArray: (key: string, value: string[]) => void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sets value for a key as JS number (double)
|
||||||
|
*/
|
||||||
|
export declare var setNumber: (key: string, value: number) => void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sets value for a key as integer
|
||||||
|
*/
|
||||||
|
export declare var setInt: (key: string, value: number) => void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sets value for a key as long integer
|
||||||
|
*/
|
||||||
|
export declare var setLong: (key: string, value: number) => void;
|
||||||
|
@ -1,108 +1,106 @@
|
|||||||
import utils_module = require("Utils/utils_ios");
|
import Common = require("UserPreferences/user_preferences_common");
|
||||||
|
|
||||||
export class UserPreferences {
|
var userDefaults = Foundation.NSUserDefaults.standardUserDefaults();
|
||||||
|
|
||||||
private userDefaults: any;
|
export var hasKey = function (key: string): boolean {
|
||||||
|
Common.checkKey(key);
|
||||||
constructor() {
|
return (null != userDefaults.objectForKey(key)) ? true : false;
|
||||||
this.userDefaults = Foundation.NSUserDefaults.standardUserDefaults();
|
|
||||||
}
|
|
||||||
|
|
||||||
public containsKey(key: string): boolean {
|
|
||||||
// FIXME: is there a better way to do this check?
|
|
||||||
return this.userDefaults.objectForKey(key) ? true : false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public getBoolean(key: string, defaultValue?: boolean): boolean {
|
|
||||||
if (this.containsKey(key)) {
|
|
||||||
return this.userDefaults.boolForKey(key);
|
|
||||||
}
|
|
||||||
if ("undefined" == typeof defaultValue) {
|
|
||||||
defaultValue = false;
|
|
||||||
}
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
public getDouble(key: string, defaultValue?: number): number {
|
|
||||||
if (this.containsKey(key)) {
|
|
||||||
return this.userDefaults.doubleForKey(key);
|
|
||||||
}
|
|
||||||
if ("undefined" == typeof defaultValue) {
|
|
||||||
defaultValue = 0.0;
|
|
||||||
}
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
public getInt(key: string, defaultValue?: number): number {
|
|
||||||
if (this.containsKey(key)) {
|
|
||||||
return this.userDefaults.integerForKey(key);
|
|
||||||
}
|
|
||||||
if ("undefined" == typeof defaultValue) {
|
|
||||||
defaultValue = 0;
|
|
||||||
}
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
public getLong(key: string, defaultValue?: number): number {
|
|
||||||
if (this.containsKey(key)) {
|
|
||||||
return this.userDefaults.integerForKey(key);
|
|
||||||
}
|
|
||||||
if ("undefined" == typeof defaultValue) {
|
|
||||||
defaultValue = 0;
|
|
||||||
}
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
public getString(key: string, defaultValue?: string): string {
|
|
||||||
if (this.containsKey(key)) {
|
|
||||||
return this.userDefaults.stringForKey(key);
|
|
||||||
}
|
|
||||||
if ("undefined" == typeof defaultValue) {
|
|
||||||
defaultValue = "";
|
|
||||||
}
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
public getStrings(key: string, defaultValue?: string[]): string[] {
|
|
||||||
if (this.containsKey(key)) {
|
|
||||||
var nsArray = this.userDefaults.stringArrayForKey(key);
|
|
||||||
var jsArray = utils_module.Collections.nsArrayToJSArray(nsArray);
|
|
||||||
return jsArray;
|
|
||||||
}
|
|
||||||
if ("undefined" == typeof defaultValue) {
|
|
||||||
defaultValue = [];
|
|
||||||
}
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
public setBoolean(key: string, value: boolean) {
|
|
||||||
this.userDefaults.setBoolForKey(value, key);
|
|
||||||
this.userDefaults.synchronize();
|
|
||||||
}
|
|
||||||
|
|
||||||
public setDouble(key: string, value: number) {
|
|
||||||
this.userDefaults.setDoubleForKey(value, key);
|
|
||||||
this.userDefaults.synchronize();
|
|
||||||
}
|
|
||||||
|
|
||||||
public setInt(key: string, value: number) {
|
|
||||||
this.userDefaults.setIntegerForKey(value, key);
|
|
||||||
this.userDefaults.synchronize();
|
|
||||||
}
|
|
||||||
|
|
||||||
public setLong(key: string, value: number) {
|
|
||||||
this.userDefaults.setIntegerForKey(value, key);
|
|
||||||
this.userDefaults.synchronize();
|
|
||||||
}
|
|
||||||
|
|
||||||
public setString(key: string, value: string) {
|
|
||||||
this.userDefaults.setObjectForKey(value, key);
|
|
||||||
this.userDefaults.synchronize();
|
|
||||||
}
|
|
||||||
|
|
||||||
public setStrings(key: string, values: string[]) {
|
|
||||||
var nsArray = utils_module.Collections.jsArrayToNSArray(values);
|
|
||||||
this.userDefaults.setObjectForKey(nsArray, key);
|
|
||||||
this.userDefaults.synchronize();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getters
|
||||||
|
export var getBoolean = function (key: string, defaultValue?: boolean): boolean {
|
||||||
|
Common.checkKey(key);
|
||||||
|
if (hasKey(key)) {
|
||||||
|
return userDefaults.boolForKey(key);
|
||||||
|
}
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
export var getString = function (key: string, defaultValue?: string): string {
|
||||||
|
Common.checkKey(key);
|
||||||
|
if (hasKey(key)) {
|
||||||
|
return userDefaults.stringForKey(key);
|
||||||
|
}
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
export var getNumber = function (key: string, defaultValue?: number): number {
|
||||||
|
Common.checkKey(key);
|
||||||
|
if (hasKey(key)) {
|
||||||
|
return userDefaults.doubleForKey(key);
|
||||||
|
}
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// setters
|
||||||
|
export var setBoolean = function (key: string, value: boolean): void {
|
||||||
|
Common.checkKey(key);
|
||||||
|
Common.ensureValidValue(value, "boolean");
|
||||||
|
userDefaults.setBoolForKey(value, key);
|
||||||
|
userDefaults.synchronize();
|
||||||
|
}
|
||||||
|
|
||||||
|
export var setString = function (key: string, value: string): void {
|
||||||
|
Common.checkKey(key);
|
||||||
|
Common.ensureValidValue(value, "string");
|
||||||
|
userDefaults.setObjectForKey(value, key);
|
||||||
|
userDefaults.synchronize();
|
||||||
|
}
|
||||||
|
|
||||||
|
export var setNumber = function (key: string, value: number): void {
|
||||||
|
Common.checkKey(key);
|
||||||
|
Common.ensureValidValue(value, "number");
|
||||||
|
userDefaults.setDoubleForKey(value, key);
|
||||||
|
userDefaults.synchronize();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
these are commented out to be used only if requested by users or otherwise needed
|
||||||
|
|
||||||
|
import utils_module = require("Utils/utils_ios");
|
||||||
|
|
||||||
|
export var getStringArray = function (key: string, defaultValue?: string[]): string[] {
|
||||||
|
Common.checkKey(key);
|
||||||
|
if (hasKey(key)) {
|
||||||
|
var nsArray = userDefaults.stringArrayForKey(key);
|
||||||
|
var jsArray = utils_module.Collections.nsArrayToJSArray(nsArray);
|
||||||
|
return jsArray;
|
||||||
|
}
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
export var getInt = function (key: string, defaultValue?: number): number {
|
||||||
|
Common.checkKey(key);
|
||||||
|
if (hasKey(key)) {
|
||||||
|
return userDefaults.integerForKey(key);
|
||||||
|
}
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
export var getLong = function (key: string, defaultValue?: number): number {
|
||||||
|
Common.checkKey(key);
|
||||||
|
if (hasKey(key)) {
|
||||||
|
return Math.ceil(userDefaults.doubleForKey(key));
|
||||||
|
}
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
export var setStringArray = function (key: string, values: string[]): void {
|
||||||
|
Common.checkKey(key);
|
||||||
|
var nsArray = utils_module.Collections.jsArrayToNSArray(values);
|
||||||
|
userDefaults.setObjectForKey(nsArray, key);
|
||||||
|
userDefaults.synchronize();
|
||||||
|
}
|
||||||
|
|
||||||
|
export var setInt = function (key: string, value: number): void {
|
||||||
|
Common.checkKey(key);
|
||||||
|
userDefaults.setIntegerForKey(value, key);
|
||||||
|
userDefaults.synchronize();
|
||||||
|
}
|
||||||
|
|
||||||
|
export var setLong = function (key: string, value: number): void {
|
||||||
|
Common.checkKey(key);
|
||||||
|
userDefaults.setDoubleForKey(Math.ceil(value), key);
|
||||||
|
userDefaults.synchronize();
|
||||||
|
}
|
||||||
|
*/
|
12
UserPreferences/user_preferences_common.ts
Normal file
12
UserPreferences/user_preferences_common.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
|
||||||
|
export var checkKey = function(key: string) : void {
|
||||||
|
if ("string" !== typeof key) {
|
||||||
|
throw new Error("key: '" + key + "' must be a string");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export var ensureValidValue = function (value: any, valueType: string): void {
|
||||||
|
if (valueType !== typeof value) {
|
||||||
|
throw new Error("value: '" + value + "' must be a " + valueType);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user