1
0
mirror of https://github.com/tooot-app/app synced 2025-06-05 22:19:13 +02:00
This commit is contained in:
Zhiyuan Zheng
2021-05-09 22:27:12 +02:00
parent 0b659913dc
commit 46b63b33d6
4 changed files with 91 additions and 30 deletions

View File

@ -1,28 +1,63 @@
import haptics from '@components/haptics'
import { displayMessage } from '@components/Message'
import CameraRoll from '@react-native-community/cameraroll'
import i18next from 'i18next'
import { RefObject } from 'react'
import { Platform } from 'react-native'
import FlashMessage from 'react-native-flash-message'
import { FileSystem, Permissions } from 'react-native-unimodules'
const saveIos = async (
type CommonProps = {
messageRef: RefObject<FlashMessage>
mode: 'light' | 'dark'
image: Nav.RootStackParamList['Screen-ImagesViewer']['imageUrls'][0]
) => {
}
const saveIos = async ({ messageRef, mode, image }: CommonProps) => {
CameraRoll.save(image.url)
.then(() => {
haptics('Success')
displayMessage({
ref: messageRef,
mode,
type: 'success',
message: i18next.t('screenImageViewer:content.save.succeed')
})
})
.catch(() => {
if (image.remote_url) {
CameraRoll.save(image.remote_url)
.then(() => haptics('Success'))
.catch(() => haptics('Error'))
.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')
})
})
} else {
haptics('Error')
displayMessage({
ref: messageRef,
mode,
type: 'error',
message: i18next.t('screenImageViewer:content.save.failed')
})
}
})
}
const saveAndroid = async (
image: Nav.RootStackParamList['Screen-ImagesViewer']['imageUrls'][0]
) => {
const saveAndroid = async ({ messageRef, mode, image }: CommonProps) => {
const fileUri: string = `${FileSystem.documentDirectory}test.jpg`
const downloadedFile: FileSystem.FileSystemDownloadResult = await FileSystem.downloadAsync(
image.url,
@ -39,8 +74,35 @@ const saveAndroid = async (
}
CameraRoll.save(downloadedFile.uri)
.then(() => haptics('Success'))
.catch(() => haptics('Error'))
.then(() => {
haptics('Success')
displayMessage({
ref: messageRef,
mode,
type: 'success',
message: 'test'
})
})
.catch(() => {
haptics('Error')
displayMessage({
ref: messageRef,
mode,
type: 'error',
message: i18next.t('screenImageViewer:content.save.failed')
})
})
}
export { saveIos, saveAndroid }
const saveImage = async (props: CommonProps) => {
switch (Platform.OS) {
case 'ios':
saveIos(props)
break
case 'android':
saveAndroid(props)
break
}
}
export default saveImage