@@ -57,30 +57,31 @@ const screenHeight = vh(100);
5757const fullScreenHeight = Dimensions . get ( 'window' ) . height ;
5858
5959type AttachmentImageProps = {
60+ asset : Asset ;
6061 ImageOverlaySelectedComponent : React . ComponentType ;
61- onPress : ( ) => void ;
62+ maxNumberOfFiles : number ;
6263 selected : boolean ;
63- uri : string ;
64+ setSelectedImages : React . Dispatch < React . SetStateAction < Asset [ ] > > ;
6465 numberOfAttachmentPickerImageColumns ?: number ;
6566} ;
6667
6768type AttachmentVideoProps = {
69+ asset : Asset ;
6870 ImageOverlaySelectedComponent : React . ComponentType ;
69- onPress : ( ) => void ;
71+ maxNumberOfFiles : number ;
7072 selected : boolean ;
71- uri : string ;
72- videoDuration : string | null ;
73+ setSelectedFiles : React . Dispatch < React . SetStateAction < File [ ] > > ;
7374 numberOfAttachmentPickerImageColumns ?: number ;
7475} ;
7576
7677const AttachmentVideo : React . FC < AttachmentVideoProps > = ( props ) => {
7778 const {
79+ asset,
7880 ImageOverlaySelectedComponent,
81+ maxNumberOfFiles,
7982 numberOfAttachmentPickerImageColumns,
80- onPress,
8183 selected,
82- uri,
83- videoDuration,
84+ setSelectedFiles,
8485 } = props ;
8586
8687 const {
@@ -90,10 +91,49 @@ const AttachmentVideo: React.FC<AttachmentVideoProps> = (props) => {
9091 } ,
9192 } = useTheme ( ) ;
9293
94+ const { duration, playableDuration, uri } = asset ;
95+
96+ const videoDuration = duration ? duration : playableDuration ;
97+
98+ const ONE_HOUR_IN_SECONDS = 3600 ;
99+
100+ let durationLabel = '00:00' ;
101+
102+ if ( videoDuration ) {
103+ const isDurationLongerThanHour = videoDuration / ONE_HOUR_IN_SECONDS >= 1 ;
104+ const formattedDurationParam = isDurationLongerThanHour ? 'HH:mm:ss' : 'mm:ss' ;
105+ const formattedVideoDuration = dayjs
106+ . duration ( videoDuration , 'second' )
107+ . format ( formattedDurationParam ) ;
108+ durationLabel = formattedVideoDuration ;
109+ }
110+
93111 const size = vw ( 100 ) / ( numberOfAttachmentPickerImageColumns || 3 ) - 2 ;
94112
113+ const onPressVideo = ( ) => {
114+ if ( selected ) {
115+ setSelectedFiles ( ( files ) => files . filter ( ( file ) => file . uri !== asset . uri ) ) ;
116+ } else {
117+ setSelectedFiles ( ( files ) => {
118+ if ( files . length >= maxNumberOfFiles ) {
119+ return files ;
120+ }
121+ return [
122+ ...files ,
123+ {
124+ duration : durationLabel ,
125+ name : asset . filename ,
126+ size : asset . fileSize ,
127+ type : 'video' ,
128+ uri : asset . uri ,
129+ } ,
130+ ] ;
131+ } ) ;
132+ }
133+ } ;
134+
95135 return (
96- < TouchableOpacity onPress = { onPress } >
136+ < TouchableOpacity onPress = { onPressVideo } >
97137 < ImageBackground
98138 source = { { uri } }
99139 style = { [
@@ -114,7 +154,7 @@ const AttachmentVideo: React.FC<AttachmentVideoProps> = (props) => {
114154 < Recorder height = { 20 } pathFill = { white } width = { 25 } />
115155 { videoDuration ? (
116156 < Text style = { [ styles . durationText , durationText , { color : white } ] } >
117- { videoDuration }
157+ { durationLabel }
118158 </ Text >
119159 ) : null }
120160 </ View >
@@ -125,11 +165,12 @@ const AttachmentVideo: React.FC<AttachmentVideoProps> = (props) => {
125165
126166const AttachmentImage : React . FC < AttachmentImageProps > = ( props ) => {
127167 const {
168+ asset,
128169 ImageOverlaySelectedComponent,
170+ maxNumberOfFiles,
129171 numberOfAttachmentPickerImageColumns,
130- onPress,
131172 selected,
132- uri ,
173+ setSelectedImages ,
133174 } = props ;
134175 const {
135176 theme : {
@@ -140,8 +181,23 @@ const AttachmentImage: React.FC<AttachmentImageProps> = (props) => {
140181
141182 const size = vw ( 100 ) / ( numberOfAttachmentPickerImageColumns || 3 ) - 2 ;
142183
184+ const { uri } = asset ;
185+
186+ const onPressImage = ( ) => {
187+ if ( selected ) {
188+ setSelectedImages ( ( images ) => images . filter ( ( image ) => image . uri !== asset . uri ) ) ;
189+ } else {
190+ setSelectedImages ( ( images ) => {
191+ if ( images . length >= maxNumberOfFiles ) {
192+ return images ;
193+ }
194+ return [ ...images , asset ] ;
195+ } ) ;
196+ }
197+ } ;
198+
143199 return (
144- < TouchableOpacity onPress = { onPress } >
200+ < TouchableOpacity onPress = { onPressImage } >
145201 < ImageBackground
146202 source = { { uri } }
147203 style = { [
@@ -196,72 +252,23 @@ const renderImage = ({
196252 ? 'video'
197253 : 'image' ;
198254
199- const videoDuration = asset . duration ? asset . duration : asset . playableDuration ;
200-
201- const ONE_HOUR_IN_SECONDS = 3600 ;
202-
203- let duration = '00:00' ;
204-
205- if ( videoDuration ) {
206- const isDurationLongerThanHour = videoDuration / ONE_HOUR_IN_SECONDS >= 1 ;
207- const formattedDurationParam = isDurationLongerThanHour ? 'HH:mm:ss' : 'mm:ss' ;
208- const formattedVideoDuration = dayjs
209- . duration ( videoDuration , 'second' )
210- . format ( formattedDurationParam ) ;
211- duration = formattedVideoDuration ;
212- }
213-
214- const onPressImage = ( ) => {
215- if ( selected ) {
216- setSelectedImages ( ( images ) => images . filter ( ( image ) => image . uri !== asset . uri ) ) ;
217- } else {
218- setSelectedImages ( ( images ) => {
219- if ( images . length >= maxNumberOfFiles ) {
220- return images ;
221- }
222- return [ ...images , asset ] ;
223- } ) ;
224- }
225- } ;
226-
227- const onPressVideo = ( ) => {
228- if ( selected ) {
229- setSelectedFiles ( ( files ) => files . filter ( ( file ) => file . uri !== asset . uri ) ) ;
230- } else {
231- setSelectedFiles ( ( files ) => {
232- if ( files . length >= maxNumberOfFiles ) {
233- return files ;
234- }
235- return [
236- ...files ,
237- {
238- duration,
239- name : asset . filename ,
240- size : asset . fileSize ,
241- type : 'video' ,
242- uri : asset . uri ,
243- } ,
244- ] ;
245- } ) ;
246- }
247- } ;
248-
249255 return fileType === 'image' ? (
250256 < AttachmentImage
257+ asset = { asset }
251258 ImageOverlaySelectedComponent = { ImageOverlaySelectedComponent }
259+ maxNumberOfFiles = { maxNumberOfFiles }
252260 numberOfAttachmentPickerImageColumns = { numberOfAttachmentPickerImageColumns }
253- onPress = { onPressImage }
254261 selected = { selected }
255- uri = { asset . uri }
262+ setSelectedImages = { setSelectedImages }
256263 />
257264 ) : (
258265 < AttachmentVideo
266+ asset = { asset }
259267 ImageOverlaySelectedComponent = { ImageOverlaySelectedComponent }
268+ maxNumberOfFiles = { maxNumberOfFiles }
260269 numberOfAttachmentPickerImageColumns = { numberOfAttachmentPickerImageColumns }
261- onPress = { onPressVideo }
262270 selected = { selected }
263- uri = { asset . uri }
264- videoDuration = { duration }
271+ setSelectedFiles = { setSelectedFiles }
265272 />
266273 ) ;
267274} ;
0 commit comments