u
This commit is contained in:
1 parent
f0cb963d23
commit
4274b26dd9
7 files changed
+78
-103
No files matched your search
@@ -1,6 +1,4 @@
|
||||
import type { ColorSchemeName } from 'react-native'
|
||||
import AsyncStorage from '@react-native-async-storage/async-storage'
|
||||
import { Appearance } from 'react-native'
|
||||
import { create } from 'zustand'
|
||||
import { createJSONStorage, persist } from 'zustand/middleware'
|
||||
|
||||
@@ -8,56 +6,17 @@ export type ThemeScheme = 'light' | 'dark' | 'auto'
|
||||
|
||||
interface AppState {
|
||||
themeScheme: ThemeScheme
|
||||
isDark: boolean
|
||||
|
||||
setThemeScheme: (theme: ThemeScheme) => void
|
||||
syncWithSystem: () => void
|
||||
}
|
||||
|
||||
function getIsDark(theme: ThemeScheme, system: ColorSchemeName) {
|
||||
if (theme === 'auto') {
|
||||
return system === 'dark'
|
||||
}
|
||||
return theme === 'dark'
|
||||
}
|
||||
|
||||
export const useAppStore = create<AppState>()(
|
||||
persist(
|
||||
(set, get) => {
|
||||
const system = Appearance.getColorScheme()
|
||||
|
||||
return {
|
||||
themeScheme: 'auto',
|
||||
|
||||
isDark: getIsDark('auto', system),
|
||||
|
||||
setThemeScheme: (theme) => {
|
||||
const system = Appearance.getColorScheme()
|
||||
|
||||
set({
|
||||
themeScheme: theme,
|
||||
isDark: getIsDark(theme, system),
|
||||
})
|
||||
},
|
||||
|
||||
syncWithSystem: () => {
|
||||
const { themeScheme } = get()
|
||||
const system = Appearance.getColorScheme()
|
||||
|
||||
if (themeScheme === 'auto') {
|
||||
set({
|
||||
isDark: getIsDark(themeScheme, system),
|
||||
})
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
set => ({
|
||||
themeScheme: 'auto',
|
||||
setThemeScheme: theme => set({ themeScheme: theme }),
|
||||
}),
|
||||
{
|
||||
name: 'app-storage',
|
||||
partialize: state => ({
|
||||
themeScheme: state.themeScheme,
|
||||
}),
|
||||
|
||||
storage: createJSONStorage(() => AsyncStorage),
|
||||
},
|
||||
),
|
||||
|
||||
Reference in new issue
Block a user