import React from 'react'; import { Switch, Text, StyleSheet } from 'react-native'; import PropTypes from 'prop-types'; import database from '../../lib/database'; import { SWITCH_TRACK_COLOR, themes } from '../../constants/colors'; import StatusBar from '../../containers/StatusBar'; import * as List from '../../containers/List'; import I18n from '../../i18n'; import RocketChat from '../../lib/rocketchat'; import { withTheme } from '../../theme'; import protectedFunction from '../../lib/methods/helpers/protectedFunction'; import SafeAreaView from '../../containers/SafeAreaView'; import log, { events, logEvent } from '../../utils/log'; import { OPTIONS } from './options'; import sharedStyles from '../Styles'; const styles = StyleSheet.create({ pickerText: { ...sharedStyles.textRegular, fontSize: 16 } }); class NotificationPreferencesView extends React.Component { static navigationOptions = () => ({ title: I18n.t('Notification_Preferences') }) static propTypes = { navigation: PropTypes.object, route: PropTypes.object, theme: PropTypes.string }; constructor(props) { super(props); this.mounted = false; this.rid = props.route.params?.rid; const room = props.route.params?.room; this.state = { room: room || {} }; 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; } }); } } componentDidMount() { this.mounted = true; } componentWillUnmount() { if (this.subscription && this.subscription.unsubscribe) { this.subscription.unsubscribe(); } } saveNotificationSettings = async(key, value, params) => { logEvent(events[`NP_${ key.toUpperCase() }`]); const { room } = this.state; const db = database.active; try { 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 } await db.action(async() => { await room.update(protectedFunction((r) => { r[key] = room[key]; })); }); } catch (e) { logEvent(events[`NP_${ key.toUpperCase() }_F`]); log(e); } } 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) }); } renderPickerOption = (key) => { const { room } = this.state; const { theme } = this.props; const text = room[key] ? OPTIONS[key].find(option => option.value === room[key]) : OPTIONS[key][0]; return {I18n.t(text?.label, { defaultValue: text?.label, second: text?.second })}; } renderSwitch = (key) => { const { room } = this.state; return ( this.onValueChangeSwitch(key, !value)} /> ); } render() { const { room } = this.state; return ( this.renderSwitch('disableNotifications')} /> this.renderSwitch('muteGroupMentions')} /> this.renderSwitch('hideUnreadStatus')} /> this.pickerSelection(title, 'desktopNotifications')} right={() => this.renderPickerOption('desktopNotifications')} /> this.pickerSelection(title, 'mobilePushNotifications')} right={() => this.renderPickerOption('mobilePushNotifications')} /> this.pickerSelection(title, 'audioNotifications')} right={() => this.renderPickerOption('audioNotifications')} /> this.pickerSelection(title, 'audioNotificationValue')} right={() => this.renderPickerOption('audioNotificationValue')} /> this.pickerSelection(title, 'desktopNotificationDuration')} right={() => this.renderPickerOption('desktopNotificationDuration')} /> this.pickerSelection(title, 'emailNotifications')} right={() => this.renderPickerOption('emailNotifications')} /> ); } } export default withTheme(NotificationPreferencesView);