-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2864 from ecency/nt/gallery-mode
Nt/gallery mode
- Loading branch information
Showing
6 changed files
with
208 additions
and
245 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import EStyleSheet from 'react-native-extended-stylesheet'; | ||
|
||
export default EStyleSheet.create({ | ||
imageViewerHeaderContainer: { | ||
alignItems: 'center', | ||
paddingVertical: 10, | ||
}, | ||
leftContainer: { | ||
position: 'absolute', | ||
flexDirection:'row', | ||
alignItems:'center', | ||
left: 8, | ||
}, | ||
rightContainer: { | ||
position: 'absolute', | ||
flexDirection:'row', | ||
right: 8, | ||
}, | ||
imageGalleryHeaderText: { | ||
color: '$iconColor', | ||
fontSize: 16 | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
import React, { forwardRef, useImperativeHandle, useState } from 'react'; | ||
import { View, Text, Platform, SafeAreaView, Share, Alert } from 'react-native'; | ||
import styles from './imageViewer.styles'; | ||
import EStyleSheet from 'react-native-extended-stylesheet'; | ||
import ImageViewing from 'react-native-image-viewing'; | ||
import { useIntl } from 'react-intl'; | ||
import IconButton from '../iconButton'; | ||
import { useDispatch } from 'react-redux'; | ||
import { PermissionsAndroid } from 'react-native'; | ||
import { CameraRoll } from '@react-native-camera-roll/camera-roll'; | ||
import { Image as ExpoImage } from 'expo-image'; | ||
import RNFetchBlob from 'rn-fetch-blob'; | ||
|
||
|
||
export const ImageViewer = forwardRef(({ }, ref) => { | ||
const intl = useIntl(); | ||
const dispatch = useDispatch(); | ||
// intl.formatMessage({ id: 'post.copy_link' }), | ||
// intl.formatMessage({ id: 'post.gallery_mode' }), | ||
// intl.formatMessage({ id: 'post.save_to_local' }), | ||
|
||
const [visible, setVisible] = useState(false); | ||
const [imageUrls, setImageUrls] = useState<string[]>([]); | ||
const [selectedIndex, setSelectedIndex] = useState(0); | ||
|
||
|
||
useImperativeHandle(ref, () => ({ | ||
show(selectedUrl: string, _imageUrls: string[]) { | ||
setImageUrls(_imageUrls) | ||
setSelectedIndex(_imageUrls.indexOf(selectedUrl)) | ||
setVisible(true); | ||
|
||
if (Platform.OS === 'ios') { | ||
ExpoImage.prefetch(_imageUrls, "memory"); | ||
} | ||
|
||
}, | ||
})); | ||
|
||
const checkAndroidPermission = async () => { | ||
try { | ||
const permission = PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE; | ||
await PermissionsAndroid.request(permission); | ||
Promise.resolve(); | ||
} catch (error) { | ||
Promise.reject(error); | ||
} | ||
}; | ||
|
||
|
||
const _downloadImage = async (uri: string) => { | ||
return RNFetchBlob.config({ | ||
fileCache: true, | ||
appendExt: 'jpg', | ||
}) | ||
.fetch('GET', uri) | ||
.then((res) => { | ||
const { status } = res.info(); | ||
|
||
if (status == 200) { | ||
return res.path(); | ||
} else { | ||
Promise.reject(); | ||
} | ||
}) | ||
.catch((errorMessage) => { | ||
Promise.reject(errorMessage); | ||
}); | ||
}; | ||
|
||
|
||
const _onSavePress = async (index: number) => { | ||
|
||
try { | ||
if (Platform.OS === 'android') { | ||
await checkAndroidPermission(); | ||
} | ||
|
||
const url = imageUrls[index]; | ||
|
||
const imagePath = Platform.select({ | ||
ios: await ExpoImage.getCachePathAsync(url), | ||
android: await _downloadImage(url) | ||
}) | ||
|
||
if (!imagePath) { | ||
return; | ||
} | ||
|
||
const uri = `file://${imagePath}` | ||
await CameraRoll.saveAsset(uri, { album: 'Ecency' }) | ||
|
||
Alert.alert(intl.formatMessage({ id: "post.image_saved" })); | ||
|
||
|
||
} catch (err) { | ||
console.warn("fail to save image", err.message) | ||
} | ||
}; | ||
|
||
|
||
|
||
const _onCopyPress = (index: number) => { | ||
const url = imageUrls[index] | ||
Share.share(Platform.OS === 'ios' ? | ||
{ url } : { message: url }) | ||
} | ||
|
||
|
||
const _renderIconButton = (iconName: string, onPress: () => void) => ( | ||
<IconButton | ||
name={iconName} | ||
iconType="MaterialCommunityIcons" | ||
color={EStyleSheet.value('$iconColor')} | ||
style={{ marginHorizontal: 4 }} | ||
size={24} | ||
onPress={onPress} | ||
/> | ||
) | ||
|
||
|
||
const _renderImageViewerHeader = (imageIndex: number) => { | ||
return ( | ||
<SafeAreaView | ||
style={{ | ||
marginTop: Platform.select({ ios: 0, android: 25 }), | ||
}}> | ||
<View style={styles.imageViewerHeaderContainer}> | ||
<View style={styles.leftContainer}> | ||
{_renderIconButton('close', _onCloseImageViewer)} | ||
<Text style={styles.imageGalleryHeaderText}>{ | ||
`Preview (${imageIndex + 1}/${imageUrls.length})`}</Text> | ||
</View> | ||
|
||
<View style={styles.rightContainer}> | ||
{_renderIconButton('content-copy', () => _onCopyPress(imageIndex))} | ||
{_renderIconButton('download', () => _onSavePress(imageIndex))} | ||
</View> | ||
|
||
</View> | ||
</SafeAreaView> | ||
); | ||
}; | ||
|
||
|
||
const _onCloseImageViewer = () => { | ||
setVisible(false); | ||
setImageUrls([]) | ||
} | ||
|
||
|
||
return ( | ||
<ImageViewing | ||
images={imageUrls.map((url) => ({ uri: url }))} | ||
imageIndex={selectedIndex} | ||
visible={visible} | ||
animationType="slide" | ||
swipeToCloseEnabled | ||
onRequestClose={_onCloseImageViewer} | ||
HeaderComponent={(data) => _renderImageViewerHeader(data.imageIndex)} | ||
/> | ||
); | ||
}); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './imageViewer'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.