Rocket.Chat.ReactNative/app/views/ThemeView.tsx

141 lines
3.6 KiB
TypeScript
Raw Normal View History

import React, { useLayoutEffect } from 'react';
2019-12-04 16:39:53 +00:00
import { SettingsStackParamList } from '../stacks/types';
2019-12-04 16:39:53 +00:00
import I18n from '../i18n';
import { useTheme } from '../theme';
2019-12-04 16:39:53 +00:00
import StatusBar from '../containers/StatusBar';
import * as List from '../containers/List';
import { supportSystemTheme } from '../lib/methods/helpers';
[CHORE] Update react-navigation to v5 (#2154) * react-navigation v5 installed * compiling * Outside working * InsideStack compiling * Switch stack * Starting room * RoomView header * SafeAreaView * Slide from right stack animation * stash * Fix params * Create channel * inapp notification * Custom status * Add server working * Refactor appStart * Attachment * in-app notification * AuthLoadingView * Remove compat * Navigation * Outside animations * Fix new server icon * block modal * AttachmentView header * Remove unnecessary code * SelectedUsersView header * StatusView * CreateDiscussionView * RoomInfoView * RoomInfoEditView style * RoomMembersView * RoomsListView header * RoomView header * Share extension * getParam * Focus/blur * Trying to fix inapp * Lint * Simpler app container * Update libs * Revert "Simpler app container" This reverts commit 1e49d80bb49481c34f415831b9da5e9d53e66057. * Load messages faster * Fix safearea on ReactionsModal * Update safe area to v3 * lint * Fix transition * stash - drawer replace working * stash - modal nav * RoomActionsView as tablet modal * RoomStack * Stop showing RoomView header when there's no room * Custom Header and different navigation based on stack * Refactor setHeader * MasterDetailContext * RoomView header * Fix isMasterDetail rule * KeyCommands kind of working * Create channel on tablet * RoomView sCU * Remove withSplit * Settings opening as modal * Settings * StatusView headerLeft * Admin panel * TwoFactor style * DirectoryView * ServerDropdown and SortDropdown animations * ThreadMessagesView * Navigate to empty RoomView on server switch when in master detail * ProfileView header * Fix navigation issues * Nav to any room info on tablet * Room info * Refactoring * Fix rooms search * Roomslist commands * SearchMessagesView close modal * Key commands * Fix undefined subscription * Disallow navigate to focused room * isFocused state on RoomsListView * Blur text inputs when focus is lost * Replace animation * Default nav theme * Refactoring * Always open Attachment with close modal button * ModalContainer backdrop following themes * Screen tracking * Refactor get active route for in-app notification * Only mark room as focused when in master detail layout * Lint * Open modals as fade from bottom on Android * typo * Fixing tests * Fix in-app update * Fixing goRoom issues * Refactor stack names * Fix unreadsCount * Fix stack * Fix header animation * Refactor ShareNavigation * Refactor navigation theme * Make sure title is set * Fix create discussion navigation * Remove unused variable * Create discussions from actions fixed * Layout animation * Screen lock on share extension * Unnecessary change * Admin border * Set header after state callback * Fix key commands on outside stack * Fix back button pressed * Remove layout animations from Android * Tweak animations on Android * Disable swipe gesture to open drawer * Fix current item on RoomsListView * Fix add server * Fix drawer * Fix broadcast * LayoutAnimation instead of Transitions * Fix onboarding back press * Fix assorted tests * Create discussion fix * RoomInfoView header * Drawer active item
2020-06-15 14:00:46 +00:00
import SafeAreaView from '../containers/SafeAreaView';
import UserPreferences from '../lib/methods/userPreferences';
import { events, logEvent } from '../lib/methods/helpers/log';
import { IThemePreference, TThemeMode, TDarkLevel } from '../definitions/ITheme';
import { THEME_PREFERENCES_KEY, themes } from '../lib/constants';
import { IBaseScreen } from '../definitions';
2019-12-04 16:39:53 +00:00
const THEME_GROUP = 'THEME_GROUP';
const DARK_GROUP = 'DARK_GROUP';
const SYSTEM_THEME: ITheme = {
label: 'Automatic',
2019-12-04 16:39:53 +00:00
value: 'automatic',
group: THEME_GROUP
};
const THEMES: ITheme[] = [
2019-12-04 16:39:53 +00:00
{
label: 'Light',
2019-12-04 16:39:53 +00:00
value: 'light',
group: THEME_GROUP
},
{
label: 'Dark',
2019-12-04 16:39:53 +00:00
value: 'dark',
group: THEME_GROUP
},
{
label: 'Black',
2019-12-04 16:39:53 +00:00
value: 'black',
group: DARK_GROUP
},
{
label: 'Dark',
value: 'dark',
group: DARK_GROUP
2019-12-04 16:39:53 +00:00
}
];
if (supportSystemTheme()) {
THEMES.unshift(SYSTEM_THEME);
}
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
interface ITheme {
label: string;
value: TThemeMode | TDarkLevel;
group: string;
}
type IThemeViewProps = IBaseScreen<SettingsStackParamList, 'ThemeView'>;
const ThemeView = ({ navigation }: IThemeViewProps): React.ReactElement => {
const { theme, themePreferences, setTheme } = useTheme();
useLayoutEffect(() => {
navigation.setOptions({
title: I18n.t('Theme')
});
}, [navigation]);
2019-12-04 16:39:53 +00:00
const isSelected = (item: ITheme) => {
2019-12-04 16:39:53 +00:00
const { group } = item;
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;
}
};
2019-12-04 16:39:53 +00:00
const onClick = (item: ITheme) => {
const { darkLevel, currentTheme } = themePreferences as IThemePreference;
2019-12-04 16:39:53 +00:00
const { value, group } = item;
let changes: Partial<IThemePreference> = {};
2019-12-04 16:39:53 +00:00
if (group === THEME_GROUP && currentTheme !== value) {
[NEW] Log events from Room, Settings and Edit status (#2206) * Create method to track user event to isolate the logic to improve future refactoring * Track Onboarding view * Track NewServer view * Refactor track method due to firebase already send the current screen * Track default login and all the oAuth options * Track default sign up in RegisterView * Change trackUserEvent signature and update all the files * Track the remaining login services * track add server, change server and search * Track SidebarView and refactor to use react-navigation * Track profile events and handle exceptions * Track create channel flux * Track send message to user via NewMessageView * Track create direct message flux * Handle failure of create channel and group in the saga * Track create discussion flux * Track navigate to directory and its actions * Track read, favorite and hide a channel, handling its errors * Track all channels sorting and grouping * Resolve requests to improve the importing logs and events * Remove unused events file * Remove unused events file * log proposed Room events * Log proposed Message actions events * Log EditStatus proposed events * Log Settings proposed events * Leave a bugsnag breadcrumb when logging an event * Move all logEvent to the top of code block and log remaining fail events * Move all the non-logic-dependent logEvent to the top of code block * Move all non-logic and non-data dependent logEvent to the top of code block * Improve the logging of sidebar events * Improve events from onboarding and newserver * Improve events from login and register view, and log enter with apple * Improve NewMessageView events * Improve CreateChannel events * Improve CreateDiscussion and SelectedUsers create group events * Improve RoomsList events and log trivial events * Improve ProfileView events * Remove single line function body for the sidebarNavigate * Improve SettingsView events * Log more events from ScreenLockConfigView * Navigate to Status and AdminPanel View using the defined sidebarNavigate method * Improve StatusView events * Improve RoomView events Co-authored-by: Diego Mello <diegolmello@gmail.com>
2020-07-30 19:51:13 +00:00
logEvent(events.THEME_SET_THEME_GROUP, { theme_group: value });
changes = { currentTheme: value as TThemeMode };
2019-12-04 16:39:53 +00:00
}
if (group === DARK_GROUP && darkLevel !== value) {
[NEW] Log events from Room, Settings and Edit status (#2206) * Create method to track user event to isolate the logic to improve future refactoring * Track Onboarding view * Track NewServer view * Refactor track method due to firebase already send the current screen * Track default login and all the oAuth options * Track default sign up in RegisterView * Change trackUserEvent signature and update all the files * Track the remaining login services * track add server, change server and search * Track SidebarView and refactor to use react-navigation * Track profile events and handle exceptions * Track create channel flux * Track send message to user via NewMessageView * Track create direct message flux * Handle failure of create channel and group in the saga * Track create discussion flux * Track navigate to directory and its actions * Track read, favorite and hide a channel, handling its errors * Track all channels sorting and grouping * Resolve requests to improve the importing logs and events * Remove unused events file * Remove unused events file * log proposed Room events * Log proposed Message actions events * Log EditStatus proposed events * Log Settings proposed events * Leave a bugsnag breadcrumb when logging an event * Move all logEvent to the top of code block and log remaining fail events * Move all the non-logic-dependent logEvent to the top of code block * Move all non-logic and non-data dependent logEvent to the top of code block * Improve the logging of sidebar events * Improve events from onboarding and newserver * Improve events from login and register view, and log enter with apple * Improve NewMessageView events * Improve CreateChannel events * Improve CreateDiscussion and SelectedUsers create group events * Improve RoomsList events and log trivial events * Improve ProfileView events * Remove single line function body for the sidebarNavigate * Improve SettingsView events * Log more events from ScreenLockConfigView * Navigate to Status and AdminPanel View using the defined sidebarNavigate method * Improve StatusView events * Improve RoomView events Co-authored-by: Diego Mello <diegolmello@gmail.com>
2020-07-30 19:51:13 +00:00
logEvent(events.THEME_SET_DARK_LEVEL, { dark_level: value });
changes = { darkLevel: value as TDarkLevel };
2019-12-04 16:39:53 +00:00
}
_setTheme(changes);
};
2019-12-04 16:39:53 +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
};
const renderIcon = () => <List.Icon name='check' color={themes[theme].tintColor} />;
2019-12-04 16:39:53 +00:00
const renderItem = ({ item }: { item: ITheme }) => {
2019-12-04 16:39:53 +00:00
const { label, value } = item;
return (
<>
<List.Item
title={label}
onPress={() => onClick(item)}
testID={`theme-view-${value}`}
right={() => (isSelected(item) ? renderIcon() : null)}
2019-12-04 16:39:53 +00:00
/>
<List.Separator />
2019-12-04 16:39:53 +00:00
</>
);
};
2019-12-04 16:39:53 +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
export default ThemeView;