Squash merge feat/expo-app: app-expo, .cursor, workflows, package.json, .husky; remove app-android, app-ios, react-app
This commit is contained in:
86
app-expo/src/core/settings/app-settings.ts
Normal file
86
app-expo/src/core/settings/app-settings.ts
Normal file
@@ -0,0 +1,86 @@
|
||||
import { Platform } from 'react-native';
|
||||
|
||||
import {
|
||||
deleteSecureItem,
|
||||
getSecureItem,
|
||||
setSecureItem,
|
||||
} from '@/core/storage/secure';
|
||||
|
||||
import { supportedLanguages, type AppLanguage } from '@/i18n/resources';
|
||||
|
||||
import { THEME_NAMES, type ThemeName } from '@/constants/theme-bridge';
|
||||
|
||||
const KEY_LANGUAGE = 'app_settings_language';
|
||||
const KEY_LARGE_TEXT = 'app_settings_large_text';
|
||||
const KEY_DARK_MODE = 'app_settings_dark_mode';
|
||||
const KEY_THEME_NAME = 'app_settings_theme_name';
|
||||
|
||||
const webFallback: Record<string, string> = {};
|
||||
|
||||
async function getStored(key: string): Promise<string | null> {
|
||||
if (Platform.OS === 'web') return webFallback[key] ?? null;
|
||||
return getSecureItem(key);
|
||||
}
|
||||
|
||||
async function setStored(key: string, value: string): Promise<void> {
|
||||
if (Platform.OS === 'web') {
|
||||
webFallback[key] = value;
|
||||
return;
|
||||
}
|
||||
await setSecureItem(key, value);
|
||||
}
|
||||
|
||||
async function deleteStored(key: string): Promise<void> {
|
||||
if (Platform.OS === 'web') {
|
||||
delete webFallback[key];
|
||||
return;
|
||||
}
|
||||
await deleteSecureItem(key);
|
||||
}
|
||||
|
||||
export async function getAppLanguage(): Promise<AppLanguage | null> {
|
||||
const v = await getStored(KEY_LANGUAGE);
|
||||
if (!v) return null;
|
||||
return supportedLanguages.includes(v as AppLanguage)
|
||||
? (v as AppLanguage)
|
||||
: null;
|
||||
}
|
||||
|
||||
export async function setAppLanguage(lang: AppLanguage): Promise<void> {
|
||||
await setStored(KEY_LANGUAGE, lang);
|
||||
}
|
||||
|
||||
export async function clearAppLanguage(): Promise<void> {
|
||||
await deleteStored(KEY_LANGUAGE);
|
||||
}
|
||||
|
||||
export async function getLargeText(): Promise<boolean> {
|
||||
const v = await getStored(KEY_LARGE_TEXT);
|
||||
return v === 'true';
|
||||
}
|
||||
|
||||
export async function setLargeText(value: boolean): Promise<void> {
|
||||
await setStored(KEY_LARGE_TEXT, value ? 'true' : 'false');
|
||||
}
|
||||
|
||||
export async function getDarkMode(): Promise<boolean> {
|
||||
const v = await getStored(KEY_DARK_MODE);
|
||||
return v === 'true';
|
||||
}
|
||||
|
||||
export async function setDarkMode(value: boolean): Promise<void> {
|
||||
await setStored(KEY_DARK_MODE, value ? 'true' : 'false');
|
||||
}
|
||||
|
||||
export async function getThemeName(): Promise<ThemeName> {
|
||||
const v = await getStored(KEY_THEME_NAME);
|
||||
if (!v || !THEME_NAMES.includes(v as ThemeName)) return 'default';
|
||||
return v as ThemeName;
|
||||
}
|
||||
|
||||
export async function setThemeName(value: ThemeName): Promise<void> {
|
||||
await setStored(KEY_THEME_NAME, value);
|
||||
}
|
||||
|
||||
export { supportedLanguages, THEME_NAMES };
|
||||
export type { AppLanguage, ThemeName };
|
||||
Reference in New Issue
Block a user