This repository has been archived by the owner on Oct 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 121
/
GalleryScreen.js
106 lines (94 loc) · 2.69 KB
/
GalleryScreen.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import React from 'react';
import { Image, StyleSheet, View, TouchableOpacity, Text, ScrollView } from 'react-native';
import { FileSystem, FaceDetector, MediaLibrary, Permissions } from 'expo';
import { MaterialIcons } from '@expo/vector-icons';
import Photo from './Photo';
const PHOTOS_DIR = FileSystem.documentDirectory + 'photos';
export default class GalleryScreen extends React.Component {
state = {
faces: {},
images: {},
photos: [],
selected: [],
};
componentDidMount = async () => {
const photos = await FileSystem.readDirectoryAsync(PHOTOS_DIR);
this.setState({ photos });
};
toggleSelection = (uri, isSelected) => {
let selected = this.state.selected;
if (isSelected) {
selected.push(uri);
} else {
selected = selected.filter(item => item !== uri);
}
this.setState({ selected });
};
saveToGallery = async () => {
const photos = this.state.selected;
if (photos.length > 0) {
const { status } = await Permissions.askAsync(Permissions.CAMERA_ROLL);
if (status !== 'granted') {
throw new Error('Denied CAMERA_ROLL permissions!');
}
const promises = photos.map(photoUri => {
return MediaLibrary.createAssetAsync(photoUri);
});
await Promise.all(promises);
alert('Successfully saved photos to user\'s gallery!');
} else {
alert('No photos to save!');
}
};
renderPhoto = fileName =>
<Photo
key={fileName}
uri={`${PHOTOS_DIR}/${fileName}`}
onSelectionToggle={this.toggleSelection}
/>;
render() {
return (
<View style={styles.container}>
<View style={styles.navbar}>
<TouchableOpacity style={styles.button} onPress={this.props.onPress}>
<MaterialIcons name="arrow-back" size={25} color="white" />
</TouchableOpacity>
<TouchableOpacity style={styles.button} onPress={this.saveToGallery}>
<Text style={styles.whiteText}>Save selected to gallery</Text>
</TouchableOpacity>
</View>
<ScrollView contentComponentStyle={{ flex: 1 }}>
<View style={styles.pictures}>
{this.state.photos.map(this.renderPhoto)}
</View>
</ScrollView>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
paddingTop: 20,
backgroundColor: 'white',
},
navbar: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
backgroundColor: '#4630EB',
},
pictures: {
flex: 1,
flexWrap: 'wrap',
flexDirection: 'row',
justifyContent: 'space-around',
paddingVertical: 8,
},
button: {
padding: 20,
},
whiteText: {
color: 'white',
}
});