2021-01-30 01:29:15 +01:00
|
|
|
import analytics from '@components/analytics'
|
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'
|
2021-02-10 00:40:44 +01:00
|
|
|
import { StackScreenProps } from '@react-navigation/stack'
|
2021-01-30 01:29:15 +01:00
|
|
|
import { findIndex } from 'lodash'
|
2021-02-10 00:40:44 +01:00
|
|
|
import React, { useCallback, useState } from 'react'
|
2021-01-31 03:09:35 +01:00
|
|
|
import { useTranslation } from 'react-i18next'
|
2021-04-10 20:03:28 +02:00
|
|
|
import { Platform, Share, StatusBar, View } from 'react-native'
|
2021-02-14 00:27:21 +01:00
|
|
|
import {
|
|
|
|
SafeAreaProvider,
|
|
|
|
useSafeAreaInsets
|
|
|
|
} from 'react-native-safe-area-context'
|
2021-03-21 12:32:49 +01:00
|
|
|
import ImageViewer from './ImageViewer/Root'
|
2021-04-19 11:34:50 +02:00
|
|
|
import { saveAndroid, saveIos } from './ImageViewer/save'
|
2021-03-21 12:32:49 +01:00
|
|
|
|
2021-03-21 23:06:53 +01:00
|
|
|
const saveImage = async (
|
|
|
|
image: Nav.RootStackParamList['Screen-ImagesViewer']['imageUrls'][0]
|
|
|
|
) => {
|
2021-04-19 11:34:50 +02:00
|
|
|
switch (Platform.OS) {
|
|
|
|
case 'ios':
|
|
|
|
saveIos(image)
|
|
|
|
break
|
|
|
|
case 'android':
|
|
|
|
saveAndroid(image)
|
|
|
|
break
|
|
|
|
}
|
2021-03-21 12:32:49 +01:00
|
|
|
}
|
2021-01-30 01:29:15 +01:00
|
|
|
|
2021-02-14 00:27:21 +01:00
|
|
|
const HeaderComponent = React.memo(
|
|
|
|
({
|
|
|
|
navigation,
|
|
|
|
currentIndex,
|
|
|
|
imageUrls
|
|
|
|
}: {
|
|
|
|
navigation: ScreenImagesViewerProp['navigation']
|
|
|
|
currentIndex: number
|
2021-03-21 23:06:53 +01:00
|
|
|
imageUrls: Nav.RootStackParamList['Screen-ImagesViewer']['imageUrls']
|
2021-02-14 00:27:21 +01:00
|
|
|
}) => {
|
|
|
|
const insets = useSafeAreaInsets()
|
|
|
|
const { t } = useTranslation('screenImageViewer')
|
|
|
|
const { showActionSheetWithOptions } = useActionSheet()
|
2021-02-11 01:33:31 +01:00
|
|
|
|
2021-02-14 00:27:21 +01:00
|
|
|
const onPress = useCallback(() => {
|
|
|
|
analytics('imageviewer_more_press')
|
|
|
|
showActionSheetWithOptions(
|
|
|
|
{
|
|
|
|
options: [
|
|
|
|
t('content.options.save'),
|
|
|
|
t('content.options.share'),
|
|
|
|
t('content.options.cancel')
|
|
|
|
],
|
|
|
|
cancelButtonIndex: 2
|
|
|
|
},
|
|
|
|
async buttonIndex => {
|
|
|
|
switch (buttonIndex) {
|
|
|
|
case 0:
|
|
|
|
analytics('imageviewer_more_save_press')
|
2021-03-21 12:32:49 +01:00
|
|
|
saveImage(imageUrls[currentIndex])
|
2021-02-14 00:27:21 +01:00
|
|
|
break
|
|
|
|
case 1:
|
|
|
|
analytics('imageviewer_more_share_press')
|
|
|
|
switch (Platform.OS) {
|
|
|
|
case 'ios':
|
2021-03-18 23:54:40 +01:00
|
|
|
Share.share({ url: imageUrls[currentIndex].url })
|
2021-02-14 00:27:21 +01:00
|
|
|
case 'android':
|
2021-03-18 23:54:40 +01:00
|
|
|
Share.share({ message: imageUrls[currentIndex].url })
|
2021-02-14 00:27:21 +01:00
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2021-02-09 00:24:02 +01:00
|
|
|
}
|
2021-02-14 00:27:21 +01:00
|
|
|
)
|
|
|
|
}, [currentIndex])
|
2021-01-30 01:29:15 +01:00
|
|
|
|
2021-02-14 00:27:21 +01:00
|
|
|
return (
|
2021-02-10 00:40:44 +01:00
|
|
|
<View
|
|
|
|
style={{
|
|
|
|
flex: 1,
|
|
|
|
flexDirection: 'row',
|
|
|
|
justifyContent: 'space-between',
|
2021-02-14 00:27:21 +01:00
|
|
|
alignItems: 'center',
|
|
|
|
marginTop: insets.top
|
2021-02-10 00:40:44 +01:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
<HeaderLeft
|
|
|
|
content='X'
|
|
|
|
native={false}
|
2021-03-23 23:16:01 +01:00
|
|
|
background
|
2021-02-10 00:40:44 +01:00
|
|
|
onPress={() => navigation.goBack()}
|
|
|
|
/>
|
|
|
|
<HeaderCenter
|
|
|
|
inverted
|
|
|
|
content={`${currentIndex + 1} / ${imageUrls.length}`}
|
|
|
|
/>
|
|
|
|
<HeaderRight
|
2021-04-09 21:43:12 +02:00
|
|
|
accessibilityLabel={t('content.actions.accessibilityLabel')}
|
|
|
|
accessibilityHint={t('content.actions.accessibilityHint')}
|
2021-02-10 00:40:44 +01:00
|
|
|
content='MoreHorizontal'
|
|
|
|
native={false}
|
2021-03-23 23:16:01 +01:00
|
|
|
background
|
2021-02-10 00:40:44 +01:00
|
|
|
onPress={onPress}
|
|
|
|
/>
|
|
|
|
</View>
|
2021-02-14 00:27:21 +01:00
|
|
|
)
|
|
|
|
},
|
|
|
|
(prev, next) => prev.currentIndex === next.currentIndex
|
|
|
|
)
|
|
|
|
|
|
|
|
export type ScreenImagesViewerProp = StackScreenProps<
|
|
|
|
Nav.RootStackParamList,
|
|
|
|
'Screen-ImagesViewer'
|
|
|
|
>
|
|
|
|
|
|
|
|
const ScreenImagesViewer = ({
|
|
|
|
route: {
|
2021-03-06 23:42:29 +01:00
|
|
|
params: { imageUrls, id }
|
2021-02-14 00:27:21 +01:00
|
|
|
},
|
|
|
|
navigation
|
|
|
|
}: ScreenImagesViewerProp) => {
|
|
|
|
if (imageUrls.length === 0) {
|
2021-04-19 22:50:55 +02:00
|
|
|
navigation.goBack()
|
2021-02-14 00:27:21 +01:00
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2021-03-06 23:42:29 +01:00
|
|
|
const initialIndex = findIndex(imageUrls, ['id', id])
|
|
|
|
const [currentIndex, setCurrentIndex] = useState(initialIndex)
|
2021-01-30 01:29:15 +01:00
|
|
|
|
2021-02-09 00:24:02 +01:00
|
|
|
return (
|
2021-02-14 00:27:21 +01:00
|
|
|
<SafeAreaProvider>
|
2021-02-13 12:33:51 +01:00
|
|
|
<StatusBar backgroundColor='rgb(0,0,0)' />
|
2021-03-21 12:32:49 +01:00
|
|
|
<ImageViewer
|
2021-02-14 00:27:21 +01:00
|
|
|
images={imageUrls}
|
2021-03-06 23:42:29 +01:00
|
|
|
imageIndex={initialIndex}
|
2021-02-10 00:40:44 +01:00
|
|
|
onImageIndexChange={index => setCurrentIndex(index)}
|
|
|
|
onRequestClose={() => navigation.goBack()}
|
2021-03-21 12:32:49 +01:00
|
|
|
onLongPress={saveImage}
|
2021-02-14 00:27:21 +01:00
|
|
|
HeaderComponent={() => (
|
|
|
|
<HeaderComponent
|
|
|
|
navigation={navigation}
|
|
|
|
currentIndex={currentIndex}
|
|
|
|
imageUrls={imageUrls}
|
|
|
|
/>
|
|
|
|
)}
|
2021-02-10 00:40:44 +01:00
|
|
|
/>
|
2021-02-14 00:27:21 +01:00
|
|
|
</SafeAreaProvider>
|
2021-02-09 00:24:02 +01:00
|
|
|
)
|
|
|
|
}
|
2021-01-30 01:29:15 +01:00
|
|
|
|
|
|
|
export default ScreenImagesViewer
|