fix(mobile): paginate tracks in playlists (#8)
Co-authored-by: Rem Helper <remhelper@abunchofknowitalls.com> Reviewed-on: https://git.abunchofknowitalls.com/joshuafhiggins/otunes/pulls/8
This commit is contained in:
1 parent
6eb8520025
commit
f3bb32e656
2 files changed
+88
-67
No files matched your search
@@ -1,6 +1,6 @@
|
||||
import { useMutation, useQuery } from "convex/react"
|
||||
import { useMutation, usePaginatedQuery, useQuery } from "convex/react"
|
||||
import { Stack, useLocalSearchParams, useRouter } from "expo-router"
|
||||
import { Pressable, ScrollView, StyleSheet, Text, View } from "react-native"
|
||||
import { FlatList, Pressable, StyleSheet, Text, View } from "react-native"
|
||||
import { Ionicons } from "@expo/vector-icons"
|
||||
|
||||
import { CoverImage } from "@/components/cover-image"
|
||||
@@ -18,11 +18,12 @@ 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 { BrowseSong } from "@otunes/convex/types"
|
||||
import { showError, showSuccess } from "@/lib/toast"
|
||||
import { playlistCoverUrl } from "@/lib/urls"
|
||||
import { radius, spacing, useThemeColors } from "@/lib/theme"
|
||||
|
||||
const PAGE_SIZE = 50
|
||||
|
||||
export default function PlaylistScreen() {
|
||||
const { id } = useLocalSearchParams<{ id: string }>()
|
||||
const router = useRouter()
|
||||
@@ -30,6 +31,19 @@ export default function PlaylistScreen() {
|
||||
const { config } = useServerConfig()
|
||||
const playlist = useQuery(api.playlists.browsePlaylistDetail, {
|
||||
id: id as Id<"playlists">,
|
||||
includeSongs: false,
|
||||
})
|
||||
const {
|
||||
results: songs,
|
||||
status,
|
||||
loadMore,
|
||||
} = usePaginatedQuery(
|
||||
api.playlists.browsePlaylistSongsPaginated,
|
||||
{ id: id as Id<"playlists"> },
|
||||
{ initialNumItems: PAGE_SIZE }
|
||||
)
|
||||
const allSongIds = useQuery(api.playlists.getPlaylistSongIds, {
|
||||
playlistId: id as Id<"playlists">,
|
||||
})
|
||||
const { playPlaylist, playPlaylistShuffled, playFromSong } = usePlayback()
|
||||
const { isSongFavorite } = useFavorites()
|
||||
@@ -41,7 +55,7 @@ export default function PlaylistScreen() {
|
||||
const coverUri = config && playlist ? playlistCoverUrl(config.serverUrl, playlist.id ?? "favorites") : ""
|
||||
const backgroundColor = useCoverBackgroundColor(coverUri, playlist?.cover != null, colors.background)
|
||||
|
||||
if (playlist === undefined || !config) {
|
||||
if (playlist === undefined || songs === undefined || allSongIds === undefined || !config) {
|
||||
return (
|
||||
<>
|
||||
<Stack.Screen
|
||||
@@ -73,7 +87,7 @@ export default function PlaylistScreen() {
|
||||
)
|
||||
}
|
||||
|
||||
const songIds = playlist.songs.map((song: BrowseSong) => song.id)
|
||||
const songIds = allSongIds
|
||||
|
||||
async function handleSubscribeToggle() {
|
||||
if (!playlist?.id) return
|
||||
@@ -146,68 +160,73 @@ export default function PlaylistScreen() {
|
||||
),
|
||||
}}
|
||||
/>
|
||||
<ScrollView
|
||||
<FlatList
|
||||
data={songs}
|
||||
keyExtractor={(song) => song.id}
|
||||
style={{ flex: 1, backgroundColor }}
|
||||
contentContainerStyle={{ paddingBottom: contentBottomInset }}
|
||||
contentInsetAdjustmentBehavior="automatic"
|
||||
>
|
||||
<View style={styles.hero}>
|
||||
<CoverImage
|
||||
uri={coverUri}
|
||||
hasCover={playlist.cover != null}
|
||||
style={styles.cover}
|
||||
/>
|
||||
<Text style={[styles.title, { color: colors.foreground }]}>{playlist.title}</Text>
|
||||
<Text style={{ color: colors.mutedForeground }}>{playlist.owner.name}</Text>
|
||||
<View style={styles.actions}>
|
||||
{!playlist.isOwner && playlist.id ? (
|
||||
onEndReached={() => {
|
||||
if (status === "CanLoadMore") loadMore(PAGE_SIZE)
|
||||
}}
|
||||
onEndReachedThreshold={0.5}
|
||||
ListHeaderComponent={
|
||||
<View style={styles.hero}>
|
||||
<CoverImage
|
||||
uri={coverUri}
|
||||
hasCover={playlist.cover != null}
|
||||
style={styles.cover}
|
||||
/>
|
||||
<Text style={[styles.title, { color: colors.foreground }]}>{playlist.title}</Text>
|
||||
<Text style={{ color: colors.mutedForeground }}>{playlist.owner.name}</Text>
|
||||
<View style={styles.actions}>
|
||||
{!playlist.isOwner && playlist.id ? (
|
||||
<Pressable
|
||||
accessibilityRole="button"
|
||||
accessibilityLabel={playlist.isSubscribed ? "Subscribed" : "Subscribe"}
|
||||
accessibilityState={{ selected: playlist.isSubscribed }}
|
||||
hitSlop={8}
|
||||
onPress={() => void handleSubscribeToggle()}
|
||||
style={[
|
||||
styles.iconAction,
|
||||
{
|
||||
backgroundColor: playlist.isSubscribed
|
||||
? colors.muted
|
||||
: colors.primary,
|
||||
},
|
||||
]}
|
||||
>
|
||||
<Ionicons
|
||||
name={playlist.isSubscribed ? "checkmark" : "add"}
|
||||
size={24}
|
||||
color={
|
||||
playlist.isSubscribed
|
||||
? colors.foreground
|
||||
: colors.primaryForeground
|
||||
}
|
||||
/>
|
||||
</Pressable>
|
||||
) : null}
|
||||
<Button
|
||||
title="Play"
|
||||
icon="play"
|
||||
onPress={() => playlist.id && void playPlaylist(playlist.id)}
|
||||
style={styles.playButton}
|
||||
/>
|
||||
<Pressable
|
||||
accessibilityRole="button"
|
||||
accessibilityLabel={playlist.isSubscribed ? "Subscribed" : "Subscribe"}
|
||||
accessibilityState={{ selected: playlist.isSubscribed }}
|
||||
accessibilityLabel="Shuffle"
|
||||
hitSlop={8}
|
||||
onPress={() => void handleSubscribeToggle()}
|
||||
style={[
|
||||
styles.iconAction,
|
||||
{
|
||||
backgroundColor: playlist.isSubscribed
|
||||
? colors.muted
|
||||
: colors.primary,
|
||||
},
|
||||
]}
|
||||
onPress={() => playlist.id && void playPlaylistShuffled(playlist.id)}
|
||||
style={[styles.iconAction, { backgroundColor: colors.muted }]}
|
||||
>
|
||||
<Ionicons
|
||||
name={playlist.isSubscribed ? "checkmark" : "add"}
|
||||
size={24}
|
||||
color={
|
||||
playlist.isSubscribed
|
||||
? colors.foreground
|
||||
: colors.primaryForeground
|
||||
}
|
||||
/>
|
||||
<Ionicons name="shuffle" size={24} color={colors.foreground} />
|
||||
</Pressable>
|
||||
) : null}
|
||||
<Button
|
||||
title="Play"
|
||||
icon="play"
|
||||
onPress={() => playlist.id && void playPlaylist(playlist.id)}
|
||||
style={styles.playButton}
|
||||
/>
|
||||
<Pressable
|
||||
accessibilityRole="button"
|
||||
accessibilityLabel="Shuffle"
|
||||
hitSlop={8}
|
||||
onPress={() => playlist.id && void playPlaylistShuffled(playlist.id)}
|
||||
style={[styles.iconAction, { backgroundColor: colors.muted }]}
|
||||
>
|
||||
<Ionicons name="shuffle" size={24} color={colors.foreground} />
|
||||
</Pressable>
|
||||
</View>
|
||||
</View>
|
||||
</View>
|
||||
|
||||
{playlist.songs.map((song: BrowseSong) => (
|
||||
}
|
||||
renderItem={({ item: song }) => (
|
||||
<SongRow
|
||||
key={song.id}
|
||||
song={song}
|
||||
onPress={() => void playFromSong(song.id, songIds)}
|
||||
contextMenu={{
|
||||
@@ -238,8 +257,8 @@ export default function PlaylistScreen() {
|
||||
) : null
|
||||
}
|
||||
/>
|
||||
))}
|
||||
</ScrollView>
|
||||
)}
|
||||
/>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -138,21 +138,23 @@ export const browsePlaylistSummary = query({
|
||||
})
|
||||
|
||||
export const browsePlaylistDetail = query({
|
||||
args: { id: v.id("playlists") },
|
||||
args: { id: v.id("playlists"), includeSongs: v.optional(v.boolean()) },
|
||||
handler: async (ctx, args): Promise<BrowsePlaylistDetail | null> => {
|
||||
const summary = await browsePlaylistSummaryHandler(ctx, args.id)
|
||||
if (!summary) {
|
||||
return null
|
||||
}
|
||||
|
||||
const artistMap = await loadArtistMap(ctx)
|
||||
const albumMap = await loadAlbumMap(ctx)
|
||||
|
||||
const songs: BrowseSong[] = []
|
||||
for (const songId of summary.songIds) {
|
||||
const song = await toBrowseSong(ctx, songId, artistMap, albumMap)
|
||||
if (song) {
|
||||
songs.push(song)
|
||||
if (args.includeSongs ?? true) {
|
||||
const artistMap = await loadArtistMap(ctx)
|
||||
const albumMap = await loadAlbumMap(ctx)
|
||||
|
||||
for (const songId of summary.songIds) {
|
||||
const song = await toBrowseSong(ctx, songId, artistMap, albumMap)
|
||||
if (song) {
|
||||
songs.push(song)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user