Musyx Mobile: Android TV player for Yandex Music
Kotlin, Compose for TV and Media3 port of the desktop Musyx, with device-code sign-in, Home, My Wave, search, collection, detail pages, Now Playing with queue and lyrics, and settings for streaming quality, appearance, font size (small, medium, large) and language. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
46610d95fa
217 files changed
+26144
No files matched your search
@@ -0,0 +1,302 @@
|
||||
//! Search, the home landing, the chart, genres and podcasts.
|
||||
|
||||
use yamuse::models as y;
|
||||
|
||||
use crate::library::{AlbumSummary, ArtistSummary, PlaylistSummary};
|
||||
use crate::models::Track;
|
||||
|
||||
/// Which part of the catalogue a search covers.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
|
||||
pub enum SearchKind {
|
||||
#[default]
|
||||
All,
|
||||
Tracks,
|
||||
Albums,
|
||||
Artists,
|
||||
Playlists,
|
||||
Podcasts,
|
||||
}
|
||||
|
||||
impl SearchKind {
|
||||
pub const ALL: [SearchKind; 6] = [
|
||||
SearchKind::All,
|
||||
SearchKind::Tracks,
|
||||
SearchKind::Albums,
|
||||
SearchKind::Artists,
|
||||
SearchKind::Playlists,
|
||||
SearchKind::Podcasts,
|
||||
];
|
||||
|
||||
pub fn label(self) -> &'static str {
|
||||
match self {
|
||||
SearchKind::All => "All",
|
||||
SearchKind::Tracks => "Tracks",
|
||||
SearchKind::Albums => "Albums",
|
||||
SearchKind::Artists => "Artists",
|
||||
SearchKind::Playlists => "Playlists",
|
||||
SearchKind::Podcasts => "Podcasts",
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn to_yamuse(self) -> yamuse::SearchType {
|
||||
match self {
|
||||
SearchKind::All => yamuse::SearchType::All,
|
||||
SearchKind::Tracks => yamuse::SearchType::Track,
|
||||
SearchKind::Albums => yamuse::SearchType::Album,
|
||||
SearchKind::Artists => yamuse::SearchType::Artist,
|
||||
SearchKind::Playlists => yamuse::SearchType::Playlist,
|
||||
SearchKind::Podcasts => yamuse::SearchType::Podcast,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum BestMatch {
|
||||
Track(Track),
|
||||
Album(AlbumSummary),
|
||||
Artist(ArtistSummary),
|
||||
Playlist(PlaylistSummary),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
pub struct SearchResults {
|
||||
/// The query Yandex actually searched, when it corrected a typo.
|
||||
pub corrected: Option<String>,
|
||||
pub best: Option<BestMatch>,
|
||||
pub tracks: Vec<Track>,
|
||||
pub albums: Vec<AlbumSummary>,
|
||||
pub artists: Vec<ArtistSummary>,
|
||||
pub playlists: Vec<PlaylistSummary>,
|
||||
pub podcasts: Vec<AlbumSummary>,
|
||||
/// Total matches per kind, beyond the page returned.
|
||||
pub totals: Totals,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
|
||||
pub struct Totals {
|
||||
pub tracks: u32,
|
||||
pub albums: u32,
|
||||
pub artists: u32,
|
||||
pub playlists: u32,
|
||||
pub podcasts: u32,
|
||||
}
|
||||
|
||||
impl Totals {
|
||||
pub fn get(&self, kind: SearchKind) -> Option<u32> {
|
||||
match kind {
|
||||
SearchKind::All => None,
|
||||
SearchKind::Tracks => Some(self.tracks),
|
||||
SearchKind::Albums => Some(self.albums),
|
||||
SearchKind::Artists => Some(self.artists),
|
||||
SearchKind::Playlists => Some(self.playlists),
|
||||
SearchKind::Podcasts => Some(self.podcasts),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl SearchResults {
|
||||
pub(crate) fn from_yamuse(s: y::Search) -> Self {
|
||||
fn part<T, U>(r: Option<y::SearchResult<T>>, f: impl Fn(T) -> Option<U>) -> (Vec<U>, u32) {
|
||||
match r {
|
||||
Some(r) => (r.results.into_iter().filter_map(f).collect(), r.total.unwrap_or(0)),
|
||||
None => (Vec::new(), 0),
|
||||
}
|
||||
}
|
||||
let best = s.best.as_ref().and_then(|b| b.entity()).and_then(|e| match e {
|
||||
y::SearchEntity::Track(t) => Track::from_yamuse(*t).map(BestMatch::Track),
|
||||
y::SearchEntity::Album(a) => AlbumSummary::from_yamuse(*a).map(BestMatch::Album),
|
||||
y::SearchEntity::Artist(a) => ArtistSummary::from_yamuse(*a).map(BestMatch::Artist),
|
||||
y::SearchEntity::Playlist(p) => PlaylistSummary::from_yamuse(&p).map(BestMatch::Playlist),
|
||||
_ => None,
|
||||
});
|
||||
let (tracks, t_total) = part(s.tracks, Track::from_yamuse);
|
||||
let (albums, al_total) = part(s.albums, AlbumSummary::from_yamuse);
|
||||
let (artists, ar_total) = part(s.artists, ArtistSummary::from_yamuse);
|
||||
let (playlists, p_total) = part(s.playlists, |p| PlaylistSummary::from_yamuse(&p));
|
||||
let (podcasts, pc_total) = part(s.podcasts, AlbumSummary::from_yamuse);
|
||||
Self {
|
||||
corrected: s.misspell_corrected.unwrap_or(false).then_some(s.misspell_result).flatten(),
|
||||
best,
|
||||
tracks,
|
||||
albums,
|
||||
artists,
|
||||
playlists,
|
||||
podcasts,
|
||||
totals: Totals {
|
||||
tracks: t_total,
|
||||
albums: al_total,
|
||||
artists: ar_total,
|
||||
playlists: p_total,
|
||||
podcasts: pc_total,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.best.is_none()
|
||||
&& self.tracks.is_empty()
|
||||
&& self.albums.is_empty()
|
||||
&& self.artists.is_empty()
|
||||
&& self.playlists.is_empty()
|
||||
&& self.podcasts.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct ChartEntry {
|
||||
pub position: u32,
|
||||
/// Places moved since the last chart: positive is up.
|
||||
pub shift: i32,
|
||||
pub track: Track,
|
||||
}
|
||||
|
||||
impl ChartEntry {
|
||||
pub(crate) fn from_short(short: y::TrackShort) -> Option<Self> {
|
||||
let chart: Option<y::Chart> = short.chart.and_then(|c| serde_json::from_value(c).ok());
|
||||
Some(Self {
|
||||
position: chart.as_ref().and_then(|c| c.position).unwrap_or(0).max(0) as u32,
|
||||
shift: chart.as_ref().and_then(|c| c.shift).unwrap_or(0) as i32,
|
||||
track: Track::from_yamuse(short.track?)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// A personal playlist ("Playlist of the Day", "Déjà Vu"…) with its blurb.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Personal {
|
||||
pub playlist: PlaylistSummary,
|
||||
pub description: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
pub struct Home {
|
||||
pub personal: Vec<Personal>,
|
||||
pub chart: Vec<ChartEntry>,
|
||||
pub new_releases: Vec<AlbumSummary>,
|
||||
pub new_playlists: Vec<PlaylistSummary>,
|
||||
}
|
||||
|
||||
impl Home {
|
||||
pub(crate) fn from_landing(landing: y::Landing) -> Self {
|
||||
let mut home = Home::default();
|
||||
for block in landing.blocks {
|
||||
for entity in &block.entities {
|
||||
match entity.data() {
|
||||
Some(y::BlockEntityData::PersonalPlaylist(g)) => {
|
||||
if let Some(playlist) = g.data.as_ref().and_then(PlaylistSummary::from_yamuse) {
|
||||
let description = g.preview_description.clone().filter(|d| !d.trim().is_empty());
|
||||
home.personal.push(Personal { playlist, description });
|
||||
}
|
||||
}
|
||||
Some(y::BlockEntityData::Album(a)) => {
|
||||
home.new_releases.extend(AlbumSummary::from_yamuse(*a));
|
||||
}
|
||||
Some(y::BlockEntityData::Playlist(p)) => {
|
||||
home.new_playlists.extend(PlaylistSummary::from_yamuse(&p));
|
||||
}
|
||||
Some(y::BlockEntityData::ChartItem(item)) => {
|
||||
let entry = item.track.and_then(Track::from_yamuse).map(|track| ChartEntry {
|
||||
position: item.chart.as_ref().and_then(|c| c.position).unwrap_or(0).max(0) as u32,
|
||||
shift: item.chart.as_ref().and_then(|c| c.shift).unwrap_or(0) as i32,
|
||||
track,
|
||||
});
|
||||
home.chart.extend(entry);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
home
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Genre {
|
||||
pub id: String,
|
||||
pub title: String,
|
||||
/// `#rrggbb` from Yandex, used to tint the genre tile.
|
||||
pub color: Option<String>,
|
||||
pub sub_genres: Vec<Genre>,
|
||||
}
|
||||
|
||||
impl Genre {
|
||||
pub(crate) fn from_yamuse(g: y::Genre) -> Option<Self> {
|
||||
let title = g
|
||||
.title
|
||||
.or_else(|| g.titles.get("en").and_then(|t| t.title.clone()))
|
||||
.or_else(|| g.titles.get("ru").and_then(|t| t.title.clone()))?;
|
||||
Some(Self {
|
||||
id: g.id?,
|
||||
title,
|
||||
color: g.color.filter(|c| c.starts_with('#')),
|
||||
sub_genres: g.sub_genres.into_iter().filter_map(Genre::from_yamuse).collect(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn search_results_convert_with_best_match_and_totals() {
|
||||
let s: y::Search = serde_json::from_str(
|
||||
r#"{"best":{"type":"artist","result":{"id":3,"name":"Земфира"}},
|
||||
"tracks":{"total":120,"results":[{"id":1,"title":"Хочешь?"}]},
|
||||
"albums":{"total":40,"results":[{"id":5,"title":"Вендетта"}]},
|
||||
"misspellCorrected":true,"misspellResult":"земфира"}"#,
|
||||
)
|
||||
.unwrap();
|
||||
let r = SearchResults::from_yamuse(s);
|
||||
assert!(matches!(r.best, Some(BestMatch::Artist(ref a)) if a.name == "Земфира"));
|
||||
assert_eq!(r.tracks.len(), 1);
|
||||
assert_eq!(r.totals.get(SearchKind::Tracks), Some(120));
|
||||
assert_eq!(r.totals.get(SearchKind::Albums), Some(40));
|
||||
assert_eq!(r.corrected.as_deref(), Some("земфира"));
|
||||
assert!(!r.is_empty());
|
||||
assert!(SearchResults::from_yamuse(serde_json::from_str("{}").unwrap()).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chart_entry_reads_position_and_shift() {
|
||||
let short: y::TrackShort = serde_json::from_str(
|
||||
r#"{"id":1,"track":{"id":1,"title":"Хит"},"chart":{"position":3,"shift":-2,"progress":"down"}}"#,
|
||||
)
|
||||
.unwrap();
|
||||
let e = ChartEntry::from_short(short).unwrap();
|
||||
assert_eq!((e.position, e.shift), (3, -2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn landing_blocks_split_by_entity_type() {
|
||||
let landing: y::Landing = serde_json::from_str(
|
||||
r#"{"blocks":[
|
||||
{"type":"personal-playlists","entities":[{"type":"personal-playlist","data":{
|
||||
"data":{"uid":1,"kind":3,"title":"Плейлист дня","owner":{"uid":1}},
|
||||
"previewDescription":"Updated every day"}}]},
|
||||
{"type":"new-releases","entities":[{"type":"album","data":{"id":9,"title":"New"}}]},
|
||||
{"type":"chart","entities":[{"type":"chart-item","data":{
|
||||
"track":{"id":7,"title":"Top"},"chart":{"position":1,"shift":0}}}]}
|
||||
]}"#,
|
||||
)
|
||||
.unwrap();
|
||||
let home = Home::from_landing(landing);
|
||||
assert_eq!(home.personal[0].playlist.title, "Плейлист дня");
|
||||
assert_eq!(home.personal[0].description.as_deref(), Some("Updated every day"));
|
||||
assert_eq!(home.new_releases[0].id, 9);
|
||||
assert_eq!(home.chart[0].position, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn genre_title_falls_back_to_localised_titles() {
|
||||
let g: y::Genre = serde_json::from_str(
|
||||
r##"{"id":"rock","titles":{"en":{"title":"Rock"}},"color":"#ff0000",
|
||||
"subGenres":[{"id":"punk","title":"Punk"}]}"##,
|
||||
)
|
||||
.unwrap();
|
||||
let g = Genre::from_yamuse(g).unwrap();
|
||||
assert_eq!(g.title, "Rock");
|
||||
assert_eq!(g.sub_genres[0].id, "punk");
|
||||
assert_eq!(g.color.as_deref(), Some("#ff0000"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user