-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.tsx
163 lines (145 loc) · 4.4 KB
/
storage.tsx
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import * as SQLite from "expo-sqlite";
const NMWSTANDARD =
"https://raw.githubusercontent.com/ESE-Peasy/NarrateMyWay/main/app/nmwstandard.json";
// Interface for data
interface location {
code: string;
description: string;
}
type NMWEntry = {
code: string;
description: string;
icon: string;
};
type NMWTable = {
version: string;
entries: NMWEntry[];
};
async function downloadNMWCodes(url: string): Promise<NMWTable> {
return new Promise((resolve, _) => {
fetch(url, {
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
})
.then((response) => {
return response.json();
})
.then((data) => {
resolve({ version: data.version, entries: data.nmw });
});
});
}
// Storage Class
class Storage {
//
// local variables
db: SQLite.WebSQLDatabase;
constructor() {
this.db = this.createDb();
}
// Create database
createDb() {
const db = SQLite.openDatabase("database.db");
return db;
}
// Create and populate database tables
async createTable(downloadCompletedCallback: Function) {
downloadNMWCodes(NMWSTANDARD).then((nmwTable) => {
this.db.transaction((tx) => {
tx.executeSql(
"CREATE TABLE IF NOT EXISTS locationCodes (id string primary key not null, description text, categoryDepth int);"
);
tx.executeSql(
"CREATE TABLE IF NOT EXISTS versionRecord (id integer primary key not null, version text);"
);
tx.executeSql("SELECT * FROM versionRecord", [], (_, results) => {
if (results.rows.length == 0) {
tx.executeSql("INSERT INTO versionRecord (version) VALUES (?)", [
nmwTable.version,
]);
nmwTable.entries.forEach((value) => {
// Getting depth of code, which will correspond to the number of zeroed fields
const fields = value.code.split("-");
let zero_count = 0;
for (let field of fields) {
if (field == "0") {
zero_count += 1;
}
}
const categoryDepth = fields.length - zero_count;
tx.executeSql(
"INSERT INTO locationCodes (id, description, categoryDepth) VALUES (?,?,?)",
[value.code, value.description, categoryDepth]
);
});
} else if (results.rows.item(0) != nmwTable.version) {
tx.executeSql("INSERT INTO versionRecord (version) VALUES (?)", [
nmwTable.version,
]);
nmwTable.entries.forEach((value) => {
// Getting depth of code, which will correspond to the number of zeroed fields
const fields = value.code.split("-");
let zero_count = 0;
for (let field of fields) {
if (field == "0") {
zero_count += 1;
}
}
const categoryDepth = fields.length - zero_count;
tx.executeSql(
"INSERT INTO locationCodes (id, description, emblem) VALUES (?,?,?)",
[value.code, value.description, categoryDepth]
);
});
}
});
}, null);
downloadCompletedCallback();
});
}
// Clear storage
clearStorage() {
this.db.transaction((tx) => {
tx.executeSql("DROP TABLE locationCodes;");
tx.executeSql("DROP TABLE versionRecord;");
});
}
// Print the version of currently stored data
printVersionData() {
this.db.transaction((tx) => {
tx.executeSql("SELECT * FROM versionRecord", [], (_, { rows }) =>
console.log(JSON.stringify(rows))
);
});
}
lookupCodesByDepth(
categoryDepth: number,
parentCode: String,
callback: Function
) {
if (parentCode == null) {
parentCode = "0-0-0";
}
const fields = parentCode.split("-");
let regexedFields: String[] = [];
for (let field of fields) {
if (field == "0") {
regexedFields.push("%");
} else {
regexedFields.push(field);
}
}
this.db.transaction((tx) => {
tx.executeSql(
"SELECT id, description FROM locationCodes WHERE (categoryDepth=? AND id LIKE ?)",
[categoryDepth, regexedFields.join("-")],
(_, results) => {
callback(results.rows);
}
);
});
}
}
export { Storage, location };