Skip to content

Commit

Permalink
chore: code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
awais-codes committed May 17, 2024
1 parent 38df4a2 commit 7b6e733
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
23 changes: 9 additions & 14 deletions packages/app/context/theme.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,19 @@ import {
useReducer,
useEffect,
} from 'react';
import { theme, darkTheme } from '../theme';
import { getTheme } from '../theme';
import { ThemeEnum, type ThemeType } from '../theme/types';
import ThirdPartyThemeProviders from './ThirdPartyThemeProviders';
import React from 'react';
import { useStorage } from '../hooks/storage/useStorage';

enum ThemeEnum {
DARK = 'DARK',
LIGHT = 'LIGHT',
}

type ThemeType = `${ThemeEnum}`;

const DEFAULT_THEME = ThemeEnum.DARK as ThemeType;
const DEFAULT_THEME = ThemeEnum.LIGHT as ThemeType;

// Set default theme in initial state
const initialState = {
isDark: DEFAULT_THEME === ThemeEnum.DARK,
isLight: DEFAULT_THEME === ThemeEnum.LIGHT,
currentTheme: theme,
currentTheme: getTheme(DEFAULT_THEME),
};

/**
Expand All @@ -36,7 +31,7 @@ const handlers = {
...state,
isDark: true,
isLight: false,
currentTheme: darkTheme,
currentTheme: getTheme(ThemeEnum.DARK),
}),
/**
* Enables light mode by updating the state object.
Expand All @@ -48,7 +43,7 @@ const handlers = {
...state,
isDark: false,
isLight: true,
currentTheme: theme,
currentTheme: getTheme(ThemeEnum.LIGHT),
}),
};

Expand All @@ -73,13 +68,13 @@ export const ThemeProvider = ({ children }: PropsWithChildren) => {
const [[isThemeLoading, storedTheme], setStoredTheme] = useStorage('theme');

/**
* Initialize app theme based on stored theme preference or else default theme
* Initializes the app theme based on the stored theme preference.
* If no preference is stored, sets the default theme preference in local storage.
*/
useEffect(() => {
if (storedTheme) {
dispatch({ type: `ENABLE_${storedTheme}_MODE` });
} else {
dispatch({ type: `ENABLE_${DEFAULT_THEME}_MODE` });
setStoredTheme(DEFAULT_THEME);
}
}, [isThemeLoading]);
Expand Down
16 changes: 16 additions & 0 deletions packages/app/theme/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { extendTheme } from 'native-base';
import { DefaultTheme } from 'react-native-paper';
import { type ThemeType } from './types';

export const theme = {
colors: {
Expand Down Expand Up @@ -127,3 +128,18 @@ export const darkPaperTheme = {
},
},
};

/**
* Get theme object by theme type eg light, dark... etc
* @param {ThemeType} themeType
* @returns {object}
*/
export const getTheme = (themeType: ThemeType) => {
switch (themeType) {
case 'DARK':
return darkTheme;
// extend types here
default:
return theme;
}
};
7 changes: 7 additions & 0 deletions packages/app/theme/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export enum ThemeEnum {
DARK = 'DARK',
LIGHT = 'LIGHT',
// add more types here
}

export type ThemeType = `${ThemeEnum}`;

0 comments on commit 7b6e733

Please sign in to comment.