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
This commit is contained in:
4 files changed
+111
-20
No files matched your search
@@ -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)
|
||||
|
||||
@@ -36,6 +36,13 @@ static FAV_ALBUMS: LazyLock<RwLock<HashSet<String>>> =
|
||||
static FAV_AWARDS: LazyLock<RwLock<HashSet<String>>> =
|
||||
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<RwLock<HashSet<u64>>> =
|
||||
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<Option<FavoritesCacheStore>> = 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<u64>) {
|
||||
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.
|
||||
|
||||
+69
-19
@@ -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::<u64>()
|
||||
.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::<ArtistState>();
|
||||
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<dyn std::error::Error>> {
|
||||
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::<u64>() {
|
||||
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::<u64>()) {
|
||||
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::<ArtistState>();
|
||||
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::<ArtistState>();
|
||||
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") => {
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user