forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplication-settings.android.ts
More file actions
76 lines (66 loc) · 1.98 KB
/
Copy pathapplication-settings.android.ts
File metadata and controls
76 lines (66 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import common = require("./application-settings-common");
import utils = require("utils/utils");
var sharedPreferences;
function ensureSharedPreferences() {
if (!sharedPreferences) {
sharedPreferences = utils.ad.getApplicationContext().getSharedPreferences("prefs.db", 0);
}
}
function verify(key: string) {
common.checkKey(key);
ensureSharedPreferences();
}
export var hasKey = function (key: string): boolean {
verify(key);
return sharedPreferences.contains(key);
}
// getters
export var getBoolean = function (key: string, defaultValue?: boolean): boolean {
verify(key);
if (hasKey(key)) {
return sharedPreferences.getBoolean(key, false);
}
return defaultValue;
}
export var getString = function (key: string, defaultValue?: string): string {
verify(key);
if (hasKey(key)) {
return sharedPreferences.getString(key, "");
}
return defaultValue;
}
export var getNumber = function (key: string, defaultValue?: number): number {
verify(key);
if (hasKey(key)) {
return sharedPreferences.getFloat(key, float(0.0));
}
return defaultValue;
}
// setters
export var setBoolean = function (key: string, value: boolean): void {
verify(key);
common.ensureValidValue(value, "boolean");
var editor = sharedPreferences.edit();
editor.putBoolean(key, value);
editor.commit();
}
export var setString = function (key: string, value: string): void {
verify(key);
common.ensureValidValue(value, "string");
var editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
export var setNumber = function (key: string, value: number): void {
verify(key);
common.ensureValidValue(value, "number");
var editor = sharedPreferences.edit();
editor.putFloat(key, float(value));
editor.commit();
}
export var remove = function (key: string): void {
verify(key);
var editor = sharedPreferences.edit();
editor.remove(key);
editor.commit();
}