Skip to content

Commit c79529a

Browse files
authored
Merge pull request #30050 from HezekielT/fix--Android-pdf-scrolling-issue
Fix: android pdf scrolling issue
2 parents e36c722 + 373d983 commit c79529a

File tree

3 files changed

+102
-5
lines changed

3 files changed

+102
-5
lines changed

src/components/Attachments/AttachmentView/AttachmentViewPdf/index.native.js src/components/Attachments/AttachmentView/AttachmentViewPdf/BaseAttachmentViewPdf.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,18 @@ import AttachmentCarouselPagerContext from '@components/Attachments/AttachmentCa
33
import PDFView from '@components/PDFView';
44
import {attachmentViewPdfDefaultProps, attachmentViewPdfPropTypes} from './propTypes';
55

6-
function AttachmentViewPdf({file, encryptedSourceUrl, isFocused, isUsedInCarousel, onPress, onScaleChanged: onScaleChangedProp, onToggleKeyboard, onLoadComplete, errorLabelStyles, style}) {
6+
function BaseAttachmentViewPdf({
7+
file,
8+
encryptedSourceUrl,
9+
isFocused,
10+
isUsedInCarousel,
11+
onPress,
12+
onScaleChanged: onScaleChangedProp,
13+
onToggleKeyboard,
14+
onLoadComplete,
15+
errorLabelStyles,
16+
style,
17+
}) {
718
const attachmentCarouselPagerContext = useContext(AttachmentCarouselPagerContext);
819

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

1728
const onScaleChanged = useCallback(
1829
(scale) => {
19-
onScaleChangedProp();
30+
onScaleChangedProp(scale);
2031

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

52-
AttachmentViewPdf.propTypes = attachmentViewPdfPropTypes;
53-
AttachmentViewPdf.defaultProps = attachmentViewPdfDefaultProps;
63+
BaseAttachmentViewPdf.propTypes = attachmentViewPdfPropTypes;
64+
BaseAttachmentViewPdf.defaultProps = attachmentViewPdfDefaultProps;
65+
BaseAttachmentViewPdf.displayName = 'BaseAttachmentViewPdf';
5466

55-
export default memo(AttachmentViewPdf);
67+
export default memo(BaseAttachmentViewPdf);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import React, {memo, useCallback, useContext} from 'react';
2+
import {StyleSheet, View} from 'react-native';
3+
import {Gesture, GestureDetector} from 'react-native-gesture-handler';
4+
import Animated, {useSharedValue} from 'react-native-reanimated';
5+
import AttachmentCarouselPagerContext from '@components/Attachments/AttachmentCarousel/Pager/AttachmentCarouselPagerContext';
6+
import styles from '@styles/styles';
7+
import BaseAttachmentViewPdf from './BaseAttachmentViewPdf';
8+
import {attachmentViewPdfDefaultProps, attachmentViewPdfPropTypes} from './propTypes';
9+
10+
function AttachmentViewPdf(props) {
11+
const {onScaleChanged, ...restProps} = props;
12+
const attachmentCarouselPagerContext = useContext(AttachmentCarouselPagerContext);
13+
const scaleRef = useSharedValue(1);
14+
const offsetX = useSharedValue(0);
15+
const offsetY = useSharedValue(0);
16+
17+
const Pan = Gesture.Pan()
18+
.manualActivation(true)
19+
.onTouchesMove((evt) => {
20+
if (offsetX.value !== 0 && offsetY.value !== 0) {
21+
// if the value of X is greater than Y and the pdf is not zoomed in,
22+
// enable the pager scroll so that the user
23+
// can swipe to the next attachment otherwise disable it.
24+
if (Math.abs(evt.allTouches[0].absoluteX - offsetX.value) > Math.abs(evt.allTouches[0].absoluteY - offsetY.value) && scaleRef.value === 1) {
25+
attachmentCarouselPagerContext.shouldPagerScroll.value = true;
26+
} else {
27+
attachmentCarouselPagerContext.shouldPagerScroll.value = false;
28+
}
29+
}
30+
offsetX.value = evt.allTouches[0].absoluteX;
31+
offsetY.value = evt.allTouches[0].absoluteY;
32+
});
33+
34+
const updateScale = useCallback(
35+
(scale) => {
36+
scaleRef.value = scale;
37+
},
38+
[scaleRef],
39+
);
40+
41+
return (
42+
<View
43+
collapsable={false}
44+
style={[styles.flex1]}
45+
>
46+
<GestureDetector gesture={Pan}>
47+
<Animated.View
48+
collapsable={false}
49+
style={[StyleSheet.absoluteFill, styles.justifyContentCenter, styles.alignItemsCenter]}
50+
>
51+
<BaseAttachmentViewPdf
52+
// eslint-disable-next-line react/jsx-props-no-spreading
53+
{...restProps}
54+
onScaleChanged={(scale) => {
55+
updateScale(scale);
56+
onScaleChanged();
57+
}}
58+
/>
59+
</Animated.View>
60+
</GestureDetector>
61+
</View>
62+
);
63+
}
64+
65+
AttachmentViewPdf.propTypes = attachmentViewPdfPropTypes;
66+
AttachmentViewPdf.defaultProps = attachmentViewPdfDefaultProps;
67+
68+
export default memo(AttachmentViewPdf);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React, {memo} from 'react';
2+
import BaseAttachmentViewPdf from './BaseAttachmentViewPdf';
3+
import {attachmentViewPdfDefaultProps, attachmentViewPdfPropTypes} from './propTypes';
4+
5+
function AttachmentViewPdf(props) {
6+
return (
7+
<BaseAttachmentViewPdf
8+
// eslint-disable-next-line react/jsx-props-no-spreading
9+
{...props}
10+
/>
11+
);
12+
}
13+
14+
AttachmentViewPdf.propTypes = attachmentViewPdfPropTypes;
15+
AttachmentViewPdf.defaultProps = attachmentViewPdfDefaultProps;
16+
17+
export default memo(AttachmentViewPdf);

0 commit comments

Comments
 (0)