-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFolderScreen.js
201 lines (173 loc) · 5.62 KB
/
FolderScreen.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import React, {useEffect, useState, memo} from 'react';
import {View, FlatList, TouchableNativeFeedback,PermissionsAndroid,StyleSheet} from 'react-native';
import {Paragraph,Banner} from 'react-native-paper';
import Icon from 'react-native-vector-icons/Entypo';
import {ExternalStorageDirectoryPath, readdir, readDir} from 'react-native-fs';
import DocumentPicker from 'react-native-document-picker';
import RNFetchBlob from 'rn-fetch-blob';
import Navbar from '../Components/Navbar';
import {isVideo, showToastWithGravityAndOffset} from '../Helper';
let VideoFolders = [];
function add(arr, name) {
const {length} = arr;
const found = arr.some(el => el.foldername === name);
if (!found) {
arr.push({selected: 'false', foldername: name});
}
return arr;
}
function ifUnique(arr, name) {
const {length} = arr;
const found = arr.some(el => el.foldername === name);
if (!found) {
return true;
}
return false;
}
function FolderScreen({navigation}) {
const [VideoUniqueFolder, setVideoUniqueFolder] = useState([]);
const [visible, setVisible] = useState(false);
// TODO ask for storage permission
useEffect(() => {
// only goes one level deep rest of folders go undetected
// callback hell
PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE).then(()=>{
PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE && PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE)
.then(response => {
if(response){
readdir(ExternalStorageDirectoryPath).then(allRootFolders => {
allRootFolders.map(eachFolder => {
readDir(`${ExternalStorageDirectoryPath}/${eachFolder}`)
.then(res => {
res.map(eachFile => {
if (eachFile.isFile() && isVideo(eachFile.name)) {
setVideoUniqueFolder(add(VideoFolders, eachFolder));
}
});
//src : https://stackoverflow.com/questions/1960473/get-all-unique-values-in-a-javascript-array-remove-duplicates
})
.catch(err => {
console.log(err)
});
});
});
}
else if(!response){
// if permission is not given show the banner
setVisible(true);
}
})
.catch(err=>{
console.log(err)
})
})
.catch(err=>{
console.log(err)
})
// works but very slow since it looks at entire folder structure
// walkDir(ExternalStorageDirectoryPath,(filepath)=>{
// console.log(filepath)
// })
}, []);
const SelectFolder = async () => {
try {
const res = await DocumentPicker.pick({
type: [DocumentPicker.types.video],
});
const filepath = await RNFetchBlob.fs.stat(res.uri);
// src: https://stackoverflow.com/questions/29496515/get-directory-from-a-file-path-or-url
try {
const selectedPath = filepath.path.split('/').slice(0, -1).join('/');
if (ifUnique(VideoUniqueFolder, selectedPath)) {
setVideoUniqueFolder([
...VideoUniqueFolder,
{selected: 'true', foldername: selectedPath},
]);
} else {
showToastWithGravityAndOffset(
'Folder with this video is already open',
);
// show a toast with message folder already open
}
} catch (err) {
// console.log(err)
}
} catch (err) {
if (DocumentPicker.isCancel(err)) {
// alert("cancelled from doc picker")
} else {
// console.log(JSON.stringify(err))
console.log(err)
}
}
};
const renderItem = ({item}) => {
return (
<TouchableNativeFeedback
onPress={() => {
if (item.selected === 'false') {
navigation.navigate('VideoList', {
path: `${ExternalStorageDirectoryPath}/${item.foldername}`,
});
} else {
navigation.navigate('VideoList', {
path: item.foldername,
});
}
}}>
<View style={styles.spaceBwFolder}>
<Icon name="folder-video" size={90} color="#1aa3ff" />
<Paragraph
numberOfLines={1}
ellipsizeMode="middle"
style={styles.folderText}>
{item.selected
? item.foldername.substring(item.foldername.lastIndexOf('/') + 1)
: item.foldername}
</Paragraph>
</View>
</TouchableNativeFeedback>
);
};
const NoVid = () => {
return <Paragraph>No Videos found in first level from root</Paragraph>;
};
return (
<>
<Navbar openFolderPicker={SelectFolder} />
<Banner
visible={visible}
actions={[
{
label: 'OK',
onPress: () => setVisible(false),
}
]}>
<Paragraph style={styles.bannerText}> Please Provide Storage permission either by restarting the app or through settings</Paragraph>
</Banner>
{/* folder display layout */}
<FlatList
data={VideoUniqueFolder}
renderItem={renderItem}
keyExtractor={item => item.foldername}
numColumns={3}
ListEmptyComponent={NoVid}
/>
</>
);
}
export default memo(FolderScreen);
const styles= StyleSheet.create({
bannerText:{
fontSize:15,
fontStyle:"italic",
fontWeight:"700"
},
folderText:{
fontWeight: 'bold',
textAlign: 'center'
},
spaceBwFolder:{
paddingHorizontal: 18}
})