2022-12-18 18:32:44 +13:00
|
|
|
import {useState} from 'react'
|
2022-12-18 19:55:56 +13:00
|
|
|
import {Button, Dimensions, Pressable, Text, View} from 'react-native'
|
2022-12-15 16:31:35 +13:00
|
|
|
import DocumentPicker, {
|
|
|
|
DocumentPickerResponse,
|
2022-12-18 19:55:56 +13:00
|
|
|
types,
|
2022-12-18 18:32:44 +13:00
|
|
|
} from 'react-native-document-picker'
|
2022-12-18 19:55:56 +13:00
|
|
|
import Video from 'react-native-video'
|
2022-12-18 18:32:44 +13:00
|
|
|
import {backgroundColor} from './constants'
|
2022-12-15 16:31:35 +13:00
|
|
|
|
2022-12-18 20:01:00 +13:00
|
|
|
const buttonWidth = '45%'
|
|
|
|
|
2022-12-15 16:31:35 +13:00
|
|
|
export default function App() {
|
2022-12-18 18:32:44 +13:00
|
|
|
const [files, setFiles] = useState<DocumentPickerResponse[]>([])
|
2022-12-18 19:55:56 +13:00
|
|
|
const [selected, setSelected] = useState(0)
|
|
|
|
const [paused, setPaused] = useState(false)
|
|
|
|
const [width, setWidth] = useState(0)
|
|
|
|
|
|
|
|
const height = Dimensions.get('screen').height
|
2022-12-15 16:31:35 +13:00
|
|
|
|
|
|
|
const pick = async () => {
|
2022-12-18 19:55:56 +13:00
|
|
|
const picked = await DocumentPicker.pickMultiple({type: types.video})
|
|
|
|
setSelected(0)
|
2022-12-18 18:32:44 +13:00
|
|
|
console.log(`Picked ${picked.length} files.`)
|
|
|
|
setFiles(picked)
|
|
|
|
}
|
2022-12-15 16:31:35 +13:00
|
|
|
|
2022-12-18 19:55:56 +13:00
|
|
|
const prev = () => {
|
|
|
|
if (selected <= 0) return
|
|
|
|
setSelected(selected - 1)
|
|
|
|
setPaused(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
const next = () => {
|
|
|
|
if (selected >= files.length - 1) return
|
|
|
|
setSelected(selected + 1)
|
|
|
|
setPaused(false)
|
2022-12-18 18:32:44 +13:00
|
|
|
}
|
2022-12-15 16:31:35 +13:00
|
|
|
|
|
|
|
return (
|
2022-12-18 18:32:44 +13:00
|
|
|
<View
|
|
|
|
style={{
|
|
|
|
backgroundColor,
|
2022-12-18 20:01:00 +13:00
|
|
|
justifyContent: 'center',
|
|
|
|
alignItems: 'center',
|
2022-12-18 18:32:44 +13:00
|
|
|
flex: 1,
|
|
|
|
flexGrow: 1,
|
|
|
|
padding: 10,
|
|
|
|
}}>
|
2022-12-18 19:55:56 +13:00
|
|
|
{files.length > 0 ? (
|
|
|
|
<View style={{alignItems: 'center'}}>
|
|
|
|
<Pressable onLongPress={pick} onPress={() => setPaused(!paused)}>
|
|
|
|
<Video
|
|
|
|
paused={paused}
|
|
|
|
style={{
|
|
|
|
height: height - height * 0.25,
|
|
|
|
width,
|
|
|
|
alignItems: 'center',
|
|
|
|
}}
|
|
|
|
source={{uri: files[selected].uri}}
|
|
|
|
resizeMode="contain"
|
|
|
|
onLoad={response => {
|
|
|
|
setWidth(response.naturalSize.width)
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</Pressable>
|
|
|
|
<Text
|
|
|
|
style={{
|
|
|
|
color: 'white',
|
2022-12-18 20:01:00 +13:00
|
|
|
margin: 10,
|
2022-12-18 19:55:56 +13:00
|
|
|
}}>
|
|
|
|
{files[selected].name} ({selected + 1}/ {files.length})
|
|
|
|
</Text>
|
2022-12-18 20:01:00 +13:00
|
|
|
<View style={{flexDirection: 'row', justifyContent: 'space-around'}}>
|
|
|
|
<View style={{width: buttonWidth, marginRight: 10}}>
|
|
|
|
<Button title="Prev" disabled={selected <= 0} onPress={prev} />
|
|
|
|
</View>
|
|
|
|
<View style={{width: buttonWidth}}>
|
|
|
|
<Button
|
|
|
|
title="Next"
|
|
|
|
disabled={selected >= files.length - 1}
|
|
|
|
onPress={next}
|
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
</View>
|
2022-12-18 19:55:56 +13:00
|
|
|
</View>
|
|
|
|
) : (
|
2022-12-18 20:01:00 +13:00
|
|
|
<View style={{width: buttonWidth}}>
|
|
|
|
<Button title="Pick" onPress={pick} />
|
2022-12-18 19:55:56 +13:00
|
|
|
</View>
|
2022-12-18 20:01:00 +13:00
|
|
|
)}
|
2022-12-15 16:31:35 +13:00
|
|
|
</View>
|
2022-12-18 18:32:44 +13:00
|
|
|
)
|
2022-12-15 16:31:35 +13:00
|
|
|
}
|