Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 829d18b

Browse files
surduAlexander Vakrilov
authored andcommitted
feat(application-settings): implemented allKeys method (NativeScript#6371)
1 parent 56a1b12 commit 829d18b

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

tests/app/application-settings/application-settings-tests.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,17 @@ export var testFlush = function () {
112112
TKUnit.assert(appSettings.hasKey(stringKey), "There is no key: " + stringKey);
113113
};
114114

115+
export var testAllKeys = function () {
116+
appSettings.setString(stringKey, "String value");
117+
appSettings.setBoolean(boolKey, true);
118+
appSettings.setNumber(numberKey, 22);
119+
120+
var allKeys = appSettings.getAllKeys();
121+
TKUnit.assert(allKeys.indexOf(stringKey) !== -1, `${stringKey} is missing from .allKeys()`);
122+
TKUnit.assert(allKeys.indexOf(boolKey) !== -1, `${boolKey} is missing from .allKeys()`);
123+
TKUnit.assert(allKeys.indexOf(numberKey) !== -1, `${numberKey} is missing from .allKeys()`);
124+
}
125+
115126
export var testInvalidKey = function () {
116127
try {
117128
appSettings.hasKey(undefined);

tns-core-modules/application-settings/application-settings.android.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,15 @@ export function clear(): void {
8383
export var flush = function (): boolean {
8484
return sharedPreferences.edit().commit();
8585
}
86+
87+
export function getAllKeys(): Array<string> {
88+
var mappedPreferences = sharedPreferences.getAll();
89+
var iterator = mappedPreferences.keySet().iterator();
90+
var result = [];
91+
while (iterator.hasNext()) {
92+
let key = iterator.next();
93+
result.push(key);
94+
}
95+
96+
return result;
97+
}

tns-core-modules/application-settings/application-settings.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,9 @@ export function clear(): void;
6767
* @return {boolean} flag indicating if changes were saved successfully to disk.
6868
*/
6969
export function flush(): boolean;
70+
71+
/**
72+
* Get all stored keys
73+
* @return Array containing all stored keys
74+
*/
75+
export function getAllKeys(): Array<string>;

tns-core-modules/application-settings/application-settings.ios.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,7 @@ export var clear = function (): void {
6565
export var flush = function (): boolean {
6666
return userDefaults.synchronize();
6767
}
68+
69+
export function getAllKeys(): Array<string> {
70+
return utils.ios.collections.nsArrayToJSArray(userDefaults.dictionaryRepresentation().allKeys);
71+
}

0 commit comments

Comments
 (0)