tooot/src/screens/ImageViewer/save.ts

127 lines
3.3 KiB
TypeScript
Raw Normal View History

2021-04-10 20:03:28 +02:00
import haptics from '@components/haptics'
2021-05-09 22:27:12 +02:00
import { displayMessage } from '@components/Message'
2021-04-10 20:03:28 +02:00
import CameraRoll from '@react-native-community/cameraroll'
2021-08-29 15:25:38 +02:00
import { RootStackParamList } from '@utils/navigation/navigators'
2021-10-24 00:43:00 +02:00
import * as FileSystem from 'expo-file-system'
2021-05-09 22:27:12 +02:00
import i18next from 'i18next'
import { RefObject } from 'react'
2022-01-08 12:39:45 +01:00
import { PermissionsAndroid, Platform } from 'react-native'
2021-05-09 22:27:12 +02:00
import FlashMessage from 'react-native-flash-message'
2021-04-10 20:03:28 +02:00
2021-05-09 22:27:12 +02:00
type CommonProps = {
messageRef: RefObject<FlashMessage>
mode: 'light' | 'dark'
2021-08-29 15:25:38 +02:00
image: RootStackParamList['Screen-ImagesViewer']['imageUrls'][0]
2021-05-09 22:27:12 +02:00
}
const saveIos = async ({ messageRef, mode, image }: CommonProps) => {
2021-04-10 20:03:28 +02:00
CameraRoll.save(image.url)
2021-04-19 11:34:50 +02:00
.then(() => {
haptics('Success')
2021-05-09 22:27:12 +02:00
displayMessage({
ref: messageRef,
mode,
type: 'success',
message: i18next.t('screenImageViewer:content.save.succeed')
})
2021-04-19 11:34:50 +02:00
})
2021-04-10 20:03:28 +02:00
.catch(() => {
if (image.remote_url) {
CameraRoll.save(image.remote_url)
2021-05-09 22:27:12 +02:00
.then(() => {
haptics('Success')
displayMessage({
ref: messageRef,
mode,
type: 'success',
message: i18next.t('screenImageViewer:content.save.succeed')
})
})
.catch(() => {
haptics('Error')
displayMessage({
ref: messageRef,
mode,
type: 'error',
message: i18next.t('screenImageViewer:content.save.failed')
})
})
2021-04-10 20:03:28 +02:00
} else {
haptics('Error')
2021-05-09 22:27:12 +02:00
displayMessage({
ref: messageRef,
mode,
type: 'error',
message: i18next.t('screenImageViewer:content.save.failed')
})
2021-04-10 20:03:28 +02:00
}
})
}
2021-05-09 22:27:12 +02:00
const saveAndroid = async ({ messageRef, mode, image }: CommonProps) => {
2021-05-30 22:12:22 +02:00
const fileUri: string = `${FileSystem.documentDirectory}${image.id}.jpg`
2021-10-24 00:43:00 +02:00
const downloadedFile: FileSystem.FileSystemDownloadResult =
await FileSystem.downloadAsync(image.url, fileUri)
2021-04-10 20:03:28 +02:00
if (downloadedFile.status != 200) {
2022-01-08 12:39:45 +01:00
haptics('Error')
displayMessage({
ref: messageRef,
mode,
type: 'error',
message: i18next.t('screenImageViewer:content.save.failed')
})
return
2021-04-10 20:03:28 +02:00
}
2022-01-08 12:39:45 +01:00
const permission = PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE
const hasPermission = await PermissionsAndroid.check(permission)
if (!hasPermission) {
const status = await PermissionsAndroid.request(permission)
if (status !== 'granted') {
haptics('Error')
displayMessage({
ref: messageRef,
mode,
type: 'error',
message: i18next.t('screenImageViewer:content.save.failed')
})
return
}
2021-04-10 20:03:28 +02:00
}
CameraRoll.save(downloadedFile.uri)
2021-05-09 22:27:12 +02:00
.then(() => {
haptics('Success')
displayMessage({
ref: messageRef,
mode,
type: 'success',
2021-05-30 22:12:22 +02:00
message: i18next.t('screenImageViewer:content.save.succeed')
2021-05-09 22:27:12 +02:00
})
})
.catch(() => {
haptics('Error')
displayMessage({
ref: messageRef,
mode,
type: 'error',
message: i18next.t('screenImageViewer:content.save.failed')
})
})
}
const saveImage = async (props: CommonProps) => {
switch (Platform.OS) {
case 'ios':
saveIos(props)
break
case 'android':
saveAndroid(props)
break
}
2021-04-10 20:03:28 +02:00
}
2021-05-09 22:27:12 +02:00
export default saveImage