2022-08-01 16:35:05 +00:00
|
|
|
import { RouteProp, useNavigation, useRoute } from '@react-navigation/core';
|
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
import { Switch, Text } from 'react-native';
|
2019-08-23 16:24:15 +00:00
|
|
|
|
2022-08-01 16:35:05 +00:00
|
|
|
import { TActionSheetOptionsItem, useActionSheet } from '../../containers/ActionSheet';
|
|
|
|
import { CustomIcon } from '../../containers/CustomIcon';
|
2020-10-30 13:59:44 +00:00
|
|
|
import * as List from '../../containers/List';
|
2020-06-15 14:00:46 +00:00
|
|
|
import SafeAreaView from '../../containers/SafeAreaView';
|
2022-08-01 16:35:05 +00:00
|
|
|
import StatusBar from '../../containers/StatusBar';
|
|
|
|
import { IRoomNotifications, TRoomNotificationsModel } from '../../definitions';
|
|
|
|
import I18n from '../../i18n';
|
|
|
|
import { SWITCH_TRACK_COLOR } from '../../lib/constants';
|
|
|
|
import { useAppSelector } from '../../lib/hooks';
|
|
|
|
import { showErrorAlertWithEMessage } from '../../lib/methods/helpers';
|
|
|
|
import { compareServerVersion } from '../../lib/methods/helpers/compareServerVersion';
|
2022-06-06 14:17:51 +00:00
|
|
|
import log, { events, logEvent } from '../../lib/methods/helpers/log';
|
2022-04-28 20:37:25 +00:00
|
|
|
import { Services } from '../../lib/services';
|
2022-08-01 16:35:05 +00:00
|
|
|
import { ChatsStackParamList } from '../../stacks/types';
|
|
|
|
import { useTheme } from '../../theme';
|
|
|
|
import sharedStyles from '../Styles';
|
|
|
|
import { OPTIONS } from './options';
|
2019-08-23 16:24:15 +00:00
|
|
|
|
2022-08-01 16:35:05 +00:00
|
|
|
type TOptions = keyof typeof OPTIONS;
|
|
|
|
type TRoomNotifications = keyof IRoomNotifications;
|
|
|
|
type TUnionOptionsRoomNotifications = TOptions | TRoomNotifications;
|
2021-11-10 14:55:37 +00:00
|
|
|
|
2022-08-01 16:35:05 +00:00
|
|
|
interface IBaseParams {
|
|
|
|
preference: TUnionOptionsRoomNotifications;
|
2022-05-02 18:24:17 +00:00
|
|
|
room: TRoomNotificationsModel;
|
2022-08-01 16:35:05 +00:00
|
|
|
onChangeValue: (pref: TUnionOptionsRoomNotifications, param: { [key: string]: string }, onError: () => void) => void;
|
2022-05-02 18:24:17 +00:00
|
|
|
}
|
|
|
|
|
2022-08-01 16:35:05 +00:00
|
|
|
const RenderListPicker = ({
|
|
|
|
preference,
|
|
|
|
room,
|
|
|
|
title,
|
|
|
|
testID,
|
|
|
|
onChangeValue
|
|
|
|
}: {
|
|
|
|
title: string;
|
|
|
|
testID: string;
|
|
|
|
} & IBaseParams) => {
|
|
|
|
const { showActionSheet, hideActionSheet } = useActionSheet();
|
|
|
|
const { colors } = useTheme();
|
|
|
|
|
|
|
|
const pref = room[preference]
|
|
|
|
? OPTIONS[preference as TOptions].find(option => option.value === room[preference])
|
|
|
|
: OPTIONS[preference as TOptions][0];
|
|
|
|
|
|
|
|
const [option, setOption] = useState(pref);
|
|
|
|
|
|
|
|
const options: TActionSheetOptionsItem[] = OPTIONS[preference as TOptions].map(i => ({
|
|
|
|
title: I18n.t(i.label, { defaultValue: i.label, second: i.second }),
|
|
|
|
onPress: () => {
|
|
|
|
hideActionSheet();
|
|
|
|
onChangeValue(preference, { [preference]: i.value.toString() }, () => setOption(option));
|
|
|
|
setOption(i);
|
|
|
|
},
|
|
|
|
right: option?.value === i.value ? () => <CustomIcon name={'check'} size={20} color={colors.tintActive} /> : undefined
|
|
|
|
}));
|
|
|
|
|
|
|
|
return (
|
|
|
|
<List.Item
|
|
|
|
title={title}
|
|
|
|
testID={testID}
|
|
|
|
onPress={() => showActionSheet({ options })}
|
|
|
|
right={() => (
|
|
|
|
<Text style={[{ ...sharedStyles.textRegular, fontSize: 16 }, { color: colors.actionTintColor }]}>
|
|
|
|
{option?.label ? I18n.t(option?.label, { defaultValue: option?.label, second: option?.second }) : option?.label}
|
|
|
|
</Text>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const RenderSwitch = ({ preference, room, onChangeValue }: IBaseParams) => {
|
|
|
|
const [switchValue, setSwitchValue] = useState(!room[preference]);
|
|
|
|
return (
|
|
|
|
<Switch
|
|
|
|
value={switchValue}
|
|
|
|
testID={preference as string}
|
|
|
|
trackColor={SWITCH_TRACK_COLOR}
|
|
|
|
onValueChange={value => {
|
|
|
|
onChangeValue(preference, { [preference]: switchValue ? '1' : '0' }, () => setSwitchValue(switchValue));
|
|
|
|
setSwitchValue(value);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const NotificationPreferencesView = (): React.ReactElement => {
|
|
|
|
const route = useRoute<RouteProp<ChatsStackParamList, 'NotificationPrefView'>>();
|
|
|
|
const { rid, room } = route.params;
|
|
|
|
const navigation = useNavigation();
|
|
|
|
const serverVersion = useAppSelector(state => state.server.version);
|
|
|
|
const [hideUnreadStatus, setHideUnreadStatus] = useState(room.hideUnreadStatus);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
navigation.setOptions({
|
|
|
|
title: I18n.t('Notification_Preferences')
|
|
|
|
});
|
|
|
|
}, []);
|
2019-09-16 20:26:32 +00:00
|
|
|
|
2022-08-01 16:35:05 +00:00
|
|
|
useEffect(() => {
|
|
|
|
const observe = room.observe();
|
|
|
|
observe.subscribe(data => {
|
|
|
|
setHideUnreadStatus(data.hideUnreadStatus);
|
|
|
|
});
|
|
|
|
}, []);
|
2020-04-06 21:40:18 +00:00
|
|
|
|
2022-08-01 16:35:05 +00:00
|
|
|
const saveNotificationSettings = async (key: TUnionOptionsRoomNotifications, params: IRoomNotifications, onError: Function) => {
|
2019-08-23 16:24:15 +00:00
|
|
|
try {
|
2022-08-01 16:35:05 +00:00
|
|
|
// @ts-ignore
|
|
|
|
logEvent(events[`NP_${key.toUpperCase()}`]);
|
|
|
|
await Services.saveNotificationSettings(rid, params);
|
2020-07-08 20:46:05 +00:00
|
|
|
} catch (e) {
|
2021-11-10 14:55:37 +00:00
|
|
|
// @ts-ignore
|
2021-09-13 20:41:05 +00:00
|
|
|
logEvent(events[`NP_${key.toUpperCase()}_F`]);
|
2020-07-08 20:46:05 +00:00
|
|
|
log(e);
|
2022-08-01 16:35:05 +00:00
|
|
|
onError();
|
|
|
|
showErrorAlertWithEMessage(e);
|
2020-07-08 20:46:05 +00:00
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-08-23 16:24:15 +00:00
|
|
|
|
2022-08-01 16:35:05 +00:00
|
|
|
return (
|
|
|
|
<SafeAreaView testID='notification-preference-view'>
|
|
|
|
<StatusBar />
|
|
|
|
<List.Container testID='notification-preference-view-list'>
|
|
|
|
<List.Section>
|
|
|
|
<List.Separator />
|
|
|
|
<List.Item
|
|
|
|
title='Receive_Notification'
|
|
|
|
testID='notification-preference-view-receive-notification'
|
|
|
|
right={() => <RenderSwitch preference='disableNotifications' room={room} onChangeValue={saveNotificationSettings} />}
|
|
|
|
/>
|
|
|
|
<List.Separator />
|
|
|
|
<List.Info info={I18n.t('Receive_notifications_from', { name: room.name })} translateInfo={false} />
|
|
|
|
</List.Section>
|
|
|
|
|
|
|
|
<List.Section>
|
|
|
|
<List.Separator />
|
|
|
|
<List.Item
|
|
|
|
title='Receive_Group_Mentions'
|
|
|
|
testID='notification-preference-view-group-mentions'
|
|
|
|
right={() => <RenderSwitch preference='muteGroupMentions' room={room} onChangeValue={saveNotificationSettings} />}
|
|
|
|
/>
|
|
|
|
<List.Separator />
|
|
|
|
<List.Info info='Receive_Group_Mentions_Info' />
|
|
|
|
</List.Section>
|
|
|
|
|
|
|
|
<List.Section>
|
|
|
|
<List.Separator />
|
|
|
|
<List.Item
|
|
|
|
title='Mark_as_unread'
|
|
|
|
testID='notification-preference-view-mark-as-unread'
|
|
|
|
right={() => <RenderSwitch preference='hideUnreadStatus' room={room} onChangeValue={saveNotificationSettings} />}
|
|
|
|
/>
|
|
|
|
<List.Separator />
|
|
|
|
<List.Info info='Mark_as_unread_Info' />
|
|
|
|
</List.Section>
|
|
|
|
|
|
|
|
{hideUnreadStatus && compareServerVersion(serverVersion, 'greaterThanOrEqualTo', '4.8.0') ? (
|
2020-10-30 13:59:44 +00:00
|
|
|
<List.Section>
|
|
|
|
<List.Separator />
|
|
|
|
<List.Item
|
2022-08-01 16:35:05 +00:00
|
|
|
title='Show_badge_for_mentions'
|
|
|
|
testID='notification-preference-view-badge-for-mentions'
|
|
|
|
right={() => <RenderSwitch preference='hideMentionStatus' room={room} onChangeValue={saveNotificationSettings} />}
|
2020-10-30 13:59:44 +00:00
|
|
|
/>
|
|
|
|
<List.Separator />
|
2022-08-01 16:35:05 +00:00
|
|
|
<List.Info info='Show_badge_for_mentions_Info' />
|
2020-10-30 13:59:44 +00:00
|
|
|
</List.Section>
|
2022-08-01 16:35:05 +00:00
|
|
|
) : null}
|
|
|
|
|
|
|
|
<List.Section title='In_App_And_Desktop'>
|
|
|
|
<List.Separator />
|
|
|
|
<RenderListPicker
|
|
|
|
preference='desktopNotifications'
|
|
|
|
room={room}
|
|
|
|
title='Alert'
|
|
|
|
testID='notification-preference-view-alert'
|
|
|
|
onChangeValue={saveNotificationSettings}
|
|
|
|
/>
|
|
|
|
<RenderListPicker
|
|
|
|
preference='audioNotificationValue'
|
|
|
|
room={room}
|
|
|
|
title='Sound'
|
|
|
|
testID='notification-preference-view-sound'
|
|
|
|
onChangeValue={saveNotificationSettings}
|
|
|
|
/>
|
|
|
|
<List.Separator />
|
|
|
|
<List.Info info='In_App_and_Desktop_Alert_info' />
|
|
|
|
</List.Section>
|
|
|
|
<List.Section title='Push_Notifications'>
|
|
|
|
<List.Separator />
|
|
|
|
<RenderListPicker
|
|
|
|
preference='mobilePushNotifications'
|
|
|
|
room={room}
|
|
|
|
title='Alert'
|
|
|
|
testID='notification-preference-view-push-notification'
|
|
|
|
onChangeValue={saveNotificationSettings}
|
|
|
|
/>
|
|
|
|
<List.Separator />
|
|
|
|
<List.Info info='Push_Notifications_Alert_Info' />
|
|
|
|
</List.Section>
|
|
|
|
<List.Section title='Email'>
|
|
|
|
<List.Separator />
|
|
|
|
<RenderListPicker
|
|
|
|
preference='emailNotifications'
|
|
|
|
room={room}
|
|
|
|
title='Alert'
|
|
|
|
testID='notification-preference-view-email-alert'
|
|
|
|
onChangeValue={saveNotificationSettings}
|
|
|
|
/>
|
|
|
|
<List.Separator />
|
|
|
|
</List.Section>
|
|
|
|
</List.Container>
|
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default NotificationPreferencesView;
|