Skip to content

Commit

Permalink
chore: fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
im-adithya committed Oct 29, 2024
1 parent 56c30b2 commit f8cb24a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 24 deletions.
22 changes: 10 additions & 12 deletions app/_layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ export const unstable_settings = {

export default function RootLayout() {
const { isDarkColorScheme, setColorScheme } = useColorScheme();
const [themeLoaded, setThemeLoaded] = React.useState(false);
const [fontsLoaded, setFontsLoaded] = React.useState(false);
const [resourcesLoaded, setResourcesLoaded] = React.useState(false);

useConnectionChecker();

Expand All @@ -54,8 +53,6 @@ export default function RootLayout() {
"OpenRunde-Semibold": require("./../assets/fonts/OpenRunde-Semibold.otf"),
"OpenRunde-Bold": require("./../assets/fonts/OpenRunde-Bold.otf"),
});

setFontsLoaded(true);
}

async function checkBiometricStatus() {
Expand All @@ -65,27 +62,28 @@ export default function RootLayout() {
}
}

const loadTheme = React.useCallback((): void => {
const theme = useAppStore.getState().theme as "light" | "dark" | "system";
setColorScheme(theme);

setThemeLoaded(true);
const loadTheme = React.useCallback((): Promise<void> => {
return new Promise((resolve) => {
const theme = useAppStore.getState().theme;
setColorScheme(theme);
resolve();
});
}, [setColorScheme]);

React.useEffect(() => {
const init = async () => {
try {
loadTheme();
await Promise.all([loadFonts(), checkBiometricStatus()]);
await Promise.all([loadTheme(), loadFonts(), checkBiometricStatus()]);
} finally {
setResourcesLoaded(true);
SplashScreen.hideAsync();
}
};

init();
}, [loadTheme]);

if (!fontsLoaded || !themeLoaded) {
if (!resourcesLoaded) {
return null;
}

Expand Down
8 changes: 5 additions & 3 deletions lib/state/appStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ interface AppState {
readonly addressBookEntries: AddressBookEntry[];
readonly isSecurityEnabled: boolean;
readonly isOnboarded: boolean;
readonly theme: string;
readonly theme: Theme;
setUnlocked: (unlocked: boolean) => void;
setTheme: (theme: string) => void;
setTheme: (theme: Theme) => void;
setOnboarded: (isOnboarded: boolean) => void;
setNWCClient: (nwcClient: NWCClient | undefined) => void;
updateCurrentWallet(wallet: Partial<Wallet>): void;
Expand All @@ -39,6 +39,8 @@ const themeKey = "theme";
const isSecurityEnabledKey = "isSecurityEnabled";
export const lastActiveTimeKey = "lastActiveTime";

export type Theme = "system" | "light" | "dark";

type Wallet = {
name?: string;
nostrWalletConnectUrl?: string;
Expand Down Expand Up @@ -145,7 +147,7 @@ export const useAppStore = create<AppState>()((set, get) => {
const isSecurityEnabled =
secureStorage.getItem(isSecurityEnabledKey) === "true";

const theme = secureStorage.getItem(themeKey) || "system";
const theme = (secureStorage.getItem(themeKey) as Theme) || "system";

const initialWallets = loadWallets();
return {
Expand Down
18 changes: 15 additions & 3 deletions lib/useColorScheme.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import { useColorScheme as useNativewindColorScheme } from "nativewind";
import { useAppStore } from "~/lib/state/appStore";

export function useColorScheme() {
const { colorScheme, setColorScheme, toggleColorScheme } =
useNativewindColorScheme();
const {
colorScheme,
setColorScheme,
toggleColorScheme: _toggleColorScheme,
} = useNativewindColorScheme();

const isDarkColorScheme = colorScheme === "dark";

const toggleColorScheme = () => {
_toggleColorScheme();
useAppStore.getState().setTheme(isDarkColorScheme ? "light" : "dark");
};

return {
colorScheme: colorScheme ?? "dark",
isDarkColorScheme: colorScheme === "dark",
isDarkColorScheme,
setColorScheme,
toggleColorScheme,
};
Expand Down
7 changes: 1 addition & 6 deletions pages/settings/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,7 @@ export function Settings() {

<TouchableOpacity
className="flex flex-row gap-4"
onPress={() => {
useAppStore
.getState()
.setTheme(colorScheme === "light" ? "dark" : "light");
toggleColorScheme();
}}
onPress={toggleColorScheme}
>
<Palette className="text-foreground" />
<Text className="text-foreground font-medium2 text-xl">Theme</Text>
Expand Down

0 comments on commit f8cb24a

Please sign in to comment.