2022-01-13 21:45:55 +00:00
|
|
|
import { StackNavigationProp } from '@react-navigation/stack';
|
2021-10-06 20:30:10 +00:00
|
|
|
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';
|
2022-01-13 21:45:55 +00:00
|
|
|
import { DisplayMode, SortBy } from '../constants/constantDisplayMode';
|
2021-10-06 20:30:10 +00:00
|
|
|
import * as HeaderButton from '../containers/HeaderButton';
|
2022-01-13 21:45:55 +00:00
|
|
|
import * as List from '../containers/List';
|
2021-10-06 20:30:10 +00:00
|
|
|
import { ICON_SIZE } from '../containers/List/constants';
|
2022-01-13 21:45:55 +00:00
|
|
|
import SafeAreaView from '../containers/SafeAreaView';
|
|
|
|
import StatusBar from '../containers/StatusBar';
|
|
|
|
import { IPreferences } from '../definitions';
|
|
|
|
import I18n from '../i18n';
|
|
|
|
import RocketChat from '../lib/rocketchat';
|
2022-01-11 14:47:23 +00:00
|
|
|
import { SettingsStackParamList } from '../stacks/types';
|
2022-01-13 21:45:55 +00:00
|
|
|
import { useTheme } from '../theme';
|
|
|
|
import log, { events, logEvent } from '../utils/log';
|
2022-01-11 14:47:23 +00:00
|
|
|
|
|
|
|
interface IDisplayPrefsView {
|
|
|
|
navigation: StackNavigationProp<SettingsStackParamList, 'DisplayPrefsView'>;
|
|
|
|
isMasterDetail: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
const DisplayPrefsView = (props: IDisplayPrefsView): JSX.Element => {
|
2021-10-06 20:30:10 +00:00
|
|
|
const { theme } = useTheme();
|
|
|
|
|
2022-01-11 14:47:23 +00:00
|
|
|
const { sortBy, groupByType, showFavorites, showUnread, showAvatar, displayMode } = useSelector(
|
|
|
|
(state: any) => state.sortPreferences
|
|
|
|
);
|
|
|
|
const { isMasterDetail } = useSelector((state: any) => state.app);
|
2021-10-06 20:30:10 +00:00
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
|
|
|
useEffect(() => {
|
2021-11-22 16:19:49 +00:00
|
|
|
const { navigation } = props;
|
2021-10-06 20:30:10 +00:00
|
|
|
navigation.setOptions({
|
2021-11-22 16:19:49 +00:00
|
|
|
title: I18n.t('Display')
|
2021-10-06 20:30:10 +00:00
|
|
|
});
|
2021-11-22 16:19:49 +00:00
|
|
|
if (!isMasterDetail) {
|
|
|
|
navigation.setOptions({
|
|
|
|
headerLeft: () => <HeaderButton.Drawer navigation={navigation} testID='display-view-drawer' />
|
|
|
|
});
|
|
|
|
}
|
2021-10-06 20:30:10 +00:00
|
|
|
}, []);
|
|
|
|
|
2022-01-13 21:45:55 +00:00
|
|
|
const setSortPreference = async (param: Partial<IPreferences>) => {
|
2021-10-06 20:30:10 +00:00
|
|
|
try {
|
|
|
|
dispatch(setPreference(param));
|
|
|
|
await RocketChat.saveSortPreference(param);
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const sortByName = async () => {
|
|
|
|
logEvent(events.DP_SORT_CHANNELS_BY_NAME);
|
2022-01-11 14:47:23 +00:00
|
|
|
await setSortPreference({ sortBy: SortBy.Alphabetical });
|
2021-10-06 20:30:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const sortByActivity = async () => {
|
|
|
|
logEvent(events.DP_SORT_CHANNELS_BY_ACTIVITY);
|
2022-01-11 14:47:23 +00:00
|
|
|
await setSortPreference({ sortBy: SortBy.Activity });
|
2021-10-06 20:30:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
2022-01-11 14:47:23 +00:00
|
|
|
await setSortPreference({ displayMode: DisplayMode.Expanded });
|
2021-10-06 20:30:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const displayCondensed = async () => {
|
|
|
|
logEvent(events.DP_DISPLAY_CONDENSED);
|
2022-01-11 14:47:23 +00:00
|
|
|
await setSortPreference({ displayMode: DisplayMode.Condensed });
|
2021-10-06 20:30:10 +00:00
|
|
|
};
|
|
|
|
|
2022-01-11 14:47:23 +00:00
|
|
|
const renderCheckBox = (value: boolean) => (
|
2021-10-06 20:30:10 +00:00
|
|
|
<List.Icon name={value ? 'checkbox-checked' : 'checkbox-unchecked'} color={value ? themes[theme].actionTintColor : null} />
|
|
|
|
);
|
|
|
|
|
2022-01-11 14:47:23 +00:00
|
|
|
const renderAvatarSwitch = (value: boolean) => (
|
2021-10-06 20:30:10 +00:00
|
|
|
<Switch value={value} onValueChange={() => toggleAvatar()} testID='display-pref-view-avatar-switch' />
|
|
|
|
);
|
|
|
|
|
2022-01-11 14:47:23 +00:00
|
|
|
const renderRadio = (value: boolean) => (
|
2021-10-06 20:30:10 +00:00
|
|
|
<RadioButton
|
|
|
|
selected={!!value}
|
|
|
|
color={value ? themes[theme].actionTintColor : themes[theme].auxiliaryText}
|
|
|
|
size={ICON_SIZE}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<SafeAreaView>
|
|
|
|
<StatusBar />
|
|
|
|
<List.Container testID='display-view-list'>
|
|
|
|
<List.Section title='Display'>
|
|
|
|
<List.Separator />
|
|
|
|
<List.Item
|
|
|
|
left={() => <List.Icon name='view-extended' />}
|
|
|
|
title='Expanded'
|
|
|
|
testID='display-pref-view-expanded'
|
2022-01-11 14:47:23 +00:00
|
|
|
right={() => renderRadio(displayMode === DisplayMode.Expanded)}
|
2021-10-06 20:30:10 +00:00
|
|
|
onPress={displayExpanded}
|
|
|
|
/>
|
|
|
|
<List.Separator />
|
|
|
|
<List.Item
|
|
|
|
left={() => <List.Icon name='view-medium' />}
|
|
|
|
title='Condensed'
|
|
|
|
testID='display-pref-view-condensed'
|
2022-01-11 14:47:23 +00:00
|
|
|
right={() => renderRadio(displayMode === DisplayMode.Condensed)}
|
2021-10-06 20:30:10 +00:00
|
|
|
onPress={displayCondensed}
|
|
|
|
/>
|
|
|
|
<List.Separator />
|
|
|
|
<List.Item
|
|
|
|
left={() => <List.Icon name='avatar' />}
|
|
|
|
title='Avatars'
|
|
|
|
testID='display-pref-view-avatars'
|
|
|
|
right={() => renderAvatarSwitch(showAvatar)}
|
|
|
|
/>
|
|
|
|
<List.Separator />
|
|
|
|
</List.Section>
|
|
|
|
|
|
|
|
<List.Section title='Sort_by'>
|
|
|
|
<List.Separator />
|
|
|
|
<List.Item
|
|
|
|
title='Activity'
|
|
|
|
testID='display-pref-view-activity'
|
|
|
|
left={() => <List.Icon name='clock' />}
|
|
|
|
onPress={sortByActivity}
|
2022-01-11 14:47:23 +00:00
|
|
|
right={() => renderRadio(sortBy === SortBy.Activity)}
|
2021-10-06 20:30:10 +00:00
|
|
|
/>
|
|
|
|
<List.Separator />
|
|
|
|
<List.Item
|
|
|
|
title='Name'
|
|
|
|
testID='display-pref-view-name'
|
|
|
|
left={() => <List.Icon name='sort-az' />}
|
|
|
|
onPress={sortByName}
|
2022-01-11 14:47:23 +00:00
|
|
|
right={() => renderRadio(sortBy === SortBy.Alphabetical)}
|
2021-10-06 20:30:10 +00:00
|
|
|
/>
|
|
|
|
<List.Separator />
|
|
|
|
</List.Section>
|
|
|
|
|
|
|
|
<List.Section title='Group_by'>
|
|
|
|
<List.Separator />
|
|
|
|
<List.Item
|
|
|
|
title='Unread_on_top'
|
|
|
|
testID='display-pref-view-unread'
|
|
|
|
left={() => <List.Icon name='flag' />}
|
|
|
|
onPress={toggleUnread}
|
|
|
|
right={() => renderCheckBox(showUnread)}
|
|
|
|
/>
|
|
|
|
<List.Separator />
|
|
|
|
<List.Item
|
|
|
|
title='Favorites'
|
|
|
|
testID='display-pref-view-favorites'
|
|
|
|
left={() => <List.Icon name='star' />}
|
|
|
|
onPress={toggleGroupByFavorites}
|
|
|
|
right={() => renderCheckBox(showFavorites)}
|
|
|
|
/>
|
|
|
|
<List.Separator />
|
|
|
|
<List.Item
|
|
|
|
title='Types'
|
|
|
|
testID='display-pref-view-types'
|
|
|
|
left={() => <List.Icon name='group-by-type' />}
|
|
|
|
onPress={toggleGroupByType}
|
|
|
|
right={() => renderCheckBox(groupByType)}
|
|
|
|
/>
|
|
|
|
<List.Separator />
|
|
|
|
</List.Section>
|
|
|
|
</List.Container>
|
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-01-11 14:47:23 +00:00
|
|
|
DisplayPrefsView.propTypes = {};
|
2021-10-06 20:30:10 +00:00
|
|
|
|
|
|
|
export default DisplayPrefsView;
|