Files
spotify/src/screens/home.rs
Tamas Csikel 7fd7e242a7
All checks were successful
release / release (push) Successful in 10m37s
0.2.0: play through Spotify Soloist
Spotify refuses librespot the audio keys ("error audio key 0 1",
librespot #1649), so nothing played. Playback now goes through Spotify
Soloist, Spotify's own headless Linux player:

- The TV downloads it from Spotify (redistribution is not allowed), keeps
  it fresh (builds expire after 90 days), runs and restarts it, and drives
  it over its local WebSocket API; its events feed Most szól.
- It needs the user's own Soloist API key: the key screen shows a QR code
  to a small page on the TV where the phone pastes it (or type it on the
  TV); asked once after sign-in, also in Settings. Then the TV is picked
  once in the phone's Spotify app (pairing); home shows that step.
- Soloist is the Connect device; librespot only signs in (pairing code)
  and browses. Its player, Connect and zeroconf are gone, and with them
  GStreamer.
- A track from a list plays and the next ones are queued (Soloist's play
  takes one URI). The queue panel is back (Soloist reports the queue).
- Settings: the player's state and key instead of quality/normalisation/
  autoplay (Soloist decides those).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 21:50:44 +01:00

161 lines
6.1 KiB
Rust

//! 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<Vec<Entity>>,
playlists: Option<Vec<Playlist>>,
liked_total: Option<u32>,
artists: Option<Vec<Artist>>,
albums: Option<Vec<Album>>,
shows: Option<Vec<Show>>,
errors: Vec<String>,
}
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::<Home, _>(
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<T>(r: std::thread::Result<anyhow::Result<T>>, errors: &mut Vec<String>) -> Option<T> {
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<u32>) -> 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
}
}