//! Kezdőlap: rows: recently played (albums, playlists, artists...), your //! playlists (with the liked songs first), your artists, saved albums, your //! podcasts. Every row loads on its own; a failing one just stays away. use std::any::Any; use crate::api::{self, Album, Artist, Entity, Playlist, Show}; use crate::app::{Cx, Look, Screen}; use crate::page::{C, Frame, L, Spec}; use crate::screens::{self, album_card, artist_card, liked_card, playlist_card, show_card}; #[derive(Default)] struct Data { recent: Option>, playlists: Option>, liked_total: Option, artists: Option>, albums: Option>, shows: Option>, errors: Vec, } pub struct Home { data: Data, loading: bool, started: bool, } impl Home { pub fn new() -> Home { Home { data: Data::default(), loading: true, started: false } } fn load(&mut self, cx: &Cx) { self.loading = true; let api = cx.api(); cx.spawn::( move || { std::thread::scope(|s| { let recent = s.spawn(|| api.recent()); let playlists = s.spawn(|| api.my_playlists(0)); let liked = s.spawn(|| api.liked(0)); let artists = s.spawn(|| api.followed_artists("")); let albums = s.spawn(|| api.saved_albums(0)); let shows = s.spawn(|| api.saved_shows(0)); let mut errors = Vec::new(); let e = &mut errors; let recent = keep(recent.join(), e); let playlists = keep(playlists.join(), e).map(|p| p.list()); let liked_total = keep(liked.join(), e).map(|p| p.total); let artists = keep(artists.join(), e).map(|a| a.0); let albums = keep(albums.join(), e).map(|p| p.list()); let shows = keep(shows.join(), e).map(|p| p.list()); Data { recent, playlists, liked_total, artists, albums, shows, errors } }) }, |s, cx, d| { s.data = d; s.loading = false; // The first picture of the page tints the background. cx.backdrop(&s.first_image(), Look::Soft, "home"); }, ); } fn first_image(&self) -> String { match self.data.recent.as_ref().and_then(|r| r.first()) { Some(Entity::Album(a)) => api::pick(&a.images, 640), Some(Entity::Playlist(p)) => p.image(640), Some(Entity::Artist(a)) => api::pick(&a.images, 640), Some(Entity::Show(s)) => api::pick(&s.images, 640), _ => String::new(), } } } /// A row's result: the data, or its error noted. fn keep(r: std::thread::Result>, errors: &mut Vec) -> Option { match r { Ok(Ok(t)) => Some(t), Ok(Err(e)) => { log::warn!("home: {e:#}"); errors.push(crate::api::describe(&e)); None } Err(_) => None, } } fn entity_card(e: &Entity, liked_total: Option) -> C { match e { Entity::Album(a) => album_card(a), Entity::Playlist(p) => playlist_card(p), Entity::Artist(a) => artist_card(a), Entity::Show(s) => show_card(s), Entity::Liked => liked_card(liked_total), } } impl Screen for Home { fn spec(&mut self, cx: &Cx) -> Spec { if !self.started { self.started = true; self.load(cx); } let frame = Frame { nav: Some(0), ..Frame::default() }; let mut lines = Vec::new(); if self.loading { lines.push(L::text("loading", "Betöltés…", "caption")); return Spec { lines, frame }; } // One step left before anything can play. let solo = cx.solo(); if !matches!(solo, crate::soloist::Status::Ready | crate::soloist::Status::Starting) { let (title, text) = crate::screens::settings::solo_text(&solo, &cx.cfg().device_name); let mut n = L::item("solo", format!("Lejátszó: {title}"), text, "", Vec::new()); if matches!(solo, crate::soloist::Status::NeedKey | crate::soloist::Status::Failed(_)) { n.chips = vec![crate::page::Ch::new("key", "Kulcs megadása").tone("primary")]; } lines.push(n); } let d = &self.data; if let Some(r) = d.recent.as_ref().filter(|r| !r.is_empty()) { lines.push(L::cards("recent", "Legutóbb játszott", "square", r.iter().map(|e| entity_card(e, d.liked_total)).collect()).captions()); } let mut pl = vec![liked_card(d.liked_total)]; pl.extend(d.playlists.iter().flatten().map(playlist_card)); lines.push(L::cards("playlists", "Lejátszási listáid", "square", pl).captions()); if let Some(a) = d.artists.as_ref().filter(|a| !a.is_empty()) { lines.push(L::cards("artists", "Előadóid", "round", a.iter().take(30).map(artist_card).collect()).captions()); } if let Some(a) = d.albums.as_ref().filter(|a| !a.is_empty()) { lines.push(L::cards("albums", "Elmentett albumaid", "square", a.iter().take(30).map(album_card).collect()).captions()); } if let Some(s) = d.shows.as_ref().filter(|s| !s.is_empty()) { lines.push(L::cards("shows", "Podcastjaid", "square", s.iter().map(show_card).collect()).captions()); } if d.recent.is_none() && d.playlists.is_none() && d.artists.is_none() { let why = d.errors.first().cloned().unwrap_or_default(); lines.push(L::notice("error", "bad", "Nem sikerült betölteni a kezdőlapot", why)); } Spec { lines, frame } } fn activate(&mut self, cx: &Cx, line: &str, item: &str) { if line == "solo" { cx.c.open_key_screen(); return; } screens::open_card(cx, item); } fn shown(&mut self, cx: &Cx) { cx.backdrop(&self.first_image(), Look::Soft, "home"); } fn as_any(&mut self) -> &mut dyn Any { self } }