58 lines
1.4 KiB
TypeScript

import { SQLiteDatabase } from "expo-sqlite";
export class Settings {
static KEYS = [
"host_language",
"libretranslate_base_url",
'ui_direction',
]
constructor(public db: SQLiteDatabase) {
}
private async getValue(key: string) {
// For security...ensure key is a valid value
if (!Settings.KEYS.includes(key)) {
throw new Error(`Invalid setting: '${key}'`)
}
const query = `
SELECT ?
FROM settings
LIMIT 1`
const result = await this.db.getFirstAsync(
query
);
return result ? (result as any)[key] : null;
}
private async setValue(key: string, value: any) {
if (!Settings.KEYS.includes(key)) {
throw new Error(`Invalid setting: '${key}'`)
}
const statement = `INSERT INTO settings (${key})
SELECT '?'
ON CONFLICT DO UPDATE SET ${key} = ?`
await this.db.runAsync(statement, [value, value]);
}
async setHostLanguage(value: string) {
await this.setValue("host_language", value);
}
async getHostLanguage() {
return await this.getValue("host_language")
}
async setLibetransalteBaseUrl(value : string) {
await this.setValue("libetransalte_base_url", value)
}
async getLibretranslateBaseUrl() {
await this.getValue("libtretranslate_base_url")
}
}