71 lines
1.7 KiB
TypeScript
71 lines
1.7 KiB
TypeScript
import { SQLiteDatabase } from "expo-sqlite";
|
|
import FileSystem from "expo-file-system"
|
|
import { getDb } from "./db";
|
|
|
|
export class Settings {
|
|
|
|
static KEYS = [
|
|
"host_language",
|
|
"libretranslate_base_url",
|
|
'ui_direction',
|
|
"whisper_model",
|
|
]
|
|
|
|
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 ${key}
|
|
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 = `REPLACE INTO settings (${key}) VALUES (?)`
|
|
await this.db.runAsync(statement, value);
|
|
}
|
|
|
|
async setHostLanguage(value: string) {
|
|
await this.setValue("host_language", value);
|
|
}
|
|
|
|
async getHostLanguage() {
|
|
return await this.getValue("host_language")
|
|
}
|
|
|
|
async setLibretranslateBaseUrl(value : string) {
|
|
await this.setValue("libretranslate_base_url", value)
|
|
}
|
|
|
|
async getLibretranslateBaseUrl() {
|
|
return await this.getValue("libretranslate_base_url")
|
|
}
|
|
|
|
async setWhisperModel(value : string) {
|
|
await this.setValue("whisper_model", value);
|
|
}
|
|
|
|
async getWhisperModel() {
|
|
return await this.getValue("whisper_model");
|
|
}
|
|
|
|
static async getDefault() {
|
|
return new Settings(await getDb())
|
|
}
|
|
|
|
} |