-
Notifications
You must be signed in to change notification settings - Fork 91
/
AsyncStorage.js
132 lines (116 loc) · 2.78 KB
/
AsyncStorage.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import React, { useState, useEffect } from "react";
import AsyncStorage from "@react-native-community/async-storage";
// hook for asyncstorage
const useStorage = (key, defaultValue) => {
const [storageValue, updateStorageValue] = useState(defaultValue);
async function getStorageValue() {
let value = defaultValue;
try {
value = JSON.parse(await AsyncStorage.getItem(key)) || defaultValue;
} catch (e) {
} finally {
updateStorageValue(value);
}
}
async function updateStorage(newValue) {
try {
if (newValue === null) {
await AsyncStorage.removeItem(key);
} else {
const value = JSON.stringify(newValue);
await AsyncStorage.setItem(key, value);
}
} catch (e) {
} finally {
updateStorageValue(newValue);
}
}
useEffect(() => {
getStorageValue();
}, []);
return [storageValue, updateStorage];
};
async function saveValue(key, value) {
try {
if (value == null) {
await removeValue(key);
return { success: true };
} else {
await AsyncStorage.setItem(key, value);
return { success: true };
}
} catch (e) {
console.log("LOG_Async Storage access Failed", e);
return { error: e };
}
}
async function saveMultiValues(data) {
const mappedValues = values.map((v, i) => {
return [i, v];
});
try {
await AsyncStorage.multiSet(mappedValues);
return { success: true };
} catch (e) {
console.log("LOG_Async Storage access Failed", e);
return { error: e };
}
}
async function getValue(key) {
try {
const value = await AsyncStorage.getItem(key);
return value;
} catch (e) {
console.log("LOG_Async Storage access Failed", e);
return false;
}
}
async function getMultiValues(keys) {
let values;
try {
values = await AsyncStorage.multiGet(keys);
} catch (e) {
console.log("LOG_Async Storage access Failed", e);
return false;
}
let value;
values.forEach((v, i) => {
value[v[0]] = v[1];
});
return value;
}
async function removeValue(key) {
try {
await AsyncStorage.removeItem(key);
return { success: true };
} catch (e) {
console.log("LOG_Async Storage access Failed", e);
return { error: e };
}
}
async function removeMultiValues(keys) {
try {
await AsyncStorage.multiRemove(keys);
return { success: true };
} catch (e) {
console.log("LOG_Async Storage access Failed", e);
return { error: e };
}
}
async function getAllKeys() {
let keys = [];
try {
keys = await AsyncStorage.getAllKeys();
} catch (e) {
console.log("LOG_Async Storage access Failed", e);
}
return keys;
}
async function clearAll() {
try {
await AsyncStorage.clear();
} catch (e) {
console.log("LOG_Async Storage access Failed", e);
}
}
export default useStorage;