//! An artist: photo, followers, Lejátszás / Keverés / Követés, the popular //! tracks, then albums, singles and appearances. use std::any::Any; use crate::api::{self, Album, Artist, Track}; use crate::app::{Cx, Look, Screen}; use crate::format; use crate::page::{Ch, Frame, Kind, L, Spec}; use crate::screens::{self, Load, album_card, row_index, track_line}; use crate::spotify::Cmd; #[derive(Default)] struct Data { artist: Artist, top: Vec, albums: Vec, singles: Vec, appears: Vec, } pub struct ArtistPage { id: String, data: Load, following: Option, started: bool, } impl ArtistPage { pub fn new(id: &str) -> ArtistPage { ArtistPage { id: id.into(), data: Load::Loading, following: None, started: false } } fn load(&mut self, cx: &Cx) { let api = cx.api(); let id = self.id.clone(); let market = cx.user().country; cx.spawn::( move || -> anyhow::Result<(Data, Option)> { let (artist, top, albums, singles, appears) = api.artist_page(&id, &market)?; let following = api.following_artist(&id).ok(); Ok((Data { artist, top, albums, singles, appears }, following)) }, |s, cx, r| match r { Ok((d, following)) => { s.following = following; let img = api::pick(&d.artist.images, 960); cx.backdrop(&img, Look::Sharp, &format!("ar:{}", s.id)); s.data = Load::Ready(d); } Err(e) => s.data = Load::Failed(api::describe(&e)), }, ); } } impl Screen for ArtistPage { fn spec(&mut self, cx: &Cx) -> Spec { if !self.started { self.started = true; self.load(cx); } let frame = Frame::default(); let Some(d) = self.data.ready() else { return Spec { lines: vec![self.data.status("status").unwrap()], frame }; }; let a = &d.artist; let mut hero = L::new("hero", Kind::Hero).cover(api::pick(&a.images, 400), true); hero.style = "ELŐADÓ".into(); hero.title = a.name.clone(); hero.subtitle = a.followers.as_ref().map(|f| format::followers(f.total)).unwrap_or_default(); hero.meta = a.genres.iter().take(4).cloned().collect::>().join(" · "); let mut chips = vec![Ch::new("play", "Lejátszás").icon("play").tone("primary"), Ch::new("shuffle", "Keverés").icon("shuffle")]; if let Some(f) = self.following { chips.push(if f { Ch::new("follow", "Követed").icon("check").active(true) } else { Ch::new("follow", "Követés").icon("plus") }); } hero.chips = chips; let mut lines = vec![hero]; let playing = cx.playing_uri(); if !d.top.is_empty() { lines.push(L::heading("top-h", "Népszerű")); for (i, t) in d.top.iter().take(10).enumerate() { lines.push(track_line(format!("trk#{i}"), t, None, true, &playing)); } } for (id, title, list) in [("albums", "Albumok", &d.albums), ("singles", "Kislemezek és EP-k", &d.singles), ("appears", "Közreműködések", &d.appears)] { if !list.is_empty() { lines.push(L::cards(id, title, "square", list.iter().map(album_card).collect()).captions()); } } Spec { lines, frame } } fn activate(&mut self, cx: &Cx, line: &str, item: &str) { let Some(d) = self.data.ready() else { return }; let uri = d.artist.uri.clone(); if line == "hero" { match item { "play" | "shuffle" => cx.play(Cmd::Context { uri, start: None, shuffle: item == "shuffle" }), "follow" => { let Some(on) = self.following.map(|f| !f) else { return }; let api = cx.api(); let id = self.id.clone(); cx.call::("follow", move || api.set_following_artist(&id, on), move |s, cx, ok| { if ok { s.following = Some(on); cx.toast(if on { "Követed" } else { "Már nem követed" }); } }); } _ => {} } return; } if line.starts_with("trk#") { let Some(i) = row_index(line) else { return }; let top: Vec = d.top.iter().take(10).cloned().collect(); if item == "more" { if let Some(t) = top.get(i) { screens::track_menu(cx, t); } } else { screens::play_from(cx, &uri, &top, i); } return; } screens::open_card(cx, item); } fn shown(&mut self, cx: &Cx) { if let Some(d) = self.data.ready() { cx.backdrop(&api::pick(&d.artist.images, 960), Look::Sharp, &format!("ar:{}", self.id)); } } fn as_any(&mut self) -> &mut dyn Any { self } }