haptic feedback to be closer than message appears, also add logical to vibrate properly when focused the room or thread

This commit is contained in:
Reinaldo Neto 2024-01-10 21:35:19 -03:00
parent 4d8562628e
commit eeb40129ff
2 changed files with 34 additions and 0 deletions

View File

@ -1,6 +1,7 @@
import EJSON from 'ejson'; import EJSON from 'ejson';
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
import { InteractionManager } from 'react-native'; import { InteractionManager } from 'react-native';
import * as Haptics from 'expo-haptics';
import log from '../helpers/log'; import log from '../helpers/log';
import protectedFunction from '../helpers/protectedFunction'; import protectedFunction from '../helpers/protectedFunction';
@ -19,6 +20,8 @@ import { IDDPMessage } from '../../../definitions/IDDPMessage';
import sdk from '../../services/sdk'; import sdk from '../../services/sdk';
import { readMessages } from '../readMessages'; import { readMessages } from '../readMessages';
import { loadMissedMessages } from '../loadMissedMessages'; import { loadMissedMessages } from '../loadMissedMessages';
import userPreferences from '../userPreferences';
import { NOTIFICATION_IN_APP_VIBRATION } from '../../constants';
const WINDOW_TIME = 1000; const WINDOW_TIME = 1000;
@ -39,6 +42,7 @@ export default class RoomSubscription {
private notifyRoomListener?: Promise<any>; private notifyRoomListener?: Promise<any>;
private messageReceivedListener?: Promise<any>; private messageReceivedListener?: Promise<any>;
private lastOpen?: Date; private lastOpen?: Date;
private threadFocused?: string;
constructor(rid: string) { constructor(rid: string) {
this.rid = rid; this.rid = rid;
@ -306,6 +310,17 @@ export default class RoomSubscription {
} }
} }
// Haptic Feedback when receiving message
const { id: userId } = reduxStore.getState().login.user;
if (
((!message.tmid && !message.tlm && !this.threadFocused) || (message.tmid && message.tmid === this.threadFocused)) &&
message.u._id !== userId
) {
const notificationInAppVibration = userPreferences.getBool(NOTIFICATION_IN_APP_VIBRATION);
if (notificationInAppVibration || notificationInAppVibration === null) {
Haptics.notificationAsync(Haptics.NotificationFeedbackType.Success);
}
}
return resolve(); return resolve();
}); });
@ -358,4 +373,12 @@ export default class RoomSubscription {
const message = buildMessage(EJSON.fromJSONValue(ddpMessage.fields.args[0])) as IMessage; const message = buildMessage(EJSON.fromJSONValue(ddpMessage.fields.args[0])) as IMessage;
this.queue[message._id] = message; this.queue[message._id] = message;
}; };
setThreadFocused = (tmid: string) => {
this.threadFocused = tmid;
};
removeThreadFocused = () => {
this.threadFocused = '';
};
} }

View File

@ -230,6 +230,7 @@ class RoomView extends React.Component<IRoomViewProps, IRoomViewState> {
}; };
private sub?: RoomClass; private sub?: RoomClass;
private unsubscribeBlur?: () => void; private unsubscribeBlur?: () => void;
private unsubscribeFocus?: () => void;
constructor(props: IRoomViewProps) { constructor(props: IRoomViewProps) {
super(props); super(props);
@ -338,6 +339,11 @@ class RoomView extends React.Component<IRoomViewProps, IRoomViewState> {
this.unsubscribeBlur = navigation.addListener('blur', () => { this.unsubscribeBlur = navigation.addListener('blur', () => {
audioPlayer.pauseCurrentAudio(); audioPlayer.pauseCurrentAudio();
}); });
this.unsubscribeFocus = navigation.addListener('focus', () => {
if (!this.tmid) {
this.sub?.removeThreadFocused();
}
});
} }
shouldComponentUpdate(nextProps: IRoomViewProps, nextState: IRoomViewState) { shouldComponentUpdate(nextProps: IRoomViewProps, nextState: IRoomViewState) {
@ -457,6 +463,9 @@ class RoomView extends React.Component<IRoomViewProps, IRoomViewState> {
if (this.unsubscribeBlur) { if (this.unsubscribeBlur) {
this.unsubscribeBlur(); this.unsubscribeBlur();
} }
if (this.unsubscribeFocus) {
this.unsubscribeFocus();
}
EventEmitter.removeListener('connected', this.handleConnected); EventEmitter.removeListener('connected', this.handleConnected);
EventEmitter.removeListener('ROOM_REMOVED', this.handleRoomRemoved); EventEmitter.removeListener('ROOM_REMOVED', this.handleRoomRemoved);
if (!this.tmid) { if (!this.tmid) {
@ -1212,6 +1221,7 @@ class RoomView extends React.Component<IRoomViewProps, IRoomViewState> {
sendLoadingEvent({ visible: false }); sendLoadingEvent({ visible: false });
}, 300); }, 300);
} }
this.sub?.setThreadFocused(item.tmid);
return navigation.push('RoomView', { return navigation.push('RoomView', {
rid: this.rid, rid: this.rid,
tmid: item.tmid, tmid: item.tmid,
@ -1223,6 +1233,7 @@ class RoomView extends React.Component<IRoomViewProps, IRoomViewState> {
} }
if ('tlm' in item) { if ('tlm' in item) {
this.sub?.setThreadFocused(item.id);
return navigation.push('RoomView', { return navigation.push('RoomView', {
rid: this.rid, rid: this.rid,
tmid: item.id, tmid: item.id,