-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVideoList.js
239 lines (223 loc) · 6.29 KB
/
VideoList.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import React, {useEffect, useState, memo} from 'react';
import {
Image,
View,
TouchableNativeFeedback,
Platform,
FlatList,
StyleSheet,
} from 'react-native';
import {readDir, unlink} from 'react-native-fs';
import filter from 'lodash.filter';
import {isVideo, showToastWithGravityAndOffset, formatBytes} from '../Helper';
import {
Paragraph,
Portal,
Dialog,
Button,
Subheading,
} from 'react-native-paper';
import {OrientationLocker, PORTRAIT} from 'react-native-orientation-locker';
import ListAppbar from '../Components/ListAppbar';
let VidListArray;
// {route.params.path}
function VideoList({route, navigation}) {
const [FileList, setFile] = useState([]);
const [ShowFilteredData, setShowFilteredData] = useState(false);
const [FilteredData, setFilteredData] = useState([]);
const [SelectedCopy, setSelectedCopy] = useState(false);
const [visible, setVisible] = useState(false);
const [FileInfo, setFileInfo] = useState(null);
const showModal = () => setVisible(true);
const hideDialog = () => setVisible(false);
useEffect(() => {
VidListArray = [];
readDir(route.params.path)
.then(res => {
res.map((eachFile, index) => {
if (isVideo(eachFile.name) && eachFile.isFile()) {
eachFile['id'] = String(index);
VidListArray.push(eachFile);
}
});
setFile(VidListArray);
})
.catch(err => {
// console.log(err);
});
}, []);
const multiListSelect = id => {
if (!ShowFilteredData) {
let renderData = [...FileList];
for (let data of renderData) {
if (data.id == id) {
data.selected = data.selected == null ? true : !data.selected;
break;
}
}
setFile(renderData);
} else {
showToastWithGravityAndOffset(
'Cannot select files when search is in progress',
);
}
};
const renderVideoItem = ({item}) => {
return (
<TouchableNativeFeedback
background={
Platform.OS === 'android'
? TouchableNativeFeedback.SelectableBackground()
: ''
}
onPress={() => {
if (!SelectedCopy) {
//selected for deletion
navigation.navigate('Player', {
uri: item.path,
filename: item.name,
});
} else {
multiListSelect(item.id);
}
}}
onLongPress={() => {
setFileInfo(item);
showModal();
}}>
<View
style={[
styles.card,
item.selected && SelectedCopy ? styles.selectedCard : null,
]}>
<Image
source={{uri: `file://${item.path}`}}
resizeMode="cover"
style={styles.image}
/>
<Paragraph numberOfLines={3}> {item.name}</Paragraph>
</View>
</TouchableNativeFeedback>
);
};
const EmptyVideo = () => {
return <Paragraph>No video Found in this folder</Paragraph>;
};
const FilterChange = data => {
setFilteredData(data);
setShowFilteredData(true);
};
const OnSort = data => {
setFile(data);
};
const onSearchBarClosed = ShowSearch => {
setShowFilteredData(!ShowSearch);
};
const OnselectCopy = () => {
setSelectedCopy(!SelectedCopy);
};
const OnDeleteFiles = () => {
// start deleting files here
let renderData = [...FileList];
const FilteredData = filter(renderData, EachVidData => {
if (EachVidData.selected) {
unlink(EachVidData.path)
.then(() => {
return false;
})
.catch(err => {
// console.log(err.message)
});
} else {
return true;
}
});
setFile(FilteredData);
// setFile(FilteredData);
};
const getDate = str => {
return String(new Date(str));
};
return (
<View style={{flex: 1}}>
<Portal>
<Dialog visible={visible} onDismiss={hideDialog}>
<Dialog.Title>File info</Dialog.Title>
<Dialog.Content>
{FileInfo ? (
<>
<Subheading style={styles.DialogContentStyle}>Name:</Subheading>
<Paragraph>{FileInfo.name}</Paragraph>
<Subheading style={styles.DialogContentStyle}>
FileSize:
</Subheading>
<Paragraph>{formatBytes(FileInfo.size)}</Paragraph>
<Subheading
style={[styles.DialogContentStyle, {fontStyle: 'italic'}]}>
PATH:
</Subheading>
<Paragraph>{FileInfo.path}</Paragraph>
<Subheading style={styles.DialogContentStyle}>
LAST MODIFIED TIME:
</Subheading>
<Paragraph>{getDate(FileInfo.mtime)}</Paragraph>
</>
) : (
<Paragraph>Something went wrong</Paragraph>
)}
<Paragraph></Paragraph>
</Dialog.Content>
<Dialog.Actions>
<Button onPress={hideDialog}>OK</Button>
</Dialog.Actions>
</Dialog>
</Portal>
<OrientationLocker orientation={PORTRAIT} />
<ListAppbar
FileList={FileList}
onFilterChange={FilterChange}
onSearchBarClosed={onSearchBarClosed}
OnSort={OnSort}
OnselectCopy={OnselectCopy}
OnDeleteFiles={OnDeleteFiles}
/>
<FlatList
// src: https://stackoverflow.com/questions/51742856/sorting-react-native-flatlist/51743104 in next line
data={!ShowFilteredData ? FileList : FilteredData}
renderItem={renderVideoItem}
keyExtractor={item => item.id}
numColumns={2}
ListEmptyComponent={EmptyVideo}
removeClippedSubviews={true}
maxToRenderPerBatch={15}
getItemLayout={(data, index) => ({
length: 160,
offset: 160 * index,
index,
})}
/>
</View>
);
}
export default memo(VideoList);
const styles = StyleSheet.create({
card: {
width: 170,
height: 160,
margin: 10,
justifyContent: 'center',
},
image: {
height: 110,
borderRadius: 10,
},
selectedCard: {
backgroundColor: 'rgba(0, 0, 1, .4)',
},
DialogContentStyle: {
fontWeight: '800',
fontSize: 17,
textDecorationLine: 'underline',
paddingTop: 8,
},
});