import { StackNavigationProp } from '@react-navigation/stack'; import React, { useEffect } from 'react'; import { Switch } from 'react-native'; import { RadioButton } from 'react-native-ui-lib'; import { useDispatch, useSelector } from 'react-redux'; import { setPreference } from '../actions/sortPreferences'; import { themes } from '../constants/colors'; import { DisplayMode, SortBy } from '../constants/constantDisplayMode'; 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 { IApplicationState, IPreferences } from '../definitions'; import I18n from '../i18n'; import RocketChat from '../lib/rocketchat'; import { SettingsStackParamList } from '../stacks/types'; import { useTheme } from '../theme'; import log, { events, logEvent } from '../utils/log'; interface IDisplayPrefsView { navigation: StackNavigationProp; isMasterDetail: boolean; } const DisplayPrefsView = (props: IDisplayPrefsView): JSX.Element => { const { theme } = useTheme(); const { sortBy, groupByType, showFavorites, showUnread, showAvatar, displayMode } = useSelector( (state: IApplicationState) => state.sortPreferences ); const { isMasterDetail } = useSelector((state: any) => state.app); const dispatch = useDispatch(); useEffect(() => { const { navigation } = props; navigation.setOptions({ title: I18n.t('Display') }); if (!isMasterDetail) { navigation.setOptions({ headerLeft: () => }); } }, []); const setSortPreference = async (param: Partial) => { try { dispatch(setPreference(param)); await RocketChat.saveSortPreference(param); } catch (e) { log(e); } }; const sortByName = async () => { logEvent(events.DP_SORT_CHANNELS_BY_NAME); await setSortPreference({ sortBy: SortBy.Alphabetical }); }; const sortByActivity = async () => { logEvent(events.DP_SORT_CHANNELS_BY_ACTIVITY); await setSortPreference({ sortBy: SortBy.Activity }); }; const toggleGroupByType = async () => { logEvent(events.DP_GROUP_CHANNELS_BY_TYPE); await setSortPreference({ groupByType: !groupByType }); }; const toggleGroupByFavorites = async () => { logEvent(events.DP_GROUP_CHANNELS_BY_FAVORITE); await setSortPreference({ showFavorites: !showFavorites }); }; const toggleUnread = async () => { logEvent(events.DP_GROUP_CHANNELS_BY_UNREAD); await setSortPreference({ showUnread: !showUnread }); }; const toggleAvatar = async () => { logEvent(events.DP_TOGGLE_AVATAR); await setSortPreference({ showAvatar: !showAvatar }); }; const displayExpanded = async () => { logEvent(events.DP_DISPLAY_EXPANDED); await setSortPreference({ displayMode: DisplayMode.Expanded }); }; const displayCondensed = async () => { logEvent(events.DP_DISPLAY_CONDENSED); await 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)} /> ); }; DisplayPrefsView.propTypes = {}; export default DisplayPrefsView;