feat: download playbacks
This commit is contained in:
@@ -1,17 +1,15 @@
|
|||||||
import React, { useEffect, useMemo, useRef, useState } from 'react'
|
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
||||||
import { Image, Platform, Pressable, ScrollView, StyleSheet, Text, View } from 'react-native'
|
import { Platform, Pressable, ScrollView, StyleSheet, Text, View } from 'react-native'
|
||||||
import { MaterialCommunityIcons } from '@expo/vector-icons'
|
import { MaterialCommunityIcons } from '@expo/vector-icons'
|
||||||
import useMinuit from 'react-native-minuit/src/hooks/useMinuit.js'
|
import useMinuit from 'react-native-minuit/src/hooks/useMinuit.js'
|
||||||
import * as FileSystem from 'expo-file-system'
|
import * as FileSystem from 'expo-file-system'
|
||||||
import { shareAsync } from 'expo-sharing'
|
import { shareAsync } from 'expo-sharing'
|
||||||
import { background } from '../../assets'
|
|
||||||
import FullscreenIntroVideo from '../../components/FullscreenIntroVideo'
|
import FullscreenIntroVideo from '../../components/FullscreenIntroVideo'
|
||||||
import BorderGradientButton from '../../components/BorderGradientButton'
|
import BorderGradientButton from '../../components/BorderGradientButton'
|
||||||
import MusicLandHeader from '../../components/MusicLandHeader'
|
import MusicLandHeader from '../../components/MusicLandHeader'
|
||||||
import AppCheckbox from '../../components/AppCheckbox'
|
import AppCheckbox from '../../components/AppCheckbox'
|
||||||
import firebase, { projectsRef, serverTimestamp, videosRef } from '../../config/firebase'
|
import firebase, { projectsRef, serverTimestamp } from '../../config/firebase'
|
||||||
import { uploadFileToFirebase } from '../../helpers/uploadToFirebase'
|
import { uploadFileToFirebase } from '../../helpers/uploadToFirebase'
|
||||||
import useDataFromRef from '../../hooks/useDataFromRef'
|
|
||||||
import Page from '../../layouts/Page'
|
import Page from '../../layouts/Page'
|
||||||
import { Routes } from '../../navigation'
|
import { Routes } from '../../navigation'
|
||||||
import { goBack, navigate } from '../../navigation/NavigationService'
|
import { goBack, navigate } from '../../navigation/NavigationService'
|
||||||
@@ -137,8 +135,9 @@ const uploadSourceRecording = async ({ uri, uid, projectId }) => {
|
|||||||
|
|
||||||
const PlaybackDownload = ({ route }) => {
|
const PlaybackDownload = ({ route }) => {
|
||||||
const { currentUID } = useUserData()
|
const { currentUID } = useUserData()
|
||||||
const { hasActiveSubscription } = useUser() || {}
|
const { hasActiveSubscription, videos } = useUser() || {}
|
||||||
const { action, uri, project } = route.params || {}
|
const { action: routeAction, uri, project } = route.params || {}
|
||||||
|
const action = routeAction || 'playback'
|
||||||
console.log('[PlaybackDownload] route params', {
|
console.log('[PlaybackDownload] route params', {
|
||||||
action,
|
action,
|
||||||
projectId: project?.id,
|
projectId: project?.id,
|
||||||
@@ -150,18 +149,14 @@ const PlaybackDownload = ({ route }) => {
|
|||||||
const [showConfirmModal, setShowConfirmModal] = useState(false)
|
const [showConfirmModal, setShowConfirmModal] = useState(false)
|
||||||
const [pendingPlaybackUrl, setPendingPlaybackUrl] = useState(project?.playbackUrl || null)
|
const [pendingPlaybackUrl, setPendingPlaybackUrl] = useState(project?.playbackUrl || null)
|
||||||
const [isPublishing, setIsPublishing] = useState(false)
|
const [isPublishing, setIsPublishing] = useState(false)
|
||||||
|
const [isDownloading, setIsDownloading] = useState(false)
|
||||||
const [hasAcceptedPublication, setHasAcceptedPublication] = useState(true)
|
const [hasAcceptedPublication, setHasAcceptedPublication] = useState(true)
|
||||||
const hasShownAfterPlaybackRef = useRef(false)
|
const hasShownAfterPlaybackRef = useRef(false)
|
||||||
|
|
||||||
const { data: video } = useDataFromRef({
|
|
||||||
ref: videosRef.doc('fr'),
|
|
||||||
simpleRef: true,
|
|
||||||
})
|
|
||||||
|
|
||||||
const afterPlaybackUrl = useMemo(() => {
|
const afterPlaybackUrl = useMemo(() => {
|
||||||
if (!video) return null
|
if (!videos) return null
|
||||||
return video?.afterPlayback
|
return videos?.afterPlayback || null
|
||||||
}, [video])
|
}, [videos])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (action !== 'playback') return
|
if (action !== 'playback') return
|
||||||
@@ -179,20 +174,66 @@ const PlaybackDownload = ({ route }) => {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
const resolveAudioUrl = useCallback(() => {
|
||||||
|
if (typeof project?.songUrl === 'string' && project.songUrl.trim()) {
|
||||||
|
return project.songUrl.trim()
|
||||||
|
}
|
||||||
|
const urls = Array.isArray(project?.musicUrls) ? project.musicUrls : []
|
||||||
|
if (!urls.length) return null
|
||||||
|
const idx = Number.isFinite(Number(project?.songIndex)) ? Number(project.songIndex) : 0
|
||||||
|
const candidate = urls[idx]
|
||||||
|
if (typeof candidate === 'string' && candidate.trim()) {
|
||||||
|
return candidate.trim()
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}, [project])
|
||||||
|
|
||||||
const handleDownloadUri = async () => {
|
const handleDownloadUri = async () => {
|
||||||
if (isPublishing) return
|
if (isPublishing || isDownloading) return
|
||||||
if (action === 'playback' && project?.id) {
|
if (action !== 'playback') {
|
||||||
if (pendingPlaybackUrl) {
|
setTooltip({
|
||||||
await triggerDownload(pendingPlaybackUrl, project?.title)
|
type: 'error',
|
||||||
|
text: 'Téléchargement indisponible pour cette action',
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Publication du playback
|
|
||||||
console.log('[PlaybackDownload] handleDownloadUri playback', {
|
const playbackUrlToDownload = pendingPlaybackUrl || project?.playbackUrl || null
|
||||||
projectId: project.id,
|
if (playbackUrlToDownload) {
|
||||||
uri,
|
setIsDownloading(true)
|
||||||
currentUID,
|
try {
|
||||||
|
await triggerDownload(playbackUrlToDownload, project?.title)
|
||||||
|
} finally {
|
||||||
|
setIsDownloading(false)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!project?.id) {
|
||||||
|
setTooltip({
|
||||||
|
type: 'error',
|
||||||
|
text: 'Projet introuvable pour ce playback',
|
||||||
})
|
})
|
||||||
const audioUrl = project?.songUrl || null
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!uri) {
|
||||||
|
setTooltip({
|
||||||
|
type: 'error',
|
||||||
|
text: 'Aucune vidéo trouvée pour ce playback',
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!currentUID) {
|
||||||
|
setTooltip({
|
||||||
|
type: 'error',
|
||||||
|
text: 'Utilisateur non authentifié',
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const audioUrl = resolveAudioUrl()
|
||||||
if (!audioUrl) {
|
if (!audioUrl) {
|
||||||
setTooltip({
|
setTooltip({
|
||||||
type: 'error',
|
type: 'error',
|
||||||
@@ -200,8 +241,17 @@ const PlaybackDownload = ({ route }) => {
|
|||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Publication du playback
|
||||||
|
console.log('[PlaybackDownload] handleDownloadUri playback', {
|
||||||
|
projectId: project.id,
|
||||||
|
uri,
|
||||||
|
currentUID,
|
||||||
|
})
|
||||||
|
|
||||||
let tempSourcePath = null
|
let tempSourcePath = null
|
||||||
try {
|
try {
|
||||||
|
setIsDownloading(true)
|
||||||
setIsLoading(true)
|
setIsLoading(true)
|
||||||
const { sourcePath, videoUrl } = await uploadSourceRecording({
|
const { sourcePath, videoUrl } = await uploadSourceRecording({
|
||||||
uri,
|
uri,
|
||||||
@@ -268,12 +318,9 @@ const PlaybackDownload = ({ route }) => {
|
|||||||
})
|
})
|
||||||
} finally {
|
} finally {
|
||||||
releaseBlobUrl(uri || null)
|
releaseBlobUrl(uri || null)
|
||||||
|
setIsDownloading(false)
|
||||||
setIsLoading(false)
|
setIsLoading(false)
|
||||||
}
|
}
|
||||||
// Handle playback download
|
|
||||||
} else {
|
|
||||||
// Handle song download
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const handlePublish = async () => {
|
const handlePublish = async () => {
|
||||||
@@ -299,7 +346,7 @@ const PlaybackDownload = ({ route }) => {
|
|||||||
}
|
}
|
||||||
let tempSourcePath = null
|
let tempSourcePath = null
|
||||||
let playbackUrlToSave = pendingPlaybackUrl || project?.playbackUrl || null
|
let playbackUrlToSave = pendingPlaybackUrl || project?.playbackUrl || null
|
||||||
const audioUrl = project?.songUrl || null
|
const audioUrl = resolveAudioUrl()
|
||||||
if (!audioUrl && !playbackUrlToSave) {
|
if (!audioUrl && !playbackUrlToSave) {
|
||||||
setTooltip({
|
setTooltip({
|
||||||
type: 'error',
|
type: 'error',
|
||||||
@@ -307,6 +354,13 @@ const PlaybackDownload = ({ route }) => {
|
|||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if (!currentUID) {
|
||||||
|
setTooltip({
|
||||||
|
type: 'error',
|
||||||
|
text: 'Utilisateur non authentifié',
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
setIsPublishing(true)
|
setIsPublishing(true)
|
||||||
@@ -417,7 +471,11 @@ const PlaybackDownload = ({ route }) => {
|
|||||||
</View>
|
</View>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
<Pressable style={styles.downloadTile} onPress={handleDownloadUri}>
|
<Pressable
|
||||||
|
style={[styles.downloadTile, (isPublishing || isDownloading) && { opacity: 0.6 }]}
|
||||||
|
onPress={handleDownloadUri}
|
||||||
|
disabled={isPublishing || isDownloading}
|
||||||
|
>
|
||||||
<MaterialCommunityIcons name="download" size={22} color={Palette.white} />
|
<MaterialCommunityIcons name="download" size={22} color={Palette.white} />
|
||||||
<Text style={styles.downloadText}>Télécharger le playback</Text>
|
<Text style={styles.downloadText}>Télécharger le playback</Text>
|
||||||
</Pressable>
|
</Pressable>
|
||||||
|
|||||||
Reference in New Issue
Block a user