feat: pagination in everything but playlists
This commit is contained in:
1 parent
27641c6a5f
commit
fc2f2d43cb
7 files changed
+134
-72
No files matched your search
@@ -1,31 +1,30 @@
|
||||
import { useQuery } from "convex/react"
|
||||
import { ScrollView, StyleSheet } from "react-native"
|
||||
import { usePaginatedQuery } from "convex/react"
|
||||
|
||||
import { MediaGrid } from "@/components/media-cards"
|
||||
import { MediaGridList } from "@/components/media-cards"
|
||||
import { LoadingView } from "@/components/ui/primitives"
|
||||
import { contentBottomInset } from "@/components/player"
|
||||
import { api } from "@otunes/convex/_generated/api"
|
||||
import { spacing, useThemeColors } from "@/lib/theme"
|
||||
|
||||
const PAGE_SIZE = 50
|
||||
|
||||
export default function AlbumsScreen() {
|
||||
const colors = useThemeColors()
|
||||
const albums = useQuery(api.library.browseAlbums)
|
||||
const { results, status, loadMore } = usePaginatedQuery(
|
||||
api.library.browseAlbumsPaginated,
|
||||
{},
|
||||
{ initialNumItems: PAGE_SIZE }
|
||||
)
|
||||
|
||||
if (albums === undefined) {
|
||||
if (results === undefined) {
|
||||
return <LoadingView />
|
||||
}
|
||||
|
||||
return (
|
||||
<ScrollView
|
||||
style={{ flex: 1, backgroundColor: colors.background }}
|
||||
contentContainerStyle={[styles.content, { paddingBottom: contentBottomInset }]}
|
||||
contentInsetAdjustmentBehavior="automatic"
|
||||
>
|
||||
<MediaGrid content={albums} />
|
||||
</ScrollView>
|
||||
<MediaGridList
|
||||
content={results}
|
||||
paddingBottom={contentBottomInset}
|
||||
onEndReached={() => {
|
||||
if (status === "CanLoadMore") loadMore(PAGE_SIZE)
|
||||
}}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
const styles = StyleSheet.create({
|
||||
content: { padding: spacing.lg },
|
||||
})
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useQuery } from "convex/react"
|
||||
import { Pressable, ScrollView, StyleSheet, Text } from "react-native"
|
||||
import { usePaginatedQuery } from "convex/react"
|
||||
import { FlatList, Pressable, StyleSheet, Text } from "react-native"
|
||||
|
||||
import { MediaContextMenu } from "@/components/media-context-menu"
|
||||
import { CoverImage } from "@/components/cover-image"
|
||||
@@ -13,26 +13,36 @@ import type { BrowseArtist } from "@otunes/convex/types"
|
||||
import { artistCoverUrl } from "@/lib/urls"
|
||||
import { spacing, useThemeColors } from "@/lib/theme"
|
||||
|
||||
const PAGE_SIZE = 50
|
||||
|
||||
export default function ArtistsScreen() {
|
||||
const { push } = useAppNavigation()
|
||||
const colors = useThemeColors()
|
||||
const { config } = useServerConfig()
|
||||
const artists = useQuery(api.library.browseArtists)
|
||||
const { results, status, loadMore } = usePaginatedQuery(
|
||||
api.library.browseArtistsPaginated,
|
||||
{},
|
||||
{ initialNumItems: PAGE_SIZE }
|
||||
)
|
||||
const { handleArtistAction, isArtistFavorite } = useMediaMenuActions()
|
||||
|
||||
if (artists === undefined || !config) {
|
||||
if (results === undefined || !config) {
|
||||
return <LoadingView />
|
||||
}
|
||||
|
||||
return (
|
||||
<ScrollView
|
||||
<FlatList
|
||||
data={results}
|
||||
keyExtractor={(artist) => artist.id}
|
||||
style={{ flex: 1, backgroundColor: colors.background }}
|
||||
contentContainerStyle={{ paddingBottom: contentBottomInset }}
|
||||
contentInsetAdjustmentBehavior="automatic"
|
||||
>
|
||||
{artists.map((artist: BrowseArtist) => (
|
||||
onEndReached={() => {
|
||||
if (status === "CanLoadMore") loadMore(PAGE_SIZE)
|
||||
}}
|
||||
onEndReachedThreshold={0.5}
|
||||
renderItem={({ item: artist }: { item: BrowseArtist }) => (
|
||||
<MediaContextMenu
|
||||
key={artist.id}
|
||||
favorited={isArtistFavorite(artist.id)}
|
||||
share={{ type: "artist", artistId: artist.id }}
|
||||
onAction={(action) => void handleArtistAction(artist, action)}
|
||||
@@ -49,8 +59,8 @@ export default function ArtistsScreen() {
|
||||
<Text style={[styles.name, { color: colors.foreground }]}>{artist.name}</Text>
|
||||
</Pressable>
|
||||
</MediaContextMenu>
|
||||
))}
|
||||
</ScrollView>
|
||||
)}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useQuery } from "convex/react"
|
||||
import { usePaginatedQuery } from "convex/react"
|
||||
import { memo, useRef } from "react"
|
||||
import { ScrollView, StyleSheet } from "react-native"
|
||||
|
||||
@@ -9,6 +9,8 @@ import { api } from "@otunes/convex/_generated/api"
|
||||
import type { BrowseAlbum, PlaylistSummary } from "@otunes/convex/types"
|
||||
import { spacing, useThemeColors } from "@/lib/theme"
|
||||
|
||||
const CAROUSEL_ITEM_LIMIT = 10
|
||||
|
||||
const HomeCarousels = memo(function HomeCarousels({
|
||||
recentlyAdded,
|
||||
recentlyPlayed,
|
||||
@@ -36,8 +38,16 @@ const HomeCarousels = memo(function HomeCarousels({
|
||||
|
||||
export default function HomeScreen() {
|
||||
const colors = useThemeColors()
|
||||
const recentlyAdded = useQuery(api.library.browseRecentlyAdded)
|
||||
const recentlyPlayed = useQuery(api.library.browseRecentlyPlayed)
|
||||
const { results: recentlyAdded } = usePaginatedQuery(
|
||||
api.library.browseRecentlyAddedPaginated,
|
||||
{},
|
||||
{ initialNumItems: CAROUSEL_ITEM_LIMIT }
|
||||
)
|
||||
const { results: recentlyPlayed } = usePaginatedQuery(
|
||||
api.library.browseRecentlyPlayedPaginated,
|
||||
{},
|
||||
{ initialNumItems: CAROUSEL_ITEM_LIMIT }
|
||||
)
|
||||
const cachedData = useRef<{
|
||||
recentlyAdded?: BrowseAlbum[]
|
||||
recentlyPlayed?: (BrowseAlbum | PlaylistSummary)[]
|
||||
|
||||
@@ -1,31 +1,30 @@
|
||||
import { useQuery } from "convex/react"
|
||||
import { ScrollView, StyleSheet } from "react-native"
|
||||
import { usePaginatedQuery } from "convex/react"
|
||||
|
||||
import { MediaGrid } from "@/components/media-cards"
|
||||
import { MediaGridList } from "@/components/media-cards"
|
||||
import { LoadingView } from "@/components/ui/primitives"
|
||||
import { contentBottomInset } from "@/components/player"
|
||||
import { api } from "@otunes/convex/_generated/api"
|
||||
import { spacing, useThemeColors } from "@/lib/theme"
|
||||
|
||||
const PAGE_SIZE = 50
|
||||
|
||||
export default function RecentlyAddedScreen() {
|
||||
const colors = useThemeColors()
|
||||
const recentlyAdded = useQuery(api.library.browseRecentlyAdded)
|
||||
const { results, status, loadMore } = usePaginatedQuery(
|
||||
api.library.browseRecentlyAddedPaginated,
|
||||
{},
|
||||
{ initialNumItems: PAGE_SIZE }
|
||||
)
|
||||
|
||||
if (recentlyAdded === undefined) {
|
||||
if (results === undefined) {
|
||||
return <LoadingView />
|
||||
}
|
||||
|
||||
return (
|
||||
<ScrollView
|
||||
style={{ flex: 1, backgroundColor: colors.background }}
|
||||
contentContainerStyle={[styles.content, { paddingBottom: contentBottomInset }]}
|
||||
contentInsetAdjustmentBehavior="automatic"
|
||||
>
|
||||
<MediaGrid content={recentlyAdded} />
|
||||
</ScrollView>
|
||||
<MediaGridList
|
||||
content={results}
|
||||
paddingBottom={contentBottomInset}
|
||||
onEndReached={() => {
|
||||
if (status === "CanLoadMore") loadMore(PAGE_SIZE)
|
||||
}}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
const styles = StyleSheet.create({
|
||||
content: { padding: spacing.lg },
|
||||
})
|
||||
@@ -1,31 +1,30 @@
|
||||
import { useQuery } from "convex/react"
|
||||
import { ScrollView, StyleSheet } from "react-native"
|
||||
import { usePaginatedQuery } from "convex/react"
|
||||
|
||||
import { MediaGrid } from "@/components/media-cards"
|
||||
import { MediaGridList } from "@/components/media-cards"
|
||||
import { LoadingView } from "@/components/ui/primitives"
|
||||
import { contentBottomInset } from "@/components/player"
|
||||
import { api } from "@otunes/convex/_generated/api"
|
||||
import { spacing, useThemeColors } from "@/lib/theme"
|
||||
|
||||
const PAGE_SIZE = 50
|
||||
|
||||
export default function RecentlyPlayedScreen() {
|
||||
const colors = useThemeColors()
|
||||
const recentlyPlayed = useQuery(api.library.browseRecentlyPlayed)
|
||||
const { results, status, loadMore } = usePaginatedQuery(
|
||||
api.library.browseRecentlyPlayedPaginated,
|
||||
{},
|
||||
{ initialNumItems: PAGE_SIZE }
|
||||
)
|
||||
|
||||
if (recentlyPlayed === undefined) {
|
||||
if (results === undefined) {
|
||||
return <LoadingView />
|
||||
}
|
||||
|
||||
return (
|
||||
<ScrollView
|
||||
style={{ flex: 1, backgroundColor: colors.background }}
|
||||
contentContainerStyle={[styles.content, { paddingBottom: contentBottomInset }]}
|
||||
contentInsetAdjustmentBehavior="automatic"
|
||||
>
|
||||
<MediaGrid content={recentlyPlayed} />
|
||||
</ScrollView>
|
||||
<MediaGridList
|
||||
content={results}
|
||||
paddingBottom={contentBottomInset}
|
||||
onEndReached={() => {
|
||||
if (status === "CanLoadMore") loadMore(PAGE_SIZE)
|
||||
}}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
const styles = StyleSheet.create({
|
||||
content: { padding: spacing.lg },
|
||||
})
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useQuery } from "convex/react"
|
||||
import { usePaginatedQuery } from "convex/react"
|
||||
import { Pressable, ScrollView, StyleSheet, Text, View } from "react-native"
|
||||
import { SymbolView } from "expo-symbols"
|
||||
import { Platform } from "react-native"
|
||||
@@ -11,6 +11,8 @@ import { useAppNavigation } from "@/lib/app-navigation"
|
||||
import { api } from "@otunes/convex/_generated/api"
|
||||
import { appleMusic, spacing, useThemeColors } from "@/lib/theme"
|
||||
|
||||
const RECENTLY_ADDED_PREVIEW_SIZE = 4
|
||||
|
||||
const links = [
|
||||
{ title: "Playlists", href: "/browse/playlists", icon: "music.note.list" as const },
|
||||
{ title: "Artists", href: "/browse/artists", icon: "mic.fill" as const },
|
||||
@@ -46,7 +48,11 @@ function LibraryIcon({ name }: { name: (typeof links)[number]["icon"] }) {
|
||||
export default function LibraryScreen() {
|
||||
const { push } = useAppNavigation()
|
||||
const colors = useThemeColors()
|
||||
const recentlyAdded = useQuery(api.library.browseRecentlyAdded)
|
||||
const { results: recentlyAdded } = usePaginatedQuery(
|
||||
api.library.browseRecentlyAddedPaginated,
|
||||
{},
|
||||
{ initialNumItems: RECENTLY_ADDED_PREVIEW_SIZE }
|
||||
)
|
||||
|
||||
return (
|
||||
<ScrollView
|
||||
@@ -75,7 +81,7 @@ export default function LibraryScreen() {
|
||||
{recentlyAdded === undefined ? (
|
||||
<LoadingView />
|
||||
) : (
|
||||
<MediaGrid content={recentlyAdded.slice(0, 4)} />
|
||||
<MediaGrid content={recentlyAdded} />
|
||||
)}
|
||||
</View>
|
||||
</ScrollView>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Ionicons } from "@expo/vector-icons"
|
||||
import { memo, useCallback } from "react"
|
||||
import { Pressable, ScrollView, StyleSheet, Text, View } from "react-native"
|
||||
import { FlatList, Pressable, ScrollView, StyleSheet, Text, View } from "react-native"
|
||||
|
||||
import { CoverImage } from "@/components/cover-image"
|
||||
import { MediaContextMenu } from "@/components/media-context-menu"
|
||||
@@ -11,7 +11,7 @@ import type { BrowseAlbum, BrowseArtist, PlaylistSummary } from "@otunes/convex/
|
||||
import { albumCoverUrl, artistCoverUrl, playlistCoverUrl } from "@/lib/urls"
|
||||
import { carouselCardWidth, radius, spacing, useThemeColors, useTwoColumnCardWidth } from "@/lib/theme"
|
||||
|
||||
type MediaItem = BrowseAlbum | BrowseArtist | PlaylistSummary
|
||||
export type MediaItem = BrowseAlbum | BrowseArtist | PlaylistSummary
|
||||
|
||||
export const AlbumCard = memo(function AlbumCard({
|
||||
album,
|
||||
@@ -175,6 +175,38 @@ export function MediaGrid({ content }: { content: MediaItem[] }) {
|
||||
)
|
||||
}
|
||||
|
||||
export function MediaGridList({
|
||||
content,
|
||||
paddingBottom,
|
||||
onEndReached,
|
||||
}: {
|
||||
content: MediaItem[]
|
||||
paddingBottom: number
|
||||
onEndReached?: () => void
|
||||
}) {
|
||||
const cardWidth = useTwoColumnCardWidth()
|
||||
const colors = useThemeColors()
|
||||
|
||||
return (
|
||||
<FlatList
|
||||
data={content}
|
||||
keyExtractor={(item) => item.id}
|
||||
numColumns={2}
|
||||
style={{ flex: 1, backgroundColor: colors.background }}
|
||||
contentContainerStyle={[styles.gridListContent, { paddingBottom }]}
|
||||
columnWrapperStyle={styles.gridListRow}
|
||||
contentInsetAdjustmentBehavior="automatic"
|
||||
onEndReached={onEndReached}
|
||||
onEndReachedThreshold={0.5}
|
||||
renderItem={({ item }) => (
|
||||
<View style={{ width: cardWidth }}>
|
||||
<CarouselCard item={item} width={cardWidth} />
|
||||
</View>
|
||||
)}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function ContentCarouselBody({
|
||||
title,
|
||||
href,
|
||||
@@ -250,4 +282,11 @@ const styles = StyleSheet.create({
|
||||
flexWrap: "wrap",
|
||||
gap: spacing.md,
|
||||
},
|
||||
gridListContent: {
|
||||
padding: spacing.lg,
|
||||
gap: spacing.md,
|
||||
},
|
||||
gridListRow: {
|
||||
gap: spacing.md,
|
||||
},
|
||||
})
|
||||
Reference in new issue
Block a user