Merge pull request #282 from NativeScript/local-settings-rename

local-settings renamed to application-settings
This commit is contained in:
Vladimir Enchev
2015-04-14 17:13:15 +03:00
13 changed files with 96 additions and 112 deletions

View File

@ -548,7 +548,7 @@
<TypeScriptCompile Include="timer\timer.ios.ts">
<DependentUpon>timer.d.ts</DependentUpon>
</TypeScriptCompile>
<TypeScriptCompile Include="apps\tests\local-settings-tests.ts" />
<TypeScriptCompile Include="apps\tests\application-settings-tests.ts" />
<TypeScriptCompile Include="file-system\file-system-access.android.ts">
<DependentUpon>file-system-access.d.ts</DependentUpon>
</TypeScriptCompile>
@ -558,14 +558,14 @@
<TypeScriptCompile Include="application\application-common.ts">
<DependentUpon>application.d.ts</DependentUpon>
</TypeScriptCompile>
<TypeScriptCompile Include="local-settings\local-settings.d.ts" />
<TypeScriptCompile Include="local-settings\local-settings.android.ts">
<DependentUpon>local-settings.d.ts</DependentUpon>
<TypeScriptCompile Include="application-settings\application-settings.d.ts" />
<TypeScriptCompile Include="application-settings\application-settings.android.ts">
<DependentUpon>application-settings.d.ts</DependentUpon>
</TypeScriptCompile>
<TypeScriptCompile Include="local-settings\local-settings.ios.ts">
<DependentUpon>local-settings.d.ts</DependentUpon>
<TypeScriptCompile Include="application-settings\application-settings.ios.ts">
<DependentUpon>application-settings.d.ts</DependentUpon>
</TypeScriptCompile>
<TypeScriptCompile Include="local-settings\local-settings-common.ts" />
<TypeScriptCompile Include="application-settings\application-settings-common.ts" />
<TypeScriptCompile Include="apps\tests\timer-tests.ts" />
</ItemGroup>
<ItemGroup>
@ -1153,7 +1153,7 @@
<Content Include="globals\Readme.md" />
<Content Include="timer\Readme.md" />
<Content Include="apps\tests\Readme.md" />
<Content Include="local-settings\Readme.md" />
<Content Include="application-settings\Readme.md" />
</ItemGroup>
<ItemGroup>
<Content Include="ui\dialogs\Readme.md" />
@ -1192,7 +1192,7 @@
</Content>
</ItemGroup>
<ItemGroup>
<Content Include="local-settings\package.json">
<Content Include="application-settings\package.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

View File

@ -0,0 +1,3 @@

Used to store strings, booleans and numbers in built-in key/value store
Uses SharedPreferences on Android and NSUserDefaults on iOS

View File

@ -1,5 +1,5 @@
import appModule = require("application");
import Common = require("local-settings/local-settings-common");
import Common = require("application-settings/application-settings-common");
var sharedPreferences = appModule.android.context.getSharedPreferences("prefs.db", 0);

View File

@ -1,7 +1,7 @@
/**
* Allows you to save and restore any kind of information related to your application.
*/
declare module "local-settings" {
declare module "application-settings" {
/**
* Checks whether such a key exists.
* @param key The key to check for.

View File

@ -1,4 +1,4 @@
import Common = require("local-settings/local-settings-common");
import Common = require("application-settings/application-settings-common");
var userDefaults = NSUserDefaults.standardUserDefaults();

View File

@ -0,0 +1,2 @@
{ "name" : "application-settings",
"main" : "application-settings.js" }

View File

@ -1,6 +1,6 @@
import observable = require("data/observable");
import dialogs = require("ui/dialogs");
import localSettings = require("local-settings");
import appSettings = require("application-settings");
import button = require("ui/button");
var everlive = require("./lib/everlive");
@ -36,7 +36,7 @@ var sessions: Array<SessionModel> = new Array<SessionModel>();
var FAVOURITES = "FAVOURITES";
var favourites: Array<string>;
try {
favourites = <Array<string>>JSON.parse(localSettings.getString(FAVOURITES, "[]"));
favourites = <Array<string>>JSON.parse(appSettings.getString(FAVOURITES, "[]"));
}
catch (error) {
console.log("Error while retrieveing favourites: " + error);
@ -61,7 +61,7 @@ function removeFromFavourites(session: SessionModel) {
function updateFavourites() {
var newValue = JSON.stringify(favourites);
localSettings.setString(FAVOURITES, newValue);
appSettings.setString(FAVOURITES, newValue);
}
var el = new everlive("mzacGkKPFlZUfbMq");

View File

@ -1,5 +1,5 @@
import observable = require("data/observable");
import localSettings = require("local-settings");
import appSettings = require("application-settings");
import dialogs = require("ui/dialogs");
var NAME = "settings-name";
@ -11,45 +11,45 @@ var SOUND_VOLUME = "settings-sound-value";
export class SettingsViewModel extends observable.Observable {
get name(): string {
return localSettings.getString(NAME, "John Doe");
return appSettings.getString(NAME, "John Doe");
}
set name(value: string) {
localSettings.setString(NAME, value);
appSettings.setString(NAME, value);
}
get height(): string {
return localSettings.getString(HEIGHT, "180");
return appSettings.getString(HEIGHT, "180");
}
set height(value: string) {
localSettings.setString(HEIGHT, value);
appSettings.setString(HEIGHT, value);
}
get weight(): string {
return localSettings.getString(WEIGHT, "80");
return appSettings.getString(WEIGHT, "80");
}
set weight(value: string) {
localSettings.setString(WEIGHT, value);
appSettings.setString(WEIGHT, value);
}
get vibrateEnabled(): boolean {
return localSettings.getBoolean(VIBRATE, true);
return appSettings.getBoolean(VIBRATE, true);
}
set vibrateEnabled(value: boolean) {
localSettings.setBoolean(VIBRATE, value);
appSettings.setBoolean(VIBRATE, value);
}
get soundEnabled(): boolean {
return localSettings.getBoolean(SOUND, true);
return appSettings.getBoolean(SOUND, true);
}
set soundEnabled(value: boolean) {
localSettings.setBoolean(SOUND, value);
appSettings.setBoolean(SOUND, value);
}
get soundVolume(): number {
return localSettings.getNumber(SOUND_VOLUME, 100);
return appSettings.getNumber(SOUND_VOLUME, 100);
}
set soundVolume(value: number) {
localSettings.setNumber(SOUND_VOLUME, value);
appSettings.setNumber(SOUND_VOLUME, value);
}
public promptName(args: observable.EventData) {
@ -65,7 +65,7 @@ export var settingsViewModel = new SettingsViewModel();
// Pure JavaScript code:
//var observable = require("data/observable");
//var localSettings = require("local-settings");
//var appSettings = require("application-settings");
//var NAME = "settings-name";
//var HEIGHT = "settings-height";
@ -76,43 +76,43 @@ export var settingsViewModel = new SettingsViewModel();
//var settings = new observable.Observable();
//Object.defineProperty(settings, "name", {
// get: function () { return localSettings.getString(NAME, "John Doe"); },
// set: function (value) { localSettings.setString(NAME, value); },
// get: function () { return appSettings.getString(NAME, "John Doe"); },
// set: function (value) { appSettings.setString(NAME, value); },
// enumerable: true,
// configurable: true
//});
//Object.defineProperty(settings, "height", {
// get: function () { return localSettings.getString(HEIGHT, "180"); },
// set: function (value) { localSettings.setString(HEIGHT, value); },
// get: function () { return appSettings.getString(HEIGHT, "180"); },
// set: function (value) { appSettings.setString(HEIGHT, value); },
// enumerable: true,
// configurable: true
//});
//Object.defineProperty(settings, "weight", {
// get: function () { return localSettings.getString(WEIGHT, "80"); },
// set: function (value) { localSettings.setString(WEIGHT, value); },
// get: function () { return appSettings.getString(WEIGHT, "80"); },
// set: function (value) { appSettings.setString(WEIGHT, value); },
// enumerable: true,
// configurable: true
//});
//Object.defineProperty(settings, "vibrateEnabled", {
// get: function () { return localSettings.getBoolean(VIBRATE, true); },
// set: function (value) { localSettings.setBoolean(VIBRATE, value); },
// get: function () { return appSettings.getBoolean(VIBRATE, true); },
// set: function (value) { appSettings.setBoolean(VIBRATE, value); },
// enumerable: true,
// configurable: true
//});
//Object.defineProperty(settings, "soundEnabled", {
// get: function () { return localSettings.getBoolean(SOUND, true); },
// set: function (value) { localSettings.setBoolean(SOUND, value); },
// get: function () { return appSettings.getBoolean(SOUND, true); },
// set: function (value) { appSettings.setBoolean(SOUND, value); },
// enumerable: true,
// configurable: true
//});
//Object.defineProperty(settings, "soundVolume", {
// get: function () { return localSettings.getNumber(SOUND_VOLUME, 100); },
// set: function (value) { localSettings.setNumber(SOUND_VOLUME, value); },
// get: function () { return appSettings.getNumber(SOUND_VOLUME, 100); },
// set: function (value) { appSettings.setNumber(SOUND_VOLUME, value); },
// enumerable: true,
// configurable: true
//});

View File

@ -1,8 +1,8 @@
// <snippet module="local-settings" title="local-settings">
// <snippet module="application-settings" title="application-settings">
// # Local Settings
// Using local settings methods requires to load "local settings" module.
// ``` JavaScript
var LocalSettings = require("local-settings");
var appSettings = require("application-settings");
// ```
// </snippet>
var TKUnit = require("./TKUnit");
@ -14,118 +14,118 @@ var noStringKey: string = "noStringKey";
var noBoolKey: string = "noBoolKey";
var noNumberKey: string = "noNumberKey";
// <snippet module="local-settings" title="local-settings">
// <snippet module="application-settings" title="application-settings">
// ## Working with string, number and boolean values
// </snippet>
export var testBoolean = function () {
LocalSettings.setBoolean(boolKey, false);
var boolValueBefore = LocalSettings.getBoolean(boolKey);
TKUnit.assert(false === boolValueBefore, "Cannot set boolean to false, currently it is: " + LocalSettings.getBoolean(boolKey));
appSettings.setBoolean(boolKey, false);
var boolValueBefore = appSettings.getBoolean(boolKey);
TKUnit.assert(false === boolValueBefore, "Cannot set boolean to false, currently it is: " + appSettings.getBoolean(boolKey));
// <snippet module="local-settings" title="local-settings">
// <snippet module="application-settings" title="application-settings">
// ### Set and get boolean value and provide default value in case it is not set
// ``` JavaScript
LocalSettings.setBoolean("boolKey", true);
var boolValue = LocalSettings.getBoolean("boolKey", false);
appSettings.setBoolean("boolKey", true);
var boolValue = appSettings.getBoolean("boolKey", false);
// ```
// </snippet>
TKUnit.assert(true === boolValue, "Cannot set boolean to true");
TKUnit.assert(true === LocalSettings.getBoolean(boolKey), "Cannot set boolean to true (no default)");
TKUnit.assert(true === appSettings.getBoolean(boolKey), "Cannot set boolean to true (no default)");
};
export var testString = function () {
// <snippet module="local-settings" title="local-settings">
// <snippet module="application-settings" title="application-settings">
// ### Set and get string value
// ``` JavaScript
LocalSettings.setString("stringKey", "String value");
var stringValue = LocalSettings.getString("stringKey");
appSettings.setString("stringKey", "String value");
var stringValue = appSettings.getString("stringKey");
// ```
// </snippet>
TKUnit.assert("String value" === stringValue, "Cannot set string value");
};
export var testNumber = function () {
// <snippet module="local-settings" title="local-settings">
// <snippet module="application-settings" title="application-settings">
// ### Set and get numeric value.
// We use `toFixed()` here in order to avoid floating point errors - ex: `54.321` becoming `54.320999999537`.
// Beware the result of `toFixed()` is a string not a number therefore you cannot use `===` or `!==` when comparing with a number.
// ``` JavaScript
LocalSettings.setNumber("numberKey", 54.321);
var value = parseFloat(LocalSettings.getNumber("numberKey").toFixed(3));
appSettings.setNumber("numberKey", 54.321);
var value = parseFloat(appSettings.getNumber("numberKey").toFixed(3));
// ```
// </snippet>
TKUnit.assert(54.321 === value, "Cannot set number value 54.321 != " + value);
};
export var testDefaults = function () {
// <snippet module="local-settings" title="local-settings">
// <snippet module="application-settings" title="application-settings">
// ### Reading values that are not set before while providing default value
// ``` JavaScript
var defaultValue = LocalSettings.getString("noStringKey", "No string value");
var defaultValue = appSettings.getString("noStringKey", "No string value");
//// will return "No string value" if there is no value for "noStringKey"
// ```
// </snippet>
TKUnit.assert("No string value" === defaultValue, "Bad default string value");
TKUnit.assert(true === LocalSettings.getBoolean(noBoolKey, true), "Bad default boolean value");
TKUnit.assert(123.45 === LocalSettings.getNumber(noNumberKey, 123.45), "Bad default number value");
TKUnit.assert(true === appSettings.getBoolean(noBoolKey, true), "Bad default boolean value");
TKUnit.assert(123.45 === appSettings.getNumber(noNumberKey, 123.45), "Bad default number value");
}
export var testDefaultsWithNoDefaultValueProvided = function () {
// <snippet module="local-settings" title="local-settings">
// <snippet module="application-settings" title="application-settings">
// ### Reading values that are not set before not providing default value
// ``` JavaScript
var defaultValue = LocalSettings.getString("noStringKey");
var defaultValue = appSettings.getString("noStringKey");
//// will return undefined if there is no value for "noStringKey"
// ```
// </snippet>
TKUnit.assert("undefined" === typeof defaultValue, "Default string value is not undefined");
TKUnit.assert("undefined" === typeof LocalSettings.getBoolean(noBoolKey), "Default boolean value is not undefined");
TKUnit.assert("undefined" === typeof LocalSettings.getNumber(noNumberKey), "Default number value is not undefined");
TKUnit.assert("undefined" === typeof appSettings.getBoolean(noBoolKey), "Default boolean value is not undefined");
TKUnit.assert("undefined" === typeof appSettings.getNumber(noNumberKey), "Default number value is not undefined");
};
// <snippet module="local-settings" title="local-settings">
// <snippet module="application-settings" title="application-settings">
// ## Other functions
// </snippet>
export var testHasKey = function () {
// <snippet module="local-settings" title="local-settings">
// <snippet module="application-settings" title="application-settings">
// ### Checking for existence of value for key
// ``` JavaScript
var hasKey = LocalSettings.hasKey("noBoolKey");
var hasKey = appSettings.hasKey("noBoolKey");
//// will return false if there is no value for "noBoolKey"
// ```
// </snippet>
TKUnit.assert(!hasKey, "There is a key: " + noBoolKey);
TKUnit.assert(!LocalSettings.hasKey(noStringKey), "There is a key: " + noStringKey);
TKUnit.assert(!LocalSettings.hasKey(noNumberKey), "There is a key: " + noNumberKey);
TKUnit.assert(!appSettings.hasKey(noStringKey), "There is a key: " + noStringKey);
TKUnit.assert(!appSettings.hasKey(noNumberKey), "There is a key: " + noNumberKey);
TKUnit.assert(LocalSettings.hasKey(boolKey), "There is no key: " + boolKey);
TKUnit.assert(LocalSettings.hasKey(stringKey), "There is no key: " + stringKey);
TKUnit.assert(LocalSettings.hasKey(numberKey), "There is no key: " + numberKey);
TKUnit.assert(appSettings.hasKey(boolKey), "There is no key: " + boolKey);
TKUnit.assert(appSettings.hasKey(stringKey), "There is no key: " + stringKey);
TKUnit.assert(appSettings.hasKey(numberKey), "There is no key: " + numberKey);
};
export var testRemove = function () {
// <snippet module="local-settings" title="local-settings">
// <snippet module="application-settings" title="application-settings">
// ### Removing value for key
// ``` JavaScript
LocalSettings.remove("boolKey");
appSettings.remove("boolKey");
// ```
// </snippet>
TKUnit.assert(!LocalSettings.hasKey(boolKey), "Failed to remove key: " + boolKey);
TKUnit.assert(!appSettings.hasKey(boolKey), "Failed to remove key: " + boolKey);
LocalSettings.remove(stringKey);
TKUnit.assert(!LocalSettings.hasKey(stringKey), "Failed to remove key: " + stringKey);
appSettings.remove(stringKey);
TKUnit.assert(!appSettings.hasKey(stringKey), "Failed to remove key: " + stringKey);
LocalSettings.remove(numberKey);
TKUnit.assert(!LocalSettings.hasKey(numberKey), "Failed to remove key: " + numberKey);
appSettings.remove(numberKey);
TKUnit.assert(!appSettings.hasKey(numberKey), "Failed to remove key: " + numberKey);
};
export var testInvalidKey = function () {
try {
LocalSettings.hasKey(undefined);
appSettings.hasKey(undefined);
TKUnit.assert(false, "There is a key undefined");
}
catch (e) {
@ -133,7 +133,7 @@ export var testInvalidKey = function () {
}
try {
LocalSettings.hasKey(null);
appSettings.hasKey(null);
TKUnit.assert(false, "There is a key null");
}
catch (e) {
@ -141,19 +141,19 @@ export var testInvalidKey = function () {
}
try {
LocalSettings.hasKey(123);
appSettings.hasKey(123);
TKUnit.assert(false, "There is a key number");
}
catch (e) {
// we should receive an exception here
}
LocalSettings.hasKey("string");
appSettings.hasKey("string");
};
export var testInvalidValue = function () {
try {
LocalSettings.setBoolean(boolKey, "str");
appSettings.setBoolean(boolKey, "str");
TKUnit.assert(false, "There is a key undefined");
}
catch (e) {
@ -161,7 +161,7 @@ export var testInvalidValue = function () {
}
try {
LocalSettings.setBoolean(boolKey, 123);
appSettings.setBoolean(boolKey, 123);
TKUnit.assert(false, "There is a key undefined");
}
catch (e) {
@ -169,7 +169,7 @@ export var testInvalidValue = function () {
}
try {
LocalSettings.setString(boolKey, true);
appSettings.setString(boolKey, true);
TKUnit.assert(false, "There is a key undefined");
}
catch (e) {
@ -177,7 +177,7 @@ export var testInvalidValue = function () {
}
try {
LocalSettings.setString(boolKey, 123);
appSettings.setString(boolKey, 123);
TKUnit.assert(false, "There is a key undefined");
}
catch (e) {
@ -185,7 +185,7 @@ export var testInvalidValue = function () {
}
try {
LocalSettings.setNumber(boolKey, true);
appSettings.setNumber(boolKey, true);
TKUnit.assert(false, "There is a key undefined");
}
catch (e) {
@ -193,7 +193,7 @@ export var testInvalidValue = function () {
}
try {
LocalSettings.setNumber(boolKey, "123");
appSettings.setNumber(boolKey, "123");
TKUnit.assert(false, "There is a key undefined");
}
catch (e) {

View File

@ -33,7 +33,7 @@ allTests["SCROLL-VIEW"] = require("./ui/scroll-view/scroll-view-tests");
allTests["APPLICATION"] = require("./application-tests");
allTests["FILE SYSTEM"] = require("./file-system-tests");
allTests["HTTP"] = require("./http-tests");
allTests["LOCAL SETTINGS"] = require("./local-settings-tests");
allTests["APPLICATION SETTINGS"] = require("./application-settings-tests");
allTests["IMAGE SOURCE"] = require("./image-source-tests");
allTests["TIMER"] = require("./timer-tests");
allTests["COLOR"] = require("./color-tests");

View File

@ -1,19 +0,0 @@

Used to store strings, booleans and numbers in built-in key/value store
Uses SharedPreferences on Android and NSUserDefaults on iOS
Sample code:
```
UserPreferences.setBoolean("boolKey", false);
var bValue = UserPreferences.getBoolean("boolKey");
var sValue = UserPreferences.getString("noSuchStringKey", "No string value");
// will return "No string value" if there is no such value
var weHaveKey = UserPreferences.hasKey("boolKey");
if (weHaveKey) {
UserPreferences.remove("boolKey");
}
```

View File

@ -1,2 +0,0 @@
{ "name" : "local-settings",
"main" : "local-settings.js" }