From 1da2417834e722dfc82da493e58c66ae63fc1f0e Mon Sep 17 00:00:00 2001 From: Gleidson Daniel Silva Date: Mon, 12 Sep 2022 10:16:00 -0300 Subject: [PATCH] Chore: Improve code and migrate some props to hooks on ThemeView (#4414) Co-authored-by: Reinaldo Neto <47038980+reinaldonetof@users.noreply.github.com> --- app/views/ThemeView.tsx | 96 ++++++++++++++++++++++++++--------------- 1 file changed, 61 insertions(+), 35 deletions(-) diff --git a/app/views/ThemeView.tsx b/app/views/ThemeView.tsx index 46b49e204..1dcdf4af3 100644 --- a/app/views/ThemeView.tsx +++ b/app/views/ThemeView.tsx @@ -1,17 +1,16 @@ import React, { useLayoutEffect } from 'react'; +import { useNavigation } from '@react-navigation/native'; -import { SettingsStackParamList } from '../stacks/types'; -import I18n from '../i18n'; -import { useTheme } from '../theme'; -import StatusBar from '../containers/StatusBar'; import * as List from '../containers/List'; -import { supportSystemTheme } from '../lib/methods/helpers'; import SafeAreaView from '../containers/SafeAreaView'; -import UserPreferences from '../lib/methods/userPreferences'; +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'; 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'; +import UserPreferences from '../lib/methods/userPreferences'; +import { useTheme } from '../theme'; const THEME_GROUP = 'THEME_GROUP'; const DARK_GROUP = 'DARK_GROUP'; @@ -58,16 +57,40 @@ interface ITheme { group: string; } -type IThemeViewProps = IBaseScreen; +const Item = ({ + onPress, + label, + value, + isSelected +}: { + onPress: () => void; + label: string; + value: string; + isSelected: boolean; +}) => { + const { colors } = useTheme(); + return ( + <> + (isSelected ? : null)} + /> + + + ); +}; -const ThemeView = ({ navigation }: IThemeViewProps): React.ReactElement => { - const { theme, themePreferences, setTheme } = useTheme(); +const ThemeView = (): React.ReactElement => { + const { themePreferences, setTheme } = useTheme(); + const { setOptions } = useNavigation(); useLayoutEffect(() => { - navigation.setOptions({ + setOptions({ title: I18n.t('Theme') }); - }, [navigation]); + }, []); const isSelected = (item: ITheme) => { const { group } = item; @@ -92,10 +115,10 @@ const ThemeView = ({ navigation }: IThemeViewProps): React.ReactElement => { logEvent(events.THEME_SET_DARK_LEVEL, { dark_level: value }); changes = { darkLevel: value as TDarkLevel }; } - _setTheme(changes); + handleTheme(changes); }; - const _setTheme = (theme: Partial) => { + const handleTheme = (theme: Partial) => { const newTheme: IThemePreference = { ...(themePreferences as IThemePreference), ...theme }; if (setTheme) { setTheme(newTheme); @@ -103,34 +126,37 @@ const ThemeView = ({ navigation }: IThemeViewProps): React.ReactElement => { } }; - const renderIcon = () => ; - - const renderItem = ({ item }: { item: ITheme }) => { - const { label, value } = item; - return ( - <> - onClick(item)} - testID={`theme-view-${value}`} - right={() => (isSelected(item) ? renderIcon() : null)} - /> - - - ); - }; - return ( - <>{themeGroup.map(item => renderItem({ item }))} + <> + {themeGroup.map(theme => ( + onClick(theme)} + label={theme.label} + value={theme.value} + isSelected={!!isSelected(theme)} + key={theme.label} + /> + ))} + - <>{darkGroup.map(item => renderItem({ item }))} + <> + {darkGroup.map(theme => ( + onClick(theme)} + label={theme.label} + value={theme.value} + isSelected={!!isSelected(theme)} + key={theme.label} + /> + ))} +