diff options
| author | MohamedBassem <me@mbassem.com> | 2024-03-11 11:05:36 +0000 |
|---|---|---|
| committer | MohamedBassem <me@mbassem.com> | 2024-03-11 11:05:36 +0000 |
| commit | 999ed977a588b2c3b2055f18db4218d77882a1a1 (patch) | |
| tree | be926d82bcd495878956483ee7b21a172666566a /packages/mobile/lib/settings.ts | |
| parent | e774f1ad01d4755651a82ed100c26587774c8ee0 (diff) | |
| download | karakeep-999ed977a588b2c3b2055f18db4218d77882a1a1.tar.zst | |
mobile: Add support for app settings
Diffstat (limited to 'packages/mobile/lib/settings.ts')
| -rw-r--r-- | packages/mobile/lib/settings.ts | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/packages/mobile/lib/settings.ts b/packages/mobile/lib/settings.ts new file mode 100644 index 00000000..85296cfa --- /dev/null +++ b/packages/mobile/lib/settings.ts @@ -0,0 +1,39 @@ +import * as SecureStore from "expo-secure-store"; +import { useEffect, useState } from "react"; + +const SETTING_NAME = "settings"; + +export type Settings = { + apiKey: string; + address: string; +}; + +export default function useAppSettings() { + const [settings, setSettings] = useState<Settings>({ + apiKey: "", + address: "", + }); + + useEffect(() => { + SecureStore.setItemAsync(SETTING_NAME, JSON.stringify(settings)); + }, [settings]); + + useEffect(() => { + SecureStore.getItemAsync(SETTING_NAME).then((val) => { + if (!val) { + return; + } + setSettings(JSON.parse(val)); + }); + }, []); + + return { settings, setSettings }; +} + +export async function getAppSettings() { + const val = await SecureStore.getItemAsync(SETTING_NAME); + if (!val) { + return null; + } + return JSON.parse(val) as Settings; +} |
