|
| 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); |
0 commit comments