changes to handle staged videos

This commit is contained in:
Samuel Newman
2025-07-07 11:58:48 +03:00
parent 34291131b4
commit ab9fe32688
3 changed files with 97 additions and 49 deletions
@@ -13,7 +13,6 @@ import {Mute_Stroke2_Corner0_Rounded as MuteIcon} from '#/components/icons/Mute'
import {Pause_Filled_Corner0_Rounded as PauseIcon} from '#/components/icons/Pause' import {Pause_Filled_Corner0_Rounded as PauseIcon} from '#/components/icons/Pause'
import {Play_Filled_Corner0_Rounded as PlayIcon} from '#/components/icons/Play' import {Play_Filled_Corner0_Rounded as PlayIcon} from '#/components/icons/Play'
import {SpeakerVolumeFull_Stroke2_Corner0_Rounded as UnmuteIcon} from '#/components/icons/Speaker' import {SpeakerVolumeFull_Stroke2_Corner0_Rounded as UnmuteIcon} from '#/components/icons/Speaker'
import {MediaInsetBorder} from '#/components/MediaInsetBorder'
import {useVideoMuteState} from '#/components/Post/Embed/VideoEmbed/VideoVolumeContext' import {useVideoMuteState} from '#/components/Post/Embed/VideoEmbed/VideoVolumeContext'
import {TimeIndicator} from './TimeIndicator' import {TimeIndicator} from './TimeIndicator'
@@ -23,12 +22,14 @@ export const VideoEmbedInnerNative = React.forwardRef(
embed, embed,
setStatus, setStatus,
setIsLoading, setIsLoading,
setIsActive, activeState,
setActiveState,
}: { }: {
embed: AppBskyEmbedVideo.View embed: AppBskyEmbedVideo.View
setStatus: (status: 'playing' | 'paused') => void setStatus: (status: 'playing' | 'paused') => void
setIsLoading: (isLoading: boolean) => void setIsLoading: (isLoading: boolean) => void
setIsActive: (isActive: boolean) => void activeState: 'active' | 'inactive' | 'staged'
setActiveState: (activeState: 'active' | 'inactive' | 'staged') => void
}, },
ref: React.Ref<{togglePlayback: () => void}>, ref: React.Ref<{togglePlayback: () => void}>,
) { ) {
@@ -60,7 +61,13 @@ export const VideoEmbedInnerNative = React.forwardRef(
beginMuted={autoplayDisabled ? false : muted} beginMuted={autoplayDisabled ? false : muted}
style={[a.rounded_sm]} style={[a.rounded_sm]}
onActiveChange={e => { onActiveChange={e => {
setIsActive(e.nativeEvent.isActive) setActiveState(
(
e.nativeEvent as unknown as {
state: 'active' | 'inactive' | 'staged'
}
).state,
)
}} }}
onLoadingChange={e => { onLoadingChange={e => {
setIsLoading(e.nativeEvent.isLoading) setIsLoading(e.nativeEvent.isLoading)
@@ -85,8 +92,13 @@ export const VideoEmbedInnerNative = React.forwardRef(
accessibilityHint="" accessibilityHint=""
/> />
<VideoControls <VideoControls
enterFullscreen={() => { activeState={activeState}
onPress={() => {
if (activeState === 'active') {
videoRef.current?.enterFullscreen(true) videoRef.current?.enterFullscreen(true)
} else {
videoRef.current?.togglePlayback()
}
}} }}
toggleMuted={() => { toggleMuted={() => {
videoRef.current?.toggleMuted() videoRef.current?.toggleMuted()
@@ -97,20 +109,21 @@ export const VideoEmbedInnerNative = React.forwardRef(
isPlaying={isPlaying} isPlaying={isPlaying}
timeRemaining={timeRemaining} timeRemaining={timeRemaining}
/> />
<MediaInsetBorder />
</View> </View>
) )
}, },
) )
function VideoControls({ function VideoControls({
enterFullscreen, activeState,
onPress,
toggleMuted, toggleMuted,
togglePlayback, togglePlayback,
timeRemaining, timeRemaining,
isPlaying, isPlaying,
}: { }: {
enterFullscreen: () => void activeState: 'active' | 'inactive' | 'staged'
onPress: () => void
toggleMuted: () => void toggleMuted: () => void
togglePlayback: () => void togglePlayback: () => void
timeRemaining: number timeRemaining: number
@@ -124,18 +137,25 @@ function VideoControls({
// 1. timeRemaining is a number - was seeing NaNs // 1. timeRemaining is a number - was seeing NaNs
// 2. duration is greater than 0 - means metadata has loaded // 2. duration is greater than 0 - means metadata has loaded
// 3. we're less than 5 second into the video // 3. we're less than 5 second into the video
const showTime = !isNaN(timeRemaining) const showTime =
!isNaN(timeRemaining) &&
(activeState === 'active' ? true : timeRemaining > 0)
return ( return (
<View style={[a.absolute, a.inset_0]}> <View style={[a.absolute, a.inset_0]}>
<Pressable <Pressable
onPress={enterFullscreen} onPress={onPress}
style={a.flex_1} style={a.flex_1}
accessibilityLabel={_(msg`Video`)} accessibilityLabel={_(msg`Video`)}
accessibilityHint={_(msg`Enters full screen`)} accessibilityHint={
activeState === 'active'
? _(msg`Enters full screen`)
: _(msg`Play video`)
}
accessibilityRole="button" accessibilityRole="button"
/> />
<ControlButton <ControlButton
disabled={activeState !== 'active'}
onPress={togglePlayback} onPress={togglePlayback}
label={isPlaying ? _(msg`Pause`) : _(msg`Play`)} label={isPlaying ? _(msg`Pause`) : _(msg`Play`)}
accessibilityHint={_(msg`Plays or pauses the video`)} accessibilityHint={_(msg`Plays or pauses the video`)}
@@ -148,6 +168,7 @@ function VideoControls({
</ControlButton> </ControlButton>
{showTime && <TimeIndicator time={timeRemaining} style={{left: 33}} />} {showTime && <TimeIndicator time={timeRemaining} style={{left: 33}} />}
{activeState === 'active' && (
<ControlButton <ControlButton
onPress={toggleMuted} onPress={toggleMuted}
label={ label={
@@ -163,6 +184,7 @@ function VideoControls({
<UnmuteIcon width={13} fill={t.palette.white} /> <UnmuteIcon width={13} fill={t.palette.white} />
)} )}
</ControlButton> </ControlButton>
)}
</View> </View>
) )
} }
@@ -173,12 +195,14 @@ function ControlButton({
label, label,
accessibilityHint, accessibilityHint,
style, style,
disabled,
}: { }: {
onPress: () => void onPress: () => void
children: React.ReactNode children: React.ReactNode
label: string label: string
accessibilityHint: string accessibilityHint: string
style?: StyleProp<ViewStyle> style?: StyleProp<ViewStyle>
disabled?: boolean
}) { }) {
return ( return (
<View <View
@@ -202,7 +226,8 @@ function ControlButton({
accessibilityLabel={label} accessibilityLabel={label}
accessibilityHint={accessibilityHint} accessibilityHint={accessibilityHint}
accessibilityRole="button" accessibilityRole="button"
hitSlop={HITSLOP_30}> hitSlop={HITSLOP_30}
disabled={disabled}>
{children} {children}
</Pressable> </Pressable>
</View> </View>
+41 -18
View File
@@ -1,15 +1,17 @@
import React, {useCallback, useState} from 'react' import {useCallback, useEffect, useRef, useState} from 'react'
import {ActivityIndicator, View} from 'react-native' import {ActivityIndicator, View} from 'react-native'
import {ImageBackground} from 'expo-image' import {ImageBackground} from 'expo-image'
import {type AppBskyEmbedVideo} from '@atproto/api' import {type AppBskyEmbedVideo} from '@atproto/api'
import {msg, Trans} from '@lingui/macro' import {msg, Trans} from '@lingui/macro'
import {useLingui} from '@lingui/react' import {useLingui} from '@lingui/react'
import {isAndroid} from '#/platform/detection'
import {ErrorBoundary} from '#/view/com/util/ErrorBoundary' import {ErrorBoundary} from '#/view/com/util/ErrorBoundary'
import {ConstrainedImage} from '#/view/com/util/images/AutoSizedImage' import {ConstrainedImage} from '#/view/com/util/images/AutoSizedImage'
import {atoms as a, useTheme} from '#/alf' import {atoms as a, platform, useTheme} from '#/alf'
import {Button} from '#/components/Button' import {Button} from '#/components/Button'
import {useThrottledValue} from '#/components/hooks/useThrottledValue' import {useThrottledValue} from '#/components/hooks/useThrottledValue'
import {MediaInsetBorder} from '#/components/MediaInsetBorder'
import {PlayButtonIcon} from '#/components/video/PlayButtonIcon' import {PlayButtonIcon} from '#/components/video/PlayButtonIcon'
import {VideoEmbedInnerNative} from './VideoEmbedInner/VideoEmbedInnerNative' import {VideoEmbedInnerNative} from './VideoEmbedInner/VideoEmbedInnerNative'
import * as VideoFallback from './VideoEmbedInner/VideoFallback' import * as VideoFallback from './VideoEmbedInner/VideoFallback'
@@ -81,26 +83,31 @@ export function VideoEmbed({embed, crop}: Props) {
function InnerWrapper({embed}: Props) { function InnerWrapper({embed}: Props) {
const {_} = useLingui() const {_} = useLingui()
const ref = React.useRef<{togglePlayback: () => void}>(null) const ref = useRef<{togglePlayback: () => void}>(null)
const [status, setStatus] = React.useState<'playing' | 'paused' | 'pending'>( const [status, setStatus] = useState<'playing' | 'paused' | 'pending'>(
'pending', 'pending',
) )
const [isLoading, setIsLoading] = React.useState(false) const [isLoading, setIsLoading] = useState(false)
const [isActive, setIsActive] = React.useState(false) const [activeState, setActiveState] = useState<
const showSpinner = useThrottledValue(isActive && isLoading, 100) 'active' | 'inactive' | 'staged'
>('inactive')
const showSpinner = useThrottledValue(
activeState === 'active' && isLoading,
100,
)
const showOverlay = const showOverlay =
!isActive || activeState === 'inactive' ||
isLoading || (activeState === 'active' && (isLoading || status === 'pending'))
(status === 'paused' && !isActive) ||
status === 'pending'
React.useEffect(() => { useEffect(() => {
if (!isActive && status !== 'pending') { if (isAndroid) {
if (activeState === 'inactive' && status !== 'pending') {
setStatus('pending') setStatus('pending')
} }
}, [isActive, status]) }
}, [activeState, status])
return ( return (
<> <>
@@ -108,7 +115,8 @@ function InnerWrapper({embed}: Props) {
embed={embed} embed={embed}
setStatus={setStatus} setStatus={setStatus}
setIsLoading={setIsLoading} setIsLoading={setIsLoading}
setIsActive={setIsActive} activeState={activeState}
setActiveState={setActiveState}
ref={ref} ref={ref}
/> />
<ImageBackground <ImageBackground
@@ -120,14 +128,26 @@ function InnerWrapper({embed}: Props) {
{ {
backgroundColor: 'transparent', // If you don't add `backgroundColor` to the styles here, backgroundColor: 'transparent', // If you don't add `backgroundColor` to the styles here,
// the play button won't show up on the first render on android 🥴😮‍💨 // the play button won't show up on the first render on android 🥴😮‍💨
display: showOverlay ? 'flex' : 'none',
}, },
// loading videos are black on android and transparent on ios
// therefore on ios we can just keep the thumbnail behind the video to reduce flicker
// but on android we need to overlay it and just turn it on and off
platform({
ios: {zIndex: showOverlay ? 1 : -1},
android: {display: showOverlay ? 'flex' : 'none'},
}),
]} ]}
cachePolicy="memory-disk" // Preferring memory cache helps to avoid flicker when re-displaying on android cachePolicy="memory-disk" // Preferring memory cache helps to avoid flicker when re-displaying on android
> >
{showOverlay && ( {showOverlay && (
<Button <Button
style={[a.flex_1, a.align_center, a.justify_center]} style={[
a.flex_1,
a.align_center,
a.justify_center,
a.absolute,
a.inset_0,
]}
onPress={() => { onPress={() => {
ref.current?.togglePlayback() ref.current?.togglePlayback()
}} }}
@@ -143,12 +163,15 @@ function InnerWrapper({embed}: Props) {
]}> ]}>
<ActivityIndicator size="large" color="white" /> <ActivityIndicator size="large" color="white" />
</View> </View>
) : ( ) : activeState === 'inactive' ? (
<PlayButtonIcon /> <PlayButtonIcon />
) : (
<View />
)} )}
</Button> </Button>
)} )}
</ImageBackground> </ImageBackground>
<MediaInsetBorder />
</> </>
) )
} }
@@ -1,9 +1,9 @@
import React from 'react' import React from 'react'
import {View} from 'react-native' import {View} from 'react-native'
import {ImagePickerAsset} from 'expo-image-picker' import {type ImagePickerAsset} from 'expo-image-picker'
import {BlueskyVideoView} from '@haileyok/bluesky-video' import {BlueskyVideoView} from '@haileyok/bluesky-video'
import {CompressedVideo} from '#/lib/media/video/types' import {type CompressedVideo} from '#/lib/media/video/types'
import {clamp} from '#/lib/numbers' import {clamp} from '#/lib/numbers'
import {useAutoplayDisabled} from '#/state/preferences' import {useAutoplayDisabled} from '#/state/preferences'
import {ExternalEmbedRemoveBtn} from '#/view/com/composer/ExternalEmbedRemoveBtn' import {ExternalEmbedRemoveBtn} from '#/view/com/composer/ExternalEmbedRemoveBtn'