tooot/src/screens/ImageViewer/index.tsx

235 lines
8.0 KiB
TypeScript
Raw Normal View History

2022-09-14 21:52:16 +02:00
import GracefullyImage from '@components/GracefullyImage'
2021-02-10 00:40:44 +01:00
import { HeaderCenter, HeaderLeft, HeaderRight } from '@components/Header'
2021-01-31 03:09:35 +01:00
import { useActionSheet } from '@expo/react-native-action-sheet'
import { androidActionSheetStyles } from '@utils/helpers/androidActionSheetStyles'
2022-08-14 22:18:41 +02:00
import { RootStackScreenProps } from '@utils/navigation/navigators'
2021-05-09 22:27:12 +02:00
import { useTheme } from '@utils/styles/ThemeManager'
2022-12-31 14:00:52 +01:00
import React, { useCallback, useState } from 'react'
2021-01-31 03:09:35 +01:00
import { useTranslation } from 'react-i18next'
2021-02-14 00:27:21 +01:00
import {
2022-09-14 21:52:16 +02:00
Dimensions,
FlatList,
PixelRatio,
Platform,
Share,
StatusBar,
View,
ViewToken
} from 'react-native'
import { Directions, Gesture, LongPressGestureHandler } from 'react-native-gesture-handler'
2022-10-31 19:42:49 +01:00
import { runOnJS, useSharedValue } from 'react-native-reanimated'
import { createZoomListComponent, Zoom } from 'react-native-reanimated-zoom'
2022-11-20 16:14:08 +01:00
import { useSafeAreaInsets } from 'react-native-safe-area-context'
import saveImage from './save'
2021-01-30 01:29:15 +01:00
2022-09-14 21:52:16 +02:00
const ZoomFlatList = createZoomListComponent(FlatList)
2021-02-14 00:27:21 +01:00
const ScreenImagesViewer = ({
route: {
2022-12-18 20:55:33 +01:00
params: { imageUrls, id, hideCounter }
2021-02-14 00:27:21 +01:00
},
navigation
2021-08-29 15:25:38 +02:00
}: RootStackScreenProps<'Screen-ImagesViewer'>) => {
2021-02-14 00:27:21 +01:00
if (imageUrls.length === 0) {
2021-04-19 22:50:55 +02:00
navigation.goBack()
2021-02-14 00:27:21 +01:00
return null
}
2022-12-18 21:16:21 +01:00
const WINDOW_WIDTH = Dimensions.get('window').width
const WINDOW_HEIGHT = Dimensions.get('window').height
2022-09-14 21:52:16 +02:00
2022-06-01 23:47:58 +02:00
const insets = useSafeAreaInsets()
const { colors } = useTheme()
2022-12-23 15:53:40 +01:00
const { t } = useTranslation(['common', 'screenImageViewer'])
2021-05-09 22:27:12 +02:00
2022-01-30 22:51:03 +01:00
const initialIndex = imageUrls.findIndex(image => image.id === id)
2021-03-06 23:42:29 +01:00
const [currentIndex, setCurrentIndex] = useState(initialIndex)
2021-01-30 01:29:15 +01:00
2022-06-01 23:47:58 +02:00
const { showActionSheetWithOptions } = useActionSheet()
2022-10-31 19:42:49 +01:00
const isZoomed = useSharedValue(false)
2022-12-31 14:00:52 +01:00
const onViewableItemsChanged = useCallback(
({ viewableItems }: { viewableItems: ViewToken[] }) => {
setCurrentIndex(viewableItems[0]?.index || 0)
},
[]
)
2021-02-09 00:24:02 +01:00
return (
2022-11-20 16:14:08 +01:00
<View style={{ backgroundColor: 'black' }}>
2022-02-17 00:09:19 +01:00
<StatusBar hidden />
2022-09-14 21:52:16 +02:00
<View
style={{
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
marginTop: insets.top,
position: 'absolute',
width: '100%',
zIndex: 999
}}
>
<HeaderLeft content='x' native={false} background onPress={() => navigation.goBack()} />
2022-12-18 20:55:33 +01:00
{!hideCounter ? (
<HeaderCenter inverted content={`${currentIndex + 1} / ${imageUrls.length}`} />
) : null}
2022-09-14 21:52:16 +02:00
<HeaderRight
2022-12-23 15:53:40 +01:00
accessibilityLabel={t('screenImageViewer:content.actions.accessibilityLabel')}
accessibilityHint={t('screenImageViewer:content.actions.accessibilityHint')}
content='more-horizontal'
2022-09-14 21:52:16 +02:00
native={false}
background
onPress={() =>
showActionSheetWithOptions(
{
options: [
t('screenImageViewer:content.options.save'),
t('screenImageViewer:content.options.share'),
t('common:buttons.cancel')
],
cancelButtonIndex: 2,
...androidActionSheetStyles(colors)
},
async buttonIndex => {
switch (buttonIndex) {
case 0:
saveImage({ image: imageUrls[currentIndex] })
break
case 1:
switch (Platform.OS) {
case 'ios':
await Share.share({ url: imageUrls[currentIndex].url })
break
case 'android':
await Share.share({ message: imageUrls[currentIndex].url })
break
}
break
}
}
)
}
2022-09-14 21:52:16 +02:00
/>
</View>
<LongPressGestureHandler
2022-12-31 14:00:52 +01:00
onActivated={() => {
showActionSheetWithOptions(
{
options: [
2022-12-23 15:53:40 +01:00
t('screenImageViewer:content.options.save'),
t('screenImageViewer:content.options.share'),
2022-12-12 23:57:07 +01:00
t('common:buttons.cancel')
],
cancelButtonIndex: 2,
2022-12-19 22:36:30 +01:00
...androidActionSheetStyles(colors)
},
async buttonIndex => {
switch (buttonIndex) {
case 0:
saveImage({ image: imageUrls[currentIndex] })
break
case 1:
switch (Platform.OS) {
case 'ios':
await Share.share({ url: imageUrls[currentIndex].url })
break
case 'android':
await Share.share({
message: imageUrls[currentIndex].url
})
break
}
break
2022-09-14 22:44:48 +02:00
}
}
)
}}
>
<ZoomFlatList
data={imageUrls}
pagingEnabled
horizontal
keyExtractor={item => item.id}
renderItem={({
item
}: {
item: RootStackScreenProps<'Screen-ImagesViewer'>['route']['params']['imageUrls'][0]
}) => {
const screenRatio = WINDOW_WIDTH / WINDOW_HEIGHT
const imageRatio = item.width && item.height ? item.width / item.height : 1
const imageWidth = item.width || 100
const imageHeight = item.height || 100
const maxWidthScale = item.width
? (item.width / WINDOW_WIDTH / PixelRatio.get()) * 4
: 0
const maxHeightScale = item.height
? (item.height / WINDOW_WIDTH / PixelRatio.get()) * 4
: 0
const max = Math.max.apply(Math, [maxWidthScale, maxHeightScale, 4])
return (
<Zoom
onZoomBegin={() => (isZoomed.value = true)}
onZoomEnd={() => (isZoomed.value = false)}
maximumZoomScale={max > 8 ? 8 : max}
simultaneousGesture={Gesture.Fling()
.direction(Directions.DOWN)
.onStart(() => {
if (isZoomed.value === false) {
runOnJS(navigation.goBack)()
}
})}
children={
<View
style={{
width: WINDOW_WIDTH,
height: WINDOW_HEIGHT,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center'
}}
>
<GracefullyImage
2023-02-08 01:10:59 +01:00
sources={{
preview: { uri: item.preview_url, width: item.width, height: item.height },
default: { uri: item.url, width: item.width, height: item.height },
remote: { uri: item.remote_url, width: item.width, height: item.height }
}}
dimension={{
width:
screenRatio > imageRatio
? (WINDOW_HEIGHT / imageHeight) * imageWidth
: WINDOW_WIDTH,
height:
screenRatio > imageRatio
? WINDOW_HEIGHT
: (WINDOW_WIDTH / imageWidth) * imageHeight
}}
2023-02-08 01:10:59 +01:00
enableLiveTextInteraction
/>
</View>
}
/>
)
}}
2022-12-31 14:00:52 +01:00
onViewableItemsChanged={onViewableItemsChanged}
viewabilityConfig={{
itemVisiblePercentThreshold: 50
2022-09-14 21:52:16 +02:00
}}
initialScrollIndex={initialIndex}
getItemLayout={(_, index) => ({
2022-12-18 21:16:21 +01:00
length: WINDOW_WIDTH,
offset: WINDOW_WIDTH * index,
index
})}
/>
</LongPressGestureHandler>
2022-11-20 16:14:08 +01:00
</View>
2021-02-09 00:24:02 +01:00
)
}
2021-01-30 01:29:15 +01:00
export default ScreenImagesViewer