[3739ec]: / mobile / ElderCare / dataHandler.js

Download this file

46 lines (38 with data), 1.1 kB

 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
// dataHandler.js
import AsyncStorage from '@react-native-async-storage/async-storage';
class Data {
constructor() {
this.storageKey = 'eldersData';
this.data = [];
this.loadData();
}
async loadData() {
try {
const jsonValue = await AsyncStorage.getItem(this.storageKey);
this.data = jsonValue != null ? JSON.parse(jsonValue) : [];
} catch (e) {
console.error('Error loading data', e);
this.data = [];
}
}
async saveData() {
try {
const jsonValue = JSON.stringify(this.data);
await AsyncStorage.setItem(this.storageKey, jsonValue);
} catch (e) {
console.error('Error saving data', e);
}
}
getData() {
return this.data;
}
async addData(id) {
this.data.push({ id });
await this.saveData();
}
async removeData(id) {
this.data = this.data.filter((elder) => elder.id !== id);
await this.saveData();
}
}
export default Data;