Skip to content

Commit

Permalink
Merge pull request #30050 from HezekielT/fix--Android-pdf-scrolling-i…
Browse files Browse the repository at this point in the history
…ssue

Fix: android pdf scrolling issue
  • Loading branch information
Gonals authored Nov 2, 2023
2 parents e36c722 + 373d983 commit c79529a
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,18 @@ import AttachmentCarouselPagerContext from '@components/Attachments/AttachmentCa
import PDFView from '@components/PDFView';
import {attachmentViewPdfDefaultProps, attachmentViewPdfPropTypes} from './propTypes';

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

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

const onScaleChanged = useCallback(
(scale) => {
onScaleChangedProp();
onScaleChangedProp(scale);

// When a pdf is shown in a carousel, we want to disable the pager scroll when the pdf is zoomed in
if (isUsedInCarousel) {
Expand Down Expand Up @@ -49,7 +60,8 @@ function AttachmentViewPdf({file, encryptedSourceUrl, isFocused, isUsedInCarouse
);
}

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

export default memo(AttachmentViewPdf);
export default memo(BaseAttachmentViewPdf);
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import React, {memo, useCallback, useContext} from 'react';
import {StyleSheet, View} 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 {attachmentViewPdfDefaultProps, attachmentViewPdfPropTypes} from './propTypes';

function AttachmentViewPdf(props) {
const {onScaleChanged, ...restProps} = 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
{...restProps}
onScaleChanged={(scale) => {
updateScale(scale);
onScaleChanged();
}}
/>
</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 {attachmentViewPdfDefaultProps, attachmentViewPdfPropTypes} 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);

0 comments on commit c79529a

Please sign in to comment.