-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asyncStorage.ts
80 lines (71 loc) · 2.17 KB
/
asyncStorage.ts
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
import AsyncStorage from "@react-native-async-storage/async-storage";
import * as SecureStore from "expo-secure-store";
export const dataKeys = {
instance: "fennec-instance",
login: "fennec-login",
username: "fennec-username",
postsLimit: "fennec-settings-posts-limit",
filters: "fennec-filters",
commentFilters: "fennec-comment-filters",
readScroll: "fennec-read-scroll",
blurNsfw: "fennec-blur-nsfw",
leftHanded: "fennec-left-handed",
collapseParent: "fennec-collapse-parent",
compactPostLayout: "fennec-compact-post-layout",
theme: "fennec-theme",
hapticsOff: "fennec-haptics-off",
favCommunities: "fennec-fav-communities",
paginatedFeed: "fennec-paginated-feed",
ignoredInstances: "fennec-ignored-instances",
votingButtons: "fennec-voting-buttons",
accounts: "fennec-accounts",
lowTraffic: "fennec-low-traffic",
dynamicHeaders: "fennec-dynamic-headers",
altUpvote: "fennec-alt-upvote",
} as const;
type Keys = keyof typeof dataKeys;
type DataValues = (typeof dataKeys)[Keys];
class AsyncStoragehandler {
readData = async (key: DataValues): Promise<string | null> => {
try {
const value = await AsyncStorage.getItem(key);
if (value !== null) {
return value;
} else return null;
} catch (e) {
console.error("regular data read:", e);
}
};
setData = async (key: DataValues, value: string) => {
try {
await AsyncStorage.setItem(key, value);
} catch (e) {
console.error("regular data write:", e);
}
};
setSecureData = async (key, value) => {
try {
await SecureStore.setItemAsync(key, value);
} catch (e) {
console.error("secure data write:", e);
}
};
readSecureData = async (key: string): Promise<string | null> => {
try {
const value = await SecureStore.getItemAsync(key);
if (value !== null) {
return value;
} else return null;
} catch (e) {
console.error("secure data read:", e);
}
};
purge() {
void AsyncStorage.clear();
}
logout() {
void SecureStore.deleteItemAsync(dataKeys.login);
void AsyncStorage.removeItem(dataKeys.username);
}
}
export const asyncStorageHandler = new AsyncStoragehandler();