Skip to content

Commit

Permalink
Merge pull request #1031 from andrew-bierman/fix/theme_state
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-bierman authored Jul 21, 2024
2 parents 8482153 + f8bbd25 commit f9a8175
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 32 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
"@babel/runtime": "^7.21.0",
"@manypkg/cli": "^0.21.0",
"@prisma/extension-accelerate": "^0.6.2",
"@react-native-async-storage/async-storage": "^1.23.1",
"check-dependency-version-consistency": "^4.1.0",
"cross-env": "^7.0.3",
"dotenv-cli": "^7.3.0",
Expand Down
37 changes: 33 additions & 4 deletions packages/app/context/theme.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { createContext, useReducer } from 'react';
import { createContext, useEffect, useReducer, useState } from 'react';
import { theme, darkTheme } from '../theme';
import ThirdPartyThemeProviders from './ThirdPartyThemeProviders';
import React from 'react';
import { Platform } from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { useStorageState } from 'app/hooks/storage/useStorageState';

const initialState = {
isDark: false,
isLight: true,
isDark: null,
isLight: null,
currentTheme: theme,
loading: true,
};
const handlers = {
/**
Expand Down Expand Up @@ -58,6 +62,29 @@ const ThemeContext = createContext({
*/
export const ThemeProvider = ({ children }) => {
const [state, dispatch] = useReducer(reducer, initialState);
const [[, storedIsEnabled], setStoredIsEnabled] =
useStorageState('isEnabled');
const [loading, setLoading] = useState(true);

useEffect(() => {
const fetchTheme = async () => {
try {
if (storedIsEnabled !== null) {
const isEnabled = JSON.parse(storedIsEnabled);
dispatch({
type: isEnabled ? 'ENABLE_DARK_MODE' : 'ENABLE_LIGHT_MODE',
});
} else {
dispatch({ type: 'ENABLE_LIGHT_MODE' });
}
} catch (e) {
console.error('Local storage is unavailable:', e);
} finally {
setLoading(false);
}
};
fetchTheme();
}, [storedIsEnabled]);

/**
* Enable dark mode.
Expand All @@ -66,6 +93,7 @@ export const ThemeProvider = ({ children }) => {
*/
const enableDarkMode = () => {
dispatch({ type: 'ENABLE_DARK_MODE' });
setStoredIsEnabled(JSON.stringify(true));
};
/**
* Enables light mode.
Expand All @@ -75,11 +103,12 @@ export const ThemeProvider = ({ children }) => {
*/
const enableLightMode = () => {
dispatch({ type: 'ENABLE_LIGHT_MODE' });
setStoredIsEnabled(JSON.stringify(false));
};

const key = `themeContext + isDark=${state.isDark} + isLight=${state.isLight}`;

return (
return loading ? null : (
<ThemeContext.Provider
key={key}
value={{
Expand Down
27 changes: 0 additions & 27 deletions packages/app/hooks/appearance/useAppearance.js

This file was deleted.

28 changes: 28 additions & 0 deletions packages/app/hooks/appearance/useAppearance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import useTheme from '../useTheme';
import { useStorageState } from 'app/hooks/storage/useStorageState';

const useAppearance = () => {
const { enableDarkMode, enableLightMode, currentTheme } = useTheme();
const [[, isEnabledString], setIsEnabled] = useStorageState('isEnabled');

// Convert string to boolean
const isEnabled = JSON.parse(isEnabledString);

const toggleSwitch = async () => {
const newIsEnabledValue = !isEnabled ? 'true' : 'false';
setIsEnabled(newIsEnabledValue);
if (!isEnabled) {
enableDarkMode();
} else {
enableLightMode();
}
};

return {
isEnabled,
toggleSwitch,
currentTheme,
};
};

export default useAppearance;
2 changes: 1 addition & 1 deletion packages/app/hooks/storage/useStorageState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function useStorageState(key: string): UseStateHook<string> {
console.error('Local storage is unavailable:', e);
}
} else {
SecureStore.getItemAsync(key).then((value) => {
await SecureStore.getItemAsync(key).then((value) => {
setState(value);
});
}
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6846,6 +6846,17 @@ __metadata:
languageName: node
linkType: hard

"@react-native-async-storage/async-storage@npm:^1.23.1":
version: 1.23.1
resolution: "@react-native-async-storage/async-storage@npm:1.23.1"
dependencies:
merge-options: "npm:^3.0.4"
peerDependencies:
react-native: ^0.0.0-0 || >=0.60 <1.0
checksum: 10/9d0acc7edb0ba9ee414d6e62c656e5d571c9d9615e7e9f4748865b965178998f2e73786b8050387ada00ddb6faaca97b5cf2e010ceaeebe91571991364a0f515
languageName: node
linkType: hard

"@react-native-community/cli-clean@npm:12.3.6":
version: 12.3.6
resolution: "@react-native-community/cli-clean@npm:12.3.6"
Expand Down Expand Up @@ -28613,6 +28624,7 @@ __metadata:
"@babel/runtime": "npm:^7.21.0"
"@manypkg/cli": "npm:^0.21.0"
"@prisma/extension-accelerate": "npm:^0.6.2"
"@react-native-async-storage/async-storage": "npm:^1.23.1"
check-dependency-version-consistency: "npm:^4.1.0"
cross-env: "npm:^7.0.3"
dotenv-cli: "npm:^7.3.0"
Expand Down

0 comments on commit f9a8175

Please sign in to comment.