Close swipeable on recycle

This commit is contained in:
Diego Mello 2022-12-13 15:53:04 -03:00
parent 2b06b01bd6
commit 7e849943fe
4 changed files with 220 additions and 198 deletions

View File

@ -51,9 +51,11 @@ const RoomItem = ({
showAvatar, showAvatar,
displayMode, displayMode,
sourceType, sourceType,
hideMentionStatus hideMentionStatus,
touchableRef
}: IRoomItemProps) => ( }: IRoomItemProps) => (
<Touchable <Touchable
ref={touchableRef}
onPress={onPress} onPress={onPress}
onLongPress={onLongPress} onLongPress={onLongPress}
favorite={favorite} favorite={favorite}

View File

@ -1,4 +1,4 @@
import React from 'react'; import React, { forwardRef, useImperativeHandle } from 'react';
import Animated, { import Animated, {
useAnimatedGestureHandler, useAnimatedGestureHandler,
useSharedValue, useSharedValue,
@ -18,13 +18,15 @@ import { useWindowDimensions } from 'react-native';
import Touch from '../Touch'; import Touch from '../Touch';
import { ACTION_WIDTH, LONG_SWIPE, SMALL_SWIPE } from './styles'; import { ACTION_WIDTH, LONG_SWIPE, SMALL_SWIPE } from './styles';
import { LeftActions, RightActions } from './Actions'; import { LeftActions, RightActions } from './Actions';
import { ITouchableProps } from './interfaces'; import { ITouchableProps, ITouchableRef } from './interfaces';
import { useTheme } from '../../theme'; import { useTheme } from '../../theme';
import I18n from '../../i18n'; import I18n from '../../i18n';
import { MAX_SIDEBAR_WIDTH } from '../../lib/constants'; import { MAX_SIDEBAR_WIDTH } from '../../lib/constants';
import { useAppSelector } from '../../lib/hooks'; import { useAppSelector } from '../../lib/hooks';
const Touchable = ({ const Touchable = forwardRef<ITouchableRef, ITouchableProps>(
(
{
children, children,
type, type,
onPress, onPress,
@ -39,7 +41,9 @@ const Touchable = ({
isFocused, isFocused,
swipeEnabled, swipeEnabled,
displayMode displayMode
}: ITouchableProps): React.ReactElement => { },
ref
): React.ReactElement => {
const { colors } = useTheme(); const { colors } = useTheme();
const { width: deviceWidth } = useWindowDimensions(); const { width: deviceWidth } = useWindowDimensions();
const isMasterDetail = useAppSelector(state => state.app.isMasterDetail); const isMasterDetail = useAppSelector(state => state.app.isMasterDetail);
@ -51,11 +55,16 @@ const Touchable = ({
let _value = 0; let _value = 0;
const close = () => { const close = () => {
console.log(`${rid} close`);
rowState.value = 0; rowState.value = 0;
transX.value = withSpring(0, { overshootClamping: true }); transX.value = withSpring(0, { overshootClamping: true });
rowOffSet.value = 0; rowOffSet.value = 0;
}; };
useImperativeHandle(ref, () => ({
close
}));
const handleToggleFav = () => { const handleToggleFav = () => {
if (toggleFav) { if (toggleFav) {
toggleFav(rid, favorite); toggleFav(rid, favorite);
@ -239,6 +248,7 @@ const Touchable = ({
</Animated.View> </Animated.View>
</LongPressGestureHandler> </LongPressGestureHandler>
); );
}; }
);
export default Touchable; export default Touchable;

View File

@ -1,11 +1,11 @@
import React, { useEffect } from 'react'; import React, { useEffect, useRef } from 'react';
import I18n from '../../i18n'; import I18n from '../../i18n';
import { useAppSelector } from '../../lib/hooks'; import { useAppSelector } from '../../lib/hooks';
import { getUserPresence } from '../../lib/methods'; import { getUserPresence } from '../../lib/methods';
import { isGroupChat } from '../../lib/methods/helpers'; import { isGroupChat } from '../../lib/methods/helpers';
import { formatDate } from '../../lib/methods/helpers/room'; import { formatDate } from '../../lib/methods/helpers/room';
import { IRoomItemContainerProps } from './interfaces'; import { IRoomItemContainerProps, ITouchableRef } from './interfaces';
import RoomItem from './RoomItem'; import RoomItem from './RoomItem';
import { ROW_HEIGHT, ROW_HEIGHT_CONDENSED } from './styles'; import { ROW_HEIGHT, ROW_HEIGHT_CONDENSED } from './styles';
@ -40,6 +40,7 @@ const RoomItemContainer = ({
const connected = useAppSelector(state => state.meteor.connected); const connected = useAppSelector(state => state.meteor.connected);
const userStatus = useAppSelector(state => state.activeUsers[id || '']?.status); const userStatus = useAppSelector(state => state.activeUsers[id || '']?.status);
const isDirect = !!(item.t === 'd' && id && !isGroupChat(item)); const isDirect = !!(item.t === 'd' && id && !isGroupChat(item));
const touchableRef = useRef<ITouchableRef>(null);
// When app reconnects, we need to fetch the rendered user's presence // When app reconnects, we need to fetch the rendered user's presence
useEffect(() => { useEffect(() => {
@ -56,6 +57,9 @@ const RoomItemContainer = ({
if (!userStatus && isDirect) { if (!userStatus && isDirect) {
getUserPresence(id); getUserPresence(id);
} }
// TODO: Remove this when we have a better way to close the swipeable
touchableRef?.current?.close();
}, [item.rid]); }, [item.rid]);
const handleOnPress = () => onPress(item); const handleOnPress = () => onPress(item);
@ -79,6 +83,7 @@ const RoomItemContainer = ({
return ( return (
<RoomItem <RoomItem
touchableRef={touchableRef}
name={name} name={name}
avatar={avatar} avatar={avatar}
isGroupChat={isGroupChat(item)} isGroupChat={isGroupChat(item)}

View File

@ -115,6 +115,7 @@ export interface IRoomItemProps extends IBaseRoomItem {
size?: number; size?: number;
sourceType: IOmnichannelSource; sourceType: IOmnichannelSource;
hideMentionStatus?: boolean; hideMentionStatus?: boolean;
touchableRef: React.RefObject<ITouchableRef>;
} }
export interface ILastMessageProps { export interface ILastMessageProps {
@ -126,6 +127,10 @@ export interface ILastMessageProps {
alert: boolean; alert: boolean;
} }
export interface ITouchableRef {
close: () => void;
}
export interface ITouchableProps extends IRoomItemTouchables { export interface ITouchableProps extends IRoomItemTouchables {
children: JSX.Element; children: JSX.Element;
type: SubscriptionType; type: SubscriptionType;