2019-12-04 16:39:53 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import {
|
|
|
|
FlatList, Text, View, StyleSheet
|
|
|
|
} from 'react-native';
|
|
|
|
import RNUserDefaults from 'rn-user-defaults';
|
|
|
|
|
|
|
|
import I18n from '../i18n';
|
|
|
|
import { withTheme } from '../theme';
|
|
|
|
import { themes } from '../constants/colors';
|
|
|
|
import sharedStyles from './Styles';
|
|
|
|
import StatusBar from '../containers/StatusBar';
|
|
|
|
import Separator from '../containers/Separator';
|
|
|
|
import ListItem from '../containers/ListItem';
|
|
|
|
import { CustomIcon } from '../lib/Icons';
|
|
|
|
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-07-30 19:51:13 +00:00
|
|
|
import { events, logEvent } from '../utils/log';
|
2019-12-04 16:39:53 +00:00
|
|
|
|
|
|
|
const THEME_GROUP = 'THEME_GROUP';
|
|
|
|
const DARK_GROUP = 'DARK_GROUP';
|
|
|
|
|
|
|
|
const SYSTEM_THEME = {
|
2020-07-24 19:23:34 +00:00
|
|
|
label: 'Automatic',
|
2019-12-04 16:39:53 +00:00
|
|
|
value: 'automatic',
|
|
|
|
group: THEME_GROUP
|
|
|
|
};
|
|
|
|
|
|
|
|
const THEMES = [
|
|
|
|
{
|
2020-07-24 19:23:34 +00:00
|
|
|
label: 'Light',
|
2019-12-04 16:39:53 +00:00
|
|
|
value: 'light',
|
|
|
|
group: THEME_GROUP
|
|
|
|
}, {
|
2020-07-24 19:23:34 +00:00
|
|
|
label: 'Dark',
|
2019-12-04 16:39:53 +00:00
|
|
|
value: 'dark',
|
|
|
|
group: THEME_GROUP
|
|
|
|
}, {
|
2020-07-24 19:23:34 +00:00
|
|
|
label: 'Dark',
|
2019-12-04 16:39:53 +00:00
|
|
|
value: 'dark',
|
|
|
|
separator: true,
|
2020-07-24 19:23:34 +00:00
|
|
|
header: 'Dark_level',
|
2019-12-04 16:39:53 +00:00
|
|
|
group: DARK_GROUP
|
|
|
|
}, {
|
2020-07-24 19:23:34 +00:00
|
|
|
label: 'Black',
|
2019-12-04 16:39:53 +00:00
|
|
|
value: 'black',
|
|
|
|
group: DARK_GROUP
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2019-12-11 19:30:32 +00:00
|
|
|
if (supportSystemTheme()) {
|
|
|
|
THEMES.unshift(SYSTEM_THEME);
|
|
|
|
}
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
const styles = StyleSheet.create({
|
|
|
|
list: {
|
|
|
|
paddingBottom: 18
|
|
|
|
},
|
|
|
|
info: {
|
|
|
|
paddingTop: 25,
|
|
|
|
paddingBottom: 18,
|
|
|
|
paddingHorizontal: 16
|
|
|
|
},
|
|
|
|
infoText: {
|
|
|
|
fontSize: 16,
|
|
|
|
...sharedStyles.textRegular
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
class ThemeView extends React.Component {
|
2020-07-31 18:30:36 +00:00
|
|
|
static navigationOptions = () => ({
|
2020-06-15 14:00:46 +00:00
|
|
|
title: I18n.t('Theme')
|
2020-07-31 18:30:36 +00:00
|
|
|
})
|
2019-12-04 16:39:53 +00:00
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
theme: PropTypes.string,
|
|
|
|
themePreferences: PropTypes.object,
|
|
|
|
setTheme: PropTypes.func
|
|
|
|
}
|
|
|
|
|
|
|
|
isSelected = (item) => {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onClick = (item) => {
|
|
|
|
const { themePreferences } = this.props;
|
|
|
|
const { darkLevel, currentTheme } = themePreferences;
|
|
|
|
const { value, group } = item;
|
|
|
|
let changes = {};
|
|
|
|
if (group === THEME_GROUP && currentTheme !== value) {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.THEME_SET_THEME_GROUP, { theme_group: value });
|
2019-12-04 16:39:53 +00:00
|
|
|
changes = { currentTheme: value };
|
|
|
|
}
|
|
|
|
if (group === DARK_GROUP && darkLevel !== value) {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.THEME_SET_DARK_LEVEL, { dark_level: value });
|
2019-12-04 16:39:53 +00:00
|
|
|
changes = { darkLevel: value };
|
|
|
|
}
|
|
|
|
this.setTheme(changes);
|
|
|
|
}
|
|
|
|
|
|
|
|
setTheme = async(theme) => {
|
|
|
|
const { setTheme, themePreferences } = this.props;
|
|
|
|
const newTheme = { ...themePreferences, ...theme };
|
|
|
|
setTheme(newTheme);
|
|
|
|
await RNUserDefaults.setObjectForKey(THEME_PREFERENCES_KEY, newTheme);
|
|
|
|
};
|
|
|
|
|
|
|
|
renderSeparator = () => {
|
|
|
|
const { theme } = this.props;
|
|
|
|
return <Separator theme={theme} />;
|
|
|
|
}
|
|
|
|
|
|
|
|
renderIcon = () => {
|
|
|
|
const { theme } = this.props;
|
|
|
|
return <CustomIcon name='check' size={20} color={themes[theme].tintColor} />;
|
|
|
|
}
|
|
|
|
|
|
|
|
renderItem = ({ item, index }) => {
|
|
|
|
const { theme } = this.props;
|
|
|
|
const { label, value } = item;
|
|
|
|
const isFirst = index === 0;
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{item.separator || isFirst ? this.renderSectionHeader(item.header) : null}
|
|
|
|
<ListItem
|
2020-07-24 19:23:34 +00:00
|
|
|
title={I18n.t(label)}
|
2019-12-04 16:39:53 +00:00
|
|
|
onPress={() => this.onClick(item)}
|
|
|
|
testID={`theme-view-${ value }`}
|
|
|
|
right={this.isSelected(item) ? this.renderIcon : null}
|
|
|
|
theme={theme}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-07-24 19:23:34 +00:00
|
|
|
renderSectionHeader = (header = 'Theme') => {
|
2019-12-04 16:39:53 +00:00
|
|
|
const { theme } = this.props;
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<View style={styles.info}>
|
2020-07-24 19:23:34 +00:00
|
|
|
<Text style={[styles.infoText, { color: themes[theme].infoText }]}>{I18n.t(header)}</Text>
|
2019-12-04 16:39:53 +00:00
|
|
|
</View>
|
|
|
|
{this.renderSeparator()}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderFooter = () => {
|
|
|
|
const { theme } = this.props;
|
|
|
|
return (
|
|
|
|
<View style={[styles.info, sharedStyles.separatorTop, { borderColor: themes[theme].separatorColor }]}>
|
|
|
|
<Text style={{ color: themes[theme].infoText }}>
|
|
|
|
{I18n.t('Applying_a_theme_will_change_how_the_app_looks')}
|
|
|
|
</Text>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { theme } = this.props;
|
|
|
|
return (
|
2020-06-15 14:00:46 +00:00
|
|
|
<SafeAreaView testID='theme-view' theme={theme}>
|
2019-12-04 16:39:53 +00:00
|
|
|
<StatusBar theme={theme} />
|
|
|
|
<FlatList
|
|
|
|
data={THEMES}
|
2020-07-24 19:23:34 +00:00
|
|
|
keyExtractor={item => item.value + item.group}
|
2019-12-04 16:39:53 +00:00
|
|
|
contentContainerStyle={[
|
|
|
|
styles.list,
|
|
|
|
{ borderColor: themes[theme].separatorColor }
|
|
|
|
]}
|
|
|
|
renderItem={this.renderItem}
|
|
|
|
ListHeaderComponent={this.renderHeader}
|
|
|
|
ListFooterComponent={this.renderFooter}
|
|
|
|
ItemSeparatorComponent={this.renderSeparator}
|
|
|
|
/>
|
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default withTheme(ThemeView);
|