-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
87 lines (75 loc) · 2.51 KB
/
index.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
import React, { useEffect, useState } from 'react'
import {
Dimensions,
SafeAreaView,
ScrollView,
StatusBar,
StyleSheet,
Text,
useColorScheme,
View,
Image
} from 'react-native';
import { launchImageLibrary } from "react-native-image-picker"
import Header from "./src/Header"
import Images from "./src/Images"
export default function Gallery(props) {
const [scrolling, setScrolling] = useState(true)
const [gallery, setGallery] = useState([])
const [unique, setUnique] = useState(0)
useEffect(() => {
var initialData = []
props.initialState.map((init,i)=>{
init["key"] = String(i)
initialData.push(init)
setUnique(i+1)
});
setGallery(initialData)
}, [])
useEffect(() => {
props.onChangeState(gallery)
}, [gallery])
function formatBytes(bytes, decimals = 2) {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}
const selectFile = async () => {
try {
launchImageLibrary({ mediaType: "photo", allowsMultiple: true }, (response) => {
var a = []
gallery.map((img,i)=>{
a.push(img)
});
if (!response.didCancel && !response.error && !response.customButton) {
var res = response.assets[0]
var imageData = {
uri: res.uri,
name: res.fileName,
size: formatBytes(res.fileSize),
key: String(unique),
}
a.push(imageData)
setGallery(a)
setUnique(unique + 1)
}
})
} catch (err) {
console.log(err)
}
};
return (
<SafeAreaView>
<Header selectFile={selectFile} />
<ScrollView
style={{ marginTop: 20, }}
scrollEnabled={scrolling}>
<Images handlerUnique={setUnique} handleScroll={setScrolling} data={gallery} unique={unique} setData={setGallery} />
<View style={{ height: 170 }}></View>
</ScrollView>
</SafeAreaView>
)
}