feat(mobile): Apple Music-style track lists with per-disc sections #32

Merged
joshuafhiggins merged 1 commits from t3/mobile-track-layout-discs into main 2026-10-17 18:22:34 -04:00
5 changed files with 351 additions and 166 deletions

No files matched your search

+165 -84
View File
@@ -4,6 +4,11 @@ import { memo, useCallback, useEffect, useMemo } from "react"
import { FlatList, Pressable, StyleSheet, Text, View } from "react-native"
import { Ionicons } from "@expo/vector-icons"
import {
CollectionDivider,
collectionInset,
DiscHeader,
} from "@/components/collection-list"
import { CoverImage } from "@/components/cover-image"
import {
@@ -66,21 +71,34 @@ export default function AlbumScreen() {
}
}, [album])
const renderTrack = useCallback(
({ item, index }: { item: BrowseAlbumTrack; index: number }) =>
album ? (
const listItems = useMemo(() => albumListItems(album?.tracks ?? []), [album])
const renderItem = useCallback(
({ item }: { item: AlbumListItem }) => {
if (item.type === "disc") {
return <DiscHeader label={item.label} color={palette.foreground} />
}
return album ? (
<AlbumTrackRow
track={item}
index={index}
track={item.track}
number={item.number}
album={album}
songIds={songIds}
palette={palette}
favorited={isSongFavorite(item.id)}
favorited={isSongFavorite(item.track.id)}
onPlay={playFromSong}
onAction={handleAlbumTrackAction}
/>
) : null,
[album, handleAlbumTrackAction, isSongFavorite, palette, playFromSong, songIds]
) : null
},
[
album,
handleAlbumTrackAction,
isSongFavorite,
palette,
playFromSong,
songIds,
]
)
if (album === undefined || !config || !settled) {
@@ -154,79 +172,129 @@ export default function AlbumScreen() {
/>
<FlatList
{...longListProps}
data={album.tracks}
keyExtractor={(track) => track.id}
data={listItems}
keyExtractor={albumListItemKey}
style={{ flex: 1, backgroundColor: palette.background }}
contentContainerStyle={{ paddingBottom: contentBottomInset }}
contentInsetAdjustmentBehavior="automatic"
indicatorStyle={palette.isDark ? "white" : "black"}
ListHeaderComponent={
<View style={styles.hero}>
<CoverImage
uri={coverUri}
hasCover={album.cover != null}
style={styles.cover}
/>
<Text style={[styles.title, { color: palette.foreground }]}>
{album.title}
</Text>
<Pressable onPress={() => push(`/artist/${album.artist.id}`)}>
<Text style={[styles.artist, { color: palette.accentText }]}>
{album.artist.name}
</Text>
</Pressable>
{metadata ? (
<Text style={{ color: palette.secondaryForeground }}>
{metadata}
</Text>
) : null}
<View style={styles.actions}>
<Pressable
onPress={() => void toggleAlbumFavorite(album.id)}
hitSlop={8}
style={[styles.iconAction, { backgroundColor: palette.surface }]}
>
<Ionicons
name={favorited ? "star" : "star-outline"}
size={24}
color={favorited ? palette.accent : palette.foreground}
/>
</Pressable>
<Button
title="Play"
icon="play"
onPress={() => void playAlbum(album.id, songIds)}
style={styles.playButton}
tint={{
background: palette.accent,
foreground: palette.accentForeground,
}}
<View>
<View style={styles.hero}>
<CoverImage
uri={coverUri}
hasCover={album.cover != null}
style={styles.cover}
/>
<Pressable
accessibilityRole="button"
accessibilityLabel="Shuffle"
hitSlop={8}
onPress={() => void playAlbumShuffled(album.id, songIds)}
style={[styles.iconAction, { backgroundColor: palette.surface }]}
>
<Ionicons
name="shuffle"
size={24}
color={palette.foreground}
/>
<Text style={[styles.title, { color: palette.foreground }]}>
{album.title}
</Text>
<Pressable onPress={() => push(`/artist/${album.artist.id}`)}>
<Text style={[styles.artist, { color: palette.accentText }]}>
{album.artist.name}
</Text>
</Pressable>
{metadata ? (
<Text style={{ color: palette.secondaryForeground }}>
{metadata}
</Text>
) : null}
<View style={styles.actions}>
<Pressable
onPress={() => void toggleAlbumFavorite(album.id)}
hitSlop={8}
style={[
styles.iconAction,
{ backgroundColor: palette.surface },
]}
>
<Ionicons
name={favorited ? "star" : "star-outline"}
size={24}
color={favorited ? palette.accent : palette.foreground}
/>
</Pressable>
<Button
title="Play"
icon="play"
onPress={() => void playAlbum(album.id, songIds)}
style={styles.playButton}
tint={{
background: palette.accent,
foreground: palette.accentForeground,
}}
/>
<Pressable
accessibilityRole="button"
accessibilityLabel="Shuffle"
hitSlop={8}
onPress={() => void playAlbumShuffled(album.id, songIds)}
style={[
styles.iconAction,
{ backgroundColor: palette.surface },
]}
>
<Ionicons
name="shuffle"
size={24}
color={palette.foreground}
/>
</Pressable>
</View>
</View>
<CollectionDivider color={palette.divider} />
</View>
}
renderItem={renderTrack}
renderItem={renderItem}
/>
</>
)
}
type AlbumListItem =
| { type: "disc"; key: string; label: string }
| { type: "track"; track: BrowseAlbumTrack; number: number }
/**
* Tracks arrive sorted by disc then track number. Disc headers are only
* inserted when the album spans more than one disc.
*/
function albumListItems(tracks: BrowseAlbumTrack[]): AlbumListItem[] {
const multiDisc = new Set(tracks.map((track) => track.diskNumber)).size > 1
const items: AlbumListItem[] = []
let currentDisc: number | null | undefined
let positionInDisc = 0
for (const track of tracks) {
if (track.diskNumber !== currentDisc) {
currentDisc = track.diskNumber
positionInDisc = 0
if (multiDisc) {
items.push({
type: "disc",
key: `disc-${currentDisc ?? "unknown"}`,
label: currentDisc == null ? "Other" : `Disc ${currentDisc}`,
})
}
}
positionInDisc += 1
items.push({
type: "track",
track,
number: track.track || positionInDisc,
})
}
return items
}
function albumListItemKey(item: AlbumListItem) {
return item.type === "disc" ? item.key : item.track.id
}
const AlbumTrackRow = memo(function AlbumTrackRow({
track,
index,
number,
album,
songIds,
palette,
@@ -235,7 +303,7 @@ const AlbumTrackRow = memo(function AlbumTrackRow({
onAction,
}: {
track: BrowseAlbumTrack
index: number
number: number
album: BrowseAlbumDetail
songIds: Id<"songs">[]
palette: CoverPalette
@@ -262,33 +330,37 @@ const AlbumTrackRow = memo(function AlbumTrackRow({
onAction={(action) => void onAction(track, album, action)}
>
<Pressable
style={[styles.trackRow, { borderBottomColor: palette.divider }]}
style={styles.trackRow}
onPress={() => void onPlay(track.id as Id<"songs">, songIds)}
>
<Text
style={[styles.trackNumber, { color: palette.secondaryForeground }]}
>
{track.track || index + 1}
{number}
</Text>
<View style={styles.trackMeta}>
<Text
style={[styles.trackTitle, { color: palette.foreground }]}
numberOfLines={1}
>
{track.title}
</Text>
{track.artists ? (
<View
style={[styles.trackContent, { borderBottomColor: palette.divider }]}
>
<View style={styles.trackMeta}>
<Text
style={{ color: palette.secondaryForeground }}
style={[styles.trackTitle, { color: palette.foreground }]}
numberOfLines={1}
>
{track.artists.join(", ")}
{track.title}
</Text>
) : null}
{track.artists ? (
<Text
style={{ color: palette.secondaryForeground }}
numberOfLines={1}
>
{track.artists.join(", ")}
</Text>
) : null}
</View>
<Text style={{ color: palette.secondaryForeground }}>
{formatDuration(track.durationSeconds)}
</Text>
</View>
<Text style={{ color: palette.secondaryForeground }}>
{formatDuration(track.durationSeconds)}
</Text>
</Pressable>
</MediaContextMenu>
)
@@ -326,13 +398,22 @@ const styles = StyleSheet.create({
trackRow: {
flexDirection: "row",
alignItems: "center",
gap: spacing.sm,
paddingLeft: collectionInset,
},
trackNumber: { width: 24, fontSize: 16, textAlign: "center" },
trackContent: {
flex: 1,
minWidth: 0,
flexDirection: "row",
alignItems: "center",
alignSelf: "stretch",
gap: spacing.md,
minHeight: 60,
paddingHorizontal: spacing.lg,
minHeight: 52,
paddingVertical: 12,
marginRight: collectionInset,
borderBottomWidth: StyleSheet.hairlineWidth,
},
trackNumber: { width: 24, textAlign: "center" },
trackMeta: { flex: 1, minWidth: 0 },
trackTitle: { fontSize: 16, fontWeight: "500" },
})
@@ -1,6 +1,11 @@
import { useMutation } from "convex/react"
import { memo, useCallback, useEffect, useMemo } from "react"
import { Stack, useIsFocused, useLocalSearchParams, useRouter } from "expo-router"
import {
Stack,
useIsFocused,
useLocalSearchParams,
useRouter,
} from "expo-router"
import { StatusBar } from "expo-status-bar"
import {
FlatList,
@@ -14,6 +19,7 @@ import {
import ReanimatedSwipeable from "react-native-gesture-handler/ReanimatedSwipeable"
import { Ionicons } from "@expo/vector-icons"
import { CollectionDivider } from "@/components/collection-list"
import { CoverImage } from "@/components/cover-image"
import {
SwipeToRemoveRow,
@@ -389,6 +395,7 @@ const PlaylistSongRow = memo(function PlaylistSongRow({
favorited={favorited}
playlistLabel={PLAYLIST_LABEL}
palette={palette}
inset
onPlay={onPlay}
onAction={onAction}
/>
@@ -470,54 +477,61 @@ const PlaylistHero = memo(function PlaylistHero({
onShuffle: () => void
}) {
return (
<View style={styles.hero}>
<CoverImage uri={coverUri} hasCover={hasCover} style={styles.cover} />
<Text style={[styles.title, { color: palette.foreground }]}>{title}</Text>
<Text style={{ color: palette.secondaryForeground }}>{ownerName}</Text>
<View style={styles.actions}>
{showSubscribe ? (
<View>
<View style={styles.hero}>
<CoverImage uri={coverUri} hasCover={hasCover} style={styles.cover} />
<Text style={[styles.title, { color: palette.foreground }]}>
{title}
</Text>
<Text style={{ color: palette.secondaryForeground }}>{ownerName}</Text>
<View style={styles.actions}>
{showSubscribe ? (
<Pressable
accessibilityRole="button"
accessibilityLabel={isSubscribed ? "Subscribed" : "Subscribe"}
accessibilityState={{ selected: isSubscribed }}
hitSlop={8}
onPress={onSubscribeToggle}
style={[
styles.iconAction,
{
backgroundColor: isSubscribed
? palette.surface
: palette.accent,
},
]}
>
<Ionicons
name={isSubscribed ? "checkmark" : "add"}
size={24}
color={
isSubscribed ? palette.foreground : palette.accentForeground
}
/>
</Pressable>
) : null}
<Button
title="Play"
icon="play"
onPress={onPlay}
style={styles.playButton}
tint={{
background: palette.accent,
foreground: palette.accentForeground,
}}
/>
<Pressable
accessibilityRole="button"
accessibilityLabel={isSubscribed ? "Subscribed" : "Subscribe"}
accessibilityState={{ selected: isSubscribed }}
accessibilityLabel="Shuffle"
hitSlop={8}
onPress={onSubscribeToggle}
style={[
styles.iconAction,
{
backgroundColor: isSubscribed ? palette.surface : palette.accent,
},
]}
onPress={onShuffle}
style={[styles.iconAction, { backgroundColor: palette.surface }]}
>
<Ionicons
name={isSubscribed ? "checkmark" : "add"}
size={24}
color={
isSubscribed ? palette.foreground : palette.accentForeground
}
/>
<Ionicons name="shuffle" size={24} color={palette.foreground} />
</Pressable>
) : null}
<Button
title="Play"
icon="play"
onPress={onPlay}
style={styles.playButton}
tint={{
background: palette.accent,
foreground: palette.accentForeground,
}}
/>
<Pressable
accessibilityRole="button"
accessibilityLabel="Shuffle"
hitSlop={8}
onPress={onShuffle}
style={[styles.iconAction, { backgroundColor: palette.surface }]}
>
<Ionicons name="shuffle" size={24} color={palette.foreground} />
</Pressable>
</View>
</View>
<CollectionDivider color={palette.divider} />
</View>
)
})
@@ -8,6 +8,7 @@ import {
type ListRenderItemInfo,
} from "react-native"
import { CollectionDivider } from "@/components/collection-list"
import { CollectionDetailSkeleton } from "@/components/skeletons"
import { Button } from "@/components/ui/primitives"
import { contentBottomInset, SongListRow } from "@/components/player"
@@ -79,6 +80,7 @@ export default function FavoritesScreen() {
song={song}
favorited={isSongFavorite(song.id)}
playlistLabel="Add to Another Playlist"
inset
onPlay={handlePlaySong}
onAction={handleSongMenuAction}
/>
@@ -155,34 +157,39 @@ const FavoritesHero = memo(function FavoritesHero({
const colors = useThemeColors()
return (
<View style={styles.hero}>
<View
style={[
styles.cover,
styles.coverPlaceholder,
{ backgroundColor: colors.coverPlaceholder },
]}
>
<Ionicons name="star" size={120} color={colors.primary} />
</View>
<Text style={[styles.title, { color: colors.foreground }]}>{title}</Text>
<View style={styles.actions}>
<Button
title="Play"
icon="play"
onPress={onPlay}
style={styles.playButton}
/>
<Pressable
accessibilityRole="button"
accessibilityLabel="Shuffle"
hitSlop={8}
onPress={onShuffle}
style={[styles.iconAction, { backgroundColor: colors.muted }]}
<View>
<View style={styles.hero}>
<View
style={[
styles.cover,
styles.coverPlaceholder,
{ backgroundColor: colors.coverPlaceholder },
]}
>
<Ionicons name="shuffle" size={24} color={colors.foreground} />
</Pressable>
<Ionicons name="star" size={120} color={colors.primary} />
</View>
<Text style={[styles.title, { color: colors.foreground }]}>
{title}
</Text>
<View style={styles.actions}>
<Button
title="Play"
icon="play"
onPress={onPlay}
style={styles.playButton}
/>
<Pressable
accessibilityRole="button"
accessibilityLabel="Shuffle"
hitSlop={8}
onPress={onShuffle}
style={[styles.iconAction, { backgroundColor: colors.muted }]}
>
<Ionicons name="shuffle" size={24} color={colors.foreground} />
</Pressable>
</View>
</View>
<CollectionDivider />
</View>
)
})
@@ -0,0 +1,46 @@
import { memo } from "react"
import { StyleSheet, Text, View } from "react-native"
import { useThemeColors } from "@/lib/theme"
/** Horizontal inset for rows and dividers in album and playlist track lists. */
export const collectionInset = 20
/** Separates a collection's header from its track list. */
export function CollectionDivider({ color }: { color?: string }) {
const colors = useThemeColors()
return (
<View
style={[styles.divider, { backgroundColor: color ?? colors.border }]}
/>
)
}
export const DiscHeader = memo(function DiscHeader({
label,
color,
}: {
label: string
color?: string
}) {
const colors = useThemeColors()
return (
<Text style={[styles.discHeader, { color: color ?? colors.foreground }]}>
{label}
</Text>
)
})
const styles = StyleSheet.create({
divider: {
height: StyleSheet.hairlineWidth,
marginHorizontal: collectionInset,
},
discHeader: {
fontSize: 17,
fontWeight: "700",
paddingHorizontal: collectionInset,
paddingTop: 24,
paddingBottom: 8,
},
})
+50 -13
View File
@@ -1,3 +1,4 @@
import { collectionInset } from "@/components/collection-list"
import { CoverImage } from "@/components/cover-image"
import { useServerConfig } from "@/contexts/server-config"
import type { BrowseSong } from "@otunes/convex/types"
@@ -20,6 +21,7 @@ export const SongRow = memo(function SongRow({
contextMenu,
trailing,
palette,
inset = false,
}: {
song: BrowseSong
onPress?: () => void
@@ -28,6 +30,8 @@ export const SongRow = memo(function SongRow({
}
trailing?: React.ReactNode
palette?: Pick<CoverPalette, "foreground" | "secondaryForeground" | "divider">
/** Album/playlist layout: the divider starts at the title, not the artwork. */
inset?: boolean
}) {
const { config } = useServerConfig()
const colors = useThemeColors()
@@ -36,20 +40,10 @@ export const SongRow = memo(function SongRow({
const foreground = palette?.foreground ?? colors.foreground
const secondaryForeground =
palette?.secondaryForeground ?? colors.mutedForeground
const divider = palette?.divider ?? colors.border
const row = (
<Pressable
style={[
styles.row,
{ borderBottomColor: palette?.divider ?? colors.border },
]}
onPress={onPress}
>
<CoverImage
uri={albumCoverUrl(config.serverUrl, song.album.id)}
hasCover={song.album.cover != null}
style={styles.rowArtwork}
/>
const content = (
<>
<View style={styles.rowMeta}>
<Text style={[styles.rowTitle, { color: foreground }]} numberOfLines={1}>
{song.title}
@@ -62,6 +56,28 @@ export const SongRow = memo(function SongRow({
{formatDuration(song.durationSeconds)}
</Text>
{trailing}
</>
)
const row = (
<Pressable
style={
inset ? styles.insetRow : [styles.row, { borderBottomColor: divider }]
}
onPress={onPress}
>
<CoverImage
uri={albumCoverUrl(config.serverUrl, song.album.id)}
hasCover={song.album.cover != null}
style={styles.rowArtwork}
/>
{inset ? (
<View style={[styles.insetContent, { borderBottomColor: divider }]}>
{content}
</View>
) : (
content
)}
</Pressable>
)
@@ -86,6 +102,7 @@ export const SongListRow = memo(function SongListRow({
favorited,
playlistLabel,
palette,
inset,
onPlay,
onAction,
}: {
@@ -93,6 +110,7 @@ export const SongListRow = memo(function SongListRow({
favorited: boolean
playlistLabel?: string
palette?: Pick<CoverPalette, "foreground" | "secondaryForeground" | "divider">
inset?: boolean
onPlay: (song: BrowseSong) => void
onAction: (song: BrowseSong, action: MediaAction) => void
}) {
@@ -102,6 +120,7 @@ export const SongListRow = memo(function SongListRow({
<SongRow
song={song}
palette={palette}
inset={inset}
onPress={handlePress}
contextMenu={{
favorited,
@@ -134,6 +153,24 @@ const styles = StyleSheet.create({
paddingHorizontal: spacing.lg,
borderBottomWidth: StyleSheet.hairlineWidth,
},
insetRow: {
flexDirection: "row",
alignItems: "center",
gap: spacing.md,
paddingLeft: collectionInset,
},
insetContent: {
flex: 1,
minWidth: 0,
flexDirection: "row",
alignItems: "center",
alignSelf: "stretch",
gap: spacing.md,
minHeight: 64,
paddingVertical: 10,
marginRight: collectionInset,
borderBottomWidth: StyleSheet.hairlineWidth,
},
rowArtwork: {
width: 44,
height: 44,