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: android pdf scrolling issue #30050

Merged
merged 17 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from 14 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
Original file line number Diff line number Diff line change
@@ -1,9 +1,34 @@
import React, {memo, useCallback, useContext, useEffect} from 'react';
import PropTypes from 'prop-types';
import AttachmentCarouselPagerContext from '@components/Attachments/AttachmentCarousel/Pager/AttachmentCarouselPagerContext';
import PDFView from '@components/PDFView';
import {attachmentViewPdfDefaultProps, attachmentViewPdfPropTypes} from './propTypes';

function AttachmentViewPdf({file, encryptedSourceUrl, isFocused, isUsedInCarousel, onPress, onScaleChanged: onScaleChangedProp, onToggleKeyboard, onLoadComplete, errorLabelStyles, style}) {
const propTypes = {
/** Updates the scale value of the pdf */
updateScale: PropTypes.func,

...attachmentViewPdfPropTypes,
};

const defaultProps = {
updateScale: () => {},
...attachmentViewPdfDefaultProps,
};

function BaseAttachmentViewPdf({
file,
encryptedSourceUrl,
isFocused,
isUsedInCarousel,
onPress,
onScaleChanged: onScaleChangedProp,
onToggleKeyboard,
onLoadComplete,
errorLabelStyles,
style,
updateScale,
}) {
const attachmentCarouselPagerContext = useContext(AttachmentCarouselPagerContext);

useEffect(() => {
Expand All @@ -16,6 +41,7 @@ function AttachmentViewPdf({file, encryptedSourceUrl, isFocused, isUsedInCarouse

const onScaleChanged = useCallback(
(scale) => {
updateScale(scale);
HezekielT marked this conversation as resolved.
Show resolved Hide resolved
onScaleChangedProp();

// When a pdf is shown in a carousel, we want to disable the pager scroll when the pdf is zoomed in
Expand All @@ -31,7 +57,7 @@ function AttachmentViewPdf({file, encryptedSourceUrl, isFocused, isUsedInCarouse
attachmentCarouselPagerContext.shouldPagerScroll.value = shouldPagerScroll;
}
},
[attachmentCarouselPagerContext, isUsedInCarousel, onScaleChangedProp],
[attachmentCarouselPagerContext, isUsedInCarousel, onScaleChangedProp, updateScale],
);

return (
Expand All @@ -49,7 +75,8 @@ function AttachmentViewPdf({file, encryptedSourceUrl, isFocused, isUsedInCarouse
);
}

AttachmentViewPdf.propTypes = attachmentViewPdfPropTypes;
AttachmentViewPdf.defaultProps = attachmentViewPdfDefaultProps;
BaseAttachmentViewPdf.propTypes = propTypes;
BaseAttachmentViewPdf.defaultProps = defaultProps;
BaseAttachmentViewPdf.displayName = 'BaseAttachmentViewPdf';

export default memo(AttachmentViewPdf);
export default memo(BaseAttachmentViewPdf);
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import React, {memo, useCallback, useContext} from 'react';
import {View, StyleSheet} from 'react-native';
import {Gesture, GestureDetector} from 'react-native-gesture-handler';
import Animated, {useSharedValue} from 'react-native-reanimated';
import AttachmentCarouselPagerContext from '@components/Attachments/AttachmentCarousel/Pager/AttachmentCarouselPagerContext';
import styles from '@styles/styles';
import BaseAttachmentViewPdf from './BaseAttachmentViewPdf';
import {attachmentViewPdfPropTypes, attachmentViewPdfDefaultProps} from './propTypes';

function AttachmentViewPdf(props) {
const attachmentCarouselPagerContext = useContext(AttachmentCarouselPagerContext);
const scaleRef = useSharedValue(1);
const offsetX = useSharedValue(0);
const offsetY = useSharedValue(0);

const Pan = Gesture.Pan()
.manualActivation(true)
.onTouchesMove((evt) => {
if (offsetX.value !== 0 && offsetY.value !== 0) {
// if the value of X is greater than Y and the pdf is not zoomed in,
// enable the pager scroll so that the user
// can swipe to the next attachment otherwise disable it.
if (Math.abs(evt.allTouches[0].absoluteX - offsetX.value) > Math.abs(evt.allTouches[0].absoluteY - offsetY.value) && scaleRef.value === 1) {
attachmentCarouselPagerContext.shouldPagerScroll.value = true;
} else {
attachmentCarouselPagerContext.shouldPagerScroll.value = false;
}
}
offsetX.value = evt.allTouches[0].absoluteX;
offsetY.value = evt.allTouches[0].absoluteY;
});

const updateScale = useCallback(
(scale) => {
scaleRef.value = scale;
},
[scaleRef],
);

return (
<View
collapsable={false}
style={[styles.flex1]}
>
<GestureDetector gesture={Pan}>
<Animated.View
collapsable={false}
style={[StyleSheet.absoluteFill, styles.justifyContentCenter, styles.alignItemsCenter]}
>
<BaseAttachmentViewPdf
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
updateScale={updateScale}
/>
</Animated.View>
</GestureDetector>
</View>
);
}

AttachmentViewPdf.propTypes = attachmentViewPdfPropTypes;
AttachmentViewPdf.defaultProps = attachmentViewPdfDefaultProps;

export default memo(AttachmentViewPdf);
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React, {memo} from 'react';
import BaseAttachmentViewPdf from './BaseAttachmentViewPdf';
import {attachmentViewPdfPropTypes, attachmentViewPdfDefaultProps} from './propTypes';

function AttachmentViewPdf(props) {
return (
<BaseAttachmentViewPdf
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
/>
);
}

AttachmentViewPdf.propTypes = attachmentViewPdfPropTypes;
AttachmentViewPdf.defaultProps = attachmentViewPdfDefaultProps;

export default memo(AttachmentViewPdf);
Loading