-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4211 from tloncorp/ja/theme-switcher
- Loading branch information
Showing
12 changed files
with
378 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { NativeStackScreenProps } from '@react-navigation/native-stack'; | ||
import { themeSettings } from '@tloncorp/shared/db'; | ||
import { | ||
ListItem, | ||
ListItemInputOption, | ||
LoadingSpinner, | ||
Pressable, | ||
RadioControl, | ||
ScreenHeader, | ||
View, | ||
} from '@tloncorp/ui'; | ||
import { useContext, useEffect, useState } from 'react'; | ||
import { ScrollView, YStack } from 'tamagui'; | ||
import type { ThemeName } from 'tamagui'; | ||
|
||
import { useIsDarkMode } from '../../hooks/useIsDarkMode'; | ||
import { RootStackParamList } from '../../navigation/types'; | ||
import { ThemeContext, clearTheme, setTheme } from '../../provider'; | ||
|
||
type Props = NativeStackScreenProps<RootStackParamList, 'Theme'>; | ||
|
||
export function ThemeScreen(props: Props) { | ||
const { setActiveTheme } = useContext(ThemeContext); | ||
const isDarkMode = useIsDarkMode(); | ||
const [selectedTheme, setSelectedTheme] = useState<ThemeName | 'auto'>( | ||
'auto' | ||
); | ||
const [loadingTheme, setLoadingTheme] = useState<ThemeName | 'auto' | null>( | ||
null | ||
); | ||
|
||
const themes: ListItemInputOption<ThemeName | 'auto'>[] = [ | ||
{ | ||
title: 'Auto', | ||
value: 'auto', | ||
subtitle: `Uses system ${isDarkMode ? 'dark' : 'light'} theme`, | ||
}, | ||
{ title: 'Tlon Light', value: 'light' }, | ||
{ title: 'Tlon Dark', value: 'dark' }, | ||
{ title: 'Dracula', value: 'dracula' }, | ||
{ title: 'Greenscreen', value: 'greenscreen' }, | ||
{ title: 'Gruvbox', value: 'gruvbox' }, | ||
{ title: 'Monokai', value: 'monokai' }, | ||
{ title: 'Nord', value: 'nord' }, | ||
{ title: 'Peony', value: 'peony' }, | ||
{ title: 'Solarized', value: 'solarized' }, | ||
]; | ||
|
||
const handleThemeChange = async (value: ThemeName | 'auto') => { | ||
if (value === selectedTheme || loadingTheme) return; | ||
|
||
setLoadingTheme(value); | ||
try { | ||
if (value === 'auto') { | ||
await clearTheme(setActiveTheme, isDarkMode); | ||
} else { | ||
await setTheme(value, setActiveTheme); | ||
} | ||
setSelectedTheme(value); | ||
} finally { | ||
setLoadingTheme(null); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
const checkSelected = async () => { | ||
const storedTheme = await themeSettings.getValue(); | ||
setSelectedTheme(storedTheme ?? 'auto'); | ||
}; | ||
checkSelected(); | ||
}, []); | ||
|
||
return ( | ||
<> | ||
<ScreenHeader | ||
title="Theme" | ||
backAction={() => props.navigation.goBack()} | ||
/> | ||
<ScrollView> | ||
<YStack flex={1} padding="$l"> | ||
{themes.map((theme) => ( | ||
<Pressable | ||
key={theme.value} | ||
disabled={loadingTheme !== null} | ||
onPress={() => handleThemeChange(theme.value)} | ||
borderRadius="$xl" | ||
> | ||
<ListItem> | ||
<ListItem.MainContent> | ||
<ListItem.Title>{theme.title}</ListItem.Title> | ||
{theme.subtitle && ( | ||
<ListItem.Subtitle>{theme.subtitle}</ListItem.Subtitle> | ||
)} | ||
</ListItem.MainContent> | ||
<ListItem.EndContent> | ||
{loadingTheme === theme.value ? ( | ||
<View padding="$m"> | ||
<LoadingSpinner color="$primaryText" size="small" /> | ||
</View> | ||
) : ( | ||
<RadioControl checked={theme.value === selectedTheme} /> | ||
)} | ||
</ListItem.EndContent> | ||
</ListItem> | ||
</Pressable> | ||
))} | ||
</YStack> | ||
</ScrollView> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,87 @@ | ||
import { themeSettings } from '@tloncorp/shared/db'; | ||
import { config } from '@tloncorp/ui'; | ||
import { useEffect, useState } from 'react'; | ||
import React from 'react'; | ||
import { TamaguiProvider, TamaguiProviderProps } from 'tamagui'; | ||
import type { ThemeName } from 'tamagui'; | ||
|
||
import { useIsDarkMode } from '../hooks/useIsDarkMode'; | ||
|
||
export const ThemeContext = React.createContext<{ | ||
setActiveTheme: (theme: ThemeName) => void; | ||
}>({ setActiveTheme: () => {} }); | ||
|
||
export function Provider({ | ||
children, | ||
...rest | ||
}: Omit<TamaguiProviderProps, 'config'>) { | ||
const isDarkMode = useIsDarkMode(); | ||
const [activeTheme, setActiveTheme] = useState<ThemeName>( | ||
isDarkMode ? 'dark' : 'light' | ||
); | ||
|
||
useEffect(() => { | ||
const loadStoredTheme = async () => { | ||
try { | ||
const storedTheme = await themeSettings.getValue(); | ||
if (storedTheme) { | ||
setActiveTheme(storedTheme); | ||
} else { | ||
// If no stored theme, follow system theme | ||
setActiveTheme(isDarkMode ? 'dark' : 'light'); | ||
} | ||
} catch (error) { | ||
console.warn('Failed to load theme preference:', error); | ||
} | ||
}; | ||
|
||
loadStoredTheme(); | ||
}, [isDarkMode]); | ||
|
||
useEffect(() => { | ||
const handleSystemThemeChange = async () => { | ||
try { | ||
const storedTheme = await themeSettings.getValue(); | ||
if (!storedTheme) { | ||
setActiveTheme(isDarkMode ? 'dark' : 'light'); | ||
} | ||
} catch (error) { | ||
console.warn('Failed to check theme preference:', error); | ||
} | ||
}; | ||
|
||
handleSystemThemeChange(); | ||
}, [isDarkMode]); | ||
|
||
return ( | ||
<TamaguiProvider {...rest} config={config}> | ||
{children} | ||
</TamaguiProvider> | ||
<ThemeContext.Provider value={{ setActiveTheme }}> | ||
<TamaguiProvider {...rest} config={config} defaultTheme={activeTheme}> | ||
{children} | ||
</TamaguiProvider> | ||
</ThemeContext.Provider> | ||
); | ||
} | ||
|
||
export const setTheme = async ( | ||
theme: ThemeName, | ||
setActiveTheme: (theme: ThemeName) => void | ||
) => { | ||
try { | ||
await themeSettings.setValue(theme); | ||
setActiveTheme(theme); | ||
} catch (error) { | ||
console.warn('Failed to save theme preference:', error); | ||
} | ||
}; | ||
|
||
export const clearTheme = async ( | ||
setActiveTheme: (theme: ThemeName) => void, | ||
isDarkMode: boolean | ||
) => { | ||
try { | ||
await themeSettings.resetValue(); | ||
setActiveTheme(isDarkMode ? 'dark' : 'light'); | ||
} catch (error) { | ||
console.warn('Failed to clear theme preference:', error); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.