Skip to content

Commit

Permalink
fix: theme switching delay and screen animation flickering
Browse files Browse the repository at this point in the history
  • Loading branch information
homocodian committed May 12, 2022
1 parent c3c2c34 commit da8b224
Show file tree
Hide file tree
Showing 9 changed files with 394 additions and 283 deletions.
12 changes: 8 additions & 4 deletions App.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { SafeAreaProvider } from "react-native-safe-area-context";
import { Provider as ReactNativePaperProvider } from "react-native-paper";
import { Provider as ReduxProvider } from "react-redux";
import { PersistGate } from "redux-persist/integration/react";

import Navigation from "./navigation";
import { store } from "./redux/store";
import { store, persistor } from "./redux/store";
import useCachedResources from "./hooks/useCachedResources";

export default function App() {
Expand All @@ -15,9 +16,12 @@ export default function App() {
return (
<SafeAreaProvider>
<ReduxProvider store={store}>
<ReactNativePaperProvider>
<Navigation />
</ReactNativePaperProvider>
{/* @ts-ignore */}
<PersistGate loading={null} persistor={persistor}>
<ReactNativePaperProvider>
<Navigation />
</ReactNativePaperProvider>
</PersistGate>
</ReduxProvider>
</SafeAreaProvider>
);
Expand Down
2 changes: 1 addition & 1 deletion app.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"expo": {
"name": "Docu Wallet",
"slug": "docu-wallet",
"version": "1.0.0",
"version": "1.0.1",
"orientation": "portrait",
"icon": "./assets/images/icon.png",
"scheme": "myapp",
Expand Down
8 changes: 5 additions & 3 deletions hooks/useTheme.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from "react";
import { useEffect, useMemo, useState } from "react";

import Colors from "../constants/Colors";
import { setDarkMode } from "../redux/features/appTheme/appThemeSlice";
Expand All @@ -11,8 +11,10 @@ function useTheme() {
const colorScheme = useColorScheme();
const dispatch = useAppDispatch();

const memoizedAppearance = useMemo(() => appearance, [appearance]);

useEffect(() => {
if (appearance === "system") {
if (memoizedAppearance === "system") {
const colorTheme = colorScheme === "dark" ? Colors.dark : Colors.light;
setTheme(colorTheme);
dispatch(setDarkMode(colorScheme === "dark" ? true : false));
Expand All @@ -23,7 +25,7 @@ function useTheme() {
} else {
setTheme(Colors.light);
}
}, [isDark, colorScheme, appearance]);
}, [isDark, colorScheme, memoizedAppearance]);

return theme;
}
Expand Down
6 changes: 4 additions & 2 deletions metro.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Learn more https://docs.expo.io/guides/customizing-metro
const { getDefaultConfig } = require('expo/metro-config');
const { getDefaultConfig } = require("expo/metro-config");

module.exports = getDefaultConfig(__dirname);
const defaultConfig = getDefaultConfig(__dirname);
defaultConfig.resolver.sourceExts = ["jsx", "js", "ts", "tsx", "mjs"];
module.exports = defaultConfig;
7 changes: 6 additions & 1 deletion navigation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ const Stack = createNativeStackNavigator<RootStackParamList>();
function RootNavigator() {
return (
// @ts-ignore
<Stack.Navigator>
<Stack.Navigator
defaultScreenOptions={{
animation: "slide_from_bottom",
statusBarAnimation: "slide",
}}
>
<Stack.Screen
name="Root"
component={TopTabNavigator}
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "document-keeper",
"version": "1.0.0",
"version": "1.0.1",
"scripts": {
"start": "expo start --dev-client",
"android": "expo run:android",
Expand All @@ -16,6 +16,7 @@
"@babel/preset-env": "^7.16.11",
"@expo/vector-icons": "^12.0.0",
"@nozbe/watermelondb": "^0.24.0",
"@react-native-async-storage/async-storage": "~1.15.0",
"@react-native-clipboard/clipboard": "^1.8.5",
"@react-native-material/core": "^1.3.7",
"@react-navigation/material-top-tabs": "^6.2.1",
Expand All @@ -41,7 +42,6 @@
"react-dom": "17.0.1",
"react-native": "0.64.3",
"react-native-blob-util": "^0.15.0",
"react-native-mmkv-storage": "^0.7.2",
"react-native-pager-view": "5.4.9",
"react-native-paper": "^4.12.0",
"react-native-pdf": "^6.5.0",
Expand All @@ -50,6 +50,8 @@
"react-native-tab-view": "^3.1.1",
"react-native-web": "0.17.1",
"react-redux": "^7.2.8",
"redux": "^4.2.0",
"redux-persist": "^6.0.0",
"sharp-cli": "^1.15.0"
},
"devDependencies": {
Expand Down
39 changes: 2 additions & 37 deletions redux/features/appTheme/appThemeSlice.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
import { Appearance as SystemAppearance } from "react-native";

import { createSlice } from "@reduxjs/toolkit";
import { MMKVLoader } from "react-native-mmkv-storage";

import { AppTheme } from "./types";

const MMKV = new MMKVLoader().initialize();
const appearanceString = MMKV.getString("appearance");

const { isDark, appearance } = getAppThemeData(appearanceString);

const initialState: AppTheme = {
isDark,
appearance,
isDark: false,
appearance: "system",
};

const appThemeSlice = createSlice({
Expand All @@ -22,10 +14,6 @@ const appThemeSlice = createSlice({
setAppAppearance: (state, action) => {
state.isDark = action.payload.isDark;
state.appearance = action.payload.appearance;
MMKV.setStringAsync(
"appearance",
action.payload.appearance as string
).catch(() => {});
},
setDarkMode: (state, action) => {
state.isDark = action.payload as boolean;
Expand All @@ -36,26 +24,3 @@ const appThemeSlice = createSlice({
export const { setAppAppearance, setDarkMode } = appThemeSlice.actions;

export default appThemeSlice.reducer;

function getAppThemeData(appearance: string | null | undefined): AppTheme {
switch (appearance) {
case "dark":
return {
isDark: true,
appearance: "dark",
};

case "light":
return {
isDark: false,
appearance: "light",
};

default:
const colorScheme = SystemAppearance.getColorScheme();
return {
isDark: colorScheme === "dark" ? true : false,
appearance: "system",
};
}
}
19 changes: 17 additions & 2 deletions redux/store.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
import { configureStore, combineReducers } from "@reduxjs/toolkit";
import AsyncStorage from "@react-native-async-storage/async-storage";
import { persistReducer, persistStore } from "redux-persist";

import appThemeReducer from "./features/appTheme/appThemeSlice";
import addNoteReducer from "./features/addNote/addNoteSlice";

const reducer = combineReducers({
const persistConfig = {
key: "root",
storage: AsyncStorage,
};

const rootReducer = combineReducers({
appTheme: appThemeReducer,
addNote: addNoteReducer,
});

const persistedReducer = persistReducer(persistConfig, rootReducer);

export const store = configureStore({
reducer: reducer,
reducer: persistedReducer,
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware({
serializableCheck: false,
}),
});
export const persistor = persistStore(store);

export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;
Loading

0 comments on commit da8b224

Please sign in to comment.