import React, { useEffect } from 'react'; import { StackNavigationProp } from '@react-navigation/stack'; import { useNavigation } from '@react-navigation/native'; import { Switch } from 'react-native'; import { RadioButton } from 'react-native-ui-lib'; import { useDispatch } from 'react-redux'; import { setPreference } from '../actions/sortPreferences'; import { DisplayMode, SortBy } from '../lib/constants'; import * as HeaderButton from '../containers/HeaderButton'; import * as List from '../containers/List'; import { ICON_SIZE } from '../containers/List/constants'; import SafeAreaView from '../containers/SafeAreaView'; import StatusBar from '../containers/StatusBar'; import { IPreferences } from '../definitions'; import I18n from '../i18n'; import { SettingsStackParamList } from '../stacks/types'; import { useTheme } from '../theme'; import { events, logEvent } from '../lib/methods/helpers/log'; import { saveSortPreference } from '../lib/methods'; import { useAppSelector } from '../lib/hooks'; const DisplayPrefsView = (): React.ReactElement => { const navigation = useNavigation>(); const { colors } = useTheme(); const { sortBy, groupByType, showFavorites, showUnread, showAvatar, displayMode } = useAppSelector( state => state.sortPreferences ); const { isMasterDetail } = useAppSelector(state => state.app); const dispatch = useDispatch(); useEffect(() => { navigation.setOptions({ title: I18n.t('Display') }); if (!isMasterDetail) { navigation.setOptions({ headerLeft: () => }); } }, [isMasterDetail, navigation]); const setSortPreference = (param: Partial) => { dispatch(setPreference(param)); saveSortPreference(param); }; const sortByName = () => { logEvent(events.DP_SORT_CHANNELS_BY_NAME); setSortPreference({ sortBy: SortBy.Alphabetical }); }; const sortByActivity = () => { logEvent(events.DP_SORT_CHANNELS_BY_ACTIVITY); setSortPreference({ sortBy: SortBy.Activity }); }; const toggleGroupByType = () => { logEvent(events.DP_GROUP_CHANNELS_BY_TYPE); setSortPreference({ groupByType: !groupByType }); }; const toggleGroupByFavorites = () => { logEvent(events.DP_GROUP_CHANNELS_BY_FAVORITE); setSortPreference({ showFavorites: !showFavorites }); }; const toggleUnread = () => { logEvent(events.DP_GROUP_CHANNELS_BY_UNREAD); setSortPreference({ showUnread: !showUnread }); }; const toggleAvatar = () => { logEvent(events.DP_TOGGLE_AVATAR); setSortPreference({ showAvatar: !showAvatar }); }; const displayExpanded = () => { logEvent(events.DP_DISPLAY_EXPANDED); setSortPreference({ displayMode: DisplayMode.Expanded }); }; const displayCondensed = () => { logEvent(events.DP_DISPLAY_CONDENSED); setSortPreference({ displayMode: DisplayMode.Condensed }); }; const renderCheckBox = (value: boolean) => ( ); const renderAvatarSwitch = (value: boolean) => ( toggleAvatar()} testID='display-pref-view-avatar-switch' /> ); const renderRadio = (value: boolean) => ( ); return ( } title='Expanded' testID='display-pref-view-expanded' right={() => renderRadio(displayMode === DisplayMode.Expanded)} onPress={displayExpanded} /> } title='Condensed' testID='display-pref-view-condensed' right={() => renderRadio(displayMode === DisplayMode.Condensed)} onPress={displayCondensed} /> } title='Avatars' testID='display-pref-view-avatars' right={() => renderAvatarSwitch(showAvatar)} /> } onPress={sortByActivity} right={() => renderRadio(sortBy === SortBy.Activity)} /> } onPress={sortByName} right={() => renderRadio(sortBy === SortBy.Alphabetical)} /> } onPress={toggleUnread} right={() => renderCheckBox(showUnread)} /> } onPress={toggleGroupByFavorites} right={() => renderCheckBox(showFavorites)} /> } onPress={toggleGroupByType} right={() => renderCheckBox(groupByType)} /> ); }; export default DisplayPrefsView;