593 lines
19 KiB
TypeScript
593 lines
19 KiB
TypeScript
import {
|
|
CollectionHero,
|
|
useCollectionHeroLayout,
|
|
} from "@/components/collection-hero"
|
|
import {
|
|
PaletteText,
|
|
PaletteIcon,
|
|
renderArtworkBackButton,
|
|
} from "@/components/palette-text"
|
|
import Animated from "react-native-reanimated"
|
|
import {
|
|
renderAndroidArtworkBackground,
|
|
useScreenPalette,
|
|
} from "@/contexts/android-tab-theme"
|
|
import { MusicEntityView } from "@/components/music-entity-view"
|
|
import { Link, Stack, useIsFocused, useLocalSearchParams } from "expo-router"
|
|
import { StatusBar } from "expo-status-bar"
|
|
import { memo, useCallback, useEffect, useMemo } from "react"
|
|
import { Platform, Pressable, StyleSheet, View } from "react-native"
|
|
|
|
import {
|
|
CollectionDivider,
|
|
collectionInset,
|
|
DiscHeader,
|
|
} from "@/components/collection-list"
|
|
import { AudioFormatBadge } from "@/components/audio-format-badge"
|
|
import { CoverImage } from "@/components/cover-image"
|
|
|
|
import {
|
|
MediaActionMenu,
|
|
MediaContextMenu,
|
|
type MediaAction,
|
|
} from "@/components/media-actions-sheet"
|
|
import { CollectionDetailSkeleton } from "@/components/skeletons"
|
|
import { Button } from "@/components/ui/primitives"
|
|
import { contentBottomInset } from "@/components/player"
|
|
import { useFavorites } from "@/contexts/favorites"
|
|
import { usePlaybackActions } from "@/contexts/playback"
|
|
import { useServerConfig } from "@/contexts/server-config"
|
|
import { useCachedQuery } from "@/hooks/use-cached-query"
|
|
import { useCoverPalette } from "@/hooks/use-cover-palette"
|
|
import { useMediaMenuActions } from "@/hooks/use-media-menu-actions"
|
|
import { api } from "@otunes/convex/_generated/api"
|
|
import type { Id } from "@otunes/convex/_generated/dataModel"
|
|
import type { BrowseAlbumDetail, BrowseAlbumTrack } from "@otunes/convex/types"
|
|
import type { CoverPalette } from "@/lib/cover-palette"
|
|
import { formatDuration, formatMetadata } from "@/lib/format"
|
|
import { useAppNavigation } from "@/lib/app-navigation"
|
|
import { longListProps } from "@/lib/list"
|
|
import { albumDetailSongs, rememberSongs } from "@/lib/song-cache"
|
|
import { albumCoverUrl } from "@/lib/urls"
|
|
import { radius, spacing, useThemeColors } from "@/lib/theme"
|
|
|
|
export default function AlbumScreen() {
|
|
const { id } = useLocalSearchParams<{ id: string }>()
|
|
const { push } = useAppNavigation()
|
|
const colors = useThemeColors()
|
|
const { config } = useServerConfig()
|
|
const album = useCachedQuery(
|
|
api.library.browseAlbumDetail,
|
|
typeof id === "string" ? { id: id as Id<"albums"> } : "skip"
|
|
)
|
|
const { playAlbum, playAlbumShuffled, playFromSong } = usePlaybackActions()
|
|
const { isAlbumFavorite, toggleAlbumFavorite, isSongFavorite } =
|
|
useFavorites()
|
|
const {
|
|
handleAlbumDetailAction,
|
|
handleAlbumTrackAction,
|
|
isAlbumFavorite: isAlbumFav,
|
|
} = useMediaMenuActions()
|
|
const isFocused = useIsFocused()
|
|
const coverUri =
|
|
config && typeof id === "string" ? albumCoverUrl(config.serverUrl, id) : ""
|
|
const heroLayout = useCollectionHeroLayout()
|
|
const { palette: artworkPalette, settled } = useCoverPalette(
|
|
coverUri,
|
|
album ? album.cover != null : album === null ? false : undefined
|
|
)
|
|
const { palette, onLayout, backgroundStyle } =
|
|
useScreenPalette(artworkPalette)
|
|
const songIds = useMemo(
|
|
() => album?.tracks.map((track) => track.id as Id<"songs">) ?? [],
|
|
[album]
|
|
)
|
|
|
|
// Older servers do not include album format availability yet.
|
|
const formatTracks = useCachedQuery(
|
|
api.library.getSongsForPlayback,
|
|
album && (album.hasLossless === undefined || album.hasAtmos === undefined)
|
|
? { ids: songIds }
|
|
: "skip"
|
|
)
|
|
const hasLossless =
|
|
album?.hasLossless ??
|
|
formatTracks?.some((track) => track && "lossless" in track.files)
|
|
const hasAtmos =
|
|
album?.hasAtmos ??
|
|
formatTracks?.some((track) => track && "atmos" in track.files)
|
|
|
|
useEffect(() => {
|
|
if (album) {
|
|
rememberSongs(albumDetailSongs(album))
|
|
}
|
|
}, [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.track}
|
|
number={item.number}
|
|
album={album}
|
|
songIds={songIds}
|
|
palette={palette}
|
|
favorited={isSongFavorite(item.track.id)}
|
|
onPlay={(songId, ids) =>
|
|
playFromSong(songId, ids, `album:${album.id}`)
|
|
}
|
|
onAction={handleAlbumTrackAction}
|
|
/>
|
|
) : null
|
|
},
|
|
[
|
|
album,
|
|
handleAlbumTrackAction,
|
|
isSongFavorite,
|
|
palette,
|
|
playFromSong,
|
|
songIds,
|
|
]
|
|
)
|
|
|
|
if (album === undefined || !config || !settled) {
|
|
return (
|
|
<>
|
|
<Stack.Screen
|
|
options={{
|
|
title: "",
|
|
headerLargeTitle: false,
|
|
...(Platform.OS === "android"
|
|
? {
|
|
headerStyle: { backgroundColor: "transparent" },
|
|
headerBackground: renderAndroidArtworkBackground,
|
|
headerBackVisible: false,
|
|
headerLeft: renderArtworkBackButton,
|
|
headerTintColor: palette.foreground,
|
|
}
|
|
: {}),
|
|
}}
|
|
/>
|
|
<CollectionDetailSkeleton
|
|
zoomCoverSize={220}
|
|
onLayout={onLayout}
|
|
style={backgroundStyle}
|
|
backgroundColor={
|
|
Platform.OS === "android" ? palette.background : undefined
|
|
}
|
|
/>
|
|
</>
|
|
)
|
|
}
|
|
|
|
if (album === null) {
|
|
return (
|
|
<>
|
|
<Stack.Screen
|
|
options={{
|
|
title: "",
|
|
headerLargeTitle: false,
|
|
}}
|
|
/>
|
|
<View style={[styles.empty, { backgroundColor: colors.background }]}>
|
|
<PaletteText style={{ color: colors.mutedForeground }}>
|
|
Album not found.
|
|
</PaletteText>
|
|
</View>
|
|
</>
|
|
)
|
|
}
|
|
|
|
const metadata = formatMetadata(album.genre, album.year)
|
|
const favorited = isAlbumFavorite(album.id)
|
|
|
|
return (
|
|
<>
|
|
{isFocused ? (
|
|
<StatusBar style={palette.isDark ? "light" : "dark"} />
|
|
) : null}
|
|
<Stack.Screen
|
|
options={{
|
|
title: "",
|
|
headerLargeTitle: false,
|
|
headerTintColor: palette.foreground,
|
|
...(Platform.OS === "android"
|
|
? {
|
|
headerStyle: { backgroundColor: "transparent" },
|
|
headerBackground: renderAndroidArtworkBackground,
|
|
headerBackVisible: false,
|
|
headerLeft: renderArtworkBackButton,
|
|
statusBarStyle: palette.isDark
|
|
? ("light" as const)
|
|
: ("dark" as const),
|
|
}
|
|
: {}),
|
|
headerRight: () => (
|
|
<MediaActionMenu
|
|
stationArtistId={album.artist.id}
|
|
selectedCount={album.tracks.length}
|
|
favorited={isAlbumFav(album.id)}
|
|
share={{ type: "album", albumId: album.id, title: album.title }}
|
|
onAction={(action) =>
|
|
void handleAlbumDetailAction(
|
|
album.id as Id<"albums">,
|
|
songIds,
|
|
action,
|
|
album.artist.id as Id<"artists">
|
|
)
|
|
}
|
|
>
|
|
<Pressable hitSlop={8}>
|
|
<PaletteIcon
|
|
tone="foreground"
|
|
name="ellipsis-horizontal"
|
|
size={22}
|
|
color={palette.isDark ? "#fff" : "#000"}
|
|
/>
|
|
</Pressable>
|
|
</MediaActionMenu>
|
|
),
|
|
}}
|
|
/>
|
|
<Animated.FlatList
|
|
onLayout={onLayout}
|
|
{...longListProps}
|
|
data={listItems}
|
|
keyExtractor={albumListItemKey}
|
|
style={[
|
|
{ flex: 1, backgroundColor: palette.background },
|
|
backgroundStyle,
|
|
]}
|
|
contentContainerStyle={{ paddingBottom: contentBottomInset }}
|
|
contentInsetAdjustmentBehavior="automatic"
|
|
indicatorStyle={palette.isDark ? "white" : "black"}
|
|
ListHeaderComponent={
|
|
<View>
|
|
<MusicEntityView kind="album" id={album.id}>
|
|
<CollectionHero
|
|
artwork={
|
|
<Link.AppleZoomTarget>
|
|
<View collapsable={false}>
|
|
<CoverImage
|
|
uri={coverUri}
|
|
hasCover={album.cover != null}
|
|
style={[
|
|
styles.cover,
|
|
heroLayout.coverSize
|
|
? {
|
|
width: heroLayout.coverSize,
|
|
height: heroLayout.coverSize,
|
|
}
|
|
: undefined,
|
|
]}
|
|
/>
|
|
</View>
|
|
</Link.AppleZoomTarget>
|
|
}
|
|
actions={
|
|
<View
|
|
style={[
|
|
styles.actions,
|
|
heroLayout.landscape && { flexDirection: "row-reverse" },
|
|
]}
|
|
>
|
|
<Pressable
|
|
onPress={() => void toggleAlbumFavorite(album.id)}
|
|
hitSlop={8}
|
|
style={[
|
|
styles.iconAction,
|
|
{ backgroundColor: palette.surface },
|
|
]}
|
|
>
|
|
<PaletteIcon
|
|
tone={favorited ? "accent" : "foreground"}
|
|
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 },
|
|
]}
|
|
>
|
|
<PaletteIcon
|
|
tone="foreground"
|
|
name="shuffle"
|
|
size={24}
|
|
color={palette.foreground}
|
|
/>
|
|
</Pressable>
|
|
</View>
|
|
}
|
|
>
|
|
<PaletteText
|
|
tone="foreground"
|
|
style={[
|
|
styles.title,
|
|
heroLayout.landscape && styles.titleLandscape,
|
|
{ color: palette.foreground },
|
|
]}
|
|
>
|
|
{album.title}
|
|
</PaletteText>
|
|
<Pressable
|
|
onPress={() => push(`/artist/${album.artist.id}`)}
|
|
style={styles.artistPressable}
|
|
>
|
|
<PaletteText
|
|
tone="accentText"
|
|
style={[
|
|
styles.artist,
|
|
heroLayout.landscape && styles.artistLandscape,
|
|
{ color: palette.accentText },
|
|
]}
|
|
>
|
|
{album.artist.name}
|
|
</PaletteText>
|
|
</Pressable>
|
|
<View
|
|
style={{
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
justifyContent: heroLayout.landscape
|
|
? "flex-start"
|
|
: "center",
|
|
flexWrap: "wrap",
|
|
gap: 8,
|
|
}}
|
|
>
|
|
{metadata ? (
|
|
<PaletteText
|
|
tone="secondaryForeground"
|
|
style={{ color: palette.secondaryForeground }}
|
|
>
|
|
{metadata}
|
|
</PaletteText>
|
|
) : null}
|
|
{hasLossless ? (
|
|
<>
|
|
{metadata ? (
|
|
<PaletteText
|
|
tone="secondaryForeground"
|
|
style={{ color: palette.secondaryForeground }}
|
|
>
|
|
·
|
|
</PaletteText>
|
|
) : null}
|
|
<AudioFormatBadge
|
|
artwork
|
|
format="lossless"
|
|
color={palette.secondaryForeground}
|
|
backgroundColor={palette.background}
|
|
/>
|
|
</>
|
|
) : null}
|
|
{hasAtmos ? (
|
|
<>
|
|
{metadata || hasLossless ? (
|
|
<PaletteText
|
|
tone="secondaryForeground"
|
|
style={{ color: palette.secondaryForeground }}
|
|
>
|
|
·
|
|
</PaletteText>
|
|
) : null}
|
|
<AudioFormatBadge
|
|
artwork
|
|
format="atmos"
|
|
color={palette.secondaryForeground}
|
|
backgroundColor={palette.background}
|
|
/>
|
|
</>
|
|
) : null}
|
|
</View>
|
|
</CollectionHero>
|
|
</MusicEntityView>
|
|
<CollectionDivider color={palette.divider} />
|
|
</View>
|
|
}
|
|
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,
|
|
number,
|
|
album,
|
|
songIds,
|
|
palette,
|
|
favorited,
|
|
onPlay,
|
|
onAction,
|
|
}: {
|
|
track: BrowseAlbumTrack
|
|
number: number
|
|
album: BrowseAlbumDetail
|
|
songIds: Id<"songs">[]
|
|
palette: CoverPalette
|
|
favorited: boolean
|
|
onPlay: (songId: Id<"songs">, songIds: Id<"songs">[]) => Promise<void>
|
|
onAction: (
|
|
track: BrowseAlbumTrack,
|
|
album: BrowseAlbumDetail,
|
|
action: MediaAction
|
|
) => Promise<void>
|
|
}) {
|
|
return (
|
|
<MediaContextMenu
|
|
stationArtistId={album.artist.id}
|
|
favorited={favorited}
|
|
navigation={{
|
|
artist: { id: album.artist.id },
|
|
album: { id: album.id },
|
|
}}
|
|
share={{
|
|
type: "song",
|
|
albumId: album.id,
|
|
songId: track.id,
|
|
title: `${track.title} - ${album.title}`,
|
|
}}
|
|
onAction={(action) => void onAction(track, album, action)}
|
|
>
|
|
<MusicEntityView kind="song" id={track.id}>
|
|
<Pressable
|
|
style={styles.trackRow}
|
|
onPress={() => void onPlay(track.id as Id<"songs">, songIds)}
|
|
>
|
|
<PaletteText
|
|
tone="secondaryForeground"
|
|
style={[styles.trackNumber, { color: palette.secondaryForeground }]}
|
|
>
|
|
{number}
|
|
</PaletteText>
|
|
<View
|
|
style={[
|
|
styles.trackContent,
|
|
{ borderBottomColor: palette.divider },
|
|
]}
|
|
>
|
|
<View style={styles.trackMeta}>
|
|
<PaletteText
|
|
tone="foreground"
|
|
style={[styles.trackTitle, { color: palette.foreground }]}
|
|
numberOfLines={1}
|
|
>
|
|
{track.title}
|
|
</PaletteText>
|
|
{track.artists ? (
|
|
<PaletteText
|
|
tone="secondaryForeground"
|
|
style={{ color: palette.secondaryForeground }}
|
|
numberOfLines={1}
|
|
>
|
|
{track.artists.join(", ")}
|
|
</PaletteText>
|
|
) : null}
|
|
</View>
|
|
<PaletteText
|
|
tone="secondaryForeground"
|
|
style={{ color: palette.secondaryForeground }}
|
|
>
|
|
{formatDuration(track.durationSeconds)}
|
|
</PaletteText>
|
|
</View>
|
|
</Pressable>
|
|
</MusicEntityView>
|
|
</MediaContextMenu>
|
|
)
|
|
})
|
|
|
|
const styles = StyleSheet.create({
|
|
empty: { flex: 1, alignItems: "center", justifyContent: "center" },
|
|
cover: {
|
|
width: 220,
|
|
height: 220,
|
|
borderRadius: radius.lg,
|
|
},
|
|
title: { fontSize: 24, fontWeight: "800", textAlign: "center" },
|
|
titleLandscape: { textAlign: "left", fontSize: 22 },
|
|
artistLandscape: { textAlign: "left", fontSize: 22, fontWeight: "400" },
|
|
artistPressable: { alignSelf: "stretch" },
|
|
artist: { fontSize: 16, fontWeight: "600", textAlign: "center" },
|
|
actions: {
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
justifyContent: "center",
|
|
gap: spacing.sm,
|
|
marginTop: spacing.sm,
|
|
},
|
|
playButton: { borderRadius: 999, minWidth: 160 },
|
|
iconAction: {
|
|
width: 44,
|
|
height: 44,
|
|
borderRadius: 22,
|
|
alignItems: "center",
|
|
justifyContent: "center",
|
|
},
|
|
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: 52,
|
|
paddingVertical: 12,
|
|
marginRight: collectionInset,
|
|
borderBottomWidth: StyleSheet.hairlineWidth,
|
|
},
|
|
trackMeta: { flex: 1, minWidth: 0 },
|
|
trackTitle: { fontSize: 16, fontWeight: "500" },
|
|
})
|