2019-12-04 16:39:53 +00:00
|
|
|
import React from 'react';
|
2021-12-02 13:33:27 +00:00
|
|
|
import { StackNavigationOptions } from '@react-navigation/stack';
|
2019-12-04 16:39:53 +00:00
|
|
|
|
|
|
|
import I18n from '../i18n';
|
|
|
|
import { withTheme } from '../theme';
|
|
|
|
import { themes } from '../constants/colors';
|
|
|
|
import StatusBar from '../containers/StatusBar';
|
2020-10-30 13:59:44 +00:00
|
|
|
import * as List from '../containers/List';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { THEME_PREFERENCES_KEY } from '../lib/rocketchat';
|
|
|
|
import { supportSystemTheme } from '../utils/deviceInfo';
|
2020-06-15 14:00:46 +00:00
|
|
|
import SafeAreaView from '../containers/SafeAreaView';
|
2020-08-19 17:14:22 +00:00
|
|
|
import UserPreferences from '../lib/userPreferences';
|
2020-07-30 19:51:13 +00:00
|
|
|
import { events, logEvent } from '../utils/log';
|
2022-01-12 12:54:04 +00:00
|
|
|
import { IThemePreference, TThemeMode, TDarkLevel } from '../definitions/ITheme';
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IThemeViewProps {
|
|
|
|
theme: string;
|
|
|
|
themePreferences: IThemePreference;
|
|
|
|
setTheme(newTheme?: IThemePreference): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
class ThemeView extends React.Component<IThemeViewProps> {
|
|
|
|
static navigationOptions = (): StackNavigationOptions => ({
|
2020-06-15 14:00:46 +00:00
|
|
|
title: I18n.t('Theme')
|
2021-09-13 20:41:05 +00:00
|
|
|
});
|
2019-12-04 16:39:53 +00:00
|
|
|
|
2021-12-02 13:33:27 +00:00
|
|
|
isSelected = (item: ITheme) => {
|
2019-12-04 16:39:53 +00:00
|
|
|
const { themePreferences } = this.props;
|
|
|
|
const { group } = item;
|
|
|
|
const { darkLevel, currentTheme } = themePreferences;
|
|
|
|
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
|
|
|
|
2021-12-02 13:33:27 +00:00
|
|
|
onClick = (item: ITheme) => {
|
2019-12-04 16:39:53 +00:00
|
|
|
const { themePreferences } = this.props;
|
|
|
|
const { darkLevel, currentTheme } = themePreferences;
|
|
|
|
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
|
|
|
}
|
|
|
|
this.setTheme(changes);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-12-04 16:39:53 +00:00
|
|
|
|
2022-03-09 19:41:26 +00:00
|
|
|
setTheme = (theme: Partial<IThemePreference>) => {
|
2019-12-04 16:39:53 +00:00
|
|
|
const { setTheme, themePreferences } = this.props;
|
|
|
|
const newTheme = { ...themePreferences, ...theme };
|
|
|
|
setTheme(newTheme);
|
2022-03-09 19:41:26 +00:00
|
|
|
UserPreferences.setMap(THEME_PREFERENCES_KEY, newTheme);
|
2019-12-04 16:39:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
renderIcon = () => {
|
|
|
|
const { theme } = this.props;
|
2020-10-30 13:59:44 +00:00
|
|
|
return <List.Icon name='check' color={themes[theme].tintColor} />;
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-12-04 16:39:53 +00:00
|
|
|
|
2021-12-02 13:33:27 +00:00
|
|
|
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}
|
2019-12-04 16:39:53 +00:00
|
|
|
onPress={() => this.onClick(item)}
|
2021-09-13 20:41:05 +00:00
|
|
|
testID={`theme-view-${value}`}
|
2022-01-17 16:10:39 +00:00
|
|
|
right={() => (this.isSelected(item) ? this.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
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2020-10-30 13:59:44 +00:00
|
|
|
<SafeAreaView testID='theme-view'>
|
|
|
|
<StatusBar />
|
|
|
|
<List.Container>
|
|
|
|
<List.Section title='Theme'>
|
|
|
|
<List.Separator />
|
2021-09-13 20:41:05 +00:00
|
|
|
{themeGroup.map(item => this.renderItem({ item }))}
|
2020-10-30 13:59:44 +00:00
|
|
|
</List.Section>
|
|
|
|
<List.Section title='Dark_level'>
|
|
|
|
<List.Separator />
|
2021-09-13 20:41:05 +00:00
|
|
|
{darkGroup.map(item => this.renderItem({ item }))}
|
2020-10-30 13:59:44 +00:00
|
|
|
</List.Section>
|
|
|
|
</List.Container>
|
2019-12-04 16:39:53 +00:00
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default withTheme(ThemeView);
|