-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTagData.js
More file actions
134 lines (112 loc) · 3.17 KB
/
Copy pathTagData.js
File metadata and controls
134 lines (112 loc) · 3.17 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
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
133
134
import AsyncStorage from "@react-native-async-storage/async-storage";
NodeDataKey = 'userDataRoot';
InitialData = { path: 'root', children: new Array() };
const getParsedNodeData = async () => {
let jsonData = JSON.parse(await AsyncStorage.getItem(NodeDataKey));
if (!jsonData || jsonData.path != 'root') {
throw new Error("User data malformed!");
}
return jsonData;
}
const saveNodeData = async (rootNode) => {
const jsonStringData = JSON.stringify(rootNode);
await AsyncStorage.setItem(NodeDataKey, jsonStringData);
}
const getNodeByPath = (path, node) => {
let pathCopy = path.slice(0);
let pathComponent = pathCopy.shift();
if (pathComponent != node.path) {
return null;
}
if (!pathCopy.length) {
return node;
}
for (i in node.children) {
let childNode = getNodeByPath(pathCopy, node.children[i]);
if (childNode) return childNode;
}
return null;
}
class TagData {
// Never hold a reference to this object.
constructor(rootNode) {
this.rootNode = rootNode;
}
commit = async () => {
try {
await saveNodeData(this.rootNode);
} catch (e) {
console.error("Error saving user data:");
console.error(e);
}
}
static importJSONString = async (jsonString) => {
try {
let jsonData = JSON.parse(jsonString);
if (!jsonData || jsonData.path != 'root') {
console.log("Error parsing JSON data:");
console.log("Root node should have an element \"path\" equal to \"root\".");
console.log(data);
throw new Error("FAILED: JSON data malformed!");
} else {
await saveNodeData(jsonData);
}
} catch (e) {
console.log("Error parsing JSON data:");
console.log(e);
throw new Error("Data is not JSON!");
}
}
static getRoot = async () => {
try {
const data = new TagData(await getParsedNodeData());
console.log("Loaded user data");
return data;
} catch (e) {
console.log(e);
console.log("Reverting to empty set.");
console.log(InitialData);
return new TagData(JSON.parse(JSON.stringify(InitialData)));
}
}
static clear = async () => {
await AsyncStorage.removeItem(NodeDataKey);
}
getNodeByPath = (path) => {
return getNodeByPath(path, this.rootNode);
}
toggleHighPriority = (path) => {
let node = this.getNodeByPath(path);
node.highPriority = !node.highPriority;
}
updatePath = (path, newItems, title) => {
let node = this.getNodeByPath(path);
// Early out if nothing is entered.
if (node == undefined && newItems.length == 0 && !title) {
return false;
}
if (!node) {
node = {
path: path.pop(),
children: [],
data: newItems,
title: title,
}
let parent = this.getNodeByPath(path);
parent.children.push(node);
} else {
node.data = newItems;
node.title = title;
}
return true;
}
deleteNode = (path) => {
let nodePath = path.pop();
let parentNode = this.getNodeByPath(path);
let nodeIdx = parentNode.children.findIndex((value) => {
return value.path == nodePath;
});
parentNode.children.splice(nodeIdx, 1);
}
}
export default TagData;