-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.jsx
109 lines (103 loc) · 3.31 KB
/
App.jsx
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
import { Camera } from "expo-camera";
import * as FileSystem from "expo-file-system";
import * as MediaLibrary from "expo-media-library";
import { useEffect, useState } from "react";
import { Button, StyleSheet, Text, View } from "react-native";
import { InputLink, Slider, TakeImage } from "./components";
import { ensureDirExist } from "./utils";
export default function App() {
const [cameraPermission, requestCameraPermission] =
Camera.useCameraPermissions();
const [mediaPermission, requestMediaPermission] = MediaLibrary.usePermissions(
{ request: true }
);
const [arrImage, setArrImage] = useState([]);
const [openCamera, setOpenCamera] = useState(false);
const handleOpenCamera = async () => {
if (!cameraPermission.granted) await requestCameraPermission();
if (!mediaPermission.granted) await requestMediaPermission();
setOpenCamera((prev) => !prev);
};
useEffect(() => {
const runEffect = async () => {
try {
// const linkDir = FileSystem.documentDirectory + "linkImage/link.txt";
// await FileSystem.deleteAsync(linkDir);
// const dirInfo = await FileSystem.getInfoAsync(linkDir);
// console.log(dirInfo);
const linkImageArr = async () => {
const linkDir = FileSystem.documentDirectory + "linkImage/";
const linkUri = linkDir + "link.txt";
const check = await ensureDirExist(linkUri);
if (!check) return [];
const links = await FileSystem.readAsStringAsync(linkUri);
const uriArr = links.split(",").filter((value) => value !== "");
return uriArr;
};
result = await linkImageArr();
setArrImage(result);
const takenImageArr = async () => {
let album = await MediaLibrary.getAlbumAsync("ImageTaken");
if (!album) return [];
const imageFiles = await MediaLibrary.getAssetsAsync({
album,
mediaType: "photo",
});
const uriArr = imageFiles.assets.map((asset) => asset.uri);
return uriArr;
};
if (mediaPermission && mediaPermission.granted) {
let result = await takenImageArr();
setArrImage(result);
}
} catch (error) {
console.log(error);
}
};
runEffect();
}, [mediaPermission]);
if (openCamera && cameraPermission.granted && mediaPermission.granted)
return (
<TakeImage
style={styles.camera}
setArrImage={setArrImage}
openCamera={setOpenCamera}
/>
);
return (
<View style={styles.container}>
<InputLink style={styles.inputContainer} setArrImage={setArrImage} />
{arrImage.length ? (
<Slider style={styles.slideContainer} arrImage={arrImage} />
) : (
<Text>There is any image to display</Text>
)}
<Button onPress={handleOpenCamera} title="Open Camera" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: "column",
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
inputContainer: {
flex: 1,
width: "80%",
alignItems: "center",
justifyContent: "center",
},
slideContainer: {
flex: 1,
width: "100%",
flexDirection: "row",
alignItems: "center",
},
camera: {
width: "100%",
height: "100%",
},
});