Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: store theme in app store and secure storage #171

Merged
merged 2 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions app/_layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ export const unstable_settings = {
};

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

useConnectionChecker();

Expand All @@ -53,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 @@ -64,19 +62,28 @@ export default function RootLayout() {
}
}

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 {
await Promise.all([loadFonts(), checkBiometricStatus()]);
await Promise.all([loadTheme(), loadFonts(), checkBiometricStatus()]);
} finally {
setResourcesLoaded(true);
SplashScreen.hideAsync();
}
};

init();
}, []);
}, [loadTheme]);

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

Expand Down
20 changes: 16 additions & 4 deletions lib/state/appStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ interface AppState {
readonly addressBookEntries: AddressBookEntry[];
readonly isSecurityEnabled: boolean;
readonly isOnboarded: boolean;
readonly theme: Theme;
setUnlocked: (unlocked: boolean) => void;
setTheme: (theme: Theme) => void;
setOnboarded: (isOnboarded: boolean) => void;
setNWCClient: (nwcClient: NWCClient | undefined) => void;
updateCurrentWallet(wallet: Partial<Wallet>): void;
Expand All @@ -33,9 +35,12 @@ const selectedWalletIdKey = "selectedWalletId";
const fiatCurrencyKey = "fiatCurrency";
const hasOnboardedKey = "hasOnboarded";
const lastAlbyPaymentKey = "lastAlbyPayment";
export const isSecurityEnabledKey = "isSecurityEnabled";
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 @@ -139,24 +144,31 @@ export const useAppStore = create<AppState>()((set, get) => {
secureStorage.getItem(selectedWalletIdKey) || "0"
);

const iSecurityEnabled =
const isSecurityEnabled =
secureStorage.getItem(isSecurityEnabledKey) === "true";

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

const initialWallets = loadWallets();
return {
unlocked: !iSecurityEnabled,
unlocked: !isSecurityEnabled,
addressBookEntries: loadAddressBookEntries(),
wallets: initialWallets,
nwcClient: getNWCClient(initialSelectedWalletId),
fiatCurrency: secureStorage.getItem(fiatCurrencyKey) || "",
isSecurityEnabled: iSecurityEnabled,
isSecurityEnabled,
theme,
isOnboarded: secureStorage.getItem(hasOnboardedKey) === "true",
selectedWalletId: initialSelectedWalletId,
updateCurrentWallet,
removeCurrentWallet,
setUnlocked: (unlocked) => {
set({ unlocked });
},
setTheme: (theme) => {
secureStorage.setItem(themeKey, theme);
set({ theme });
},
setOnboarded: (isOnboarded) => {
if (isOnboarded) {
secureStorage.setItem(hasOnboardedKey, "true");
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
Loading