Allow images as well as videos
This commit is contained in:
		
							parent
							
								
									f9ea32573e
								
							
						
					
					
						commit
						237cd64887
					
				
							
								
								
									
										46
									
								
								App.tsx
									
									
									
									
									
								
							
							
						
						
									
										46
									
								
								App.tsx
									
									
									
									
									
								
							|  | @ -4,8 +4,8 @@ import DocumentPicker, { | ||||||
|   DocumentPickerResponse, |   DocumentPickerResponse, | ||||||
|   types, |   types, | ||||||
| } from 'react-native-document-picker' | } from 'react-native-document-picker' | ||||||
| import Video from 'react-native-video' |  | ||||||
| import {backgroundColor} from './constants' | import {backgroundColor} from './constants' | ||||||
|  | import Item from './Item' | ||||||
| 
 | 
 | ||||||
| const buttonWidth = '45%' | const buttonWidth = '45%' | ||||||
| 
 | 
 | ||||||
|  | @ -13,12 +13,11 @@ export default function App() { | ||||||
|   const [files, setFiles] = useState<DocumentPickerResponse[]>([]) |   const [files, setFiles] = useState<DocumentPickerResponse[]>([]) | ||||||
|   const [selected, setSelected] = useState(0) |   const [selected, setSelected] = useState(0) | ||||||
|   const [paused, setPaused] = useState(false) |   const [paused, setPaused] = useState(false) | ||||||
|   const [width, setWidth] = useState(0) |  | ||||||
| 
 |  | ||||||
|   const height = Dimensions.get('screen').height |  | ||||||
| 
 | 
 | ||||||
|   const pick = async () => { |   const pick = async () => { | ||||||
|     const picked = await DocumentPicker.pickMultiple({type: types.video}) |     const picked = await DocumentPicker.pickMultiple({ | ||||||
|  |       type: [types.video, types.images], | ||||||
|  |     }) | ||||||
|     setSelected(0) |     setSelected(0) | ||||||
|     console.log(`Picked ${picked.length} files.`) |     console.log(`Picked ${picked.length} files.`) | ||||||
|     setFiles(picked) |     setFiles(picked) | ||||||
|  | @ -47,29 +46,14 @@ export default function App() { | ||||||
|         padding: 10, |         padding: 10, | ||||||
|       }}> |       }}> | ||||||
|       {files.length > 0 ? ( |       {files.length > 0 ? ( | ||||||
|         <View style={{alignItems: 'center'}}> |         <> | ||||||
|           <Pressable onLongPress={pick} onPress={() => setPaused(!paused)}> |           <Item | ||||||
|             <Video |             paused={paused} | ||||||
|               paused={paused} |             setPaused={setPaused} | ||||||
|               style={{ |             onLongPress={pick} | ||||||
|                 height: height - height * 0.25, |             selected={selected} | ||||||
|                 width, |             files={files} | ||||||
|                 alignItems: 'center', |           /> | ||||||
|               }} |  | ||||||
|               source={{uri: files[selected].uri}} |  | ||||||
|               resizeMode="contain" |  | ||||||
|               onLoad={response => { |  | ||||||
|                 setWidth(response.naturalSize.width) |  | ||||||
|               }} |  | ||||||
|             /> |  | ||||||
|           </Pressable> |  | ||||||
|           <Text |  | ||||||
|             style={{ |  | ||||||
|               color: 'white', |  | ||||||
|               margin: 10, |  | ||||||
|             }}> |  | ||||||
|             {files[selected].name} ({selected + 1}/ {files.length}) |  | ||||||
|           </Text> |  | ||||||
|           <View style={{flexDirection: 'row', justifyContent: 'space-around'}}> |           <View style={{flexDirection: 'row', justifyContent: 'space-around'}}> | ||||||
|             <View style={{width: buttonWidth, marginRight: 10}}> |             <View style={{width: buttonWidth, marginRight: 10}}> | ||||||
|               <Button title="Prev" disabled={selected <= 0} onPress={prev} /> |               <Button title="Prev" disabled={selected <= 0} onPress={prev} /> | ||||||
|  | @ -82,9 +66,11 @@ export default function App() { | ||||||
|               /> |               /> | ||||||
|             </View> |             </View> | ||||||
|           </View> |           </View> | ||||||
|         </View> |         </> | ||||||
|       ) : ( |       ) : ( | ||||||
|         <Pressable onPress={pick}> |         <Pressable | ||||||
|  |           onPress={pick} | ||||||
|  |           style={{height: '100%', justifyContent: 'center'}}> | ||||||
|           <Text style={{color: 'white', fontSize: 24}}>Select video(s)...</Text> |           <Text style={{color: 'white', fontSize: 24}}>Select video(s)...</Text> | ||||||
|         </Pressable> |         </Pressable> | ||||||
|       )} |       )} | ||||||
|  |  | ||||||
							
								
								
									
										70
									
								
								Item.tsx
									
									
									
									
									
								
							
							
						
						
									
										70
									
								
								Item.tsx
									
									
									
									
									
								
							|  | @ -1,42 +1,66 @@ | ||||||
| import {useState} from 'react' | import {useMemo, useState} from 'react' | ||||||
| import {Dimensions, Pressable, Text, View} from 'react-native' | import {Dimensions, Image, Pressable, Text, View} from 'react-native' | ||||||
| import {DocumentPickerResponse} from 'react-native-document-picker' | import {DocumentPickerResponse, types} from 'react-native-document-picker' | ||||||
| import Video from 'react-native-video' | import Video from 'react-native-video' | ||||||
| 
 | 
 | ||||||
| export default function Item({ | export default function Item({ | ||||||
|   value, |   files, | ||||||
|   onLongPress, |   onLongPress, | ||||||
|  |   selected, | ||||||
|  |   paused, | ||||||
|  |   setPaused, | ||||||
| }: { | }: { | ||||||
|   value: DocumentPickerResponse |   files: DocumentPickerResponse[] | ||||||
|   onLongPress: () => void |   onLongPress: () => void | ||||||
|  |   selected: number | ||||||
|  |   paused: boolean | ||||||
|  |   setPaused: (value: boolean) => void | ||||||
| }) { | }) { | ||||||
|   const [paused, setPaused] = useState(false) |  | ||||||
|   const [width, setWidth] = useState(0) |   const [width, setWidth] = useState(0) | ||||||
|  |   const screenHeight = Dimensions.get('screen').height | ||||||
|  |   const file = useMemo(() => files[selected], [files, selected]) | ||||||
| 
 | 
 | ||||||
|   const height = Dimensions.get('screen').height |   const video = ( | ||||||
|  |     <Pressable onLongPress={onLongPress} onPress={() => setPaused(!paused)}> | ||||||
|  |       <Video | ||||||
|  |         paused={paused} | ||||||
|  |         style={{ | ||||||
|  |           height: screenHeight - screenHeight * 0.25, | ||||||
|  |           width, | ||||||
|  |           alignItems: 'center', | ||||||
|  |         }} | ||||||
|  |         source={{uri: file.uri}} | ||||||
|  |         resizeMode="contain" | ||||||
|  |         onLoad={response => { | ||||||
|  |           setWidth(response.naturalSize.width) | ||||||
|  |         }} | ||||||
|  |       /> | ||||||
|  |     </Pressable> | ||||||
|  |   ) | ||||||
|  | 
 | ||||||
|  |   const image = ( | ||||||
|  |     <Pressable onLongPress={onLongPress}> | ||||||
|  |       <Image | ||||||
|  |         source={{ | ||||||
|  |           uri: file.uri, | ||||||
|  |           width: 300, | ||||||
|  |           height: screenHeight - screenHeight * 0.25, | ||||||
|  |         }} | ||||||
|  |         resizeMode="contain" | ||||||
|  |         onLoad={({nativeEvent}) => setWidth(nativeEvent.source.width)} | ||||||
|  |       /> | ||||||
|  |     </Pressable> | ||||||
|  |   ) | ||||||
| 
 | 
 | ||||||
|   return ( |   return ( | ||||||
|     <View style={{alignItems: 'center'}}> |     <View style={{alignItems: 'center'}}> | ||||||
|       <Pressable onLongPress={onLongPress} onPress={() => setPaused(!paused)}> |       {file.type?.includes('video') ? video : image} | ||||||
|         <Video |  | ||||||
|           paused={paused} |  | ||||||
|           style={{ |  | ||||||
|             height: height - height * 0.25, |  | ||||||
|             width, |  | ||||||
|             alignItems: 'center', |  | ||||||
|           }} |  | ||||||
|           source={{uri: value.uri}} |  | ||||||
|           resizeMode="contain" |  | ||||||
|           onLoad={response => { |  | ||||||
|             setWidth(response.naturalSize.width) |  | ||||||
|           }} |  | ||||||
|         /> |  | ||||||
|       </Pressable> |  | ||||||
|       <Text |       <Text | ||||||
|         style={{ |         style={{ | ||||||
|           color: 'white', |           color: 'white', | ||||||
|  |           margin: 10, | ||||||
|         }}> |         }}> | ||||||
|         {value.name} |         {file.name} ({selected + 1}/ {files.length}) | ||||||
|       </Text> |       </Text> | ||||||
|     </View> |     </View> | ||||||
|   ) |   ) | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user