This commit is contained in:
Zhiyuan Zheng 2021-04-10 20:03:28 +02:00
parent df606e8cdc
commit 81ffaabcdc
No known key found for this signature in database
GPG Key ID: 078A93AB607D85E0
3 changed files with 50 additions and 39 deletions

View File

@ -21,7 +21,6 @@ const ComposeEditAttachment: React.FC<ScreenComposeEditAttachmentProp> = ({
},
navigation
}) => {
console.log('rendering')
const { t } = useTranslation('screenCompose')
const headerLeft = useCallback(

View File

@ -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 }

View File

@ -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(