UNPKG

3.51 kBJavaScriptView Raw
1import * as common from './application-settings-common';
2import { Application } from '../application';
3let sharedPreferences;
4function ensureSharedPreferences() {
5 if (!sharedPreferences) {
6 sharedPreferences = Application.android.getNativeApplication().getApplicationContext().getSharedPreferences('prefs.db', 0);
7 }
8}
9function verify(key) {
10 if (!common.checkKey(key)) {
11 return false;
12 }
13 ensureSharedPreferences();
14 return true;
15}
16export function hasKey(key) {
17 if (!verify(key)) {
18 return;
19 }
20 return sharedPreferences.contains(key);
21}
22// getters
23export function getBoolean(key, defaultValue) {
24 if (!verify(key)) {
25 return;
26 }
27 if (hasKey(key)) {
28 return sharedPreferences.getBoolean(key, false);
29 }
30 return defaultValue;
31}
32export function getString(key, defaultValue) {
33 if (!verify(key)) {
34 return;
35 }
36 if (hasKey(key)) {
37 return sharedPreferences.getString(key, '');
38 }
39 return defaultValue;
40}
41export function getNumber(key, defaultValue) {
42 if (!verify(key)) {
43 return;
44 }
45 if (hasKey(key)) {
46 let val;
47 // TODO: Remove this migration step in a future release
48 try {
49 val = sharedPreferences.getLong(key, long(0));
50 }
51 catch (err) {
52 // If value is old, it might have been stored as a float so we store it anew as a long value to avoid errors
53 const oldVal = sharedPreferences.getFloat(key, float(0.0));
54 setNumber(key, oldVal);
55 val = sharedPreferences.getLong(key, long(0));
56 }
57 // SharedPreferences has no getter or setter for double so we retrieve value as a long and convert it to double
58 return java.lang.Double.longBitsToDouble(val);
59 }
60 return defaultValue;
61}
62// setters
63export function setBoolean(key, value) {
64 if (!verify(key)) {
65 return;
66 }
67 if (!common.ensureValidValue(value, 'boolean')) {
68 return;
69 }
70 const editor = sharedPreferences.edit();
71 editor.putBoolean(key, value);
72 editor.apply();
73}
74export function setString(key, value) {
75 if (!verify(key)) {
76 return;
77 }
78 if (!common.ensureValidValue(value, 'string')) {
79 return;
80 }
81 const editor = sharedPreferences.edit();
82 editor.putString(key, value);
83 editor.apply();
84}
85export function setNumber(key, value) {
86 if (!verify(key)) {
87 return;
88 }
89 if (!common.ensureValidValue(value, 'number')) {
90 return;
91 }
92 const editor = sharedPreferences.edit();
93 // SharedPreferences has no getter or setter for double so we convert value and store it as a long
94 editor.putLong(key, java.lang.Double.doubleToRawLongBits(double(value)));
95 editor.apply();
96}
97export function remove(key) {
98 if (!verify(key)) {
99 return;
100 }
101 const editor = sharedPreferences.edit();
102 editor.remove(key);
103 editor.apply();
104}
105export function clear() {
106 ensureSharedPreferences();
107 sharedPreferences.edit().clear().apply();
108}
109export function flush() {
110 ensureSharedPreferences();
111 return sharedPreferences.edit().commit();
112}
113export function getAllKeys() {
114 ensureSharedPreferences();
115 const mappedPreferences = sharedPreferences.getAll();
116 const iterator = mappedPreferences.keySet().iterator();
117 const result = [];
118 while (iterator.hasNext()) {
119 const key = iterator.next();
120 result.push(key);
121 }
122 return result;
123}
124//# sourceMappingURL=index.android.js.map
\No newline at end of file