2021-09-13 20:41:05 +00:00
|
|
|
import React from 'react';
|
2022-06-06 13:23:49 +00:00
|
|
|
import Animated, {
|
|
|
|
useAnimatedGestureHandler,
|
|
|
|
useSharedValue,
|
|
|
|
useAnimatedStyle,
|
|
|
|
withSpring,
|
|
|
|
runOnJS
|
|
|
|
} from 'react-native-reanimated';
|
2022-04-20 21:37:54 +00:00
|
|
|
import {
|
|
|
|
LongPressGestureHandler,
|
|
|
|
PanGestureHandler,
|
2022-06-06 13:23:49 +00:00
|
|
|
State,
|
|
|
|
HandlerStateChangeEventPayload,
|
|
|
|
PanGestureHandlerEventPayload
|
2022-04-20 21:37:54 +00:00
|
|
|
} from 'react-native-gesture-handler';
|
2021-09-13 20:41:05 +00:00
|
|
|
|
2022-08-17 13:32:21 +00:00
|
|
|
import Touch from '../Touch';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { ACTION_WIDTH, LONG_SWIPE, SMALL_SWIPE } from './styles';
|
|
|
|
import { LeftActions, RightActions } from './Actions';
|
2022-04-20 21:37:54 +00:00
|
|
|
import { ITouchableProps } from './interfaces';
|
2022-06-06 13:23:49 +00:00
|
|
|
import { useTheme } from '../../theme';
|
|
|
|
import I18n from '../../i18n';
|
|
|
|
|
|
|
|
const Touchable = ({
|
|
|
|
children,
|
|
|
|
type,
|
|
|
|
onPress,
|
|
|
|
onLongPress,
|
|
|
|
testID,
|
|
|
|
width,
|
|
|
|
favorite,
|
|
|
|
isRead,
|
|
|
|
rid,
|
|
|
|
toggleFav,
|
|
|
|
toggleRead,
|
|
|
|
hideChannel,
|
|
|
|
isFocused,
|
|
|
|
swipeEnabled,
|
|
|
|
displayMode
|
|
|
|
}: ITouchableProps): React.ReactElement => {
|
2022-08-17 13:32:21 +00:00
|
|
|
const { colors } = useTheme();
|
2022-06-06 13:23:49 +00:00
|
|
|
|
|
|
|
const rowOffSet = useSharedValue(0);
|
|
|
|
const transX = useSharedValue(0);
|
|
|
|
const rowState = useSharedValue(0); // 0: closed, 1: right opened, -1: left opened
|
|
|
|
let _value = 0;
|
|
|
|
|
|
|
|
const close = () => {
|
|
|
|
rowState.value = 0;
|
|
|
|
transX.value = withSpring(0, { overshootClamping: true });
|
|
|
|
rowOffSet.value = 0;
|
|
|
|
};
|
2021-09-13 20:41:05 +00:00
|
|
|
|
2022-06-06 13:23:49 +00:00
|
|
|
const handleToggleFav = () => {
|
|
|
|
if (toggleFav) {
|
|
|
|
toggleFav(rid, favorite);
|
|
|
|
}
|
|
|
|
close();
|
|
|
|
};
|
2021-09-13 20:41:05 +00:00
|
|
|
|
2022-06-06 13:23:49 +00:00
|
|
|
const handleToggleRead = () => {
|
|
|
|
if (toggleRead) {
|
|
|
|
toggleRead(rid, isRead);
|
|
|
|
}
|
|
|
|
};
|
2021-09-13 20:41:05 +00:00
|
|
|
|
2022-06-06 13:23:49 +00:00
|
|
|
const handleHideChannel = () => {
|
|
|
|
if (hideChannel) {
|
|
|
|
hideChannel(rid, type);
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-06-06 13:23:49 +00:00
|
|
|
const onToggleReadPress = () => {
|
|
|
|
handleToggleRead();
|
|
|
|
close();
|
|
|
|
};
|
|
|
|
|
|
|
|
const onHidePress = () => {
|
|
|
|
handleHideChannel();
|
|
|
|
close();
|
|
|
|
};
|
|
|
|
|
|
|
|
const handlePress = () => {
|
|
|
|
if (rowState.value !== 0) {
|
|
|
|
close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (onPress) {
|
|
|
|
onPress();
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-06-06 13:23:49 +00:00
|
|
|
const handleLongPress = () => {
|
|
|
|
if (rowState.value !== 0) {
|
|
|
|
close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (onLongPress) {
|
|
|
|
onLongPress();
|
|
|
|
}
|
|
|
|
};
|
2021-09-13 20:41:05 +00:00
|
|
|
|
2022-06-06 13:23:49 +00:00
|
|
|
const onLongPressHandlerStateChange = ({ nativeEvent }: { nativeEvent: HandlerStateChangeEventPayload }) => {
|
|
|
|
if (nativeEvent.state === State.ACTIVE) {
|
|
|
|
handleLongPress();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleRelease = (event: PanGestureHandlerEventPayload) => {
|
|
|
|
const { translationX } = event;
|
|
|
|
_value += translationX;
|
2021-09-13 20:41:05 +00:00
|
|
|
let toValue = 0;
|
2022-06-06 13:23:49 +00:00
|
|
|
if (rowState.value === 0) {
|
2021-09-13 20:41:05 +00:00
|
|
|
// if no option is opened
|
|
|
|
if (translationX > 0 && translationX < LONG_SWIPE) {
|
2022-06-06 13:23:49 +00:00
|
|
|
if (I18n.isRTL) {
|
2021-09-13 20:41:05 +00:00
|
|
|
toValue = 2 * ACTION_WIDTH;
|
|
|
|
} else {
|
|
|
|
toValue = ACTION_WIDTH;
|
|
|
|
}
|
2022-06-06 13:23:49 +00:00
|
|
|
rowState.value = -1;
|
2021-09-13 20:41:05 +00:00
|
|
|
} else if (translationX >= LONG_SWIPE) {
|
|
|
|
toValue = 0;
|
2022-06-06 13:23:49 +00:00
|
|
|
if (I18n.isRTL) {
|
|
|
|
handleHideChannel();
|
2021-09-13 20:41:05 +00:00
|
|
|
} else {
|
2022-06-06 13:23:49 +00:00
|
|
|
handleToggleRead();
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
|
|
|
} else if (translationX < 0 && translationX > -LONG_SWIPE) {
|
|
|
|
// open trailing option if he swipe left
|
2022-06-06 13:23:49 +00:00
|
|
|
if (I18n.isRTL) {
|
2021-09-13 20:41:05 +00:00
|
|
|
toValue = -ACTION_WIDTH;
|
|
|
|
} else {
|
|
|
|
toValue = -2 * ACTION_WIDTH;
|
|
|
|
}
|
2022-06-06 13:23:49 +00:00
|
|
|
rowState.value = 1;
|
2021-09-13 20:41:05 +00:00
|
|
|
} else if (translationX <= -LONG_SWIPE) {
|
|
|
|
toValue = 0;
|
2022-06-06 13:23:49 +00:00
|
|
|
rowState.value = 1;
|
|
|
|
if (I18n.isRTL) {
|
|
|
|
handleToggleRead();
|
2021-09-13 20:41:05 +00:00
|
|
|
} else {
|
2022-06-06 13:23:49 +00:00
|
|
|
handleHideChannel();
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
toValue = 0;
|
|
|
|
}
|
2022-06-06 13:23:49 +00:00
|
|
|
} else if (rowState.value === -1) {
|
2021-09-13 20:41:05 +00:00
|
|
|
// if left option is opened
|
2022-06-06 13:23:49 +00:00
|
|
|
if (_value < SMALL_SWIPE) {
|
2021-09-13 20:41:05 +00:00
|
|
|
toValue = 0;
|
2022-06-06 13:23:49 +00:00
|
|
|
rowState.value = 0;
|
|
|
|
} else if (_value > LONG_SWIPE) {
|
2021-09-13 20:41:05 +00:00
|
|
|
toValue = 0;
|
2022-06-06 13:23:49 +00:00
|
|
|
rowState.value = 0;
|
|
|
|
if (I18n.isRTL) {
|
|
|
|
handleHideChannel();
|
2021-09-13 20:41:05 +00:00
|
|
|
} else {
|
2022-06-06 13:23:49 +00:00
|
|
|
handleToggleRead();
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
2022-06-06 13:23:49 +00:00
|
|
|
} else if (I18n.isRTL) {
|
2021-09-13 20:41:05 +00:00
|
|
|
toValue = 2 * ACTION_WIDTH;
|
|
|
|
} else {
|
|
|
|
toValue = ACTION_WIDTH;
|
|
|
|
}
|
2022-06-06 13:23:49 +00:00
|
|
|
} else if (rowState.value === 1) {
|
2021-09-13 20:41:05 +00:00
|
|
|
// if right option is opened
|
2022-06-06 13:23:49 +00:00
|
|
|
if (_value > -2 * SMALL_SWIPE) {
|
2021-09-13 20:41:05 +00:00
|
|
|
toValue = 0;
|
2022-06-06 13:23:49 +00:00
|
|
|
rowState.value = 0;
|
|
|
|
} else if (_value < -LONG_SWIPE) {
|
|
|
|
if (I18n.isRTL) {
|
|
|
|
handleToggleRead();
|
2021-09-13 20:41:05 +00:00
|
|
|
} else {
|
2022-06-06 13:23:49 +00:00
|
|
|
handleHideChannel();
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
2022-06-06 13:23:49 +00:00
|
|
|
} else if (I18n.isRTL) {
|
2021-09-13 20:41:05 +00:00
|
|
|
toValue = -ACTION_WIDTH;
|
|
|
|
} else {
|
|
|
|
toValue = -2 * ACTION_WIDTH;
|
|
|
|
}
|
|
|
|
}
|
2022-06-06 13:23:49 +00:00
|
|
|
transX.value = withSpring(toValue, { overshootClamping: true });
|
|
|
|
rowOffSet.value = toValue;
|
|
|
|
_value = toValue;
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
|
|
|
|
2022-06-06 13:23:49 +00:00
|
|
|
const onGestureEvent = useAnimatedGestureHandler({
|
|
|
|
onActive: event => {
|
|
|
|
transX.value = event.translationX + rowOffSet.value;
|
|
|
|
if (transX.value > 2 * width) transX.value = 2 * width;
|
|
|
|
},
|
|
|
|
onEnd: event => {
|
|
|
|
runOnJS(handleRelease)(event);
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
2022-06-06 13:23:49 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const animatedStyles = useAnimatedStyle(() => ({ transform: [{ translateX: transX.value }] }));
|
|
|
|
|
|
|
|
return (
|
|
|
|
<LongPressGestureHandler onHandlerStateChange={onLongPressHandlerStateChange}>
|
|
|
|
<Animated.View>
|
|
|
|
<PanGestureHandler activeOffsetX={[-20, 20]} onGestureEvent={onGestureEvent} enabled={swipeEnabled}>
|
|
|
|
<Animated.View>
|
|
|
|
<LeftActions
|
|
|
|
transX={transX}
|
|
|
|
isRead={isRead}
|
|
|
|
width={width}
|
|
|
|
onToggleReadPress={onToggleReadPress}
|
|
|
|
displayMode={displayMode}
|
|
|
|
/>
|
|
|
|
<RightActions
|
|
|
|
transX={transX}
|
|
|
|
favorite={favorite}
|
|
|
|
width={width}
|
|
|
|
toggleFav={handleToggleFav}
|
|
|
|
onHidePress={onHidePress}
|
|
|
|
displayMode={displayMode}
|
|
|
|
/>
|
|
|
|
<Animated.View style={animatedStyles}>
|
|
|
|
<Touch
|
|
|
|
onPress={handlePress}
|
|
|
|
testID={testID}
|
2021-09-13 20:41:05 +00:00
|
|
|
style={{
|
2022-06-06 13:23:49 +00:00
|
|
|
backgroundColor: isFocused ? colors.chatComponentBackground : colors.backgroundColor
|
2022-08-08 21:02:08 +00:00
|
|
|
}}
|
|
|
|
>
|
2022-06-06 13:23:49 +00:00
|
|
|
{children}
|
|
|
|
</Touch>
|
2021-09-13 20:41:05 +00:00
|
|
|
</Animated.View>
|
2022-06-06 13:23:49 +00:00
|
|
|
</Animated.View>
|
|
|
|
</PanGestureHandler>
|
|
|
|
</Animated.View>
|
|
|
|
</LongPressGestureHandler>
|
|
|
|
);
|
|
|
|
};
|
2021-09-13 20:41:05 +00:00
|
|
|
|
|
|
|
export default Touchable;
|