2019-08-23 16:24:15 +00:00
|
|
|
import React from 'react';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { StyleSheet, Switch, Text } from 'react-native';
|
2021-11-10 14:55:37 +00:00
|
|
|
import { RouteProp } from '@react-navigation/core';
|
|
|
|
import { StackNavigationProp } from '@react-navigation/stack';
|
|
|
|
import Model from '@nozbe/watermelondb/Model';
|
|
|
|
import { Observable, Subscription } from 'rxjs';
|
2019-08-23 16:24:15 +00:00
|
|
|
|
2020-04-06 21:40:18 +00:00
|
|
|
import database from '../../lib/database';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { SWITCH_TRACK_COLOR, themes } from '../../lib/constants';
|
2019-08-23 16:24:15 +00:00
|
|
|
import StatusBar from '../../containers/StatusBar';
|
2020-10-30 13:59:44 +00:00
|
|
|
import * as List from '../../containers/List';
|
2019-08-23 16:24:15 +00:00
|
|
|
import I18n from '../../i18n';
|
|
|
|
import RocketChat from '../../lib/rocketchat';
|
2022-04-12 16:27:05 +00:00
|
|
|
import { TSupportedThemes, 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-08-05 13:15:56 +00:00
|
|
|
import log, { events, logEvent } from '../../utils/log';
|
2020-10-30 13:59:44 +00:00
|
|
|
import sharedStyles from '../Styles';
|
2022-04-14 20:30:41 +00:00
|
|
|
import { IOptionsField, OPTIONS } from './options';
|
2021-12-03 19:27:57 +00:00
|
|
|
import { ChatsStackParamList } from '../../stacks/types';
|
2022-03-09 17:04:25 +00:00
|
|
|
import { IRoomNotifications } from '../../definitions';
|
2020-10-30 13:59:44 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
pickerText: {
|
|
|
|
...sharedStyles.textRegular,
|
|
|
|
fontSize: 16
|
|
|
|
}
|
|
|
|
});
|
2019-08-23 16:24:15 +00:00
|
|
|
|
2021-11-10 14:55:37 +00:00
|
|
|
interface INotificationPreferencesView {
|
2021-12-03 19:27:57 +00:00
|
|
|
navigation: StackNavigationProp<ChatsStackParamList, 'NotificationPrefView'>;
|
|
|
|
route: RouteProp<ChatsStackParamList, 'NotificationPrefView'>;
|
2022-04-12 16:27:05 +00:00
|
|
|
theme: TSupportedThemes;
|
2021-11-10 14:55:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class NotificationPreferencesView extends React.Component<INotificationPreferencesView, any> {
|
2020-07-31 18:30:36 +00:00
|
|
|
static navigationOptions = () => ({
|
2020-06-15 14:00:46 +00:00
|
|
|
title: I18n.t('Notification_Preferences')
|
2021-09-13 20:41:05 +00:00
|
|
|
});
|
2019-08-23 16:24:15 +00:00
|
|
|
|
2021-11-10 14:55:37 +00:00
|
|
|
private mounted: boolean;
|
2022-02-15 18:50:55 +00:00
|
|
|
private rid: string;
|
2021-11-10 14:55:37 +00:00
|
|
|
private roomObservable?: Observable<Model>;
|
|
|
|
private subscription?: Subscription;
|
2019-08-23 16:24:15 +00:00
|
|
|
|
2021-11-10 14:55:37 +00:00
|
|
|
constructor(props: INotificationPreferencesView) {
|
2019-08-23 16:24:15 +00:00
|
|
|
super(props);
|
2019-09-16 20:26:32 +00:00
|
|
|
this.mounted = false;
|
2022-02-15 18:50:55 +00:00
|
|
|
this.rid = props.route.params?.rid ?? '';
|
2020-06-15 14:00:46 +00:00
|
|
|
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();
|
2021-11-10 14:55:37 +00:00
|
|
|
this.subscription = this.roomObservable.subscribe((changes: any) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
if (this.mounted) {
|
|
|
|
this.setState({ room: changes });
|
|
|
|
} else {
|
2021-11-10 14:55:37 +00:00
|
|
|
// @ts-ignore
|
2021-09-13 20:41:05 +00:00
|
|
|
this.state.room = changes;
|
|
|
|
}
|
|
|
|
});
|
2019-09-16 20:26:32 +00:00
|
|
|
}
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-09 17:04:25 +00:00
|
|
|
saveNotificationSettings = async (key: string, value: string | boolean, params: IRoomNotifications) => {
|
2021-11-10 14:55:37 +00:00
|
|
|
// @ts-ignore
|
2021-09-13 20:41:05 +00:00
|
|
|
logEvent(events[`NP_${key.toUpperCase()}`]);
|
2020-04-06 21:40:18 +00:00
|
|
|
const { room } = this.state;
|
|
|
|
const db = database.active;
|
|
|
|
|
2019-08-23 16:24:15 +00:00
|
|
|
try {
|
2022-02-01 13:39:09 +00:00
|
|
|
await db.write(async () => {
|
2021-09-13 20:41:05 +00:00
|
|
|
await room.update(
|
2021-11-10 14:55:37 +00:00
|
|
|
protectedFunction((r: any) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
r[key] = value;
|
|
|
|
})
|
|
|
|
);
|
2020-07-08 20:46:05 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
const result = await RocketChat.saveNotificationSettings(this.rid, params);
|
|
|
|
if (result.success) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
// do nothing
|
2020-04-06 21:40:18 +00:00
|
|
|
}
|
|
|
|
|
2022-02-01 13:39:09 +00:00
|
|
|
await db.write(async () => {
|
2021-09-13 20:41:05 +00:00
|
|
|
await room.update(
|
2021-11-10 14:55:37 +00:00
|
|
|
protectedFunction((r: any) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
r[key] = room[key];
|
|
|
|
})
|
|
|
|
);
|
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);
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-08-23 16:24:15 +00:00
|
|
|
|
2021-11-10 14:55:37 +00:00
|
|
|
onValueChangeSwitch = (key: string, value: string | boolean) =>
|
|
|
|
this.saveNotificationSettings(key, value, { [key]: value ? '1' : '0' });
|
2020-04-06 21:40:18 +00:00
|
|
|
|
2021-11-10 14:55:37 +00:00
|
|
|
onValueChangePicker = (key: string, value: string) => this.saveNotificationSettings(key, value, { [key]: value.toString() });
|
2020-04-06 21:40:18 +00:00
|
|
|
|
2021-11-10 14:55:37 +00:00
|
|
|
pickerSelection = (title: string, key: string) => {
|
2020-04-06 21:40:18 +00:00
|
|
|
const { room } = this.state;
|
|
|
|
const { navigation } = this.props;
|
|
|
|
navigation.navigate('PickerView', {
|
|
|
|
title,
|
|
|
|
data: OPTIONS[key],
|
|
|
|
value: room[key],
|
2021-11-10 14:55:37 +00:00
|
|
|
onChangeValue: (value: string) => this.onValueChangePicker(key, value)
|
2020-04-06 21:40:18 +00:00
|
|
|
});
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-08-23 16:24:15 +00:00
|
|
|
|
2021-11-10 14:55:37 +00:00
|
|
|
renderPickerOption = (key: string) => {
|
2019-08-23 16:24:15 +00:00
|
|
|
const { room } = this.state;
|
2019-12-04 16:39:53 +00:00
|
|
|
const { theme } = this.props;
|
2022-04-14 20:30:41 +00:00
|
|
|
const text = room[key] ? OPTIONS[key].find(option => option.value === room[key]) : (OPTIONS[key][0] as IOptionsField);
|
2021-09-13 20:41:05 +00:00
|
|
|
return (
|
|
|
|
<Text style={[styles.pickerText, { color: themes[theme].actionTintColor }]}>
|
2022-04-14 20:30:41 +00:00
|
|
|
{text?.label ? I18n.t(text?.label, { defaultValue: text?.label, second: text?.second }) : text?.label}
|
2021-09-13 20:41:05 +00:00
|
|
|
</Text>
|
|
|
|
);
|
|
|
|
};
|
2019-08-23 16:24:15 +00:00
|
|
|
|
2021-11-10 14:55:37 +00:00
|
|
|
renderSwitch = (key: string) => {
|
2019-08-23 16:24:15 +00:00
|
|
|
const { room } = this.state;
|
|
|
|
return (
|
|
|
|
<Switch
|
|
|
|
value={!room[key]}
|
|
|
|
testID={key}
|
|
|
|
trackColor={SWITCH_TRACK_COLOR}
|
|
|
|
onValueChange={value => this.onValueChangeSwitch(key, !value)}
|
|
|
|
/>
|
|
|
|
);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-08-23 16:24:15 +00:00
|
|
|
|
|
|
|
render() {
|
|
|
|
const { room } = this.state;
|
|
|
|
return (
|
2020-10-30 13:59:44 +00:00
|
|
|
<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={() => this.renderSwitch('disableNotifications')}
|
|
|
|
/>
|
|
|
|
<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={() => this.renderSwitch('muteGroupMentions')}
|
|
|
|
/>
|
|
|
|
<List.Separator />
|
|
|
|
<List.Info info='Receive_Group_Mentions_Info' />
|
|
|
|
</List.Section>
|
|
|
|
|
|
|
|
<List.Section>
|
|
|
|
<List.Separator />
|
|
|
|
<List.Item
|
|
|
|
title='Show_Unread_Counter'
|
|
|
|
testID='notification-preference-view-unread-count'
|
|
|
|
right={() => this.renderSwitch('hideUnreadStatus')}
|
|
|
|
/>
|
|
|
|
<List.Separator />
|
|
|
|
<List.Info info='Show_Unread_Counter_Info' />
|
|
|
|
</List.Section>
|
|
|
|
|
|
|
|
<List.Section title='In_App_And_Desktop'>
|
|
|
|
<List.Separator />
|
|
|
|
<List.Item
|
|
|
|
title='Alert'
|
|
|
|
testID='notification-preference-view-alert'
|
2021-11-10 14:55:37 +00:00
|
|
|
onPress={(title: string) => this.pickerSelection(title, 'desktopNotifications')}
|
2020-10-30 13:59:44 +00:00
|
|
|
right={() => this.renderPickerOption('desktopNotifications')}
|
|
|
|
/>
|
|
|
|
<List.Separator />
|
|
|
|
<List.Info info='In_App_and_Desktop_Alert_info' />
|
|
|
|
</List.Section>
|
|
|
|
|
|
|
|
<List.Section title='Push_Notifications'>
|
|
|
|
<List.Separator />
|
|
|
|
<List.Item
|
|
|
|
title='Alert'
|
|
|
|
testID='notification-preference-view-push-notification'
|
2021-11-10 14:55:37 +00:00
|
|
|
onPress={(title: string) => this.pickerSelection(title, 'mobilePushNotifications')}
|
2020-10-30 13:59:44 +00:00
|
|
|
right={() => this.renderPickerOption('mobilePushNotifications')}
|
|
|
|
/>
|
|
|
|
<List.Separator />
|
|
|
|
<List.Info info='Push_Notifications_Alert_Info' />
|
|
|
|
</List.Section>
|
|
|
|
|
|
|
|
<List.Section title='Desktop_Options'>
|
|
|
|
<List.Separator />
|
|
|
|
<List.Item
|
|
|
|
title='Audio'
|
|
|
|
testID='notification-preference-view-audio'
|
2021-11-10 14:55:37 +00:00
|
|
|
onPress={(title: string) => this.pickerSelection(title, 'audioNotifications')}
|
2020-10-30 13:59:44 +00:00
|
|
|
right={() => this.renderPickerOption('audioNotifications')}
|
|
|
|
/>
|
|
|
|
<List.Separator />
|
|
|
|
<List.Item
|
|
|
|
title='Sound'
|
|
|
|
testID='notification-preference-view-sound'
|
2021-11-10 14:55:37 +00:00
|
|
|
onPress={(title: string) => this.pickerSelection(title, 'audioNotificationValue')}
|
2020-10-30 13:59:44 +00:00
|
|
|
right={() => this.renderPickerOption('audioNotificationValue')}
|
|
|
|
/>
|
|
|
|
<List.Separator />
|
|
|
|
<List.Item
|
|
|
|
title='Notification_Duration'
|
|
|
|
testID='notification-preference-view-notification-duration'
|
2021-11-10 14:55:37 +00:00
|
|
|
onPress={(title: string) => this.pickerSelection(title, 'desktopNotificationDuration')}
|
2020-10-30 13:59:44 +00:00
|
|
|
right={() => this.renderPickerOption('desktopNotificationDuration')}
|
|
|
|
/>
|
|
|
|
<List.Separator />
|
|
|
|
</List.Section>
|
|
|
|
|
|
|
|
<List.Section title='Email'>
|
|
|
|
<List.Separator />
|
|
|
|
<List.Item
|
|
|
|
title='Alert'
|
|
|
|
testID='notification-preference-view-email-alert'
|
2021-11-10 14:55:37 +00:00
|
|
|
onPress={(title: string) => this.pickerSelection(title, 'emailNotifications')}
|
2020-10-30 13:59:44 +00:00
|
|
|
right={() => this.renderPickerOption('emailNotifications')}
|
|
|
|
/>
|
|
|
|
<List.Separator />
|
|
|
|
</List.Section>
|
|
|
|
</List.Container>
|
2019-08-23 16:24:15 +00:00
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-12-04 16:39:53 +00:00
|
|
|
|
|
|
|
export default withTheme(NotificationPreferencesView);
|