2022-05-05 15:24:36 +00:00
|
|
|
import React, { useLayoutEffect } from 'react';
|
2022-09-12 13:16:00 +00:00
|
|
|
import { useNavigation } from '@react-navigation/native';
|
2019-12-04 16:39:53 +00:00
|
|
|
|
2020-10-30 13:59:44 +00:00
|
|
|
import * as List from '../containers/List';
|
2020-06-15 14:00:46 +00:00
|
|
|
import SafeAreaView from '../containers/SafeAreaView';
|
2022-09-12 13:16:00 +00:00
|
|
|
import StatusBar from '../containers/StatusBar';
|
|
|
|
import { IThemePreference, TDarkLevel, TThemeMode } from '../definitions/ITheme';
|
|
|
|
import I18n from '../i18n';
|
|
|
|
import { THEME_PREFERENCES_KEY } from '../lib/constants';
|
|
|
|
import { supportSystemTheme } from '../lib/methods/helpers';
|
2022-06-06 14:17:51 +00:00
|
|
|
import { events, logEvent } from '../lib/methods/helpers/log';
|
2022-09-12 13:16:00 +00:00
|
|
|
import UserPreferences from '../lib/methods/userPreferences';
|
|
|
|
import { useTheme } from '../theme';
|
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-09-12 13:16:00 +00:00
|
|
|
const Item = ({
|
|
|
|
onPress,
|
|
|
|
label,
|
|
|
|
value,
|
|
|
|
isSelected
|
|
|
|
}: {
|
|
|
|
onPress: () => void;
|
|
|
|
label: string;
|
|
|
|
value: string;
|
|
|
|
isSelected: boolean;
|
|
|
|
}) => {
|
|
|
|
const { colors } = useTheme();
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<List.Item
|
|
|
|
title={label}
|
|
|
|
onPress={onPress}
|
|
|
|
testID={`theme-view-${value}`}
|
|
|
|
right={() => (isSelected ? <List.Icon name='check' color={colors.tintColor} /> : null)}
|
|
|
|
/>
|
|
|
|
<List.Separator />
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
2022-05-05 15:24:36 +00:00
|
|
|
|
2022-09-12 13:16:00 +00:00
|
|
|
const ThemeView = (): React.ReactElement => {
|
|
|
|
const { themePreferences, setTheme } = useTheme();
|
|
|
|
const { setOptions } = useNavigation();
|
2021-12-02 13:33:27 +00:00
|
|
|
|
2022-05-05 15:24:36 +00:00
|
|
|
useLayoutEffect(() => {
|
2022-09-12 13:16:00 +00:00
|
|
|
setOptions({
|
2022-05-05 15:24:36 +00:00
|
|
|
title: I18n.t('Theme')
|
|
|
|
});
|
2022-09-12 13:16:00 +00:00
|
|
|
}, []);
|
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-09-12 13:16:00 +00:00
|
|
|
handleTheme(changes);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-12-04 16:39:53 +00:00
|
|
|
|
2022-09-12 13:16:00 +00:00
|
|
|
const handleTheme = (theme: Partial<IThemePreference>) => {
|
2022-05-05 15:24:36 +00:00
|
|
|
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
|
|
|
return (
|
|
|
|
<SafeAreaView testID='theme-view'>
|
|
|
|
<StatusBar />
|
|
|
|
<List.Container>
|
|
|
|
<List.Section title='Theme'>
|
|
|
|
<List.Separator />
|
2022-09-12 13:16:00 +00:00
|
|
|
<>
|
|
|
|
{themeGroup.map(theme => (
|
|
|
|
<Item
|
|
|
|
onPress={() => onClick(theme)}
|
|
|
|
label={theme.label}
|
|
|
|
value={theme.value}
|
|
|
|
isSelected={!!isSelected(theme)}
|
|
|
|
key={theme.label}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</>
|
2022-05-05 15:24:36 +00:00
|
|
|
</List.Section>
|
|
|
|
<List.Section title='Dark_level'>
|
|
|
|
<List.Separator />
|
2022-09-12 13:16:00 +00:00
|
|
|
<>
|
|
|
|
{darkGroup.map(theme => (
|
|
|
|
<Item
|
|
|
|
onPress={() => onClick(theme)}
|
|
|
|
label={theme.label}
|
|
|
|
value={theme.value}
|
|
|
|
isSelected={!!isSelected(theme)}
|
|
|
|
key={theme.label}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</>
|
2022-05-05 15:24:36 +00:00
|
|
|
</List.Section>
|
|
|
|
</List.Container>
|
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
};
|
2019-12-04 16:39:53 +00:00
|
|
|
|
2022-05-05 15:24:36 +00:00
|
|
|
export default ThemeView;
|