From d906e5564705528c1a0e936bf4fa6dfbf0534145 Mon Sep 17 00:00:00 2001 From: vicrodh <103399283+vicrodh@users.noreply.github.com> Date: Sat, 27 Jun 2026 15:21:42 -0600 Subject: [PATCH] feat(artist): make the follow heart + artist-card follow a working toggle The ArtistView header heart dispatched a dead ("artist","","favorite") with an empty id. Wire it to the artist follow (Qobuz favorite = follow) as a real toggle that reflects state: bind the heart to ArtistState.is-following, seed it on artist open, and toggle add/remove via a new in-memory artist fav cache (fav_cache::is_artist_favorite / set_all_artists / set_artist) seeded by search + the artist page. The ("artist","follow") arm becomes a toggle (was add-only), so the search/discovery artist-card Follow chips can unfollow too. Optimistic flip on the cache + every visible surface, revert on failure. cc --- crates/qbz-ui/ui/artist/ArtistPageView.slint | 3 +- crates/qbz/src/fav_cache.rs | 38 +++++++++ crates/qbz/src/main.rs | 88 +++++++++++++++----- crates/qbz/src/search.rs | 2 + 4 files changed, 111 insertions(+), 20 deletions(-) diff --git a/crates/qbz-ui/ui/artist/ArtistPageView.slint b/crates/qbz-ui/ui/artist/ArtistPageView.slint index b3225d42f..422fac6a8 100644 --- a/crates/qbz-ui/ui/artist/ArtistPageView.slint +++ b/crates/qbz-ui/ui/artist/ArtistPageView.slint @@ -379,8 +379,9 @@ export component ArtistPageView inherits Rectangle { spacing: 12px; CircleAction { icon: @image-url("../assets/icons/heart.svg"); + active: ArtistState.is-following; clicked => { - root.media-action("artist", "", "favorite"); + root.media-action("artist", ArtistState.id, "follow"); } } // Radio with dropdown — QBZ radio (local engine) diff --git a/crates/qbz/src/fav_cache.rs b/crates/qbz/src/fav_cache.rs index 61bf4c0cd..3f1ddaf1a 100644 --- a/crates/qbz/src/fav_cache.rs +++ b/crates/qbz/src/fav_cache.rs @@ -36,6 +36,13 @@ static FAV_ALBUMS: LazyLock>> = static FAV_AWARDS: LazyLock>> = LazyLock::new(|| RwLock::new(HashSet::new())); +/// Process-wide set of the user's followed ARTIST ids (u64). IN-MEMORY ONLY +/// (no per-user disk store): the search + artist-page surfaces re-seed it from +/// `favorite_artist_ids` on load, so the follow heart toggle always has the +/// current state without a per-click network hit. +static FAV_ARTISTS: LazyLock>> = + LazyLock::new(|| RwLock::new(HashSet::new())); + /// Per-user persistent ID store. `None` until a session (online or offline) /// is activated; pure in-memory behavior in that window. static STORE: Mutex> = Mutex::new(None); @@ -114,6 +121,9 @@ pub fn teardown() { if let Ok(mut guard) = FAV_AWARDS.write() { guard.clear(); } + if let Ok(mut guard) = FAV_ARTISTS.write() { + guard.clear(); + } } /// Replace the cache with a freshly-fetched set and mirror it to the @@ -235,6 +245,34 @@ pub fn set_album(album_id: &str, favorite: bool) { } } +// ==================== Followed (favorite) artists ==================== + +/// True when the artist id is in the user's followed-artist set. +pub fn is_artist_favorite(artist_id: u64) -> bool { + FAV_ARTISTS + .read() + .map(|g| g.contains(&artist_id)) + .unwrap_or(false) +} + +/// Replace the followed-artist set with a freshly-fetched id list (in-memory). +pub fn set_all_artists(ids: HashSet) { + if let Ok(mut guard) = FAV_ARTISTS.write() { + *guard = ids; + } +} + +/// Insert / remove a single artist id (optimistic follow toggle). +pub fn set_artist(artist_id: u64, favorite: bool) { + if let Ok(mut guard) = FAV_ARTISTS.write() { + if favorite { + guard.insert(artist_id); + } else { + guard.remove(&artist_id); + } + } +} + // ==================== Followed awards ==================== /// True when the award id is in the user's followed-award set. diff --git a/crates/qbz/src/main.rs b/crates/qbz/src/main.rs index 551f39337..1dd236a80 100644 --- a/crates/qbz/src/main.rs +++ b/crates/qbz/src/main.rs @@ -1507,6 +1507,30 @@ fn navigate_artist( }); artwork::spawn_loads(jobs, weak.clone(), image_cache.clone()); + // Seed the follow heart: pull the user's followed artists (also + // refreshes the in-memory cache the toggle reads), then reflect + // whether THIS artist is followed. + { + let runtime_fav = runtime.clone(); + let weak_fav = weak.clone(); + let aid_fav = artist_id.clone(); + tokio::spawn(async move { + if let Ok(ids) = runtime_fav.core().favorite_artist_ids().await { + let is_fav = aid_fav + .parse::() + .map(|a| ids.contains(&a)) + .unwrap_or(false); + crate::fav_cache::set_all_artists(ids); + let _ = weak_fav.upgrade_in_event_loop(move |w| { + let ast = w.global::(); + if ast.get_id().as_str() == aid_fav.as_str() { + ast.set_is_following(is_fav); + } + }); + } + }); + } + // Magazine / Stories — fetch the editorial teasers in // parallel; the sidebar section stays hidden if there are none. { @@ -8672,28 +8696,54 @@ fn main() -> Result<(), Box> { id.clone(), ), ("artist", "follow") => { - let runtime = runtime.clone(); - let weak = weak.clone(); - let artist_id = id.clone(); - handle.spawn(async move { - match runtime.core().add_favorite("artist", &artist_id).await { - Ok(()) => { - // reco: log the artist favorite (add-only follow - // arm). Qobuz artist id -> u64. - if let Ok(aid) = artist_id.parse::() { - tokio::task::spawn_blocking(move || { - crate::reco::log_favorite_artist(aid) + // Toggle the artist follow (= Qobuz artist favorite). State + // source = the in-memory artist fav cache (seeded by search + + // the artist page). Optimistic flip on the cache + every + // visible surface (search cards + the ArtistView heart), + // revert on network failure. + if let (Some(w), Ok(aid)) = (weak.upgrade(), id.parse::()) { + let following = crate::fav_cache::is_artist_favorite(aid); + let make = !following; + crate::fav_cache::set_artist(aid, make); + search::mark_artist_followed(&w, &id, make); + let ast = w.global::(); + if ast.get_id().as_str() == id.as_str() { + ast.set_is_following(make); + } + let runtime = runtime.clone(); + let weak = weak.clone(); + let artist_id = id.clone(); + handle.spawn(async move { + let res = if make { + runtime.core().add_favorite("artist", &artist_id).await + } else { + runtime.core().remove_favorite("artist", &artist_id).await + }; + match res { + Ok(()) => { + // reco: log the favorite only on ADD. + if make { + tokio::task::spawn_blocking(move || { + crate::reco::log_favorite_artist(aid) + }); + } + } + Err(e) => { + log::error!( + "[qbz-slint] toggle follow artist failed: {e}" + ); + crate::fav_cache::set_artist(aid, following); + let _ = weak.upgrade_in_event_loop(move |w| { + search::mark_artist_followed(&w, &artist_id, following); + let ast = w.global::(); + if ast.get_id().as_str() == artist_id.as_str() { + ast.set_is_following(following); + } }); } - let _ = weak.upgrade_in_event_loop(move |w| { - search::mark_artist_followed(&w, &artist_id, true); - }); } - Err(e) => { - log::error!("[qbz-slint] follow artist failed: {e}"); - } - } - }); + }); + } } // === Label landing actions =============================== ("label", "follow") => { diff --git a/crates/qbz/src/search.rs b/crates/qbz/src/search.rs index 6dfea1c50..c1b543637 100644 --- a/crates/qbz/src/search.rs +++ b/crates/qbz/src/search.rs @@ -1039,6 +1039,8 @@ where ); let results = results.map_err(|e| e.to_string())?; let favs = favs.unwrap_or_default(); + // Seed the in-memory artist fav cache so the follow toggle has current state. + crate::fav_cache::set_all_artists(favs.clone()); Ok(map_search_all(query, results, &favs)) }