feat(home): filter user sections by genre
- Add getSelectedGenreName() to genre filter store - Add filterAlbumsByGenre() helper in HomeView - Apply genre filter to Recently Played and Favorite Albums - Fetch 3x more items when filtering to ensure enough results
This commit is contained in:
1 parent
eee364e29c
commit
22ba76a8d1
2 files changed
+29
-6
No files matched your search
@@ -18,6 +18,7 @@
|
|||||||
} from '$lib/stores/homeSettingsStore';
|
} from '$lib/stores/homeSettingsStore';
|
||||||
import {
|
import {
|
||||||
getSelectedGenreId,
|
getSelectedGenreId,
|
||||||
|
getSelectedGenreName,
|
||||||
hasActiveFilter as hasGenreFilter
|
hasActiveFilter as hasGenreFilter
|
||||||
} from '$lib/stores/genreFilterStore';
|
} from '$lib/stores/genreFilterStore';
|
||||||
import { setPlaybackContext } from '$lib/stores/playbackContextStore';
|
import { setPlaybackContext } from '$lib/stores/playbackContextStore';
|
||||||
@@ -445,6 +446,15 @@
|
|||||||
loadHome();
|
loadHome();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function filterAlbumsByGenre(albums: AlbumCardData[]): AlbumCardData[] {
|
||||||
|
const selectedGenreName = getSelectedGenreName();
|
||||||
|
if (!selectedGenreName) return albums;
|
||||||
|
// Filter albums whose genre contains the selected genre name (case-insensitive)
|
||||||
|
return albums.filter(album =>
|
||||||
|
album.genre.toLowerCase().includes(selectedGenreName.toLowerCase())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
async function loadHome() {
|
async function loadHome() {
|
||||||
isInitializing = true;
|
isInitializing = true;
|
||||||
isOverlayVisible = true;
|
isOverlayVisible = true;
|
||||||
@@ -558,9 +568,12 @@
|
|||||||
// Recently Played (albums) - start immediately with seeds
|
// Recently Played (albums) - start immediately with seeds
|
||||||
if (isSectionVisible('recentAlbums')) {
|
if (isSectionVisible('recentAlbums')) {
|
||||||
const recentAlbumIds = normalizeAlbumIds(seeds.recentlyPlayedAlbumIds);
|
const recentAlbumIds = normalizeAlbumIds(seeds.recentlyPlayedAlbumIds);
|
||||||
fetchAlbums(recentAlbumIds.slice(0, homeLimits.recentAlbums)).then(async albums => {
|
// Fetch more if filtering, to have enough after filter
|
||||||
recentAlbums = albums;
|
const fetchLimit = hasGenreFilter() ? homeLimits.recentAlbums * 3 : homeLimits.recentAlbums;
|
||||||
await loadAllAlbumDownloadStatuses(albums);
|
fetchAlbums(recentAlbumIds.slice(0, fetchLimit)).then(async albums => {
|
||||||
|
const filtered = filterAlbumsByGenre(albums).slice(0, homeLimits.recentAlbums);
|
||||||
|
recentAlbums = filtered;
|
||||||
|
await loadAllAlbumDownloadStatuses(filtered);
|
||||||
loadingRecentAlbums = false;
|
loadingRecentAlbums = false;
|
||||||
markSectionFinished();
|
markSectionFinished();
|
||||||
});
|
});
|
||||||
@@ -587,9 +600,12 @@
|
|||||||
...seeds.favoriteAlbumIds,
|
...seeds.favoriteAlbumIds,
|
||||||
...favoriteTrackDetails.map(track => track.albumId)
|
...favoriteTrackDetails.map(track => track.albumId)
|
||||||
]);
|
]);
|
||||||
const albums = await fetchAlbums(favoriteAlbumIds.slice(0, homeLimits.favoriteAlbums));
|
// Fetch more if filtering, to have enough after filter
|
||||||
favoriteAlbums = albums;
|
const fetchLimit = hasGenreFilter() ? homeLimits.favoriteAlbums * 3 : homeLimits.favoriteAlbums;
|
||||||
await loadAllAlbumDownloadStatuses(albums);
|
const albums = await fetchAlbums(favoriteAlbumIds.slice(0, fetchLimit));
|
||||||
|
const filtered = filterAlbumsByGenre(albums).slice(0, homeLimits.favoriteAlbums);
|
||||||
|
favoriteAlbums = filtered;
|
||||||
|
await loadAllAlbumDownloadStatuses(filtered);
|
||||||
loadingFavoriteAlbums = false;
|
loadingFavoriteAlbums = false;
|
||||||
markSectionFinished();
|
markSectionFinished();
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -109,6 +109,13 @@ export function getSelectedGenreId(): number | undefined {
|
|||||||
return ids.length === 1 ? ids[0] : undefined;
|
return ids.length === 1 ? ids[0] : undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getSelectedGenreName(): string | undefined {
|
||||||
|
const id = getSelectedGenreId();
|
||||||
|
if (!id) return undefined;
|
||||||
|
const genre = state.availableGenres.find(g => g.id === id);
|
||||||
|
return genre?.name;
|
||||||
|
}
|
||||||
|
|
||||||
export function isGenreSelected(genreId: number): boolean {
|
export function isGenreSelected(genreId: number): boolean {
|
||||||
return state.selectedGenreIds.has(genreId);
|
return state.selectedGenreIds.has(genreId);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user