diff --git a/src/screens/Compose/EditAttachment.tsx b/src/screens/Compose/EditAttachment.tsx index 1dc94d87..8af3822a 100644 --- a/src/screens/Compose/EditAttachment.tsx +++ b/src/screens/Compose/EditAttachment.tsx @@ -21,7 +21,6 @@ const ComposeEditAttachment: React.FC = ({ }, navigation }) => { - console.log('rendering') const { t } = useTranslation('screenCompose') const headerLeft = useCallback( diff --git a/src/screens/ImageViewer/save.ts b/src/screens/ImageViewer/save.ts new file mode 100644 index 00000000..aa72e4f2 --- /dev/null +++ b/src/screens/ImageViewer/save.ts @@ -0,0 +1,44 @@ +import haptics from '@components/haptics' +import CameraRoll from '@react-native-community/cameraroll' +import { FileSystem, Permissions } from 'react-native-unimodules' + +const saveIos = async ( + image: Nav.RootStackParamList['Screen-ImagesViewer']['imageUrls'][0] +) => { + CameraRoll.save(image.url) + .then(() => haptics('Success')) + .catch(() => { + if (image.remote_url) { + CameraRoll.save(image.remote_url) + .then(() => haptics('Success')) + .catch(() => haptics('Error')) + } else { + haptics('Error') + } + }) +} + +const saveAndroid = async ( + image: Nav.RootStackParamList['Screen-ImagesViewer']['imageUrls'][0] +) => { + const fileUri: string = `${FileSystem.documentDirectory}test.jpg` + const downloadedFile: FileSystem.FileSystemDownloadResult = await FileSystem.downloadAsync( + image.url, + fileUri + ) + + if (downloadedFile.status != 200) { + console.warn('error!') + } + + const perm = await Permissions.askAsync(Permissions.MEDIA_LIBRARY) + if (perm.status != 'granted') { + return + } + + CameraRoll.save(downloadedFile.uri) + .then(() => haptics('Success')) + .catch(() => haptics('Error')) +} + +export { saveIos, saveAndroid } diff --git a/src/screens/ImagesViewer.tsx b/src/screens/ImagesViewer.tsx index bb04d426..ed4eded8 100644 --- a/src/screens/ImagesViewer.tsx +++ b/src/screens/ImagesViewer.tsx @@ -1,19 +1,11 @@ import analytics from '@components/analytics' -import haptics from '@components/haptics' import { HeaderCenter, HeaderLeft, HeaderRight } from '@components/Header' import { useActionSheet } from '@expo/react-native-action-sheet' -import CameraRoll from '@react-native-community/cameraroll' import { StackScreenProps } from '@react-navigation/stack' import { findIndex } from 'lodash' import React, { useCallback, useState } from 'react' import { useTranslation } from 'react-i18next' -import { - PermissionsAndroid, - Platform, - Share, - StatusBar, - View -} from 'react-native' +import { Platform, Share, StatusBar, View } from 'react-native' import { SafeAreaProvider, useSafeAreaInsets @@ -23,35 +15,11 @@ import ImageViewer from './ImageViewer/Root' const saveImage = async ( image: Nav.RootStackParamList['Screen-ImagesViewer']['imageUrls'][0] ) => { - if (Platform.OS === 'ios') { - CameraRoll.save(image.url) - .then(() => haptics('Success')) - .catch(() => { - if (image.remote_url) { - CameraRoll.save(image.remote_url) - .then(() => haptics('Success')) - .catch(() => haptics('Error')) - } else { - haptics('Error') - } - }) - } else if (Platform.OS === 'android') { - const hasAndroidPermission = async () => { - const permission = PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE - - const hasPermission = await PermissionsAndroid.check(permission) - if (hasPermission) { - return true - } - - const status = await PermissionsAndroid.request(permission) - return status === 'granted' - } - - if (!(await hasAndroidPermission())) { - return - } - } + const save = require('./ImageViewer/save') + Platform.select({ + ios: save.saveIos(image), + android: save.saveAndroid(image) + }) } const HeaderComponent = React.memo(