feat(slint): immersive suggestions panel (split) with live artist queries
Add ImmersiveSuggestionsPanel for the split-right slot (split-panel 2): a cards row (up to 2 artist-playlist book-collage cards + a Song-Radio diamond-collage card) and a recommended-tracks list. New suggestions.rs controller assembles everything in Rust (ADR-006): artist tracks_appears_on with an artist-tracks fallback under 5, dedupe by title, drop the current track, shuffle, take 10; cards from the first two playlists plus a track radio. New core method QbzCore::create_smart_track_radio mirrors the smart artist radio (spawn_blocking + block_on, seed hoisted to index 0). Does NOT use reco_store (live Qobuz queries only). Collage tilt deferred (flat for now); blacklist drop happens downstream in play_tracks. cc
This commit is contained in:
8 files changed
+1296
-2
No files matched your search
@@ -173,6 +173,13 @@ pub enum ArtworkTarget {
|
||||
/// One hero-mosaic cover slot (0-8) of the My QBZ collection-detail view
|
||||
/// (`MyQbzDetailState.cover{N}`).
|
||||
MyQbzDetailCover { slot: usize },
|
||||
/// One collage cover slot (0-3) of an immersive Suggestions card
|
||||
/// (`SuggestionsState.cards[card_idx].cover{slot}`). Playlist cards use
|
||||
/// up to 3 slots (book collage), the radio card up to 4 (diamond collage).
|
||||
SuggestionCardCover { card_idx: usize, slot: usize },
|
||||
/// A row thumbnail in the immersive Suggestions recommended-tracks list
|
||||
/// (`SuggestionsState.tracks[idx]`).
|
||||
SuggestionTrackCover { idx: usize },
|
||||
}
|
||||
|
||||
impl ArtworkTarget {
|
||||
@@ -186,6 +193,7 @@ impl ArtworkTarget {
|
||||
| ArtworkTarget::FavoriteTrack { .. }
|
||||
| ArtworkTarget::MixTrack { .. }
|
||||
| ArtworkTarget::PlaylistTrack { .. }
|
||||
| ArtworkTarget::SuggestionTrackCover { .. }
|
||||
| ArtworkTarget::LocalArtistRowImage { .. } => 96,
|
||||
// Sidebar micro-collage tiles render at ~10-20px; decode tiny.
|
||||
ArtworkTarget::SidebarPlaylistCover { .. } => 48,
|
||||
@@ -1141,5 +1149,25 @@ fn apply_artwork(
|
||||
ArtworkTarget::MyQbzDetailCover { slot } => {
|
||||
crate::myqbz_detail::set_hero_cover(window, slot, image);
|
||||
}
|
||||
ArtworkTarget::SuggestionCardCover { card_idx, slot } => {
|
||||
let model = window.global::<crate::SuggestionsState>().get_cards();
|
||||
if let Some(mut item) = model.row_data(card_idx) {
|
||||
match slot {
|
||||
0 => item.cover0 = image,
|
||||
1 => item.cover1 = image,
|
||||
2 => item.cover2 = image,
|
||||
3 => item.cover3 = image,
|
||||
_ => return,
|
||||
}
|
||||
model.set_row_data(card_idx, item);
|
||||
}
|
||||
}
|
||||
ArtworkTarget::SuggestionTrackCover { idx } => {
|
||||
let model = window.global::<crate::SuggestionsState>().get_tracks();
|
||||
if let Some(mut item) = model.row_data(idx) {
|
||||
item.artwork = image;
|
||||
model.set_row_data(idx, item);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -101,6 +101,7 @@ mod shader_underlay;
|
||||
mod settings;
|
||||
mod share;
|
||||
mod sidebar;
|
||||
mod suggestions;
|
||||
mod toast;
|
||||
mod tray;
|
||||
mod tray_settings;
|
||||
@@ -1675,6 +1676,43 @@ fn navigate_award(
|
||||
});
|
||||
}
|
||||
|
||||
/// Load the immersive Suggestions split panel for the current track. Reads the
|
||||
/// artist-id + track-id + title off NowPlayingState, resets the panel, and
|
||||
/// spawns the live artist load (mirror of navigate_award). An empty artist-id
|
||||
/// or track-id resets to the no-track empty state. Refreshed on track change
|
||||
/// while the panel is open via the mount's `changed live-track-id` -> the
|
||||
/// SuggestionsActions.load callback (which calls this).
|
||||
fn navigate_suggestions(
|
||||
runtime: Arc<AppRuntime<SlintAdapter>>,
|
||||
weak: slint::Weak<AppWindow>,
|
||||
handle: &tokio::runtime::Handle,
|
||||
image_cache: artwork::ImageCache,
|
||||
artist_id: String,
|
||||
track_id: String,
|
||||
track_name: String,
|
||||
) {
|
||||
// No track / no artist -> reset to the empty state and stop. apply with an
|
||||
// empty payload clears cards/tracks and leaves artist-id "" (the empty
|
||||
// branch in the panel).
|
||||
let (Ok(aid), Ok(tid)) = (artist_id.parse::<u64>(), track_id.parse::<u64>()) else {
|
||||
let _ = weak.upgrade_in_event_loop(|w| {
|
||||
suggestions::apply_suggestions(&w, suggestions::empty_payload());
|
||||
});
|
||||
return;
|
||||
};
|
||||
handle.spawn(async move {
|
||||
let _ = weak.upgrade_in_event_loop(|w| {
|
||||
suggestions::reset_suggestions(&w);
|
||||
});
|
||||
let payload = suggestions::load_suggestions(&runtime, aid, tid, track_name).await;
|
||||
let jobs = suggestions::suggestions_artwork_jobs(&payload);
|
||||
let _ = weak.upgrade_in_event_loop(move |w| {
|
||||
suggestions::apply_suggestions(&w, payload);
|
||||
});
|
||||
artwork::spawn_loads(jobs, weak.clone(), image_cache.clone());
|
||||
});
|
||||
}
|
||||
|
||||
/// Open the full AwardAlbumsView listing for `award_id` (paginated grid +
|
||||
/// client-side search). No hero — the kicker uses the passed name (1:1 Tauri).
|
||||
fn navigate_award_albums(
|
||||
@@ -8308,6 +8346,169 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
});
|
||||
}
|
||||
|
||||
// Immersive Suggestions panel actions (Checkpoint D — split-panel == 2).
|
||||
{
|
||||
// load(track-id) — entry + now-playing-change refresh. Reads the
|
||||
// artist-id + title off NowPlayingState (the panel only has the track
|
||||
// id) and kicks the live artist load (mirror of navigate_award).
|
||||
let runtime = app_runtime.clone();
|
||||
let weak = window.as_weak();
|
||||
let handle = tokio_rt.handle().clone();
|
||||
let image_cache = image_cache.clone();
|
||||
window
|
||||
.global::<SuggestionsActions>()
|
||||
.on_load(move |track_id| {
|
||||
let Some(w) = weak.upgrade() else {
|
||||
return;
|
||||
};
|
||||
let np = w.global::<NowPlayingState>();
|
||||
let artist_id = np.get_artist_id().to_string();
|
||||
let track_id = track_id.to_string();
|
||||
let track_name = np.get_title().to_string();
|
||||
// Dedup: skip a reload when the panel already shows this artist
|
||||
// for this seed track (the changed-watcher can refire on
|
||||
// unrelated NowPlayingState churn).
|
||||
let ss = w.global::<SuggestionsState>();
|
||||
if ss.get_artist_id().as_str() == artist_id
|
||||
&& ss.get_seed_track_id().as_str() == track_id
|
||||
&& !track_id.is_empty()
|
||||
{
|
||||
return;
|
||||
}
|
||||
navigate_suggestions(
|
||||
runtime.clone(),
|
||||
weak.clone(),
|
||||
&handle,
|
||||
image_cache.clone(),
|
||||
artist_id,
|
||||
track_id,
|
||||
track_name,
|
||||
);
|
||||
});
|
||||
}
|
||||
{
|
||||
// play / queue / play-next a curated artist playlist by id — reuse the
|
||||
// existing playback seams (same paths the playlist cards use).
|
||||
let runtime = app_runtime.clone();
|
||||
let weak = window.as_weak();
|
||||
let handle = tokio_rt.handle().clone();
|
||||
window
|
||||
.global::<SuggestionsActions>()
|
||||
.on_play_playlist(move |playlist_id| {
|
||||
let id = playlist_id.to_string();
|
||||
if id.is_empty() {
|
||||
return;
|
||||
}
|
||||
playback::play_playlist(runtime.clone(), weak.clone(), handle.clone(), id);
|
||||
});
|
||||
}
|
||||
{
|
||||
let runtime = app_runtime.clone();
|
||||
let weak = window.as_weak();
|
||||
let handle = tokio_rt.handle().clone();
|
||||
window
|
||||
.global::<SuggestionsActions>()
|
||||
.on_queue_playlist(move |playlist_id| {
|
||||
let id = playlist_id.to_string();
|
||||
if id.is_empty() {
|
||||
return;
|
||||
}
|
||||
playback::enqueue_playlist(
|
||||
runtime.clone(),
|
||||
weak.clone(),
|
||||
handle.clone(),
|
||||
id,
|
||||
false,
|
||||
);
|
||||
});
|
||||
}
|
||||
{
|
||||
let runtime = app_runtime.clone();
|
||||
let weak = window.as_weak();
|
||||
let handle = tokio_rt.handle().clone();
|
||||
window
|
||||
.global::<SuggestionsActions>()
|
||||
.on_play_next_playlist(move |playlist_id| {
|
||||
let id = playlist_id.to_string();
|
||||
if id.is_empty() {
|
||||
return;
|
||||
}
|
||||
playback::enqueue_playlist(
|
||||
runtime.clone(),
|
||||
weak.clone(),
|
||||
handle.clone(),
|
||||
id,
|
||||
true,
|
||||
);
|
||||
});
|
||||
}
|
||||
{
|
||||
// start-radio — build the Song Radio off the seed track via core, then
|
||||
// start it (set_queue + play) through the shared play_tracks seam. The
|
||||
// radio card spinner is flipped on optimistically and cleared on apply.
|
||||
let runtime = app_runtime.clone();
|
||||
let weak = window.as_weak();
|
||||
let handle = tokio_rt.handle().clone();
|
||||
window
|
||||
.global::<SuggestionsActions>()
|
||||
.on_start_radio(move |track_id, track_name, artist_id| {
|
||||
let (Ok(tid), Ok(aid)) =
|
||||
(track_id.parse::<u64>(), artist_id.parse::<u64>())
|
||||
else {
|
||||
return;
|
||||
};
|
||||
let track_name = track_name.to_string();
|
||||
suggestions::set_radio_loading(&weak, true);
|
||||
let runtime = runtime.clone();
|
||||
let weak = weak.clone();
|
||||
let handle2 = handle.clone();
|
||||
handle.spawn(async move {
|
||||
let result = runtime
|
||||
.core()
|
||||
.create_smart_track_radio(tid, aid, track_name)
|
||||
.await;
|
||||
suggestions::set_radio_loading(&weak, false);
|
||||
match result {
|
||||
Ok(tracks) if !tracks.is_empty() => {
|
||||
playback::play_tracks(runtime, weak, handle2, tracks, 0);
|
||||
}
|
||||
Ok(_) => {
|
||||
log::warn!("[qbz-slint] song radio returned no tracks");
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("[qbz-slint] song radio failed: {e}");
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
{
|
||||
// play-track — play a single recommended track by id NOW.
|
||||
let runtime = app_runtime.clone();
|
||||
let weak = window.as_weak();
|
||||
let handle = tokio_rt.handle().clone();
|
||||
window
|
||||
.global::<SuggestionsActions>()
|
||||
.on_play_track(move |track_id| {
|
||||
let Ok(tid) = track_id.parse::<u64>() else {
|
||||
return;
|
||||
};
|
||||
let runtime = runtime.clone();
|
||||
let weak = weak.clone();
|
||||
let handle2 = handle.clone();
|
||||
handle.spawn(async move {
|
||||
match runtime.core().get_track(tid).await {
|
||||
Ok(track) => {
|
||||
playback::play_tracks(runtime, weak, handle2, vec![track], 0);
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("[qbz-slint] suggestions play-track {tid} failed: {e}");
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Artist Blacklist Manager actions (Task 11). Mutations are synchronous
|
||||
// (in-memory set + single SQLite ops via the artist_blacklist wrapper), so
|
||||
// no tokio handle is needed; each callback runs on the event-loop thread.
|
||||
|
||||
@@ -0,0 +1,419 @@
|
||||
//! Immersive Suggestions panel controller (split-only, split-panel == 2).
|
||||
//!
|
||||
//! Ports Tauri's `SuggestionsPanel.svelte` 1:1, with all assembly logic in
|
||||
//! Rust (ADR-006): live artist queries only (NEVER `reco_store` — that powers
|
||||
//! the home page; see data-panels.md §6). Two data products:
|
||||
//!
|
||||
//! * RECOMMENDED TRACKS — `artist.tracks_appears_on`, falling back to
|
||||
//! `get_artist_tracks(limit 30)` when sparse (<5), deduped by exact
|
||||
//! lowercase title, the current track filtered out, shuffled, take 10.
|
||||
//! * CARDS — the first 2 curated `artist.playlists` (each a book-collage of
|
||||
//! up to 3 distinct album covers, fetched via `get_playlist`) + ONE seed
|
||||
//! "Song Radio" card (diamond-collage of up to 4 rec-track covers).
|
||||
//!
|
||||
//! The shuffle is a deterministic splitmix64 seeded off the artist+track ids
|
||||
//! (matches qbz-radio's RNG family; avoids pulling `rand` just for this).
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::sync::Arc;
|
||||
|
||||
use qbz_app::shell::AppRuntime;
|
||||
use qbz_core::FrontendAdapter;
|
||||
use slint::{ComponentHandle, Model, ModelRc, VecModel};
|
||||
|
||||
use crate::artwork::{ArtworkJob, ArtworkTarget};
|
||||
use crate::{AppWindow, SuggestionCard, SuggestionsState, TrackItem};
|
||||
|
||||
/// Recommended-track target count (Tauri `slice(0, 10)`).
|
||||
const REC_LIMIT: usize = 10;
|
||||
/// Sparse threshold below which the artist-tracks fallback runs (Tauri `< 5`).
|
||||
const SPARSE_THRESHOLD: usize = 5;
|
||||
/// Artist-tracks fallback page size (Tauri `limit: 30`).
|
||||
const FALLBACK_LIMIT: u32 = 30;
|
||||
/// Max curated playlist cards (Tauri `slice(0, 2)`).
|
||||
const MAX_PLAYLIST_CARDS: usize = 2;
|
||||
/// Book-collage cover count per playlist card (Tauri 3).
|
||||
const BOOK_COVERS: usize = 3;
|
||||
/// Diamond-collage cover count for the radio card (Tauri max 4).
|
||||
const RADIO_COVERS: usize = 4;
|
||||
|
||||
/// A resolved playlist card (book collage of up to 3 distinct album covers).
|
||||
pub(crate) struct PlaylistCard {
|
||||
id: String,
|
||||
name: String,
|
||||
track_count: u32,
|
||||
/// Up to 3 distinct album-cover URLs for the book collage.
|
||||
cover_urls: Vec<String>,
|
||||
}
|
||||
|
||||
/// The fully-assembled suggestions for one (artist, track) pair.
|
||||
pub struct SuggestionsPayload {
|
||||
pub artist_id: String,
|
||||
pub seed_track_id: String,
|
||||
pub seed_track_name: String,
|
||||
pub seed_artist_id: String,
|
||||
pub playlist_cards: Vec<PlaylistCard>,
|
||||
pub rec_tracks: Vec<qbz_models::Track>,
|
||||
/// Up to 4 distinct rec-track album covers for the radio diamond collage.
|
||||
pub radio_cover_urls: Vec<String>,
|
||||
pub error: bool,
|
||||
}
|
||||
|
||||
/// An empty payload (no cards, no tracks, no error) — the "no track selected"
|
||||
/// reset state applied when the immersive panel opens with no current track.
|
||||
pub fn empty_payload() -> SuggestionsPayload {
|
||||
SuggestionsPayload {
|
||||
artist_id: String::new(),
|
||||
seed_track_id: String::new(),
|
||||
seed_track_name: String::new(),
|
||||
seed_artist_id: String::new(),
|
||||
playlist_cards: Vec::new(),
|
||||
rec_tracks: Vec::new(),
|
||||
radio_cover_urls: Vec::new(),
|
||||
error: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Deterministic splitmix64 step (qbz-radio's RNG family). Used for an
|
||||
/// in-place Fisher-Yates shuffle so the rec list is varied but reproducible
|
||||
/// per (artist, track) — no `rand` dependency pulled just for this.
|
||||
fn splitmix64(state: &mut u64) -> u64 {
|
||||
*state = state.wrapping_add(0x9E3779B97F4A7C15);
|
||||
let mut z = *state;
|
||||
z = (z ^ (z >> 30)).wrapping_mul(0xBF58476D1CE4E5B9);
|
||||
z = (z ^ (z >> 27)).wrapping_mul(0x94D049BB133111EB);
|
||||
z ^ (z >> 31)
|
||||
}
|
||||
|
||||
/// Fisher-Yates shuffle seeded off the (artist, track) ids.
|
||||
fn shuffle_tracks(tracks: &mut [qbz_models::Track], seed: u64) {
|
||||
let mut state = seed ^ 0xD1B54A32D192ED03;
|
||||
for i in (1..tracks.len()).rev() {
|
||||
let j = (splitmix64(&mut state) % (i as u64 + 1)) as usize;
|
||||
tracks.swap(i, j);
|
||||
}
|
||||
}
|
||||
|
||||
/// Best collage cover URL for a track's album (large → best variant).
|
||||
fn track_album_cover(track: &qbz_models::Track) -> Option<String> {
|
||||
track
|
||||
.album
|
||||
.as_ref()
|
||||
.and_then(|a| a.image.best().cloned())
|
||||
.filter(|s| !s.is_empty())
|
||||
}
|
||||
|
||||
/// Album id of a track (for distinct-cover dedupe in the book collage).
|
||||
fn track_album_id(track: &qbz_models::Track) -> Option<String> {
|
||||
track
|
||||
.album
|
||||
.as_ref()
|
||||
.map(|a| a.id.clone())
|
||||
.filter(|s| !s.is_empty())
|
||||
}
|
||||
|
||||
/// Build the suggestions payload for `artist_id` + `current_track_id`. All
|
||||
/// queries are live Qobuz artist calls (NEVER reco_store). On the top-level
|
||||
/// artist-detail failure, returns an error payload (drives the panel's error
|
||||
/// branch); individual playlist-cover fetch failures are tolerated.
|
||||
pub async fn load_suggestions<A>(
|
||||
runtime: &Arc<AppRuntime<A>>,
|
||||
artist_id: u64,
|
||||
current_track_id: u64,
|
||||
seed_track_name: String,
|
||||
) -> SuggestionsPayload
|
||||
where
|
||||
A: FrontendAdapter + Send + Sync + 'static,
|
||||
{
|
||||
let artist = match runtime.core().get_artist_detail(artist_id, None, None).await {
|
||||
Ok(a) => a,
|
||||
Err(e) => {
|
||||
log::error!("[qbz-slint] suggestions get_artist_detail({artist_id}) failed: {e}");
|
||||
return SuggestionsPayload {
|
||||
artist_id: artist_id.to_string(),
|
||||
seed_track_id: current_track_id.to_string(),
|
||||
seed_track_name,
|
||||
seed_artist_id: artist_id.to_string(),
|
||||
playlist_cards: Vec::new(),
|
||||
rec_tracks: Vec::new(),
|
||||
radio_cover_urls: Vec::new(),
|
||||
error: true,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
// ---- Recommended tracks --------------------------------------------
|
||||
// Base = tracks_appears_on (current track filtered, deduped by title).
|
||||
let mut rec: Vec<qbz_models::Track> = Vec::new();
|
||||
let mut seen_titles: HashSet<String> = HashSet::new();
|
||||
if let Some(container) = artist.tracks_appears_on.as_ref() {
|
||||
for track in &container.items {
|
||||
if track.id == current_track_id {
|
||||
continue;
|
||||
}
|
||||
let key = track.title.to_lowercase().trim().to_string();
|
||||
if key.is_empty() || !seen_titles.insert(key) {
|
||||
continue;
|
||||
}
|
||||
rec.push(track.clone());
|
||||
}
|
||||
}
|
||||
|
||||
// Sparse fallback: merge artist popular tracks (dedupe by title + id).
|
||||
if rec.len() < SPARSE_THRESHOLD {
|
||||
match runtime
|
||||
.core()
|
||||
.get_artist_tracks(artist_id, FALLBACK_LIMIT, 0)
|
||||
.await
|
||||
{
|
||||
Ok(container) => {
|
||||
let existing_ids: HashSet<u64> = rec.iter().map(|t| t.id).collect();
|
||||
for track in container.items {
|
||||
if track.id == current_track_id || existing_ids.contains(&track.id) {
|
||||
continue;
|
||||
}
|
||||
let key = track.title.to_lowercase().trim().to_string();
|
||||
if key.is_empty() || !seen_titles.insert(key) {
|
||||
continue;
|
||||
}
|
||||
rec.push(track);
|
||||
}
|
||||
}
|
||||
Err(e) => log::warn!("[qbz-slint] suggestions artist-tracks fallback failed: {e}"),
|
||||
}
|
||||
}
|
||||
|
||||
// Shuffle (deterministic per artist+track), take 10.
|
||||
let seed = (artist_id << 1) ^ current_track_id.wrapping_add(1);
|
||||
shuffle_tracks(&mut rec, seed);
|
||||
rec.truncate(REC_LIMIT);
|
||||
|
||||
// Radio diamond collage: up to 4 distinct rec-track album covers.
|
||||
let mut radio_cover_urls: Vec<String> = Vec::new();
|
||||
for track in &rec {
|
||||
if let Some(url) = track_album_cover(track) {
|
||||
if !radio_cover_urls.contains(&url) {
|
||||
radio_cover_urls.push(url);
|
||||
if radio_cover_urls.len() >= RADIO_COVERS {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---- Curated playlist cards (first 2) ------------------------------
|
||||
let mut playlist_cards: Vec<PlaylistCard> = Vec::new();
|
||||
if let Some(playlists) = artist.playlists.as_ref() {
|
||||
for playlist in playlists.iter().take(MAX_PLAYLIST_CARDS) {
|
||||
// Fetch the full playlist to harvest up to 3 distinct album covers.
|
||||
let mut cover_urls: Vec<String> = Vec::new();
|
||||
match runtime.core().get_playlist(playlist.id).await {
|
||||
Ok(full) => {
|
||||
if let Some(container) = full.tracks.as_ref() {
|
||||
let mut seen_albums: HashSet<String> = HashSet::new();
|
||||
for track in &container.items {
|
||||
let (Some(url), Some(album_id)) =
|
||||
(track_album_cover(track), track_album_id(track))
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
if seen_albums.insert(album_id) {
|
||||
cover_urls.push(url);
|
||||
if cover_urls.len() >= BOOK_COVERS {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
log::warn!(
|
||||
"[qbz-slint] suggestions get_playlist({}) failed: {e}",
|
||||
playlist.id
|
||||
);
|
||||
}
|
||||
}
|
||||
// Fallback to the playlist's own images when no track covers found.
|
||||
if cover_urls.is_empty() {
|
||||
if let Some(images) = playlist.images.as_ref() {
|
||||
if let Some(img) = images.iter().find(|s| !s.is_empty()) {
|
||||
cover_urls.push(img.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
playlist_cards.push(PlaylistCard {
|
||||
id: playlist.id.to_string(),
|
||||
name: playlist.name.clone(),
|
||||
track_count: playlist.tracks_count,
|
||||
cover_urls,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
SuggestionsPayload {
|
||||
artist_id: artist_id.to_string(),
|
||||
seed_track_id: current_track_id.to_string(),
|
||||
seed_track_name,
|
||||
seed_artist_id: artist_id.to_string(),
|
||||
playlist_cards,
|
||||
rec_tracks: rec,
|
||||
radio_cover_urls,
|
||||
error: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Build a `SuggestionCard` for a playlist (book collage).
|
||||
fn playlist_to_card(card: &PlaylistCard) -> SuggestionCard {
|
||||
SuggestionCard {
|
||||
kind: "playlist".into(),
|
||||
title: card.name.clone().into(),
|
||||
subtitle: format!("{} tracks", card.track_count).into(),
|
||||
cover_urls: ModelRc::new(VecModel::from(
|
||||
card.cover_urls
|
||||
.iter()
|
||||
.map(|s| slint::SharedString::from(s.as_str()))
|
||||
.collect::<Vec<_>>(),
|
||||
)),
|
||||
cover0: slint::Image::default(),
|
||||
cover1: slint::Image::default(),
|
||||
cover2: slint::Image::default(),
|
||||
cover3: slint::Image::default(),
|
||||
playlist_id: card.id.clone().into(),
|
||||
seed_track_id: "".into(),
|
||||
seed_track_name: "".into(),
|
||||
seed_artist_id: "".into(),
|
||||
badge: "qobuz".into(),
|
||||
loading: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Build the seed "Song Radio" card (diamond collage).
|
||||
fn radio_card(payload: &SuggestionsPayload) -> SuggestionCard {
|
||||
SuggestionCard {
|
||||
kind: "radio".into(),
|
||||
title: "Song Radio".into(),
|
||||
subtitle: payload.seed_track_name.clone().into(),
|
||||
cover_urls: ModelRc::new(VecModel::from(
|
||||
payload
|
||||
.radio_cover_urls
|
||||
.iter()
|
||||
.map(|s| slint::SharedString::from(s.as_str()))
|
||||
.collect::<Vec<_>>(),
|
||||
)),
|
||||
cover0: slint::Image::default(),
|
||||
cover1: slint::Image::default(),
|
||||
cover2: slint::Image::default(),
|
||||
cover3: slint::Image::default(),
|
||||
playlist_id: "".into(),
|
||||
seed_track_id: payload.seed_track_id.clone().into(),
|
||||
seed_track_name: payload.seed_track_name.clone().into(),
|
||||
seed_artist_id: payload.seed_artist_id.clone().into(),
|
||||
badge: "qbz".into(),
|
||||
loading: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Apply the assembled suggestions to `SuggestionsState`. Runs on the event loop.
|
||||
pub fn apply_suggestions(window: &AppWindow, payload: SuggestionsPayload) {
|
||||
let mut cards: Vec<SuggestionCard> =
|
||||
payload.playlist_cards.iter().map(playlist_to_card).collect();
|
||||
// The radio card always trails the playlist cards (Tauri order).
|
||||
if !payload.seed_track_id.is_empty() {
|
||||
cards.push(radio_card(&payload));
|
||||
}
|
||||
let tracks: Vec<TrackItem> = payload
|
||||
.rec_tracks
|
||||
.iter()
|
||||
.map(crate::playlist::to_item)
|
||||
.collect();
|
||||
|
||||
let state = window.global::<SuggestionsState>();
|
||||
state.set_artist_id(payload.artist_id.into());
|
||||
state.set_seed_track_id(payload.seed_track_id.into());
|
||||
state.set_cards(ModelRc::new(VecModel::from(cards)));
|
||||
state.set_tracks(ModelRc::new(VecModel::from(tracks)));
|
||||
state.set_error(if payload.error { "error".into() } else { "".into() });
|
||||
state.set_loading(false);
|
||||
}
|
||||
|
||||
/// Flip the radio card's `loading` flag (the building spinner) on/off. The
|
||||
/// radio card is the LAST card in the model (Tauri order: playlists then
|
||||
/// radio). Scheduled on the event loop; safe to call from any thread.
|
||||
pub fn set_radio_loading(weak: &slint::Weak<AppWindow>, loading: bool) {
|
||||
let _ = weak.upgrade_in_event_loop(move |w| {
|
||||
let model = w.global::<SuggestionsState>().get_cards();
|
||||
let n = model.row_count();
|
||||
if n == 0 {
|
||||
return;
|
||||
}
|
||||
// Find the radio card (kind == "radio"); fall back to the last card.
|
||||
let idx = (0..n)
|
||||
.find(|&i| {
|
||||
model
|
||||
.row_data(i)
|
||||
.map(|c| c.kind.as_str() == "radio")
|
||||
.unwrap_or(false)
|
||||
})
|
||||
.unwrap_or(n - 1);
|
||||
if let Some(mut card) = model.row_data(idx) {
|
||||
card.loading = loading;
|
||||
model.set_row_data(idx, card);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// Clear the suggestions state before a (re)load. Runs on the event loop.
|
||||
pub fn reset_suggestions(window: &AppWindow) {
|
||||
let state = window.global::<SuggestionsState>();
|
||||
state.set_cards(ModelRc::new(VecModel::from(Vec::<SuggestionCard>::new())));
|
||||
state.set_tracks(ModelRc::new(VecModel::from(Vec::<TrackItem>::new())));
|
||||
state.set_error("".into());
|
||||
state.set_loading(true);
|
||||
}
|
||||
|
||||
/// Artwork jobs for the assembled suggestions: per-card collage slots +
|
||||
/// rec-track row thumbnails.
|
||||
pub fn suggestions_artwork_jobs(payload: &SuggestionsPayload) -> Vec<ArtworkJob> {
|
||||
let mut jobs = Vec::new();
|
||||
// Card collage slots: playlist cards first (their order matches the model),
|
||||
// then the radio card (appended last in apply_suggestions).
|
||||
for (card_idx, card) in payload.playlist_cards.iter().enumerate() {
|
||||
for (slot, url) in card.cover_urls.iter().enumerate() {
|
||||
if !url.is_empty() {
|
||||
jobs.push(ArtworkJob {
|
||||
url: url.clone(),
|
||||
target: ArtworkTarget::SuggestionCardCover { card_idx, slot },
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
if !payload.seed_track_id.is_empty() {
|
||||
let radio_idx = payload.playlist_cards.len();
|
||||
for (slot, url) in payload.radio_cover_urls.iter().enumerate() {
|
||||
if !url.is_empty() {
|
||||
jobs.push(ArtworkJob {
|
||||
url: url.clone(),
|
||||
target: ArtworkTarget::SuggestionCardCover {
|
||||
card_idx: radio_idx,
|
||||
slot,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
// Rec-track row thumbnails.
|
||||
for (idx, track) in payload.rec_tracks.iter().enumerate() {
|
||||
if let Some(url) = track
|
||||
.album
|
||||
.as_ref()
|
||||
.and_then(|a| a.image.smallest().cloned())
|
||||
.filter(|s| !s.is_empty())
|
||||
{
|
||||
jobs.push(ArtworkJob {
|
||||
url,
|
||||
target: ArtworkTarget::SuggestionTrackCover { idx },
|
||||
});
|
||||
}
|
||||
}
|
||||
jobs
|
||||
}
|
||||
Reference in new issue
Block a user