2022-08-08 21:02:08 +00:00
|
|
|
import { Appearance } from 'react-native';
|
2019-12-04 16:39:53 +00:00
|
|
|
import changeNavigationBarColor from 'react-native-navigation-bar-color';
|
|
|
|
import setRootViewColor from 'rn-root-view';
|
|
|
|
|
2022-06-06 14:17:51 +00:00
|
|
|
import { IThemePreference, TThemeMode } from '../../../definitions/ITheme';
|
|
|
|
import { themes, THEME_PREFERENCES_KEY } from '../../constants';
|
|
|
|
import UserPreferences from '../userPreferences';
|
|
|
|
import { TSupportedThemes } from '../../../theme';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { isAndroid } from './deviceInfo';
|
2023-05-18 21:09:33 +00:00
|
|
|
import { debounce } from './debounce';
|
2019-12-04 16:39:53 +00:00
|
|
|
|
2022-01-12 12:54:04 +00:00
|
|
|
let themeListener: { remove: () => void } | null;
|
2019-12-04 16:39:53 +00:00
|
|
|
|
2022-03-09 19:41:26 +00:00
|
|
|
export const initialTheme = (): IThemePreference => {
|
|
|
|
const theme = UserPreferences.getMap(THEME_PREFERENCES_KEY) as IThemePreference;
|
|
|
|
const initialTheme: IThemePreference = {
|
|
|
|
currentTheme: defaultTheme(),
|
|
|
|
darkLevel: 'black'
|
|
|
|
};
|
|
|
|
return theme || initialTheme;
|
|
|
|
};
|
|
|
|
|
2022-01-12 12:54:04 +00:00
|
|
|
export const defaultTheme = (): TThemeMode => {
|
2019-12-04 16:39:53 +00:00
|
|
|
const systemTheme = Appearance.getColorScheme();
|
2022-08-08 21:02:08 +00:00
|
|
|
if (systemTheme) {
|
2019-12-04 16:39:53 +00:00
|
|
|
return systemTheme;
|
|
|
|
}
|
|
|
|
return 'light';
|
|
|
|
};
|
|
|
|
|
2022-03-31 23:04:29 +00:00
|
|
|
export const getTheme = (themePreferences: IThemePreference): TSupportedThemes => {
|
2019-12-04 16:39:53 +00:00
|
|
|
const { darkLevel, currentTheme } = themePreferences;
|
|
|
|
let theme = currentTheme;
|
|
|
|
if (currentTheme === 'automatic') {
|
|
|
|
theme = defaultTheme();
|
|
|
|
}
|
|
|
|
return theme === 'dark' ? darkLevel : 'light';
|
|
|
|
};
|
|
|
|
|
2022-01-12 12:54:04 +00:00
|
|
|
export const newThemeState = (prevState: { themePreferences: IThemePreference }, newTheme: IThemePreference) => {
|
2019-12-04 16:39:53 +00:00
|
|
|
// new theme preferences
|
|
|
|
const themePreferences = {
|
|
|
|
...prevState.themePreferences,
|
|
|
|
...newTheme
|
|
|
|
};
|
|
|
|
// set new state of themePreferences
|
|
|
|
// and theme (based on themePreferences)
|
|
|
|
return { themePreferences, theme: getTheme(themePreferences) };
|
|
|
|
};
|
|
|
|
|
2022-01-12 12:54:04 +00:00
|
|
|
export const setNativeTheme = async (themePreferences: IThemePreference): Promise<void> => {
|
2019-12-04 16:39:53 +00:00
|
|
|
const theme = getTheme(themePreferences);
|
|
|
|
if (isAndroid) {
|
|
|
|
const iconsLight = theme === 'light';
|
2020-05-13 20:26:26 +00:00
|
|
|
try {
|
2022-01-12 12:54:04 +00:00
|
|
|
// The late param as default is true @ react-native-navigation-bar-color/src/index.js line 8
|
|
|
|
await changeNavigationBarColor(themes[theme].navbarBackground, iconsLight, true);
|
2020-05-13 20:26:26 +00:00
|
|
|
} catch (error) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
2019-12-04 16:39:53 +00:00
|
|
|
}
|
|
|
|
setRootViewColor(themes[theme].backgroundColor);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const unsubscribeTheme = () => {
|
|
|
|
if (themeListener && themeListener.remove) {
|
|
|
|
themeListener.remove();
|
|
|
|
themeListener = null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-05-18 21:09:33 +00:00
|
|
|
type AppearancePreferences = {
|
|
|
|
colorScheme: 'light' | 'dark';
|
|
|
|
};
|
|
|
|
|
|
|
|
export const subscribeTheme = (themePreferences: IThemePreference, theme: TSupportedThemes, setTheme: () => void): void => {
|
2019-12-04 16:39:53 +00:00
|
|
|
const { currentTheme } = themePreferences;
|
2023-05-18 21:09:33 +00:00
|
|
|
if (currentTheme === 'automatic') {
|
|
|
|
unsubscribeTheme();
|
|
|
|
themeListener = Appearance.addChangeListener(
|
|
|
|
// listener issue https://github.com/facebook/react-native/issues/36713
|
|
|
|
debounce((appearance: AppearancePreferences) => {
|
|
|
|
const simplifiedTheme = theme === 'black' ? 'dark' : theme;
|
|
|
|
if (simplifiedTheme !== appearance.colorScheme) {
|
|
|
|
setTheme();
|
|
|
|
}
|
|
|
|
}, 300)
|
|
|
|
);
|
|
|
|
} else {
|
2019-12-04 16:39:53 +00:00
|
|
|
// unsubscribe appearance changes when automatic was disabled
|
|
|
|
unsubscribeTheme();
|
|
|
|
}
|
|
|
|
// set native components theme
|
|
|
|
setNativeTheme(themePreferences);
|
|
|
|
};
|