2019-08-23 16:24:15 +00:00
|
|
|
import React from 'react';
|
|
|
|
import {
|
|
|
|
View, ScrollView, Switch, Text
|
|
|
|
} from 'react-native';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
2020-04-06 21:40:18 +00:00
|
|
|
import database from '../../lib/database';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { SWITCH_TRACK_COLOR, themes } from '../../constants/colors';
|
2019-08-23 16:24:15 +00:00
|
|
|
import StatusBar from '../../containers/StatusBar';
|
|
|
|
import ListItem from '../../containers/ListItem';
|
|
|
|
import Separator from '../../containers/Separator';
|
|
|
|
import I18n from '../../i18n';
|
|
|
|
import scrollPersistTaps from '../../utils/scrollPersistTaps';
|
|
|
|
import styles from './styles';
|
|
|
|
import RocketChat from '../../lib/rocketchat';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { withTheme } from '../../theme';
|
2020-04-06 21:40:18 +00:00
|
|
|
import protectedFunction from '../../lib/methods/helpers/protectedFunction';
|
2020-06-15 14:00:46 +00:00
|
|
|
import SafeAreaView from '../../containers/SafeAreaView';
|
2020-07-08 20:46:05 +00:00
|
|
|
import log from '../../utils/log';
|
2019-08-23 16:24:15 +00:00
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
const SectionTitle = React.memo(({ title, theme }) => (
|
|
|
|
<Text
|
|
|
|
style={[
|
|
|
|
styles.sectionTitle,
|
|
|
|
{
|
|
|
|
backgroundColor: themes[theme].auxiliaryBackground,
|
|
|
|
color: themes[theme].infoText
|
|
|
|
}
|
|
|
|
]}
|
|
|
|
>
|
|
|
|
{title}
|
|
|
|
</Text>
|
|
|
|
));
|
2019-08-23 16:24:15 +00:00
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
const SectionSeparator = React.memo(({ theme }) => (
|
|
|
|
<View
|
|
|
|
style={[
|
|
|
|
styles.sectionSeparatorBorder,
|
|
|
|
{ backgroundColor: themes[theme].auxiliaryBackground }
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
));
|
2019-08-23 16:24:15 +00:00
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
const Info = React.memo(({ info, theme }) => (
|
|
|
|
<Text
|
|
|
|
style={[
|
|
|
|
styles.infoText,
|
|
|
|
{
|
|
|
|
color: themes[theme].infoText,
|
|
|
|
backgroundColor: themes[theme].auxiliaryBackground
|
|
|
|
}
|
|
|
|
]}
|
|
|
|
>
|
|
|
|
{info}
|
|
|
|
</Text>
|
|
|
|
));
|
2019-08-23 16:24:15 +00:00
|
|
|
|
|
|
|
SectionTitle.propTypes = {
|
2019-12-04 16:39:53 +00:00
|
|
|
title: PropTypes.string,
|
|
|
|
theme: PropTypes.string
|
|
|
|
};
|
|
|
|
|
|
|
|
SectionSeparator.propTypes = {
|
|
|
|
theme: PropTypes.string
|
2019-08-23 16:24:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Info.propTypes = {
|
2019-12-04 16:39:53 +00:00
|
|
|
info: PropTypes.string,
|
|
|
|
theme: PropTypes.string
|
2019-08-23 16:24:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const OPTIONS = {
|
|
|
|
desktopNotifications: [{
|
2020-07-24 19:23:34 +00:00
|
|
|
label: 'Default', value: 'default'
|
2019-08-23 16:24:15 +00:00
|
|
|
}, {
|
2020-07-24 19:23:34 +00:00
|
|
|
label: 'All_Messages', value: 'all'
|
2019-08-23 16:24:15 +00:00
|
|
|
}, {
|
2020-07-24 19:23:34 +00:00
|
|
|
label: 'Mentions', value: 'mentions'
|
2019-08-23 16:24:15 +00:00
|
|
|
}, {
|
2020-07-24 19:23:34 +00:00
|
|
|
label: 'Nothing', value: 'nothing'
|
2019-08-23 16:24:15 +00:00
|
|
|
}],
|
|
|
|
audioNotifications: [{
|
2020-07-24 19:23:34 +00:00
|
|
|
label: 'Default', value: 'default'
|
2019-08-23 16:24:15 +00:00
|
|
|
}, {
|
2020-07-24 19:23:34 +00:00
|
|
|
label: 'All_Messages', value: 'all'
|
2019-08-23 16:24:15 +00:00
|
|
|
}, {
|
2020-07-24 19:23:34 +00:00
|
|
|
label: 'Mentions', value: 'mentions'
|
2019-08-23 16:24:15 +00:00
|
|
|
}, {
|
2020-07-24 19:23:34 +00:00
|
|
|
label: 'Nothing', value: 'nothing'
|
2019-08-23 16:24:15 +00:00
|
|
|
}],
|
|
|
|
mobilePushNotifications: [{
|
2020-07-24 19:23:34 +00:00
|
|
|
label: 'Default', value: 'default'
|
2019-08-23 16:24:15 +00:00
|
|
|
}, {
|
2020-07-24 19:23:34 +00:00
|
|
|
label: 'All_Messages', value: 'all'
|
2019-08-23 16:24:15 +00:00
|
|
|
}, {
|
2020-07-24 19:23:34 +00:00
|
|
|
label: 'Mentions', value: 'mentions'
|
2019-08-23 16:24:15 +00:00
|
|
|
}, {
|
2020-07-24 19:23:34 +00:00
|
|
|
label: 'Nothing', value: 'nothing'
|
2019-08-23 16:24:15 +00:00
|
|
|
}],
|
|
|
|
emailNotifications: [{
|
2020-07-24 19:23:34 +00:00
|
|
|
label: 'Default', value: 'default'
|
2019-08-23 16:24:15 +00:00
|
|
|
}, {
|
2020-07-24 19:23:34 +00:00
|
|
|
label: 'All_Messages', value: 'all'
|
2019-08-23 16:24:15 +00:00
|
|
|
}, {
|
2020-07-24 19:23:34 +00:00
|
|
|
label: 'Mentions', value: 'mentions'
|
2019-08-23 16:24:15 +00:00
|
|
|
}, {
|
2020-07-24 19:23:34 +00:00
|
|
|
label: 'Nothing', value: 'nothing'
|
2019-08-23 16:24:15 +00:00
|
|
|
}],
|
|
|
|
desktopNotificationDuration: [{
|
2020-07-24 19:23:34 +00:00
|
|
|
label: 'Default', value: 0
|
2019-08-23 16:24:15 +00:00
|
|
|
}, {
|
2020-07-24 19:23:34 +00:00
|
|
|
label: 'Seconds', second: 1, value: 1
|
2019-08-23 16:24:15 +00:00
|
|
|
}, {
|
2020-07-24 19:23:34 +00:00
|
|
|
label: 'Seconds', second: 2, value: 2
|
2019-08-23 16:24:15 +00:00
|
|
|
}, {
|
2020-07-24 19:23:34 +00:00
|
|
|
label: 'Seconds', second: 3, value: 3
|
2019-08-23 16:24:15 +00:00
|
|
|
}, {
|
2020-07-24 19:23:34 +00:00
|
|
|
label: 'Seconds', second: 4, value: 4
|
2019-08-23 16:24:15 +00:00
|
|
|
}, {
|
2020-07-24 19:23:34 +00:00
|
|
|
label: 'Seconds', second: 5, value: 5
|
2019-08-23 16:24:15 +00:00
|
|
|
}],
|
|
|
|
audioNotificationValue: [{
|
|
|
|
label: 'None', value: 'none None'
|
|
|
|
}, {
|
2020-07-24 19:23:34 +00:00
|
|
|
label: 'Default', value: '0 Default'
|
2019-08-23 16:24:15 +00:00
|
|
|
}, {
|
|
|
|
label: 'Beep', value: 'beep Beep'
|
|
|
|
}, {
|
|
|
|
label: 'Ding', value: 'ding Ding'
|
|
|
|
}, {
|
|
|
|
label: 'Chelle', value: 'chelle Chelle'
|
|
|
|
}, {
|
|
|
|
label: 'Droplet', value: 'droplet Droplet'
|
|
|
|
}, {
|
|
|
|
label: 'Highbell', value: 'highbell Highbell'
|
|
|
|
}, {
|
|
|
|
label: 'Seasons', value: 'seasons Seasons'
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
class NotificationPreferencesView extends React.Component {
|
2020-06-15 14:00:46 +00:00
|
|
|
static navigationOptions = {
|
|
|
|
title: I18n.t('Notification_Preferences')
|
|
|
|
}
|
2019-08-23 16:24:15 +00:00
|
|
|
|
|
|
|
static propTypes = {
|
2019-12-04 16:39:53 +00:00
|
|
|
navigation: PropTypes.object,
|
2020-06-15 14:00:46 +00:00
|
|
|
route: PropTypes.object,
|
2019-12-04 16:39:53 +00:00
|
|
|
theme: PropTypes.string
|
|
|
|
};
|
2019-08-23 16:24:15 +00:00
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2019-09-16 20:26:32 +00:00
|
|
|
this.mounted = false;
|
2020-06-15 14:00:46 +00:00
|
|
|
this.rid = props.route.params?.rid;
|
|
|
|
const room = props.route.params?.room;
|
2019-09-17 14:43:49 +00:00
|
|
|
this.state = {
|
|
|
|
room: room || {}
|
|
|
|
};
|
2019-09-16 20:26:32 +00:00
|
|
|
if (room && room.observe) {
|
|
|
|
this.roomObservable = room.observe();
|
|
|
|
this.subscription = this.roomObservable
|
|
|
|
.subscribe((changes) => {
|
|
|
|
if (this.mounted) {
|
|
|
|
this.setState({ room: changes });
|
|
|
|
} else {
|
|
|
|
this.state.room = changes;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-08-23 16:24:15 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
componentDidMount() {
|
|
|
|
this.mounted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.subscription && this.subscription.unsubscribe) {
|
|
|
|
this.subscription.unsubscribe();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 21:40:18 +00:00
|
|
|
saveNotificationSettings = async(key, value, params) => {
|
|
|
|
const { room } = this.state;
|
|
|
|
const db = database.active;
|
|
|
|
|
2019-08-23 16:24:15 +00:00
|
|
|
try {
|
2020-07-08 20:46:05 +00:00
|
|
|
await db.action(async() => {
|
|
|
|
await room.update(protectedFunction((r) => {
|
|
|
|
r[key] = value;
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
const result = await RocketChat.saveNotificationSettings(this.rid, params);
|
|
|
|
if (result.success) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
// do nothing
|
2020-04-06 21:40:18 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 20:46:05 +00:00
|
|
|
await db.action(async() => {
|
|
|
|
await room.update(protectedFunction((r) => {
|
|
|
|
r[key] = room[key];
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
2019-08-23 16:24:15 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 21:40:18 +00:00
|
|
|
onValueChangeSwitch = (key, value) => this.saveNotificationSettings(key, value, { [key]: value ? '1' : '0' });
|
|
|
|
|
|
|
|
onValueChangePicker = (key, value) => this.saveNotificationSettings(key, value, { [key]: value.toString() });
|
|
|
|
|
|
|
|
pickerSelection = (title, key) => {
|
|
|
|
const { room } = this.state;
|
|
|
|
const { navigation } = this.props;
|
|
|
|
navigation.navigate('PickerView', {
|
|
|
|
title,
|
|
|
|
data: OPTIONS[key],
|
|
|
|
value: room[key],
|
|
|
|
onChangeValue: value => this.onValueChangePicker(key, value)
|
|
|
|
});
|
2019-08-23 16:24:15 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 21:40:18 +00:00
|
|
|
renderPickerOption = (key) => {
|
2019-08-23 16:24:15 +00:00
|
|
|
const { room } = this.state;
|
2019-12-04 16:39:53 +00:00
|
|
|
const { theme } = this.props;
|
2020-04-06 21:40:18 +00:00
|
|
|
const text = room[key] ? OPTIONS[key].find(option => option.value === room[key]) : OPTIONS[key][0];
|
2020-07-24 19:23:34 +00:00
|
|
|
return <Text style={[styles.pickerText, { color: themes[theme].actionTintColor }]}>{I18n.t(text?.label, { defaultValue: text?.label, second: text?.second })}</Text>;
|
2019-08-23 16:24:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
renderSwitch = (key) => {
|
|
|
|
const { room } = this.state;
|
|
|
|
return (
|
|
|
|
<Switch
|
|
|
|
value={!room[key]}
|
|
|
|
testID={key}
|
|
|
|
trackColor={SWITCH_TRACK_COLOR}
|
|
|
|
onValueChange={value => this.onValueChangeSwitch(key, !value)}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { room } = this.state;
|
2019-12-04 16:39:53 +00:00
|
|
|
const { theme } = this.props;
|
2019-08-23 16:24:15 +00:00
|
|
|
return (
|
2020-06-15 14:00:46 +00:00
|
|
|
<SafeAreaView testID='notification-preference-view' theme={theme}>
|
2019-12-04 16:39:53 +00:00
|
|
|
<StatusBar theme={theme} />
|
2019-08-23 16:24:15 +00:00
|
|
|
<ScrollView
|
|
|
|
{...scrollPersistTaps}
|
2019-12-04 16:39:53 +00:00
|
|
|
style={{ backgroundColor: themes[theme].auxiliaryBackground }}
|
2019-08-23 16:24:15 +00:00
|
|
|
contentContainerStyle={styles.contentContainer}
|
|
|
|
testID='notification-preference-view-list'
|
|
|
|
>
|
2019-12-04 16:39:53 +00:00
|
|
|
<Separator theme={theme} />
|
2019-08-23 16:24:15 +00:00
|
|
|
<ListItem
|
|
|
|
title={I18n.t('Receive_Notification')}
|
|
|
|
testID='notification-preference-view-receive-notification'
|
|
|
|
right={() => this.renderSwitch('disableNotifications')}
|
2019-12-04 16:39:53 +00:00
|
|
|
theme={theme}
|
2019-08-23 16:24:15 +00:00
|
|
|
/>
|
2019-12-04 16:39:53 +00:00
|
|
|
<Separator theme={theme} />
|
|
|
|
<Info info={I18n.t('Receive_notifications_from', { name: room.name })} theme={theme} />
|
|
|
|
<SectionSeparator theme={theme} />
|
2019-08-23 16:24:15 +00:00
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
<Separator theme={theme} />
|
2019-08-23 16:24:15 +00:00
|
|
|
<ListItem
|
|
|
|
title={I18n.t('Receive_Group_Mentions')}
|
|
|
|
testID='notification-preference-view-group-mentions'
|
|
|
|
right={() => this.renderSwitch('muteGroupMentions')}
|
2019-12-04 16:39:53 +00:00
|
|
|
theme={theme}
|
2019-08-23 16:24:15 +00:00
|
|
|
/>
|
2019-12-04 16:39:53 +00:00
|
|
|
<Separator theme={theme} />
|
|
|
|
<Info info={I18n.t('Receive_Group_Mentions_Info')} theme={theme} />
|
2019-08-23 16:24:15 +00:00
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
<SectionSeparator theme={theme} />
|
|
|
|
<Separator theme={theme} />
|
2019-08-23 16:24:15 +00:00
|
|
|
<ListItem
|
|
|
|
title={I18n.t('Show_Unread_Counter')}
|
|
|
|
testID='notification-preference-view-unread-count'
|
|
|
|
right={() => this.renderSwitch('hideUnreadStatus')}
|
2019-12-04 16:39:53 +00:00
|
|
|
theme={theme}
|
2019-08-23 16:24:15 +00:00
|
|
|
/>
|
2019-12-04 16:39:53 +00:00
|
|
|
<Separator theme={theme} />
|
|
|
|
<Info info={I18n.t('Show_Unread_Counter_Info')} theme={theme} />
|
2019-08-23 16:24:15 +00:00
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
<SectionSeparator theme={theme} />
|
|
|
|
<SectionTitle title={I18n.t('IN_APP_AND_DESKTOP')} theme={theme} />
|
|
|
|
<Separator theme={theme} />
|
2019-08-23 16:24:15 +00:00
|
|
|
|
|
|
|
<ListItem
|
|
|
|
title={I18n.t('Alert')}
|
|
|
|
testID='notification-preference-view-alert'
|
2020-04-06 21:40:18 +00:00
|
|
|
onPress={title => this.pickerSelection(title, 'desktopNotifications')}
|
|
|
|
right={() => this.renderPickerOption('desktopNotifications')}
|
2019-12-04 16:39:53 +00:00
|
|
|
theme={theme}
|
2019-08-23 16:24:15 +00:00
|
|
|
/>
|
2019-12-04 16:39:53 +00:00
|
|
|
<Separator theme={theme} />
|
|
|
|
<Info info={I18n.t('In_App_and_Desktop_Alert_info')} theme={theme} />
|
2019-08-23 16:24:15 +00:00
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
<SectionSeparator theme={theme} />
|
|
|
|
<SectionTitle title={I18n.t('PUSH_NOTIFICATIONS')} theme={theme} />
|
|
|
|
<Separator theme={theme} />
|
2019-08-23 16:24:15 +00:00
|
|
|
|
|
|
|
<ListItem
|
|
|
|
title={I18n.t('Alert')}
|
|
|
|
testID='notification-preference-view-push-notification'
|
2020-04-06 21:40:18 +00:00
|
|
|
onPress={title => this.pickerSelection(title, 'mobilePushNotifications')}
|
|
|
|
right={() => this.renderPickerOption('mobilePushNotifications')}
|
2019-12-04 16:39:53 +00:00
|
|
|
theme={theme}
|
2019-08-23 16:24:15 +00:00
|
|
|
/>
|
2019-12-04 16:39:53 +00:00
|
|
|
<Separator theme={theme} />
|
|
|
|
<Info info={I18n.t('Push_Notifications_Alert_Info')} theme={theme} />
|
2019-08-23 16:24:15 +00:00
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
<SectionSeparator theme={theme} />
|
|
|
|
<SectionTitle title={I18n.t('DESKTOP_OPTIONS')} theme={theme} />
|
|
|
|
<Separator theme={theme} />
|
2019-08-23 16:24:15 +00:00
|
|
|
|
|
|
|
<ListItem
|
|
|
|
title={I18n.t('Audio')}
|
|
|
|
testID='notification-preference-view-audio'
|
2020-04-06 21:40:18 +00:00
|
|
|
onPress={title => this.pickerSelection(title, 'audioNotifications')}
|
|
|
|
right={() => this.renderPickerOption('audioNotifications')}
|
2019-12-04 16:39:53 +00:00
|
|
|
theme={theme}
|
2019-08-23 16:24:15 +00:00
|
|
|
/>
|
2019-12-04 16:39:53 +00:00
|
|
|
<Separator theme={theme} />
|
2019-08-23 16:24:15 +00:00
|
|
|
<ListItem
|
|
|
|
title={I18n.t('Sound')}
|
|
|
|
testID='notification-preference-view-sound'
|
2020-04-06 21:40:18 +00:00
|
|
|
onPress={title => this.pickerSelection(title, 'audioNotificationValue')}
|
|
|
|
right={() => this.renderPickerOption('audioNotificationValue')}
|
2019-12-04 16:39:53 +00:00
|
|
|
theme={theme}
|
2019-08-23 16:24:15 +00:00
|
|
|
/>
|
2019-12-04 16:39:53 +00:00
|
|
|
<Separator theme={theme} />
|
2019-08-23 16:24:15 +00:00
|
|
|
<ListItem
|
|
|
|
title={I18n.t('Notification_Duration')}
|
|
|
|
testID='notification-preference-view-notification-duration'
|
2020-04-06 21:40:18 +00:00
|
|
|
onPress={title => this.pickerSelection(title, 'desktopNotificationDuration')}
|
|
|
|
right={() => this.renderPickerOption('desktopNotificationDuration')}
|
2019-12-04 16:39:53 +00:00
|
|
|
theme={theme}
|
2019-08-23 16:24:15 +00:00
|
|
|
/>
|
2019-12-04 16:39:53 +00:00
|
|
|
<Separator theme={theme} />
|
2019-08-23 16:24:15 +00:00
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
<SectionSeparator theme={theme} />
|
|
|
|
<SectionTitle title={I18n.t('EMAIL')} theme={theme} />
|
|
|
|
<Separator theme={theme} />
|
2019-08-23 16:24:15 +00:00
|
|
|
|
|
|
|
<ListItem
|
|
|
|
title={I18n.t('Alert')}
|
|
|
|
testID='notification-preference-view-email-alert'
|
2020-04-06 21:40:18 +00:00
|
|
|
onPress={title => this.pickerSelection(title, 'emailNotifications')}
|
|
|
|
right={() => this.renderPickerOption('emailNotifications')}
|
2019-12-04 16:39:53 +00:00
|
|
|
theme={theme}
|
2019-08-23 16:24:15 +00:00
|
|
|
/>
|
2019-12-04 16:39:53 +00:00
|
|
|
<Separator theme={theme} />
|
2019-08-23 16:24:15 +00:00
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
<View style={[styles.marginBottom, { backgroundColor: themes[theme].auxiliaryBackground }]} />
|
2019-08-23 16:24:15 +00:00
|
|
|
</ScrollView>
|
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-12-04 16:39:53 +00:00
|
|
|
|
|
|
|
export default withTheme(NotificationPreferencesView);
|