IO-1165 Add video playback support.

This commit is contained in:
Patrick Fic
2021-06-10 13:51:59 -07:00
parent b857a9bfbd
commit 1d4fcb303b
4 changed files with 59 additions and 42 deletions

View File

@@ -1,4 +1,3 @@
import { Video } from "expo-av";
import React, { useMemo, useState } from "react";
import {
FlatList,
@@ -11,15 +10,8 @@ import {
import env from "../../env";
import { DetermineFileType } from "../../util/document-upload.utility";
import MediaCacheOverlay from "../media-cache-overlay/media-cache-overlay.component";
export default function JobDocumentsComponent({ job, loading, refetch }) {
const [previewVisible, setPreviewVisible] = useState(false);
const [videoUri, setVideoUri] = useState(null);
const [status, setStatus] = React.useState({});
console.log(
"🚀 ~ file: job-documents.component.jsx ~ line 19 ~ status",
status
);
const [imgIndex, setImgIndex] = useState(0);
const onRefresh = async () => {
@@ -30,9 +22,12 @@ export default function JobDocumentsComponent({ job, loading, refetch }) {
() =>
job.documents.map((doc) => {
return {
source: {
uri: GenerateSrcUrl(doc),
},
videoUrl:
DetermineFileType(doc.type) === "video" && GenerateSrcUrl(doc),
source:
DetermineFileType(doc.type) === "video"
? { uri: GenerateThumbUrl(doc) }
: { uri: GenerateSrcUrl(doc) },
};
}),
[job.documents]
@@ -50,16 +45,10 @@ export default function JobDocumentsComponent({ job, loading, refetch }) {
keyExtractor={(item) => item.id}
renderItem={(object) => (
<TouchableOpacity
style={{ flex: 1 / 4, aspectRatio: 1 }}
onPress={() => {
if (DetermineFileType(object.item.type) === "image") {
//If image
setImgIndex(object.index);
setPreviewVisible(true);
} else {
//If Video
setVideoUri(GenerateSrcUrl(object.item));
}
style={{ flex: 1 / 4, aspectRatio: 1, margin: 4 }}
onPress={async () => {
setImgIndex(object.index);
setPreviewVisible(true);
}}
>
<Image
@@ -74,18 +63,7 @@ export default function JobDocumentsComponent({ job, loading, refetch }) {
)}
/>
<Text>{job.documents.length}</Text>
<Video
source={
videoUri
? {
uri: videoUri,
}
: null
}
useNativeControls
isLooping
onPlaybackStatusUpdate={(status) => setStatus(() => status)}
/>
<MediaCacheOverlay
photos={fullphotos}
imgIndex={imgIndex}
@@ -99,7 +77,7 @@ export default function JobDocumentsComponent({ job, loading, refetch }) {
export const GenerateSrcUrl = (value) => {
let extension = value.extension;
if (extension && extension.includes("heic")) extension = "jpg";
if (extension && extension.toLowerCase().includes("heic")) extension = "jpg";
return `${env.REACT_APP_CLOUDINARY_ENDPOINT}/${DetermineFileType(
value.type

View File

@@ -1,7 +1,14 @@
import { Ionicons } from "@expo/vector-icons";
import React from "react";
import { Modal, SafeAreaView, TouchableOpacity, View } from "react-native";
import { Video } from "expo-av";
import React, { useState } from "react";
import {
Dimensions,
Modal,
SafeAreaView,
TouchableOpacity,
} from "react-native";
import Gallery from "react-native-image-gallery";
export default function MediaCacheOverlay({
photos,
previewVisible,
@@ -9,6 +16,11 @@ export default function MediaCacheOverlay({
imgIndex,
setImgIndex,
}) {
const [currentIndex, setcurrentIndex] = useState(0);
const [dragging, setDragging] = useState(false);
const videoRef = React.useRef(null);
return (
<Modal
onDismiss={() => setPreviewVisible(false)}
@@ -17,7 +29,14 @@ export default function MediaCacheOverlay({
transparent={false}
>
<SafeAreaView style={{ flex: 1, backgroundColor: "black" }}>
<Gallery initialPage={imgIndex} images={photos} />
<Gallery
initialPage={imgIndex}
images={photos}
onPageScroll={({ position }) => setcurrentIndex(position)}
onPageScrollStateChanged={(state) =>
state === "idle" ? setDragging(false) : setDragging(true)
}
/>
<TouchableOpacity
style={{ position: "absolute" }}
onPress={() => setPreviewVisible(false)}
@@ -29,7 +48,29 @@ export default function MediaCacheOverlay({
style={{ margin: 20 }}
/>
</TouchableOpacity>
{!dragging && photos[currentIndex] && photos[currentIndex].videoUrl && (
<TouchableOpacity
style={{
position: "absolute",
left: Dimensions.get("window").width / 2 - 32,
top: Dimensions.get("window").height / 2 - 32,
justifyContent: "center",
alignItems: "center",
}}
onPress={async () => {
await videoRef.current.loadAsync(
{ uri: photos[currentIndex].videoUrl },
{},
false
);
videoRef.current.presentFullscreenPlayer();
}}
>
<Ionicons name="play" size={64} color="white" />
</TouchableOpacity>
)}
</SafeAreaView>
<Video ref={videoRef} useNativeControls />
</Modal>
);
}

View File

@@ -154,7 +154,6 @@ export function UploadProgress({
//Sequentially await the proms.
for (var i = 0; i < data.length + 4; i = i + 4) {
console.log("i :>> ", i);
let proms = [];
if (data[i]) {
proms.push(CreateUploadProm(data[i]));
@@ -168,9 +167,8 @@ export function UploadProgress({
if (data[i + 3]) {
proms.push(CreateUploadProm(data[i + 3]));
}
console.log("Proms Length", proms.length);
await Promise.all(proms);
console.log("Await done.");
}
if (deleteAfterUpload) {
@@ -196,7 +194,6 @@ export function UploadProgress({
const CreateUploadProm = async (p) => {
let filename;
filename = p.filename || p.uri.split("/").pop();
console.log("Start upping.", p.id);
await handleUpload(
{
@@ -214,7 +211,7 @@ export function UploadProgress({
photo: p,
}
);
console.log("Done upping.", p.id);
//Set the state to mark that it's done.
setProgress((progress) => ({
...progress,