New BCL approach & BuildTasks

This commit is contained in:
atanasovg
2014-03-12 18:26:58 +02:00
commit 39b505384a
42 changed files with 2907 additions and 0 deletions

View File

@@ -0,0 +1,6 @@
Sample code:
```
// TODO: Add API usage
// TODO: We may think to expose an instance through the Application class - e.g. tk.ui.Application.userPreferences, like in Windows (Phone) 8.
```

View File

@@ -0,0 +1,103 @@
import utils_module = require("Utils/utils_android");
import app_module = require("Application/application");
export module tk {
// TODO: Think better naming and namespaces - e.g. tk.storage
export module preferences {
export class UserPreferences {
private sharedPreferences: any;
constructor() {
this.sharedPreferences = app_module.tk.ui.Application.current.android.context.getSharedPreferences("prefs.db", 0);
}
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.tk.utils.Collections.stringArrayToStringSet(defaultValue);
var res = this.sharedPreferences.getStringSet(key, hashSet);
return utils_module.tk.utils.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.tk.utils.Collections.stringArrayToStringSet(values);
editor.putStringSet(key, hashSet);
editor.commit();
}
}
}
}

19
UserPreferences/user_preferences.d.ts vendored Normal file
View File

@@ -0,0 +1,19 @@
export declare module tk {
export module preferences {
export class UserPreferences {
containsKey(key: string): boolean;
getBoolean(key: string, defaultValue?: boolean): boolean;
getDouble(key: string, defaultValue?: number): number;
getInt(key: string, defaultValue?: number): number;
getLong(key: string, defaultValue?: number): number;
getString(key: string, defaultValue?: string): string;
getStrings(key: string, defaultValue?: string[]): string[];
setBoolean(key: string, value: boolean);
setDouble(key: string, value: number);
setInt(key: string, value: number);
setLong(key: string, value: number);
setString(key: string, value: string);
setStrings(key: string, value: string[]);
}
}
}

View File

@@ -0,0 +1,114 @@
import utils_module = require("Utils/utils_ios");
module tk {
// TODO: Think better naming and namespaces - e.g. tk.storage
export module preferences {
export class UserPreferences {
private userDefaults: any;
constructor() {
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.tk.utils.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.tk.utils.Collections.jsArrayToNSArray(values);
this.userDefaults.setObjectForKey(nsArray, key);
this.userDefaults.synchronize();
}
}
}
}