From 1e642758ada64f973506d0a2fb45fb0f37da43ac Mon Sep 17 00:00:00 2001 From: vicrodh <103399283+vicrodh@users.noreply.github.com> Date: Fri, 6 Feb 2026 22:08:10 -0600 Subject: [PATCH] integrate home data cache for instant back-navigation On mount, check if a valid cache exists (< 5 min, same genre filter). If valid, restore all section data instantly and scroll position, skipping all API calls. Cache is saved automatically when all sections finish loading. Invalidated on genre filter change or home settings change. --- src/lib/components/views/HomeView.svelte | 85 ++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 5 deletions(-) diff --git a/src/lib/components/views/HomeView.svelte b/src/lib/components/views/HomeView.svelte index 6a11ceafa..6d9cb613f 100644 --- a/src/lib/components/views/HomeView.svelte +++ b/src/lib/components/views/HomeView.svelte @@ -2,6 +2,13 @@ import { onMount } from 'svelte'; import { invoke } from '@tauri-apps/api/core'; import { Music, User, Loader2 } from 'lucide-svelte'; + import { + getHomeCache, + setHomeCache, + clearHomeCache, + isHomeCacheValid, + updateHomeCacheScrollTop + } from '$lib/stores/homeDataCache'; import { t } from '$lib/i18n'; import HorizontalScrollRow from '../HorizontalScrollRow.svelte'; import AlbumCard from '../AlbumCard.svelte'; @@ -299,19 +306,87 @@ ); + let homeViewEl: HTMLDivElement | undefined; + onMount(() => { - // Subscribe to home settings changes + // Subscribe to home settings changes — invalidate cache on change const unsubscribe = subscribeHomeSettings(() => { homeSettings = getSettings(); homeLimits = getSettings().limits; + clearHomeCache(); }); - // Load home data - loadHome(); + // Try to restore from cache + const currentGenreIds = Array.from(getSelectedGenreIds()); + if (isHomeCacheValid(currentGenreIds)) { + const cached = getHomeCache()!; + // Restore all data instantly + newReleases = cached.newReleases; + pressAwards = cached.pressAwards; + mostStreamed = cached.mostStreamed; + qobuzissimes = cached.qobuzissimes; + editorPicks = cached.editorPicks; + recentAlbums = cached.recentAlbums; + continueTracks = cached.continueTracks; + topArtists = cached.topArtists; + favoriteAlbums = cached.favoriteAlbums; + qobuzPlaylists = cached.qobuzPlaylists; + essentialDiscography = cached.essentialDiscography; + playlistTags = cached.playlistTags; + + // Mark all sections as loaded + loadingNewReleases = false; + loadingPressAwards = false; + loadingMostStreamed = false; + loadingQobuzissimes = false; + loadingEditorPicks = false; + loadingRecentAlbums = false; + loadingContinueTracks = false; + loadingTopArtists = false; + loadingFavoriteAlbums = false; + loadingQobuzPlaylists = false; + loadingEssentialDiscography = false; + + // Restore scroll position after DOM renders + requestAnimationFrame(() => { + if (homeViewEl && cached.scrollTop > 0) { + homeViewEl.scrollTop = cached.scrollTop; + } + }); + + // Fire-and-forget: refresh download statuses in background + const allAlbums = [ + ...cached.newReleases, ...cached.pressAwards, ...cached.mostStreamed, + ...cached.qobuzissimes, ...cached.editorPicks, + ...cached.recentAlbums, ...cached.favoriteAlbums + ]; + loadAllAlbumDownloadStatuses(allAlbums); + } else { + loadHome(); + } return unsubscribe; }); + // Save cache when all sections finish loading successfully + $effect(() => { + if (!anyLoading && hasContent) { + const genreIds = Array.from(getSelectedGenreIds()); + setHomeCache({ + newReleases, pressAwards, mostStreamed, qobuzissimes, editorPicks, + recentAlbums, continueTracks, topArtists, favoriteAlbums, + qobuzPlaylists, essentialDiscography, playlistTags, + genreIds + }); + } + }); + + // Save scroll position incrementally + function handleHomeScroll(e: Event) { + const target = e.target as HTMLElement; + updateHomeCacheScrollTop(target.scrollTop); + } + function handleArtistImageError(artistId: number) { failedArtistImages = new Set([...failedArtistImages, artistId]); } @@ -577,7 +652,7 @@ } function handleGenreFilterChange() { - // Reload home page with new genre filter + clearHomeCache(); loadHome(); } @@ -826,7 +901,7 @@ } -
+
{#if homeSettings.greeting.enabled}