feat: settings storage & theme persistence

This commit is contained in:
Adrian Castro
2024-03-19 11:05:13 +01:00
parent f1032f8033
commit 6586c9a412
4 changed files with 82 additions and 8 deletions

View File

@@ -2,6 +2,8 @@ import { setAppIcon } from "expo-dynamic-app-icon";
import { create } from "zustand";
import { immer } from "zustand/middleware/immer";
import { getTheme, saveTheme } from "~/settings";
export type ThemeStoreOption = "main" | "blue" | "gray" | "red" | "teal";
export interface ThemeStore {
@@ -10,13 +12,28 @@ export interface ThemeStore {
}
export const useThemeStore = create(
immer<ThemeStore>((set) => ({
theme: "main",
setTheme(v) {
immer<ThemeStore>((set) => {
void getTheme().then((savedTheme) => {
set((s) => {
s.theme = v;
setAppIcon(v);
s.theme = savedTheme;
});
},
})),
});
return {
theme: "main",
setTheme: (newTheme) => {
setAppIcon(newTheme);
saveTheme(newTheme)
.then(() => {
set((s) => {
s.theme = newTheme;
});
})
.catch((error) => {
console.error("Failed to save theme:", error);
});
},
};
}),
);