-
Notifications
You must be signed in to change notification settings - Fork 0
/
Settings.ts
119 lines (102 loc) · 2.49 KB
/
Settings.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
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
import {KeyboardTypeOptions} from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
export interface SettingsSection {
displayTitle: string;
id: string;
items: Setting[];
}
export interface Setting {
name: string;
id: string;
description?: string;
default: any;
type: 'boolean' | 'number' | 'string';
keyboardType?: KeyboardTypeOptions;
}
export const settings: SettingsSection[] = [
{
displayTitle: 'General',
id: 'general',
items: [
{
name: 'Severe weather bar',
id: 'weather_bar',
description:
'Toggles whether or not to display a sticky warning bar when severe weather is detected.',
default: true,
type: 'boolean',
},
],
},
{
displayTitle: 'Notifications',
id: 'notifications',
items: [
{
name: 'Receive notifications',
id: 'notify_all',
description: 'This option toggles all notifications.',
default: true,
type: 'boolean',
},
{
name: 'Notify Threshold',
id: 'notify_thres',
description: 'Notify if percentage matches or exceeds setting.',
default: 0.5,
type: 'number',
keyboardType: 'numeric',
},
],
},
];
const storageKey = 'settings';
interface SettingsMap {
// @ts-expect-error
[key: string];
}
class SettingsManager {
settingsMap: SettingsMap;
constructor() {
this.settingsMap = settings
.map(setting => {
return {
...setting,
items: setting.items.reduce((obj, item) => {
// @ts-expect-error
item.value = item.default;
// @ts-expect-error
obj[item.id] = item;
return obj;
}, {}),
};
})
// @ts-expect-error
.reduce((obj, item) => ((obj[item.id] = item), obj), {});
}
async init() {
const savedSettings = await AsyncStorage.getItem(storageKey);
if (savedSettings == null) return;
this.settingsMap = {
...this.settingsMap,
...JSON.parse(savedSettings),
};
}
resetSettings() {}
editSetting(
sectionId: string,
settingId: string,
currentState: any,
newValue: any,
) {
const newObject = {
...currentState,
};
newObject[sectionId]['items'][settingId].value = newValue;
return newObject;
}
async saveSettings() {
await AsyncStorage.setItem(storageKey, JSON.stringify(this.settingsMap));
}
}
export default new SettingsManager();