Swiper/App.tsx

94 lines
2.6 KiB
TypeScript
Raw Normal View History

2022-12-18 18:32:44 +13:00
import {useState} from 'react'
import {Button, Dimensions, Pressable, Text, View} from 'react-native'
import DocumentPicker, {
DocumentPickerResponse,
types,
2022-12-18 18:32:44 +13:00
} from 'react-native-document-picker'
import Video from 'react-native-video'
2022-12-18 18:32:44 +13:00
import {backgroundColor} from './constants'
2022-12-18 20:01:00 +13:00
const buttonWidth = '45%'
export default function App() {
2022-12-18 18:32:44 +13:00
const [files, setFiles] = useState<DocumentPickerResponse[]>([])
const [selected, setSelected] = useState(0)
const [paused, setPaused] = useState(false)
const [width, setWidth] = useState(0)
const height = Dimensions.get('screen').height
const pick = async () => {
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)
}
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
}
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,
}}>
{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,
}}>
{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>
</View>
) : (
2022-12-18 22:25:18 +13:00
<Pressable onPress={pick}>
<Text style={{color: 'white', fontSize: 24}}>Select video(s)...</Text>
</Pressable>
2022-12-18 20:01:00 +13:00
)}
</View>
2022-12-18 18:32:44 +13:00
)
}