From 4a740c918c13d95a03855ace40fbbfb82cba0098 Mon Sep 17 00:00:00 2001 From: Joshua Higgins Date: Thu, 8 Oct 2026 17:07:49 -0400 Subject: [PATCH] feat(mobile): add client-first playback caching - Cache queries and songs across mobile and web - Show cached content and skeletons during loading - Move shared playback and cache utilities into Convex client --- .../app/(app)/(tabs)/home/album/[id].tsx | 21 +- .../app/(app)/(tabs)/home/artist/[id].tsx | 9 +- .../app/(app)/(tabs)/home/browse/albums.tsx | 19 +- .../app/(app)/(tabs)/home/browse/artists.tsx | 19 +- .../(app)/(tabs)/home/browse/playlists.tsx | 8 +- .../app/(app)/(tabs)/home/browse/songs.tsx | 19 +- apps/mobile/app/(app)/(tabs)/home/index.tsx | 32 +- .../app/(app)/(tabs)/home/playlist/[id].tsx | 37 +- .../(app)/(tabs)/home/playlist/favorites.tsx | 17 +- apps/mobile/app/(app)/(tabs)/home/queue.tsx | 166 +++-- .../app/(app)/(tabs)/home/recently-added.tsx | 11 +- .../app/(app)/(tabs)/home/recently-played.tsx | 11 +- .../mobile/app/(app)/(tabs)/home/settings.tsx | 6 +- .../mobile/app/(app)/(tabs)/library/index.tsx | 10 +- apps/mobile/app/(app)/_layout.tsx | 24 +- apps/mobile/app/(app)/player.tsx | 6 +- .../components/convex-auth-recovery.tsx | 19 +- .../components/profile-header-button.tsx | 4 +- apps/mobile/components/search-results.tsx | 10 +- apps/mobile/components/skeletons.tsx | 257 +++++++ apps/mobile/contexts/add-to-playlist.tsx | 36 +- apps/mobile/contexts/convex-app.tsx | 5 +- apps/mobile/contexts/favorites.tsx | 5 +- apps/mobile/contexts/persistent-cache.tsx | 51 ++ apps/mobile/contexts/playback.tsx | 704 +++++++++--------- apps/mobile/hooks/use-cached-query.ts | 88 +++ apps/mobile/hooks/use-media-menu-actions.ts | 39 +- apps/mobile/lib/persistent-cache.ts | 67 ++ apps/mobile/lib/playback-files.ts | 48 ++ apps/mobile/lib/session.ts | 10 + apps/mobile/lib/song-cache.ts | 49 ++ .../components/add-to-playlist-provider.tsx | 2 +- apps/web/components/album-card.tsx | 2 +- apps/web/components/app-sidebar.tsx | 2 +- apps/web/components/artist-view.tsx | 2 +- apps/web/components/artists-view.tsx | 2 +- apps/web/components/playback-provider.tsx | 8 +- apps/web/components/playlist-card.tsx | 2 +- apps/web/hooks/use-cached-query.ts | 2 +- apps/web/lib/persistent-cache.ts | 238 ++---- apps/web/lib/shuffle-array.ts | 8 - apps/web/lib/song-cache.ts | 147 +--- .../convex/client}/cached-query.ts | 17 +- packages/convex/client/persistent-cache.ts | 222 ++++++ .../convex/client}/playback-queue.ts | 12 +- .../convex/client}/playback-store.ts | 10 +- packages/convex/client/song-cache.ts | 135 ++++ packages/convex/package.json | 20 + 48 files changed, 1710 insertions(+), 928 deletions(-) create mode 100644 apps/mobile/components/skeletons.tsx create mode 100644 apps/mobile/contexts/persistent-cache.tsx create mode 100644 apps/mobile/hooks/use-cached-query.ts create mode 100644 apps/mobile/lib/persistent-cache.ts create mode 100644 apps/mobile/lib/playback-files.ts create mode 100644 apps/mobile/lib/song-cache.ts delete mode 100644 apps/web/lib/shuffle-array.ts rename {apps/web/lib => packages/convex/client}/cached-query.ts (77%) create mode 100644 packages/convex/client/persistent-cache.ts rename {apps/web/lib => packages/convex/client}/playback-queue.ts (94%) rename {apps/web/lib => packages/convex/client}/playback-store.ts (97%) create mode 100644 packages/convex/client/song-cache.ts diff --git a/apps/mobile/app/(app)/(tabs)/home/album/[id].tsx b/apps/mobile/app/(app)/(tabs)/home/album/[id].tsx index e223014..cbdb52c 100644 --- a/apps/mobile/app/(app)/(tabs)/home/album/[id].tsx +++ b/apps/mobile/app/(app)/(tabs)/home/album/[id].tsx @@ -1,5 +1,5 @@ -import { useQuery } from "convex/react" import { Stack, useLocalSearchParams } from "expo-router" +import { useEffect } from "react" import { FlatList, Pressable, StyleSheet, Text, View } from "react-native" import { Ionicons } from "@expo/vector-icons" @@ -9,11 +9,13 @@ import { MediaActionMenu, MediaContextMenu, } from "@/components/media-actions-sheet" -import { Button, LoadingView } from "@/components/ui/primitives" +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 { useCoverBackgroundColor } from "@/hooks/use-cover-background-color" import { useMediaMenuActions } from "@/hooks/use-media-menu-actions" import { api } from "@otunes/convex/_generated/api" @@ -21,6 +23,7 @@ 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 { albumDetailSongs, rememberSongs } from "@/lib/song-cache" import { albumCoverUrl } from "@/lib/urls" import { radius, spacing, useThemeColors } from "@/lib/theme" @@ -29,7 +32,7 @@ export default function AlbumScreen() { const { push } = useAppNavigation() const colors = useThemeColors() const { config } = useServerConfig() - const album = useQuery( + const album = useCachedQuery( api.library.browseAlbumDetail, typeof id === "string" ? { id: id as Id<"albums"> } : "skip" ) @@ -49,6 +52,12 @@ export default function AlbumScreen() { colors.background ) + useEffect(() => { + if (album) { + rememberSongs(albumDetailSongs(album)) + } + }, [album]) + if (album === undefined || !config) { return ( <> @@ -58,7 +67,7 @@ export default function AlbumScreen() { headerLargeTitle: false, }} /> - + ) } @@ -159,14 +168,14 @@ export default function AlbumScreen() {