71 lines
1.9 KiB
TypeScript
71 lines
1.9 KiB
TypeScript
import AsyncStorage from "@react-native-async-storage/async-storage";
|
|
import { create } from "zustand";
|
|
import { createJSONStorage, persist } from "zustand/middleware";
|
|
|
|
import { NotificationPreferences, SettingsState } from "./types";
|
|
|
|
interface SettingsStore extends SettingsState {
|
|
setTheme: (theme: "light" | "dark" | "system") => void;
|
|
setContentPreviewEnabled: (enabled: boolean) => void;
|
|
setAccordionDefaultExpanded: (expanded: boolean) => void;
|
|
toggleAccordionDefaultExpanded: () => void;
|
|
setNotificationPreference: (
|
|
key: keyof NotificationPreferences,
|
|
value: boolean
|
|
) => void;
|
|
setLanguage: (language: string) => void;
|
|
resetSettings: () => void;
|
|
}
|
|
|
|
const DEFAULT_NOTIFICATIONS: NotificationPreferences = {
|
|
push: true,
|
|
clinicalUpdates: true,
|
|
weeklyDigest: false,
|
|
sound: true,
|
|
vibration: true,
|
|
};
|
|
|
|
const DEFAULT_SETTINGS: SettingsState = {
|
|
theme: "light",
|
|
contentPreviewEnabled: false,
|
|
accordionDefaultExpanded: false,
|
|
notifications: DEFAULT_NOTIFICATIONS,
|
|
language: "en",
|
|
};
|
|
|
|
export const useSettingsStore = create<SettingsStore>()(
|
|
persist(
|
|
(set) => ({
|
|
...DEFAULT_SETTINGS,
|
|
|
|
setTheme: (theme) => set({ theme }),
|
|
|
|
setContentPreviewEnabled: (contentPreviewEnabled) =>
|
|
set({ contentPreviewEnabled }),
|
|
|
|
setAccordionDefaultExpanded: (accordionDefaultExpanded) =>
|
|
set({ accordionDefaultExpanded }),
|
|
|
|
toggleAccordionDefaultExpanded: () =>
|
|
set((state) => ({
|
|
accordionDefaultExpanded: !state.accordionDefaultExpanded,
|
|
})),
|
|
|
|
setNotificationPreference: (key, value) =>
|
|
set((state) => ({
|
|
notifications: {
|
|
...state.notifications,
|
|
[key]: value,
|
|
},
|
|
})),
|
|
|
|
setLanguage: (language) => set({ language }),
|
|
|
|
resetSettings: () => set(DEFAULT_SETTINGS),
|
|
}),
|
|
{
|
|
name: "acupedia-settings-storage",
|
|
storage: createJSONStorage(() => AsyncStorage),
|
|
}
|
|
)
|
|
); |