mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 11:42:04 +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.makeKeyAndVisible();
|
||||
|
||||
if (appModule.onLaunch) {
|
||||
this.window.rootViewController = appModule.onLaunch();
|
||||
if (exports.onLaunch) {
|
||||
this.window.rootViewController = exports.onLaunch();
|
||||
} else {
|
||||
log("Missing TK.UI.Application.current.onLaunch");
|
||||
log("Missing Application.onLaunch");
|
||||
}
|
||||
|
||||
log("applicationDidFinishLaunchingWithOptions finished.");
|
||||
@ -66,8 +66,8 @@ class iOSApplication {
|
||||
|
||||
applicationDidBecomeActive: function (application) {
|
||||
log("applicationDidBecomeActive: " + application);
|
||||
if (appModule.onResume) {
|
||||
appModule.onResume();
|
||||
if (exports.onResume) {
|
||||
exports.onResume();
|
||||
}
|
||||
},
|
||||
|
||||
@ -77,8 +77,8 @@ class iOSApplication {
|
||||
|
||||
applicationDidEnterBackground: function (application) {
|
||||
log("applicationDidEnterBackground: " + application);
|
||||
if (appModule.onSuspend) {
|
||||
appModule.onSuspend();
|
||||
if (exports.onSuspend) {
|
||||
exports.onSuspend();
|
||||
}
|
||||
},
|
||||
|
||||
@ -88,15 +88,15 @@ class iOSApplication {
|
||||
|
||||
applicationWillTerminate: function (application) {
|
||||
log("applicationWillTerminate: " + application);
|
||||
if (appModule.onExit) {
|
||||
appModule.onExit();
|
||||
if (exports.onExit) {
|
||||
exports.onExit();
|
||||
}
|
||||
},
|
||||
|
||||
applicationDidReceiveMemoryWarning: function (application) {
|
||||
log("applicationDidReceiveMemoryWarning: " + application);
|
||||
if (appModule.onLowMemory) {
|
||||
appModule.onLowMemory();
|
||||
if (exports.onLowMemory) {
|
||||
exports.onLowMemory();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,19 +1,14 @@
|
||||
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;
|
||||
|
||||
|
@ -174,12 +174,16 @@
|
||||
<TypeScriptCompile Include="timer\timer.android.ts">
|
||||
<DependentUpon>timer.d.ts</DependentUpon>
|
||||
</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.ios.ts">
|
||||
<DependentUpon>timer.d.ts</DependentUpon>
|
||||
</TypeScriptCompile>
|
||||
<Content Include="_references.ts" />
|
||||
<TypeScriptCompile Include="Console\console.d.ts" />
|
||||
<Content Include="Image\Readme.md" />
|
||||
</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 {
|
||||
private sharedPreferences: any;
|
||||
var sharedPreferences = appModule.android.context.getSharedPreferences("prefs.db", 0);
|
||||
|
||||
constructor() {
|
||||
this.sharedPreferences = appModule.android.context.getSharedPreferences("prefs.db", 0);
|
||||
export var hasKey = function (key: string): boolean {
|
||||
Common.checkKey(key);
|
||||
return sharedPreferences.contains(key);
|
||||
}
|
||||
|
||||
// getters
|
||||
export var getBoolean = function (key: string, defaultValue?: boolean): boolean {
|
||||
Common.checkKey(key);
|
||||
if (hasKey(key)) {
|
||||
return sharedPreferences.getBoolean(key, false);
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public containsKey(key: string): boolean {
|
||||
return this.sharedPreferences.contains(key);
|
||||
export var getString = function(key: string, defaultValue?: string): string {
|
||||
Common.checkKey(key);
|
||||
if (hasKey(key)) {
|
||||
return sharedPreferences.getString(key, "");
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public getBoolean(key: string, defaultValue?: boolean): boolean {
|
||||
if ("undefined" == typeof defaultValue) {
|
||||
defaultValue = false;
|
||||
}
|
||||
return this.sharedPreferences.getBoolean(key, 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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
// 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();
|
||||
}
|
||||
|
||||
public getInt(key: string, defaultValue?: number): number {
|
||||
if ("undefined" == typeof defaultValue) {
|
||||
defaultValue = 0;
|
||||
}
|
||||
return this.sharedPreferences.getInt(key, defaultValue);
|
||||
}
|
||||
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();
|
||||
}
|
||||
|
||||
public getLong(key: string, defaultValue?: number): number {
|
||||
if ("undefined" == typeof defaultValue) {
|
||||
defaultValue = 0;
|
||||
}
|
||||
return this.sharedPreferences.getLong(key, defaultValue);
|
||||
}
|
||||
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();
|
||||
}
|
||||
|
||||
public getString(key: string, defaultValue?: string): string {
|
||||
if ("undefined" == typeof defaultValue) {
|
||||
defaultValue = null; // is this ok?
|
||||
}
|
||||
return this.sharedPreferences.getString(key, defaultValue);
|
||||
}
|
||||
/*
|
||||
these are commented out to be used only if requested by users or otherwise needed
|
||||
|
||||
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);
|
||||
}
|
||||
import utils_module = require("Utils/utils_android");
|
||||
|
||||
public setBoolean(key: string, value: boolean) {
|
||||
var editor = this.sharedPreferences.edit();
|
||||
editor.putBoolean(key, value);
|
||||
editor.commit();
|
||||
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);
|
||||
}
|
||||
|
||||
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();
|
||||
export var getInt = function (key: string, defaultValue?: number): number {
|
||||
Common.checkKey(key);
|
||||
if (hasKey(key)) {
|
||||
return sharedPreferences.getInt(key, 0);
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public setInt(key: string, value: number) {
|
||||
var editor = this.sharedPreferences.edit();
|
||||
editor.putInt(key, value);
|
||||
editor.commit();
|
||||
export var getLong = function (key: string, defaultValue?: number): number {
|
||||
Common.checkKey(key);
|
||||
if (hasKey(key)) {
|
||||
return sharedPreferences.getLong(key, long(0));
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public setLong(key: string, value: number) {
|
||||
var editor = this.sharedPreferences.edit();
|
||||
editor.putLong(key, long(value));
|
||||
editor.commit();
|
||||
}
|
||||
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();
|
||||
}
|
||||
|
||||
public setString(key: string, value: string) {
|
||||
var editor = this.sharedPreferences.edit();
|
||||
editor.putString(key, value);
|
||||
editor.commit();
|
||||
}
|
||||
export var setInt = function (key: string, value: number): void {
|
||||
Common.checkKey(key);
|
||||
var editor = sharedPreferences.edit();
|
||||
editor.putInt(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();
|
||||
}
|
||||
}
|
||||
export var setLong = function (key: string, value: number): void {
|
||||
Common.checkKey(key);
|
||||
var editor = sharedPreferences.edit();
|
||||
editor.putLong(key, long(value));
|
||||
editor.commit();
|
||||
}
|
||||
|
||||
*/
|
71
UserPreferences/user_preferences.d.ts
vendored
71
UserPreferences/user_preferences.d.ts
vendored
@ -12,4 +12,73 @@
|
||||
setLong(key: string, value: number);
|
||||
setString(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);
|
||||
return (null != userDefaults.objectForKey(key)) ? true : false;
|
||||
}
|
||||
|
||||
constructor() {
|
||||
this.userDefaults = Foundation.NSUserDefaults.standardUserDefaults();
|
||||
// getters
|
||||
export var getBoolean = function (key: string, defaultValue?: boolean): boolean {
|
||||
Common.checkKey(key);
|
||||
if (hasKey(key)) {
|
||||
return userDefaults.boolForKey(key);
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public containsKey(key: string): boolean {
|
||||
// FIXME: is there a better way to do this check?
|
||||
return this.userDefaults.objectForKey(key) ? true : false;
|
||||
export var getString = function (key: string, defaultValue?: string): string {
|
||||
Common.checkKey(key);
|
||||
if (hasKey(key)) {
|
||||
return userDefaults.stringForKey(key);
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public getBoolean(key: string, defaultValue?: boolean): boolean {
|
||||
if (this.containsKey(key)) {
|
||||
return this.userDefaults.boolForKey(key);
|
||||
}
|
||||
if ("undefined" == typeof defaultValue) {
|
||||
defaultValue = false;
|
||||
}
|
||||
return defaultValue;
|
||||
export var getNumber = function (key: string, defaultValue?: number): number {
|
||||
Common.checkKey(key);
|
||||
if (hasKey(key)) {
|
||||
return userDefaults.doubleForKey(key);
|
||||
}
|
||||
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;
|
||||
}
|
||||
// setters
|
||||
export var setBoolean = function (key: string, value: boolean): void {
|
||||
Common.checkKey(key);
|
||||
Common.ensureValidValue(value, "boolean");
|
||||
userDefaults.setBoolForKey(value, key);
|
||||
userDefaults.synchronize();
|
||||
}
|
||||
|
||||
public getInt(key: string, defaultValue?: number): number {
|
||||
if (this.containsKey(key)) {
|
||||
return this.userDefaults.integerForKey(key);
|
||||
}
|
||||
if ("undefined" == typeof defaultValue) {
|
||||
defaultValue = 0;
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
export var setString = function (key: string, value: string): void {
|
||||
Common.checkKey(key);
|
||||
Common.ensureValidValue(value, "string");
|
||||
userDefaults.setObjectForKey(value, key);
|
||||
userDefaults.synchronize();
|
||||
}
|
||||
|
||||
public getLong(key: string, defaultValue?: number): number {
|
||||
if (this.containsKey(key)) {
|
||||
return this.userDefaults.integerForKey(key);
|
||||
}
|
||||
if ("undefined" == typeof defaultValue) {
|
||||
defaultValue = 0;
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
export var setNumber = function (key: string, value: number): void {
|
||||
Common.checkKey(key);
|
||||
Common.ensureValidValue(value, "number");
|
||||
userDefaults.setDoubleForKey(value, key);
|
||||
userDefaults.synchronize();
|
||||
}
|
||||
|
||||
public getString(key: string, defaultValue?: string): string {
|
||||
if (this.containsKey(key)) {
|
||||
return this.userDefaults.stringForKey(key);
|
||||
}
|
||||
if ("undefined" == typeof defaultValue) {
|
||||
defaultValue = "";
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
/*
|
||||
these are commented out to be used only if requested by users or otherwise needed
|
||||
|
||||
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;
|
||||
}
|
||||
import utils_module = require("Utils/utils_ios");
|
||||
|
||||
public setBoolean(key: string, value: boolean) {
|
||||
this.userDefaults.setBoolForKey(value, key);
|
||||
this.userDefaults.synchronize();
|
||||
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;
|
||||
}
|
||||
|
||||
public setDouble(key: string, value: number) {
|
||||
this.userDefaults.setDoubleForKey(value, key);
|
||||
this.userDefaults.synchronize();
|
||||
export var getInt = function (key: string, defaultValue?: number): number {
|
||||
Common.checkKey(key);
|
||||
if (hasKey(key)) {
|
||||
return userDefaults.integerForKey(key);
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public setInt(key: string, value: number) {
|
||||
this.userDefaults.setIntegerForKey(value, key);
|
||||
this.userDefaults.synchronize();
|
||||
export var getLong = function (key: string, defaultValue?: number): number {
|
||||
Common.checkKey(key);
|
||||
if (hasKey(key)) {
|
||||
return Math.ceil(userDefaults.doubleForKey(key));
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public setLong(key: string, value: number) {
|
||||
this.userDefaults.setIntegerForKey(value, key);
|
||||
this.userDefaults.synchronize();
|
||||
}
|
||||
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();
|
||||
}
|
||||
|
||||
public setString(key: string, value: string) {
|
||||
this.userDefaults.setObjectForKey(value, key);
|
||||
this.userDefaults.synchronize();
|
||||
}
|
||||
export var setInt = function (key: string, value: number): void {
|
||||
Common.checkKey(key);
|
||||
userDefaults.setIntegerForKey(value, key);
|
||||
userDefaults.synchronize();
|
||||
}
|
||||
|
||||
public setStrings(key: string, values: string[]) {
|
||||
var nsArray = utils_module.Collections.jsArrayToNSArray(values);
|
||||
this.userDefaults.setObjectForKey(nsArray, key);
|
||||
this.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