2022-05-05 15:24:36 +00:00
|
|
|
import React, { useLayoutEffect } from 'react';
|
2019-12-04 16:39:53 +00:00
|
|
|
|
2022-05-05 15:24:36 +00:00
|
|
|
import { SettingsStackParamList } from '../stacks/types';
|
2019-12-04 16:39:53 +00:00
|
|
|
import I18n from '../i18n';
|
2022-05-05 15:24:36 +00:00
|
|
|
import { useTheme } from '../theme';
|
2019-12-04 16:39:53 +00:00
|
|
|
import StatusBar from '../containers/StatusBar';
|
2020-10-30 13:59:44 +00:00
|
|
|
import * as List from '../containers/List';
|
2022-06-06 14:17:51 +00:00
|
|
|
import { supportSystemTheme } from '../lib/methods/helpers';
|
2020-06-15 14:00:46 +00:00
|
|
|
import SafeAreaView from '../containers/SafeAreaView';
|
2022-04-07 13:13:19 +00:00
|
|
|
import UserPreferences from '../lib/methods/userPreferences';
|
2022-06-06 14:17:51 +00:00
|
|
|
import { events, logEvent } from '../lib/methods/helpers/log';
|
2022-01-12 12:54:04 +00:00
|
|
|
import { IThemePreference, TThemeMode, TDarkLevel } from '../definitions/ITheme';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { THEME_PREFERENCES_KEY, themes } from '../lib/constants';
|
2022-05-05 15:24:36 +00:00
|
|
|
import { IBaseScreen } from '../definitions';
|
2019-12-04 16:39:53 +00:00
|
|
|
|
|
|
|
const THEME_GROUP = 'THEME_GROUP';
|
|
|
|
const DARK_GROUP = 'DARK_GROUP';
|
|
|
|
|
2022-01-12 12:54:04 +00:00
|
|
|
const SYSTEM_THEME: ITheme = {
|
2020-07-24 19:23:34 +00:00
|
|
|
label: 'Automatic',
|
2019-12-04 16:39:53 +00:00
|
|
|
value: 'automatic',
|
|
|
|
group: THEME_GROUP
|
|
|
|
};
|
|
|
|
|
2022-01-12 12:54:04 +00:00
|
|
|
const THEMES: ITheme[] = [
|
2019-12-04 16:39:53 +00:00
|
|
|
{
|
2020-07-24 19:23:34 +00:00
|
|
|
label: 'Light',
|
2019-12-04 16:39:53 +00:00
|
|
|
value: 'light',
|
|
|
|
group: THEME_GROUP
|
2021-09-13 20:41:05 +00:00
|
|
|
},
|
|
|
|
{
|
2020-07-24 19:23:34 +00:00
|
|
|
label: 'Dark',
|
2019-12-04 16:39:53 +00:00
|
|
|
value: 'dark',
|
|
|
|
group: THEME_GROUP
|
2021-09-13 20:41:05 +00:00
|
|
|
},
|
|
|
|
{
|
2020-07-24 19:23:34 +00:00
|
|
|
label: 'Black',
|
2019-12-04 16:39:53 +00:00
|
|
|
value: 'black',
|
|
|
|
group: DARK_GROUP
|
2021-09-13 20:41:05 +00:00
|
|
|
},
|
|
|
|
{
|
2021-07-12 18:05:09 +00:00
|
|
|
label: 'Dark',
|
|
|
|
value: 'dark',
|
|
|
|
group: DARK_GROUP
|
2019-12-04 16:39:53 +00:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2019-12-11 19:30:32 +00:00
|
|
|
if (supportSystemTheme()) {
|
|
|
|
THEMES.unshift(SYSTEM_THEME);
|
|
|
|
}
|
|
|
|
|
2020-10-30 13:59:44 +00:00
|
|
|
const themeGroup = THEMES.filter(item => item.group === THEME_GROUP);
|
|
|
|
const darkGroup = THEMES.filter(item => item.group === DARK_GROUP);
|
2019-12-04 16:39:53 +00:00
|
|
|
|
2021-12-02 13:33:27 +00:00
|
|
|
interface ITheme {
|
|
|
|
label: string;
|
2022-01-12 12:54:04 +00:00
|
|
|
value: TThemeMode | TDarkLevel;
|
2021-12-02 13:33:27 +00:00
|
|
|
group: string;
|
|
|
|
}
|
|
|
|
|
2022-05-05 15:24:36 +00:00
|
|
|
type IThemeViewProps = IBaseScreen<SettingsStackParamList, 'ThemeView'>;
|
|
|
|
|
|
|
|
const ThemeView = ({ navigation }: IThemeViewProps): React.ReactElement => {
|
|
|
|
const { theme, themePreferences, setTheme } = useTheme();
|
2021-12-02 13:33:27 +00:00
|
|
|
|
2022-05-05 15:24:36 +00:00
|
|
|
useLayoutEffect(() => {
|
|
|
|
navigation.setOptions({
|
|
|
|
title: I18n.t('Theme')
|
|
|
|
});
|
|
|
|
}, [navigation]);
|
2019-12-04 16:39:53 +00:00
|
|
|
|
2022-05-05 15:24:36 +00:00
|
|
|
const isSelected = (item: ITheme) => {
|
2019-12-04 16:39:53 +00:00
|
|
|
const { group } = item;
|
2022-05-05 15:24:36 +00:00
|
|
|
const { darkLevel, currentTheme } = themePreferences as IThemePreference;
|
2019-12-04 16:39:53 +00:00
|
|
|
if (group === THEME_GROUP) {
|
|
|
|
return item.value === currentTheme;
|
|
|
|
}
|
|
|
|
if (group === DARK_GROUP) {
|
|
|
|
return item.value === darkLevel;
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-12-04 16:39:53 +00:00
|
|
|
|
2022-05-05 15:24:36 +00:00
|
|
|
const onClick = (item: ITheme) => {
|
|
|
|
const { darkLevel, currentTheme } = themePreferences as IThemePreference;
|
2019-12-04 16:39:53 +00:00
|
|
|
const { value, group } = item;
|
2022-01-12 12:54:04 +00:00
|
|
|
let changes: Partial<IThemePreference> = {};
|
2019-12-04 16:39:53 +00:00
|
|
|
if (group === THEME_GROUP && currentTheme !== value) {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.THEME_SET_THEME_GROUP, { theme_group: value });
|
2022-01-12 12:54:04 +00:00
|
|
|
changes = { currentTheme: value as TThemeMode };
|
2019-12-04 16:39:53 +00:00
|
|
|
}
|
|
|
|
if (group === DARK_GROUP && darkLevel !== value) {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.THEME_SET_DARK_LEVEL, { dark_level: value });
|
2022-01-12 12:54:04 +00:00
|
|
|
changes = { darkLevel: value as TDarkLevel };
|
2019-12-04 16:39:53 +00:00
|
|
|
}
|
2022-05-05 15:24:36 +00:00
|
|
|
_setTheme(changes);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-12-04 16:39:53 +00:00
|
|
|
|
2022-05-05 15:24:36 +00:00
|
|
|
const _setTheme = (theme: Partial<IThemePreference>) => {
|
|
|
|
const newTheme: IThemePreference = { ...(themePreferences as IThemePreference), ...theme };
|
|
|
|
if (setTheme) {
|
|
|
|
setTheme(newTheme);
|
|
|
|
UserPreferences.setMap(THEME_PREFERENCES_KEY, newTheme);
|
|
|
|
}
|
2019-12-04 16:39:53 +00:00
|
|
|
};
|
|
|
|
|
2022-05-05 15:24:36 +00:00
|
|
|
const renderIcon = () => <List.Icon name='check' color={themes[theme].tintColor} />;
|
2019-12-04 16:39:53 +00:00
|
|
|
|
2022-05-05 15:24:36 +00:00
|
|
|
const renderItem = ({ item }: { item: ITheme }) => {
|
2019-12-04 16:39:53 +00:00
|
|
|
const { label, value } = item;
|
|
|
|
return (
|
|
|
|
<>
|
2020-10-30 13:59:44 +00:00
|
|
|
<List.Item
|
|
|
|
title={label}
|
2022-05-05 15:24:36 +00:00
|
|
|
onPress={() => onClick(item)}
|
2021-09-13 20:41:05 +00:00
|
|
|
testID={`theme-view-${value}`}
|
2022-05-05 15:24:36 +00:00
|
|
|
right={() => (isSelected(item) ? renderIcon() : null)}
|
2019-12-04 16:39:53 +00:00
|
|
|
/>
|
2020-10-30 13:59:44 +00:00
|
|
|
<List.Separator />
|
2019-12-04 16:39:53 +00:00
|
|
|
</>
|
|
|
|
);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-12-04 16:39:53 +00:00
|
|
|
|
2022-05-05 15:24:36 +00:00
|
|
|
return (
|
|
|
|
<SafeAreaView testID='theme-view'>
|
|
|
|
<StatusBar />
|
|
|
|
<List.Container>
|
|
|
|
<List.Section title='Theme'>
|
|
|
|
<List.Separator />
|
|
|
|
<>{themeGroup.map(item => renderItem({ item }))}</>
|
|
|
|
</List.Section>
|
|
|
|
<List.Section title='Dark_level'>
|
|
|
|
<List.Separator />
|
|
|
|
<>{darkGroup.map(item => renderItem({ item }))}</>
|
|
|
|
</List.Section>
|
|
|
|
</List.Container>
|
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
};
|
2019-12-04 16:39:53 +00:00
|
|
|
|
2022-05-05 15:24:36 +00:00
|
|
|
export default ThemeView;
|