399 lines
13 KiB
TypeScript
399 lines
13 KiB
TypeScript
import { useEffect, useState, type ComponentProps, type ReactNode } from "react"
|
|
import { Pressable, StyleSheet, View, useWindowDimensions } from "react-native"
|
|
import Animated, {
|
|
Easing,
|
|
ReduceMotion,
|
|
interpolate,
|
|
useAnimatedStyle,
|
|
useSharedValue,
|
|
withTiming,
|
|
} from "react-native-reanimated"
|
|
import { GestureDetector, type Gesture } from "react-native-gesture-handler"
|
|
import { useSafeAreaInsets } from "react-native-safe-area-context"
|
|
import { useRouter } from "expo-router"
|
|
import { StatusBar } from "expo-status-bar"
|
|
|
|
import { AudioOutputButton } from "@/components/audio-output-button"
|
|
import { CoverImage } from "@/components/cover-image"
|
|
import { LyricsView } from "@/components/lyrics-view"
|
|
import { NowPlayingQueue } from "@/components/now-playing-queue"
|
|
import { PlayerProgress } from "@/components/player-progress"
|
|
import { PlayerSlider } from "@/components/player-slider"
|
|
import { PlayerSymbol } from "@/components/player-symbol"
|
|
import { usePlaybackActions, usePlaybackQueue } from "@/contexts/playback"
|
|
import { useServerConfig } from "@/contexts/server-config"
|
|
import { useSystemVolume } from "@/hooks/use-system-volume"
|
|
import type { NowPlayingColors } from "@/lib/now-playing-colors"
|
|
import { albumCoverUrl } from "@/lib/urls"
|
|
import type { Lyrics } from "@otunes/convex/client/lyrics"
|
|
|
|
/** What the Now Playing screen shows beside or instead of the artwork. */
|
|
export type PlayerPanel = "queue" | "lyrics"
|
|
|
|
const queueMotion = {
|
|
duration: 420,
|
|
easing: Easing.bezier(0.22, 1, 0.36, 1),
|
|
reduceMotion: ReduceMotion.System,
|
|
}
|
|
|
|
/**
|
|
* Tablet player keeps transport controls visible while the queue or lyrics
|
|
* open beside them.
|
|
*/
|
|
export function TabletNowPlaying({
|
|
colors,
|
|
backgroundStyle,
|
|
dismissStyle,
|
|
dismissGestures,
|
|
panel,
|
|
togglePanel,
|
|
lyrics,
|
|
metadata,
|
|
}: {
|
|
colors: NowPlayingColors
|
|
backgroundStyle: ComponentProps<typeof Animated.View>["style"]
|
|
dismissStyle: ComponentProps<typeof Animated.View>["style"]
|
|
dismissGestures: {
|
|
handle: ReturnType<typeof Gesture.Pan>
|
|
artwork: ReturnType<typeof Gesture.Pan>
|
|
}
|
|
panel: PlayerPanel | null
|
|
togglePanel: (panel: PlayerPanel) => void
|
|
lyrics: Lyrics | null | undefined
|
|
metadata: ReactNode
|
|
}) {
|
|
const router = useRouter()
|
|
const insets = useSafeAreaInsets()
|
|
const { width, height } = useWindowDimensions()
|
|
const { config } = useServerConfig()
|
|
const { currentSong, isPlaying, shuffleEnabled, repeatEnabled } =
|
|
usePlaybackQueue()
|
|
const { setIsPlaying, skipNext, skipPrevious, toggleShuffle, cycleRepeat } =
|
|
usePlaybackActions()
|
|
const { volume, setVolume, available: volumeAvailable } = useSystemVolume()
|
|
const [queueHeight, setQueueHeight] = useState(0)
|
|
const wideLayout = width >= 700
|
|
const showQueue = panel === "queue"
|
|
const showLyrics = panel === "lyrics"
|
|
const sideBySide = panel !== null && wideLayout
|
|
const progress = useSharedValue(sideBySide ? 1 : 0)
|
|
useEffect(() => {
|
|
progress.value = withTiming(sideBySide ? 1 : 0, queueMotion)
|
|
}, [sideBySide, progress])
|
|
const closedCoverSize = Math.max(
|
|
160,
|
|
Math.min(322, width * 0.52, height - 460)
|
|
)
|
|
const openCoverSize = Math.max(160, Math.min(322, width * 0.34, height - 460))
|
|
const mainMotion = useAnimatedStyle(() => ({
|
|
transform: [{ translateX: -width * 0.2625 * progress.value }],
|
|
}))
|
|
const artworkMotion = useAnimatedStyle(() => ({
|
|
width: interpolate(
|
|
progress.value,
|
|
[0, 1],
|
|
[closedCoverSize, openCoverSize]
|
|
),
|
|
height: interpolate(
|
|
progress.value,
|
|
[0, 1],
|
|
[closedCoverSize, openCoverSize]
|
|
),
|
|
}))
|
|
const controlsMotion = useAnimatedStyle(() => ({
|
|
width: interpolate(
|
|
progress.value,
|
|
[0, 1],
|
|
[Math.min(456, width - 80), Math.min(456, width * 0.475 - 80)]
|
|
),
|
|
}))
|
|
const queueStyle = useAnimatedStyle(() => ({
|
|
opacity: interpolate(progress.value, [0, 0.15, 1], [0, 0, 1]),
|
|
transform: [{ translateX: (1 - progress.value) * 48 }],
|
|
}))
|
|
if (!currentSong || !config) return null
|
|
const control = (
|
|
label: string,
|
|
symbol: React.ComponentProps<typeof PlayerSymbol>["name"],
|
|
fallback: React.ComponentProps<typeof PlayerSymbol>["fallback"],
|
|
onPress: () => void,
|
|
size = 32,
|
|
selected = false,
|
|
inactive = false
|
|
) => (
|
|
<Pressable
|
|
onPress={onPress}
|
|
accessibilityRole="button"
|
|
accessibilityLabel={label}
|
|
accessibilityState={{ selected }}
|
|
style={[styles.button, selected && { backgroundColor: colors.control }]}
|
|
>
|
|
<PlayerSymbol
|
|
name={symbol}
|
|
fallback={fallback}
|
|
size={size}
|
|
color={inactive ? colors.secondaryForeground : colors.foreground}
|
|
/>
|
|
</Pressable>
|
|
)
|
|
const panelContent = showLyrics ? (
|
|
<LyricsView
|
|
key={currentSong.id}
|
|
lyrics={lyrics}
|
|
colors={colors}
|
|
fontSize={34}
|
|
/>
|
|
) : (
|
|
<NowPlayingQueue
|
|
tablet
|
|
colors={colors}
|
|
viewportHeight={queueHeight}
|
|
currentHeader={null}
|
|
/>
|
|
)
|
|
return (
|
|
<Animated.View style={[styles.root, backgroundStyle, dismissStyle]}>
|
|
<StatusBar style={colors.isDark ? "light" : "dark"} />
|
|
<GestureDetector gesture={dismissGestures.handle}>
|
|
<Pressable
|
|
onPress={() => router.back()}
|
|
accessibilityRole="button"
|
|
accessibilityLabel="Close player"
|
|
style={[styles.handleRow, { marginTop: insets.top }]}
|
|
>
|
|
<View
|
|
style={[styles.handle, { backgroundColor: colors.sliderFill }]}
|
|
/>
|
|
</Pressable>
|
|
</GestureDetector>
|
|
<View style={styles.body}>
|
|
<Animated.View style={[styles.main, mainMotion]}>
|
|
<GestureDetector gesture={dismissGestures.artwork}>
|
|
<View style={styles.artworkArea}>
|
|
<Animated.View style={artworkMotion}>
|
|
<CoverImage
|
|
uri={albumCoverUrl(config.serverUrl, currentSong.album.id)}
|
|
hasCover={currentSong.album.cover != null}
|
|
style={{ width: "100%", height: "100%", borderRadius: 10 }}
|
|
/>
|
|
</Animated.View>
|
|
</View>
|
|
</GestureDetector>
|
|
<Animated.View style={[styles.controls, controlsMotion]}>
|
|
<View style={styles.meta}>{metadata}</View>
|
|
<PlayerProgress colors={colors} />
|
|
<View style={styles.transport}>
|
|
{control(
|
|
"Shuffle",
|
|
"shuffle",
|
|
"shuffle",
|
|
() => void toggleShuffle(),
|
|
21,
|
|
shuffleEnabled
|
|
)}
|
|
{control(
|
|
"Previous",
|
|
"backward.fill",
|
|
"play-back",
|
|
() => void skipPrevious(),
|
|
38
|
|
)}
|
|
{control(
|
|
isPlaying ? "Pause" : "Play",
|
|
isPlaying ? "pause.fill" : "play.fill",
|
|
isPlaying ? "pause" : "play",
|
|
() => setIsPlaying(!isPlaying),
|
|
44
|
|
)}
|
|
{control(
|
|
"Next",
|
|
"forward.fill",
|
|
"play-forward",
|
|
() => void skipNext(),
|
|
38
|
|
)}
|
|
{control(
|
|
"Repeat",
|
|
repeatEnabled === "once" ? "repeat.1" : "repeat",
|
|
"repeat",
|
|
() => void cycleRepeat(),
|
|
21,
|
|
repeatEnabled !== "none",
|
|
repeatEnabled === "none"
|
|
)}
|
|
</View>
|
|
<View style={styles.volume}>
|
|
<View
|
|
pointerEvents={volumeAvailable ? "auto" : "none"}
|
|
accessibilityState={{ disabled: !volumeAvailable }}
|
|
>
|
|
<PlayerSlider
|
|
accessibilityLabel="Volume"
|
|
commitHoldMs={0}
|
|
animateValueChanges
|
|
value={volume}
|
|
maximumValue={1}
|
|
fillColor={colors.sliderFill}
|
|
activeFillColor={colors.foreground}
|
|
trackColor={colors.sliderTrack}
|
|
onValueChange={volumeAvailable ? setVolume : undefined}
|
|
leading={
|
|
<PlayerSymbol
|
|
name="speaker.fill"
|
|
fallback="volume-low"
|
|
size={14}
|
|
color={colors.secondaryForeground}
|
|
/>
|
|
}
|
|
trailing={
|
|
<PlayerSymbol
|
|
name="speaker.wave.3.fill"
|
|
fallback="volume-high"
|
|
size={18}
|
|
color={colors.secondaryForeground}
|
|
/>
|
|
}
|
|
/>
|
|
</View>
|
|
</View>
|
|
</Animated.View>
|
|
</Animated.View>
|
|
{wideLayout ? (
|
|
<Animated.View
|
|
pointerEvents={panel ? "auto" : "none"}
|
|
accessibilityElementsHidden={!panel}
|
|
importantForAccessibility={panel ? "auto" : "no-hide-descendants"}
|
|
style={[styles.queue, queueStyle]}
|
|
onLayout={(event) =>
|
|
setQueueHeight(event.nativeEvent.layout.height)
|
|
}
|
|
>
|
|
{queueHeight > 0 ? panelContent : null}
|
|
</Animated.View>
|
|
) : null}
|
|
{panel && !sideBySide ? (
|
|
<View
|
|
style={[
|
|
styles.portraitQueue,
|
|
{ backgroundColor: colors.background },
|
|
]}
|
|
onLayout={(event) =>
|
|
setQueueHeight(event.nativeEvent.layout.height)
|
|
}
|
|
>
|
|
{queueHeight > 0 ? panelContent : null}
|
|
</View>
|
|
) : null}
|
|
</View>
|
|
<View
|
|
style={[
|
|
styles.footer,
|
|
{ paddingBottom: Math.max(insets.bottom, 24) + 12 },
|
|
]}
|
|
>
|
|
<AudioOutputButton
|
|
color={colors.foreground}
|
|
style={styles.footerButton}
|
|
/>
|
|
<View style={styles.footerActions}>
|
|
<Pressable
|
|
onPress={() => togglePanel("lyrics")}
|
|
disabled={!lyrics && !showLyrics}
|
|
accessibilityRole="button"
|
|
accessibilityLabel={showLyrics ? "Hide lyrics" : "Show lyrics"}
|
|
accessibilityState={{
|
|
selected: showLyrics,
|
|
disabled: !lyrics && !showLyrics,
|
|
}}
|
|
style={[
|
|
styles.footerButton,
|
|
!lyrics && !showLyrics && { opacity: 0.4 },
|
|
showLyrics && { backgroundColor: colors.selected },
|
|
]}
|
|
>
|
|
<PlayerSymbol
|
|
name="quote.bubble"
|
|
fallback="chatbox-outline"
|
|
size={25}
|
|
color={showLyrics ? colors.selectedForeground : colors.foreground}
|
|
/>
|
|
</Pressable>
|
|
<Pressable
|
|
onPress={() => togglePanel("queue")}
|
|
accessibilityRole="button"
|
|
accessibilityLabel={showQueue ? "Hide queue" : "Show queue"}
|
|
accessibilityState={{ selected: showQueue }}
|
|
style={[
|
|
styles.footerButton,
|
|
showQueue && { backgroundColor: colors.selected },
|
|
]}
|
|
>
|
|
<PlayerSymbol
|
|
name="list.bullet"
|
|
fallback="list"
|
|
size={25}
|
|
color={showQueue ? colors.selectedForeground : colors.foreground}
|
|
/>
|
|
</Pressable>
|
|
</View>
|
|
</View>
|
|
</Animated.View>
|
|
)
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
root: { flex: 1, overflow: "hidden" },
|
|
handleRow: { height: 32, alignItems: "center", justifyContent: "center" },
|
|
handle: { width: 60, height: 4, borderRadius: 2 },
|
|
body: { flex: 1, flexDirection: "row", minHeight: 0 },
|
|
main: {
|
|
flex: 1,
|
|
minWidth: 0,
|
|
alignItems: "center",
|
|
paddingTop: 20,
|
|
paddingBottom: 0,
|
|
},
|
|
artworkArea: {
|
|
flex: 1,
|
|
width: "100%",
|
|
alignItems: "center",
|
|
justifyContent: "center",
|
|
paddingBottom: 36,
|
|
},
|
|
controls: { width: "100%", maxWidth: 456, paddingHorizontal: 0 },
|
|
meta: { height: 48 },
|
|
transport: {
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
justifyContent: "space-between",
|
|
height: 80,
|
|
paddingHorizontal: 14,
|
|
},
|
|
button: {
|
|
width: 64,
|
|
minWidth: 36,
|
|
flexShrink: 1,
|
|
height: 64,
|
|
borderRadius: 32,
|
|
alignItems: "center",
|
|
justifyContent: "center",
|
|
},
|
|
volume: { height: 36, justifyContent: "center" },
|
|
queue: { position: "absolute", left: "47.5%", right: 20, top: 0, bottom: 0 },
|
|
portraitQueue: { ...StyleSheet.absoluteFill, bottom: 270 },
|
|
footer: {
|
|
flexDirection: "row",
|
|
justifyContent: "space-between",
|
|
paddingHorizontal: 44,
|
|
paddingTop: 12,
|
|
},
|
|
footerActions: { flexDirection: "row", gap: 16 },
|
|
footerButton: {
|
|
width: 40,
|
|
height: 40,
|
|
borderRadius: 20,
|
|
alignItems: "center",
|
|
justifyContent: "center",
|
|
},
|
|
})
|