Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix broken two touch zoom for Attachment Images #4440

Merged
merged 4 commits into from
Aug 10, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions src/components/ImageView/index.native.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, {PureComponent} from 'react';
import PropTypes from 'prop-types';
import {View} from 'react-native';
import {View, PanResponder} from 'react-native';
import ImageZoom from 'react-native-image-pan-zoom';
import _ from 'underscore';
import ImageWithSizeCalculation from '../ImageWithSizeCalculation';
import styles, {getWidthAndHeightStyle} from '../../styles/styles';
import variables from '../../styles/variables';
Expand Down Expand Up @@ -31,6 +32,28 @@ class ImageView extends PureComponent {
this.doubleClickInterval = 175;
this.imageZoomScale = 1;
this.lastClickTime = 0;
this.amountOfTouches = 0;

// PanResponder used to capture how many touches are active on the attachment image
this.panResponder = PanResponder.create({
onStartShouldSetPanResponder: this.handleOnStartShouldSetPanResponder.bind(this),
});
}

/**
* Handler for the initial tap for the PanResponder on our ImageZoom overlay View
*
* @param {Event} e
* @param {GestureState} gestureState
* @returns {Boolean}
*/
handleOnStartShouldSetPanResponder(e, gestureState) {
Jag96 marked this conversation as resolved.
Show resolved Hide resolved
if (_.isNumber(gestureState.numberActiveTouches)) {
this.amountOfTouches = gestureState.numberActiveTouches;
}

// We don't need to set the panResponder since all we care about is checking the gestureState, so return false
return false;
}

render() {
Expand All @@ -52,12 +75,13 @@ class ImageView extends PureComponent {
cropHeight={windowHeight}
imageWidth={this.state.imageWidth}
imageHeight={this.state.imageHeight}
onStartShouldSetPanResponder={(e) => {
onStartShouldSetPanResponder={() => {
const isDoubleClick = new Date().getTime() - this.lastClickTime <= this.doubleClickInterval;
const touches = this.amountOfTouches;
Jag96 marked this conversation as resolved.
Show resolved Hide resolved
this.lastClickTime = new Date().getTime();

// Let ImageZoom handle the event if the tap is more than one touchPoint or if we are zoomed in
if (e.nativeEvent.touches.length === 2 || this.imageZoomScale !== 1) {
if (touches === 2 || this.imageZoomScale !== 1) {
return true;
}

Expand Down Expand Up @@ -95,6 +119,20 @@ class ImageView extends PureComponent {
this.setState({imageHeight, imageWidth});
}}
/>
{/**
Create an invisible view on top of the image so we can capture and set the amount of touches before
the ImageZoom's PanResponder does. Children will be triggered first, so this needs to be inside the
ImageZoom to work
*/}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoa, nice workaround

<View
/* eslint-disable-next-line react/jsx-props-no-spreading */
{...this.panResponder.panHandlers}
style={[
styles.w100,
styles.h100,
styles.invisible,
]}
/>
</ImageZoom>
</View>
);
Expand Down