1 | import * as Common from './application-settings-common';
|
2 | import * as utils from '../utils';
|
3 | const userDefaults = NSUserDefaults.standardUserDefaults;
|
4 | export function hasKey(key) {
|
5 | if (!Common.checkKey(key)) {
|
6 | return;
|
7 | }
|
8 | return userDefaults.objectForKey(key) !== null;
|
9 | }
|
10 |
|
11 | export function getBoolean(key, defaultValue) {
|
12 | if (!Common.checkKey(key)) {
|
13 | return;
|
14 | }
|
15 | if (hasKey(key)) {
|
16 | return userDefaults.boolForKey(key);
|
17 | }
|
18 | return defaultValue;
|
19 | }
|
20 | export function getString(key, defaultValue) {
|
21 | if (!Common.checkKey(key)) {
|
22 | return;
|
23 | }
|
24 | if (hasKey(key)) {
|
25 | return userDefaults.stringForKey(key);
|
26 | }
|
27 | return defaultValue;
|
28 | }
|
29 | export function getNumber(key, defaultValue) {
|
30 | if (!Common.checkKey(key)) {
|
31 | return;
|
32 | }
|
33 | if (hasKey(key)) {
|
34 | return userDefaults.doubleForKey(key);
|
35 | }
|
36 | return defaultValue;
|
37 | }
|
38 |
|
39 | export function setBoolean(key, value) {
|
40 | if (!Common.checkKey(key)) {
|
41 | return;
|
42 | }
|
43 | if (!Common.ensureValidValue(value, 'boolean')) {
|
44 | return;
|
45 | }
|
46 | userDefaults.setBoolForKey(value, key);
|
47 | }
|
48 | export function setString(key, value) {
|
49 | if (!Common.checkKey(key)) {
|
50 | return;
|
51 | }
|
52 | if (!Common.ensureValidValue(value, 'string')) {
|
53 | return;
|
54 | }
|
55 | userDefaults.setObjectForKey(value, key);
|
56 | }
|
57 | export function setNumber(key, value) {
|
58 | if (!Common.checkKey(key)) {
|
59 | return;
|
60 | }
|
61 | if (!Common.ensureValidValue(value, 'number')) {
|
62 | return;
|
63 | }
|
64 | userDefaults.setDoubleForKey(value, key);
|
65 | }
|
66 | export function remove(key) {
|
67 | if (!Common.checkKey(key)) {
|
68 | return;
|
69 | }
|
70 | userDefaults.removeObjectForKey(key);
|
71 | }
|
72 | export function clear() {
|
73 | userDefaults.removePersistentDomainForName(NSBundle.mainBundle.bundleIdentifier);
|
74 | }
|
75 | export function flush() {
|
76 | return userDefaults.synchronize();
|
77 | }
|
78 | export function getAllKeys() {
|
79 | return utils.ios.collections.nsArrayToJSArray(userDefaults.dictionaryRepresentation().allKeys);
|
80 | }
|
81 |
|
\ | No newline at end of file |