From 945a9bf21d1c5465550d90f3608ff5f82bb71637 Mon Sep 17 00:00:00 2001 From: Jorrin Date: Thu, 21 Mar 2024 21:58:41 +0100 Subject: [PATCH] optimize volume and brightness overlays --- .prettierignore => apps/expo/.prettierignore | 0 apps/expo/app.config.ts | 1 + .../src/components/player/VideoPlayer.tsx | 137 ++++++++---------- .../src/components/player/settings/Sheet.tsx | 2 +- apps/expo/src/hooks/player/useBrightness.ts | 39 +++-- apps/expo/src/hooks/player/useVolume.ts | 26 ++-- apps/expo/src/stores/player/slices/audio.ts | 13 ++ .../src/stores/player/slices/interface.ts | 12 ++ 8 files changed, 119 insertions(+), 111 deletions(-) rename .prettierignore => apps/expo/.prettierignore (100%) diff --git a/.prettierignore b/apps/expo/.prettierignore similarity index 100% rename from .prettierignore rename to apps/expo/.prettierignore diff --git a/apps/expo/app.config.ts b/apps/expo/app.config.ts index e096b8e..1d45576 100644 --- a/apps/expo/app.config.ts +++ b/apps/expo/app.config.ts @@ -53,6 +53,7 @@ const defineConfig = (): ExpoConfig => ({ "expo-build-properties", { android: { + minSdkVersion: 24, packagingOptions: { pickFirst: [ "lib/x86/libcrypto.so", diff --git a/apps/expo/src/components/player/VideoPlayer.tsx b/apps/expo/src/components/player/VideoPlayer.tsx index 4662062..3e24d05 100644 --- a/apps/expo/src/components/player/VideoPlayer.tsx +++ b/apps/expo/src/components/player/VideoPlayer.tsx @@ -27,31 +27,25 @@ import { isPointInSliderVicinity } from "./VideoSlider"; export const VideoPlayer = () => { const { brightness, - debouncedBrightness, showBrightnessOverlay, + currentBrightness, setShowBrightnessOverlay, handleBrightnessChange, } = useBrightness(); - const { - currentVolume, - debouncedVolume, - showVolumeOverlay, - setShowVolumeOverlay, - handleVolumeChange, - } = useVolume(); + const { showVolumeOverlay, setShowVolumeOverlay, volume, currentVolume } = + useVolume(); const { currentSpeed } = usePlaybackSpeed(); const { synchronizePlayback } = useAudioTrack(); const { dismissFullscreenPlayer } = usePlayer(); const [videoSrc, setVideoSrc] = useState(); const [isLoading, setIsLoading] = useState(true); const [resizeMode, setResizeMode] = useState(ResizeMode.CONTAIN); - const [shouldPlay, setShouldPlay] = useState(true); const [hasStartedPlaying, setHasStartedPlaying] = useState(false); const isGestureInSliderVicinity = useSharedValue(false); const router = useRouter(); const scale = useSharedValue(1); - const [lastVelocityY, setLastVelocityY] = useState(0); + const state = usePlayerStore((state) => state.interface.state); const isIdle = usePlayerStore((state) => state.interface.isIdle); const stream = usePlayerStore((state) => state.interface.currentStream); const selectedAudioTrack = useAudioTrackStore((state) => state.selectedTrack); @@ -60,8 +54,8 @@ export const VideoPlayer = () => { const setVideoRef = usePlayerStore((state) => state.setVideoRef); const setStatus = usePlayerStore((state) => state.setStatus); const setIsIdle = usePlayerStore((state) => state.setIsIdle); - const playAudio = usePlayerStore((state) => state.playAudio); - const pauseAudio = usePlayerStore((state) => state.pauseAudio); + const toggleAudio = usePlayerStore((state) => state.toggleAudio); + const toggleState = usePlayerStore((state) => state.toggleState); const [gestureControlsEnabled, setGestureControlsEnabled] = useState(true); @@ -89,20 +83,12 @@ export const VideoPlayer = () => { } }); - const togglePlayback = () => { - setShouldPlay(!shouldPlay); - if (shouldPlay) { - void playAudio(); - } else { - void pauseAudio(); - } - }; - const doubleTapGesture = Gesture.Tap() .enabled(gestureControlsEnabled) .numberOfTaps(2) .onEnd(() => { - runOnJS(togglePlayback)(); + runOnJS(toggleAudio)(); + runOnJS(toggleState)(); }); const screenHalfWidth = Dimensions.get("window").width / 2; @@ -114,6 +100,11 @@ export const VideoPlayer = () => { if (isGestureInSliderVicinity.value) { return; } + if (event.x > screenHalfWidth) { + runOnJS(setShowVolumeOverlay)(true); + } else { + runOnJS(setShowBrightnessOverlay)(true); + } }) .onUpdate((event) => { const divisor = 5000; @@ -123,35 +114,25 @@ export const VideoPlayer = () => { const directionMultiplier = event.velocityY < 0 ? 1 : -1; const change = directionMultiplier * Math.abs(event.velocityY / divisor); - const newVolume = Math.max(0, Math.min(1, currentVolume.value + change)); - const newBrightness = Math.max(0, Math.min(1, brightness.value + change)); if (event.x > screenHalfWidth) { - runOnJS(handleVolumeChange)(newVolume); + const newVolume = Math.max(0, Math.min(1, volume.value + change)); + volume.value = newVolume; } else { + const newBrightness = Math.max( + 0, + Math.min(1, brightness.value + change), + ); brightness.value = newBrightness; runOnJS(handleBrightnessChange)(newBrightness); } - - if ( - (event.velocityY < 0 && lastVelocityY >= 0) || - (event.velocityY >= 0 && lastVelocityY < 0) - ) { - runOnJS(setLastVelocityY)(event.velocityY); - } - - if (event.x > screenHalfWidth) { - runOnJS(handleVolumeChange)(newVolume); - runOnJS(setShowVolumeOverlay)(true); - } else { - runOnJS(handleBrightnessChange)(newBrightness); - runOnJS(setShowBrightnessOverlay)(true); - } }) - .onEnd(() => { - runOnJS(setLastVelocityY)(0); - runOnJS(setShowVolumeOverlay)(false); - runOnJS(setShowBrightnessOverlay)(false); + .onEnd((event) => { + if (event.x > screenHalfWidth) { + runOnJS(setShowVolumeOverlay)(false); + } else { + runOnJS(setShowBrightnessOverlay)(false); + } }); const composedGesture = Gesture.Race( @@ -254,9 +235,9 @@ export const VideoPlayer = () => {