Files
otunes/apps/mobile/app/(app)/player.tsx
T
joshuafhigginsandremhelper 3f68283d89 Hide player bar when queue is empty (#4)
- Stop showing the mini player when there is no queued track
- Reset playback state to 0 and paused at end of queue
- Keep the current song in queue history when skipping the last track

---------

Co-authored-by: Rem Helper <remhelper@abunchofknowitalls.com>
Reviewed-on: https://git.abunchofknowitalls.com/joshuafhiggins/otunes/pulls/4
2026-08-02 04:14:02 +00:00

382 lines
11 KiB
TypeScript

import { Ionicons } from "@expo/vector-icons"
import Slider from "@react-native-community/slider"
import { BlurView } from "expo-blur"
import { useRouter } from "expo-router"
import * as Haptics from "expo-haptics"
import { useEffect, useRef } from "react"
import {
Platform,
Pressable,
StyleSheet,
Text,
View,
} from "react-native"
import { SafeAreaView, useSafeAreaInsets } from "react-native-safe-area-context"
import { StatusBar } from "expo-status-bar"
import { MediaActionMenu, type MediaAction } from "@/components/media-actions-sheet"
import { useMediaMenuActions } from "@/hooks/use-media-menu-actions"
import { CoverImage } from "@/components/cover-image"
import { useFavorites } from "@/contexts/favorites"
import { usePlayback } from "@/contexts/playback"
import { useServerConfig } from "@/contexts/server-config"
import { formatDuration, formatRemainingDuration } from "@/lib/format"
import { useAppNavigation } from "@/lib/app-navigation"
import { albumCoverUrl } from "@/lib/urls"
import { appleMusic, radius, spacing, usePlayerTheme } from "@/lib/theme"
export default function PlayerScreen() {
const router = useRouter()
const hadSongRef = useRef(false)
const { push } = useAppNavigation()
const insets = useSafeAreaInsets()
const player = usePlayerTheme()
const { config } = useServerConfig()
const { isSongFavorite, toggleSongFavorite } = useFavorites()
const { handleSongAction } = useMediaMenuActions()
const {
currentSong,
isPlaying,
shuffleEnabled,
repeatEnabled,
currentTime,
duration,
volume,
queueSongIds,
setIsPlaying,
setVolume,
seekTo,
toggleShuffle,
cycleRepeat,
skipNext,
skipPrevious,
} = usePlayback()
useEffect(() => {
if (currentSong) {
hadSongRef.current = true
return
}
if (hadSongRef.current) {
router.back()
}
}, [currentSong, router])
if (!currentSong || !config) {
return null
}
const hasCover = currentSong.album.cover != null
const coverUri = albumCoverUrl(config.serverUrl, currentSong.album.id)
const favorited = isSongFavorite(currentSong.id)
const bottomInset = Math.max(insets.bottom, spacing.md) + spacing.lg
const { colors } = player
async function handleAction(action: MediaAction) {
if (!currentSong) return
await handleSongAction(currentSong, action)
}
function togglePlayback() {
void Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light)
setIsPlaying(!isPlaying)
}
return (
<View style={[styles.root, { backgroundColor: colors.background }]}>
<StatusBar style={player.statusBar} />
<CoverImage
uri={coverUri}
hasCover={hasCover}
style={[styles.backgroundImage, { opacity: player.backgroundImageOpacity }]}
contentFit="cover"
blurRadius={60}
placeholderVariant="solid"
/>
{Platform.OS === "ios" ? (
<>
<BlurView intensity={90} tint={player.blurTint} style={StyleSheet.absoluteFill} />
<View style={[styles.scrim, { backgroundColor: player.scrim }]} />
</>
) : (
<View style={[StyleSheet.absoluteFill, { backgroundColor: player.androidScrim }]} />
)}
<SafeAreaView style={styles.safe} edges={["top"]}>
<Pressable
onPress={() => router.back()}
hitSlop={12}
style={styles.handleRow}
accessibilityRole="button"
accessibilityLabel="Close player"
>
<View style={[styles.handle, { backgroundColor: player.handle }]} />
</Pressable>
<View style={styles.upper}>
<View style={styles.artworkWrap}>
<CoverImage uri={coverUri} hasCover={hasCover} style={styles.artwork} />
</View>
</View>
<View style={[styles.controls, { paddingBottom: bottomInset }]}>
<View style={styles.metaRow}>
<View style={styles.metaText}>
<Text style={[styles.songTitle, { color: colors.foreground }]} numberOfLines={2}>
{currentSong.title}
</Text>
<Text style={[styles.artistName, { color: colors.mutedForeground }]} numberOfLines={1}>
{currentSong.artist.name}
</Text>
</View>
<View style={styles.metaActions}>
<Pressable
onPress={() => void toggleSongFavorite(currentSong.id)}
hitSlop={10}
style={[styles.circleButton, { backgroundColor: player.buttonSurface }]}
>
<Ionicons
name={favorited ? "star" : "star-outline"}
size={20}
color={favorited ? appleMusic.pink : colors.foreground}
/>
</Pressable>
<MediaActionMenu
favorited={favorited}
navigation={{
artist: { id: currentSong.artist.id },
album: { id: currentSong.album.id },
}}
includeActions={[
"play-next",
"play-last",
"shuffle",
"favorite",
"go-artist",
"go-album",
]}
onAction={handleAction}
>
<Pressable
hitSlop={10}
style={[styles.circleButton, { backgroundColor: player.buttonSurface }]}
>
<Ionicons name="ellipsis-horizontal" size={20} color={colors.foreground} />
</Pressable>
</MediaActionMenu>
</View>
</View>
<View style={styles.progressSection}>
<Slider
style={styles.slider}
minimumValue={0}
maximumValue={duration > 0 ? duration : 1}
value={Math.min(currentTime, duration || 0)}
onSlidingComplete={(value) => void seekTo(value)}
minimumTrackTintColor={colors.foreground}
maximumTrackTintColor={player.trackInactive}
thumbTintColor={colors.foreground}
/>
<View style={styles.timeRow}>
<Text style={[styles.time, { color: colors.mutedForeground }]}>
{formatDuration(currentTime)}
</Text>
<Text style={[styles.time, { color: colors.mutedForeground }]}>
{formatRemainingDuration(currentTime, duration)}
</Text>
</View>
</View>
<View style={styles.transport}>
<Pressable onPress={() => void skipPrevious()} hitSlop={12}>
<Ionicons name="play-back" size={36} color={colors.foreground} />
</Pressable>
<Pressable onPress={togglePlayback} style={styles.playButton} hitSlop={12}>
<Ionicons
name={isPlaying ? "pause" : "play"}
size={52}
color={colors.foreground}
/>
</Pressable>
<Pressable onPress={() => void skipNext()} hitSlop={12}>
<Ionicons name="play-forward" size={36} color={colors.foreground} />
</Pressable>
</View>
<View style={styles.volumeSection}>
<Ionicons name="volume-low" size={18} color={colors.mutedForeground} />
<Slider
style={styles.volumeSlider}
minimumValue={0}
maximumValue={1}
value={volume}
onValueChange={setVolume}
minimumTrackTintColor={colors.foreground}
maximumTrackTintColor={player.trackInactive}
thumbTintColor={colors.foreground}
/>
<Ionicons name="volume-high" size={18} color={colors.mutedForeground} />
</View>
<View style={styles.footer}>
<Pressable onPress={() => void toggleShuffle()} hitSlop={12} style={styles.footerButton}>
<Ionicons
name="shuffle"
size={22}
color={shuffleEnabled ? appleMusic.pink : colors.mutedForeground}
/>
</Pressable>
<Pressable onPress={() => void cycleRepeat()} hitSlop={12} style={styles.footerButton}>
<Ionicons
name={repeatEnabled === "once" ? "repeat-outline" : "repeat"}
size={22}
color={repeatEnabled !== "none" ? appleMusic.pink : colors.mutedForeground}
/>
</Pressable>
<Pressable
onPress={() => push("/queue")}
hitSlop={12}
style={styles.footerButton}
disabled={queueSongIds.length === 0}
>
<Ionicons name="list" size={22} color={colors.mutedForeground} />
</Pressable>
</View>
</View>
</SafeAreaView>
</View>
)
}
const styles = StyleSheet.create({
root: {
flex: 1,
},
backgroundImage: {
...StyleSheet.absoluteFill,
},
scrim: {
...StyleSheet.absoluteFill,
},
safe: {
flex: 1,
paddingHorizontal: spacing.xl,
},
handleRow: {
alignItems: "center",
paddingTop: spacing.sm,
paddingBottom: spacing.md,
},
handle: {
width: 36,
height: 5,
borderRadius: radius.full,
},
upper: {
flex: 1,
minHeight: 0,
justifyContent: "center",
},
artworkWrap: {
alignItems: "center",
justifyContent: "center",
paddingVertical: spacing.sm,
},
artwork: {
width: "100%",
maxWidth: 320,
aspectRatio: 1,
borderRadius: 12,
shadowColor: "#000",
shadowOpacity: 0.45,
shadowRadius: 24,
shadowOffset: { width: 0, height: 12 },
},
controls: {
flexShrink: 0,
},
metaRow: {
flexDirection: "row",
alignItems: "flex-start",
gap: spacing.md,
marginBottom: spacing.md,
},
metaText: {
flex: 1,
minWidth: 0,
},
songTitle: {
fontSize: 22,
fontWeight: "700",
lineHeight: 28,
},
artistName: {
fontSize: 18,
marginTop: spacing.xs,
},
metaActions: {
flexDirection: "row",
alignItems: "center",
gap: spacing.md,
paddingTop: 4,
},
circleButton: {
width: 34,
height: 34,
borderRadius: 17,
alignItems: "center",
justifyContent: "center",
},
progressSection: {
marginBottom: spacing.lg,
},
slider: {
width: "100%",
height: 28,
},
timeRow: {
flexDirection: "row",
justifyContent: "space-between",
marginTop: -4,
},
time: {
fontSize: 13,
fontVariant: ["tabular-nums"],
},
transport: {
flexDirection: "row",
alignItems: "center",
justifyContent: "center",
gap: 48,
marginBottom: spacing.lg,
},
playButton: {
width: 72,
alignItems: "center",
},
volumeSection: {
flexDirection: "row",
alignItems: "center",
gap: spacing.sm,
marginBottom: spacing.md,
},
volumeSlider: {
flex: 1,
height: 28,
},
footer: {
flexDirection: "row",
justifyContent: "space-between",
paddingHorizontal: spacing.xxl,
paddingTop: spacing.sm,
},
footerButton: {
minWidth: 44,
minHeight: 44,
alignItems: "center",
justifyContent: "center",
},
})