-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
168 lines (155 loc) · 4.41 KB
/
App.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import React, { useState, useEffect } from 'react';
import {
StyleSheet,
View,
Button,
Linking,
Alert,
Text
} from 'react-native';
import { EventRegister } from 'react-native-event-listeners';
import * as DocumentPicker from 'expo-document-picker';
import * as mime from 'react-native-mime-types';
import * as Permissions from 'expo-permissions';
import * as ImagePicker from 'expo-image-picker';
import * as Progress from 'react-native-progress';
import Firebase from './lib/firebase';
export default function App() {
const [progress, setProgress] = useState(0);
const [url, setUrl] = useState("");
useEffect(() => {
this.listener = EventRegister.addEventListener('onProgress', (data) => {
setProgress(data);
});
});
useEffect(() => {
return () => {
EventRegister.removeEventListener(this.listener);
}
}, []);
_onPressDocumentPicker = async () => {
const result = await DocumentPicker.getDocumentAsync({});
console.log('Result:', result);
if (result.type != "cancel" && !result.cancelled) {
this.addToFirebase(result.uri, result.name)
}
}
_onPressLibrary = async () => {
await this.pickContent((result) => {
console.log('Result:', result);
if (result.type != "cancel" && !result.cancelled) {
this.addToFirebase(result.uri, result.name);
}
});
}
_onPressCamera = async () => {
await this.takePicture((result) =>{
console.log('Result:', result);
if (result.type != "cancel" && !result.cancelled) {
this.addToFirebase(result.uri, result.name);
}
});
}
addToFirebase = (uri, name) => {
setUrl('');
var extension = uri.split('.').pop();
var contentType = mime.lookup(extension);
let firebase = Firebase.getInstance();
if(!name) { name = '' }
firebase.saveStorage('new/', uri, name, contentType, extension)
.then((res) => {
console.log(res);
setUrl(res.url);
})
.catch((error) => {
console.log(error);
});
}
pickContent = async (callback) => {
const { status } = await Permissions.askAsync(Permissions.CAMERA_ROLL);
if (status !== 'granted') {
callback({cancelled: true});
this.openParameter('Enable photos', 'Grant access to access to your library');
} else {
let result = await ImagePicker.launchImageLibraryAsync({
allowsEditing: true,
mediaTypes: 'All'
});
callback(result);
}
};
takePicture = async (callback) => {
const { status } = await Permissions.askAsync(Permissions.CAMERA, Permissions.CAMERA_ROLL);
if (status !== 'granted') {
callback({cancelled: true});
this.openParameter('Enable photos', 'Grant access to access to your library');
} else {
let result = await ImagePicker.launchCameraAsync({
allowsEditing: true,
mediaTypes: 'All'
});
callback(result);
}
};
openParameter = (title, content) => {
Alert.alert(
title,
content,
[
{text: 'Open parameter', onPress: () => Linking.openURL('app-settings:')},
{text: 'Cancel', onPress: () => console.log('Cancel Pressed'), style: 'cancel'},
],
{ cancelable: false }
)
}
return (
<View style={styles.container}>
<Button
color="#333"
title="Upload content from your drive"
onPress={this._onPressDocumentPicker}
style={styles.button}
/>
<Button
color="#333"
title="Upload content from your library"
onPress={this._onPressLibrary}
style={styles.button}
/>
<Button
color="#333"
title="Upload content from your camera"
onPress={this._onPressCamera}
style={styles.button}
/>
{
progress != 0 ?
<Progress.Circle progress={progress} size={75} showsText={true} />
: null
}
{
url != '' ?
<Text style={styles.text}>File URL: {url}</Text>
: null
}
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
button: {
borderRadius: 10,
color: '#fff',
marginBottom: 20
},
text: {
marginTop: 20,
textAlign: 'center',
marginHorizontal: 10
}
});