- Virtualize mobile lists and defer queue loading - Split playback state from actions to reduce rerenders - Skip invalid or unnecessary Convex queries Reviewed-on: #18
271 lines
8.4 KiB
TypeScript
271 lines
8.4 KiB
TypeScript
import { useQuery } from "convex/react"
|
|
import { Stack, useLocalSearchParams } from "expo-router"
|
|
import { FlatList, Pressable, StyleSheet, Text, View } from "react-native"
|
|
import { Ionicons } from "@expo/vector-icons"
|
|
|
|
import { CoverImage } from "@/components/cover-image"
|
|
|
|
import {
|
|
MediaActionMenu,
|
|
MediaContextMenu,
|
|
} from "@/components/media-actions-sheet"
|
|
import { Button, LoadingView } 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 { useCoverBackgroundColor } from "@/hooks/use-cover-background-color"
|
|
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 { BrowseAlbumTrack } from "@otunes/convex/types"
|
|
import { formatDuration, formatMetadata } from "@/lib/format"
|
|
import { useAppNavigation } from "@/lib/app-navigation"
|
|
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 = useQuery(
|
|
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 coverUri =
|
|
config && album ? albumCoverUrl(config.serverUrl, album.id) : ""
|
|
const backgroundColor = useCoverBackgroundColor(
|
|
coverUri,
|
|
album?.cover != null,
|
|
colors.background
|
|
)
|
|
|
|
if (album === undefined || !config) {
|
|
return (
|
|
<>
|
|
<Stack.Screen
|
|
options={{
|
|
title: "",
|
|
headerLargeTitle: false,
|
|
}}
|
|
/>
|
|
<LoadingView />
|
|
</>
|
|
)
|
|
}
|
|
|
|
if (album === null) {
|
|
return (
|
|
<>
|
|
<Stack.Screen
|
|
options={{
|
|
title: "",
|
|
headerLargeTitle: false,
|
|
}}
|
|
/>
|
|
<View style={[styles.empty, { backgroundColor: colors.background }]}>
|
|
<Text style={{ color: colors.mutedForeground }}>
|
|
Album not found.
|
|
</Text>
|
|
</View>
|
|
</>
|
|
)
|
|
}
|
|
|
|
const songIds = album.tracks.map(
|
|
(track: BrowseAlbumTrack) => track.id as Id<"songs">
|
|
)
|
|
const metadata = formatMetadata(album.genre, album.year)
|
|
|
|
return (
|
|
<>
|
|
<Stack.Screen
|
|
options={{
|
|
title: "",
|
|
headerLargeTitle: false,
|
|
headerRight: () => (
|
|
<MediaActionMenu
|
|
selectedCount={album.tracks.length}
|
|
favorited={isAlbumFav(album.id)}
|
|
share={{ type: "album", albumId: album.id }}
|
|
onAction={(action) =>
|
|
void handleAlbumDetailAction(
|
|
album.id as Id<"albums">,
|
|
songIds,
|
|
action
|
|
)
|
|
}
|
|
>
|
|
<Pressable hitSlop={8}>
|
|
<Ionicons
|
|
name="ellipsis-horizontal"
|
|
size={22}
|
|
color={colors.foreground}
|
|
/>
|
|
</Pressable>
|
|
</MediaActionMenu>
|
|
),
|
|
}}
|
|
/>
|
|
<FlatList
|
|
data={album.tracks}
|
|
keyExtractor={(track) => track.id}
|
|
style={{ flex: 1, backgroundColor }}
|
|
contentContainerStyle={{ paddingBottom: contentBottomInset }}
|
|
contentInsetAdjustmentBehavior="automatic"
|
|
ListHeaderComponent={
|
|
<View style={styles.hero}>
|
|
<CoverImage
|
|
uri={coverUri}
|
|
hasCover={album.cover != null}
|
|
style={styles.cover}
|
|
/>
|
|
<Text style={[styles.title, { color: colors.foreground }]}>
|
|
{album.title}
|
|
</Text>
|
|
<Pressable onPress={() => push(`/artist/${album.artist.id}`)}>
|
|
<Text style={[styles.artist, { color: colors.mutedForeground }]}>
|
|
{album.artist.name}
|
|
</Text>
|
|
</Pressable>
|
|
{metadata ? (
|
|
<Text style={{ color: colors.mutedForeground }}>{metadata}</Text>
|
|
) : null}
|
|
<View style={styles.actions}>
|
|
<Pressable
|
|
onPress={() => void toggleAlbumFavorite(album.id)}
|
|
hitSlop={8}
|
|
style={[styles.iconAction, { backgroundColor: colors.muted }]}
|
|
>
|
|
<Ionicons
|
|
name={isAlbumFavorite(album.id) ? "star" : "star-outline"}
|
|
size={24}
|
|
color={
|
|
isAlbumFavorite(album.id)
|
|
? colors.primary
|
|
: colors.foreground
|
|
}
|
|
/>
|
|
</Pressable>
|
|
<Button
|
|
title="Play"
|
|
icon="play"
|
|
onPress={() => void playAlbum(album.id)}
|
|
style={styles.playButton}
|
|
/>
|
|
<Pressable
|
|
accessibilityRole="button"
|
|
accessibilityLabel="Shuffle"
|
|
hitSlop={8}
|
|
onPress={() => void playAlbumShuffled(album.id)}
|
|
style={[styles.iconAction, { backgroundColor: colors.muted }]}
|
|
>
|
|
<Ionicons name="shuffle" size={24} color={colors.foreground} />
|
|
</Pressable>
|
|
</View>
|
|
</View>
|
|
}
|
|
renderItem={({ item: track, index }) => (
|
|
<MediaContextMenu
|
|
favorited={isSongFavorite(track.id)}
|
|
navigation={{
|
|
artist: { id: album.artist.id },
|
|
album: { id: album.id },
|
|
}}
|
|
share={{
|
|
type: "song",
|
|
albumId: album.id,
|
|
songId: track.id,
|
|
}}
|
|
onAction={(action) =>
|
|
void handleAlbumTrackAction(track, album, action)
|
|
}
|
|
>
|
|
<Pressable
|
|
style={[styles.trackRow, { borderBottomColor: colors.border }]}
|
|
onPress={() => void playFromSong(track.id, songIds)}
|
|
>
|
|
<Text
|
|
style={[styles.trackNumber, { color: colors.mutedForeground }]}
|
|
>
|
|
{track.track || index + 1}
|
|
</Text>
|
|
<View style={styles.trackMeta}>
|
|
<Text
|
|
style={[styles.trackTitle, { color: colors.foreground }]}
|
|
numberOfLines={1}
|
|
>
|
|
{track.title}
|
|
</Text>
|
|
{track.artists ? (
|
|
<Text
|
|
style={{ color: colors.mutedForeground }}
|
|
numberOfLines={1}
|
|
>
|
|
{track.artists.join(", ")}
|
|
</Text>
|
|
) : null}
|
|
</View>
|
|
<Text style={{ color: colors.mutedForeground }}>
|
|
{formatDuration(track.durationSeconds)}
|
|
</Text>
|
|
</Pressable>
|
|
</MediaContextMenu>
|
|
)}
|
|
/>
|
|
</>
|
|
)
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
empty: { flex: 1, alignItems: "center", justifyContent: "center" },
|
|
hero: {
|
|
alignItems: "center",
|
|
padding: spacing.lg,
|
|
gap: spacing.sm,
|
|
},
|
|
cover: {
|
|
width: 220,
|
|
height: 220,
|
|
borderRadius: radius.lg,
|
|
},
|
|
title: { fontSize: 24, fontWeight: "800", textAlign: "center" },
|
|
artist: { fontSize: 16, fontWeight: "600" },
|
|
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.md,
|
|
minHeight: 60,
|
|
paddingHorizontal: spacing.lg,
|
|
paddingVertical: 12,
|
|
borderBottomWidth: StyleSheet.hairlineWidth,
|
|
},
|
|
trackNumber: { width: 24, textAlign: "center" },
|
|
trackMeta: { flex: 1, minWidth: 0 },
|
|
trackTitle: { fontSize: 16, fontWeight: "500" },
|
|
})
|