2022-09-12 22:18:27 +00:00
|
|
|
import { useEffect, useState, useRef, useContext } from 'react';
|
|
|
|
import SQLite from "react-native-sqlite-storage";
|
|
|
|
|
|
|
|
const DATABAG_DB = 'databag_v001.db';
|
|
|
|
|
|
|
|
export function useStoreContext() {
|
2022-09-14 07:27:49 +00:00
|
|
|
const [state, setState] = useState({});
|
2022-09-12 22:18:27 +00:00
|
|
|
const db = useRef(null);
|
|
|
|
|
|
|
|
const updateState = (value) => {
|
|
|
|
setState((s) => ({ ...s, ...value }))
|
|
|
|
}
|
|
|
|
|
|
|
|
const actions = {
|
2022-09-14 07:27:49 +00:00
|
|
|
init: async () => {
|
|
|
|
SQLite.DEBUG(false);
|
|
|
|
SQLite.enablePromise(true);
|
|
|
|
db.current = await SQLite.openDatabase({ name: DATABAG_DB, location: "default" });
|
|
|
|
await db.current.executeSql("CREATE TABLE IF NOT EXISTS app (key text, value text, unique(key));");
|
|
|
|
await db.current.executeSql("INSERT OR IGNORE INTO app (key, value) values ('session', null);");
|
|
|
|
return await getAppValue(db.current, 'session');
|
|
|
|
},
|
2022-09-12 22:18:27 +00:00
|
|
|
setSession: async (access) => {
|
|
|
|
await db.current.executeSql("UPDATE app SET value=? WHERE key='session';", [encodeObject(access)]);
|
|
|
|
},
|
|
|
|
clearSession: async () => {
|
|
|
|
await db.current.executeSql("UPDATE app set value=? WHERE key='session';", [null]);
|
|
|
|
},
|
2022-09-14 07:27:49 +00:00
|
|
|
getProfile: async (guid) => {
|
|
|
|
const dataId = `${guid}_profile`;
|
|
|
|
return await getAppValue(db.current, dataId, {});
|
2022-09-13 18:46:28 +00:00
|
|
|
},
|
2022-09-14 07:27:49 +00:00
|
|
|
setProfile: async (guid, profile) => {
|
|
|
|
const dataId = `${guid}_profile`;
|
|
|
|
await db.current.executeSql("UPDATE app SET value=? WHERE key='?';", [encodeObject(profile)], dataId);
|
2022-09-13 18:46:28 +00:00
|
|
|
},
|
2022-09-14 07:27:49 +00:00
|
|
|
getProfileRevision: async (guid) => {
|
|
|
|
const dataId = `${guid}_profileRevision`;
|
|
|
|
return await getAppValue(db.current, dataId, 0);
|
2022-09-13 18:46:28 +00:00
|
|
|
},
|
2022-09-14 07:27:49 +00:00
|
|
|
setProfileRevision: async (guid, revision) => {
|
|
|
|
const dataId = `${guid}_profileRevision`;
|
|
|
|
await db.current.executeSql("UPDATE app SET value=? WHERE key='?';", [encodeObject(revision)], dataId);
|
2022-09-12 22:18:27 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
return { state, actions }
|
|
|
|
}
|
|
|
|
|
|
|
|
function decodeObject(s: string) {
|
|
|
|
if(s == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return JSON.parse(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
function encodeObject(o: any) {
|
|
|
|
if(o == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return JSON.stringify(o);
|
|
|
|
}
|
|
|
|
|
|
|
|
function hasResult(res) {
|
|
|
|
if(res === undefined || res[0] === undefined || res[0].rows === undefined || res[0].rows.length == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-09-13 18:46:28 +00:00
|
|
|
async function getAppValue(sql: SQLite.SQLiteDatabase, id: string, unset) {
|
2022-09-12 22:18:27 +00:00
|
|
|
const res = await sql.executeSql(`SELECT * FROM app WHERE key='${id}';`);
|
|
|
|
if (hasResult(res)) {
|
|
|
|
return decodeObject(res[0].rows.item(0).value);
|
|
|
|
}
|
2022-09-13 18:46:28 +00:00
|
|
|
return unset;
|
2022-09-12 22:18:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|