Skip to content

Commit

Permalink
fix: Use a unique identifier to add albums instead of the album name. C…
Browse files Browse the repository at this point in the history
…loses #12. (#183)
  • Loading branch information
nick42d authored Nov 20, 2024
1 parent b9b8cbe commit ed3404d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 35 deletions.
8 changes: 5 additions & 3 deletions youtui/src/app/server/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ pub enum GetArtistSongsProgressUpdate {
Songs {
song_list: Vec<AlbumSong>,
album: String,
album_id: AlbumID<'static>,
year: String,
artist: String,
},
Expand Down Expand Up @@ -277,15 +278,15 @@ fn get_artist_songs(
let api = api.clone();
async move {
let query = GetAlbumQuery::new(&a_id);
query_api_with_retry(&api, query).await
(query_api_with_retry(&api, query).await, a_id)
}
})
.collect::<FuturesOrdered<_>>();
while let Some(maybe_album) = stream.next().await {
while let Some((maybe_album, album_id)) = stream.next().await {
let album = match maybe_album {
Ok(album) => album,
Err(e) => {
error!("Error <{e}> getting album");
error!("Error <{e}> getting album {:?}", album_id);
return;
}
};
Expand All @@ -302,6 +303,7 @@ fn get_artist_songs(
GetArtistSongsProgressUpdate::Songs {
song_list: tracks,
album: title,
album_id,
year,
artist: artists
.into_iter()
Expand Down
44 changes: 19 additions & 25 deletions youtui/src/app/structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::borrow::Cow;
use std::rc::Rc;
use std::sync::Arc;
use std::time::Duration;
use ytmapi_rs::common::AlbumID;
use ytmapi_rs::parse::AlbumSong;

pub trait SongListComponent {
Expand Down Expand Up @@ -31,9 +32,10 @@ pub struct ListSong {
pub download_status: DownloadStatus,
pub id: ListSongID,
pub actual_duration: Option<Duration>,
year: Rc<String>,
artists: Vec<Rc<String>>,
album: Rc<String>,
pub year: Rc<String>,
pub artists: Vec<Rc<String>>,
pub album: Rc<String>,
pub album_id: Rc<AlbumID<'static>>,
}
#[derive(Clone)]
pub enum ListStatus {
Expand Down Expand Up @@ -92,24 +94,6 @@ impl DownloadStatus {
}

impl ListSong {
fn _set_year(&mut self, year: Rc<String>) {
self.year = year;
}
fn _set_album(&mut self, album: Rc<String>) {
self.album = album;
}
pub fn get_year(&self) -> &String {
&self.year
}
fn _set_artists(&mut self, artists: Vec<Rc<String>>) {
self.artists = artists;
}
pub fn get_artists(&self) -> &Vec<Rc<String>> {
&self.artists
}
pub fn get_album(&self) -> &String {
&self.album
}
pub fn get_track_no(&self) -> usize {
self.raw.track_no
}
Expand All @@ -128,16 +112,16 @@ impl ListSong {
}),
self.get_track_no().to_string().into(),
// TODO: Remove allocation
self.get_artists()
self.artists
.first()
.map(|a| a.to_string())
.unwrap_or_default()
.into(),
self.get_album().into(),
self.album.as_ref().into(),
(&self.raw.title).into(),
// TODO: Remove allocation
(&self.raw.duration).into(),
self.get_year().into(),
self.year.as_ref().into(),
]
.into_iter(),
)
Expand Down Expand Up @@ -185,23 +169,32 @@ impl AlbumSongsList {
&mut self,
raw_list: Vec<AlbumSong>,
album: String,
album_id: AlbumID<'static>,
year: String,
artist: String,
) {
// The album is shared by all the songs.
// So no need to clone/allocate for eache one.
// Instead we'll share ownership via Rc.
let album = Rc::new(album);
let album_id = Rc::new(album_id);
let year = Rc::new(year);
let artist = Rc::new(artist);
for song in raw_list {
self.add_raw_song(song, album.clone(), year.clone(), artist.clone());
self.add_raw_song(
song,
album.clone(),
album_id.clone(),
year.clone(),
artist.clone(),
);
}
}
pub fn add_raw_song(
&mut self,
song: AlbumSong,
album: Rc<String>,
album_id: Rc<AlbumID<'static>>,
year: Rc<String>,
artist: Rc<String>,
) -> ListSongID {
Expand All @@ -213,6 +206,7 @@ impl AlbumSongsList {
year,
artists: vec![artist],
album,
album_id,
actual_duration: None,
});
id
Expand Down
12 changes: 7 additions & 5 deletions youtui/src/app/ui/browser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use std::{borrow::Cow, mem, sync::Arc};
use tokio::sync::mpsc;
use tracing::error;
use ytmapi_rs::{
common::SearchSuggestion,
common::{AlbumID, SearchSuggestion},
parse::{AlbumSong, SearchResultArtist},
};

Expand Down Expand Up @@ -361,7 +361,7 @@ impl Browser {
.list
// Even if list is filtered, still play the whole album.
.get_list_iter()
.filter(|song| song.get_album() == cur_song.get_album())
.filter(|song| song.album_id == cur_song.album_id)
.cloned()
.collect();
send_or_error(
Expand All @@ -382,7 +382,7 @@ impl Browser {
.list
// Even if list is filtered, still play the whole album.
.get_list_iter()
.filter(|song| song.get_album() == cur_song.get_album())
.filter(|song| song.album_id == cur_song.album_id)
// XXX: Could instead be inside an Rc.
.cloned()
.collect();
Expand Down Expand Up @@ -445,7 +445,8 @@ impl Browser {
album,
year,
artist,
} => this.handle_append_song_list(song_list, album, year, artist),
album_id,
} => this.handle_append_song_list(song_list, album, album_id, year, artist),
GetArtistSongsProgressUpdate::AllSongsSent => this.handle_song_list_loaded(),
};

Expand Down Expand Up @@ -509,12 +510,13 @@ impl Browser {
&mut self,
song_list: Vec<AlbumSong>,
album: String,
album_id: AlbumID<'static>,
year: String,
artist: String,
) {
self.album_songs_list
.list
.append_raw_songs(song_list, album, year, artist);
.append_raw_songs(song_list, album, album_id, year, artist);
// If sort commands exist, sort the list.
// Naive - can result in multiple calls to sort every time songs are appended.
self.album_songs_list.apply_sort_commands();
Expand Down
4 changes: 2 additions & 2 deletions youtui/src/app/ui/footer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub fn draw_footer(f: &mut Frame, w: &super::YoutuiWindow, chunk: Rect) {
| PlayState::Buffering(id) => w
.playlist
.get_song_from_id(id)
.map(|s| s.get_album().to_owned())
.map(|s| s.album.as_ref().to_owned())
.unwrap_or("".to_string()),
PlayState::NotPlaying => "".to_string(),
PlayState::Stopped => "".to_string(),
Expand All @@ -88,7 +88,7 @@ pub fn draw_footer(f: &mut Frame, w: &super::YoutuiWindow, chunk: Rect) {
// TODO: tidy this up as ListSong only contains one artist currently.
// TODO: Remove allocation
.map(|s| {
s.get_artists()
s.artists
.clone()
.first()
.map(|a| a.to_string())
Expand Down

0 comments on commit ed3404d

Please sign in to comment.