diff --git a/src/playlists.rs b/src/playlists.rs index 03e6ec2..05e9c49 100644 --- a/src/playlists.rs +++ b/src/playlists.rs @@ -3,7 +3,7 @@ use std::ops::Deref; use std::path::PathBuf; use std::sync::{Arc, RwLock, RwLockReadGuard}; -use rspotify::spotify::model::playlist::SimplifiedPlaylist; +use rspotify::spotify::model::playlist::{FullPlaylist, SimplifiedPlaylist}; use config; use events::EventManager; @@ -16,7 +16,9 @@ const CACHE_FILE: &str = "playlists.db"; #[derive(Clone, Debug, Deserialize, Serialize)] pub struct Playlist { - pub meta: SimplifiedPlaylist, + pub id: String, + pub name: String, + pub snapshot_id: String, pub tracks: Vec, } @@ -42,7 +44,7 @@ impl ListItem for Playlist { } fn display_left(&self) -> String { - self.meta.name.clone() + self.name.clone() } fn display_right(&self) -> String { @@ -108,10 +110,30 @@ impl Playlists { } } - pub fn process_playlist(list: &SimplifiedPlaylist, spotify: &Spotify) -> Playlist { - debug!("got list: {}", list.name); - let id = list.id.clone(); + pub fn process_simplified_playlist(list: &SimplifiedPlaylist, spotify: &Spotify) -> Playlist { + Playlists::_process_playlist( + list.id.clone(), + list.name.clone(), + list.snapshot_id.clone(), + spotify, + ) + } + pub fn process_full_playlist(list: &FullPlaylist, spotify: &Spotify) -> Playlist { + Playlists::_process_playlist( + list.id.clone(), + list.name.clone(), + list.snapshot_id.clone(), + spotify, + ) + } + + pub fn _process_playlist( + id: String, + name: String, + snapshot_id: String, + spotify: &Spotify, + ) -> Playlist { let mut collected_tracks = Vec::new(); let mut tracks_result = spotify.user_playlist_tracks(&id, 100, 0); @@ -135,15 +157,17 @@ impl Playlists { } } Playlist { - meta: list.clone(), + id: id.clone(), + name: name.clone(), + snapshot_id: snapshot_id.clone(), tracks: collected_tracks, } } fn needs_download(&self, remote: &SimplifiedPlaylist) -> bool { for local in self.store.read().expect("can't readlock playlists").iter() { - if local.meta.id == remote.id { - return local.meta.snapshot_id != remote.snapshot_id; + if local.id == remote.id { + return local.snapshot_id != remote.snapshot_id; } } true @@ -152,7 +176,7 @@ impl Playlists { fn append_or_update(&self, updated: &Playlist) -> usize { let mut store = self.store.write().expect("can't writelock playlists"); for (index, mut local) in store.iter_mut().enumerate() { - if local.meta.id == updated.meta.id { + if local.id == updated.id { *local = updated.clone(); return index; } @@ -163,7 +187,7 @@ impl Playlists { pub fn delete_playlist(&self, id: &str) { let mut store = self.store.write().expect("can't writelock playlists"); - if let Some(position) = store.iter().position(|ref i| i.meta.id == id) { + if let Some(position) = store.iter().position(|ref i| i.id == id) { if self.spotify.delete_playlist(id) { store.remove(position); self.save_cache(); @@ -195,13 +219,13 @@ impl Playlists { while let Some(ref lists) = lists_result.clone() { for remote in &lists.items { // remove from stale playlists so we won't prune it later on - if let Some(index) = stale_lists.iter().position(|x| x.meta.id == remote.id) { + if let Some(index) = stale_lists.iter().position(|x| x.id == remote.id) { stale_lists.remove(index); } if self.needs_download(remote) { info!("updating playlist {}", remote.name); - let playlist = Self::process_playlist(&remote, &self.spotify); + let playlist = Self::process_simplified_playlist(remote, &self.spotify); self.append_or_update(&playlist); // trigger redraw self.ev.trigger(); @@ -226,9 +250,9 @@ impl Playlists { .read() .unwrap() .iter() - .position(|x| x.meta.id == stale.meta.id); + .position(|x| x.id == stale.id); if let Some(index) = index { - debug!("removing stale list: {:?}", stale.meta.name); + debug!("removing stale list: {:?}", stale.name); self.store.write().unwrap().remove(index); } } diff --git a/src/ui/playlists.rs b/src/ui/playlists.rs index d4426c3..a53055c 100644 --- a/src/ui/playlists.rs +++ b/src/ui/playlists.rs @@ -35,7 +35,7 @@ impl PlaylistView { if let Some(playlist) = current { let playlists = self.playlists.clone(); - let id = playlist.meta.id.clone(); + let id = playlist.id.clone(); let dialog = Dialog::text("Are you sure you want to delete this playlist?") .padding((1, 1, 1, 0)) .title("Delete playlist") diff --git a/src/ui/queue.rs b/src/ui/queue.rs index 8c9d77b..ab753ff 100644 --- a/src/ui/queue.rs +++ b/src/ui/queue.rs @@ -68,7 +68,7 @@ impl QueueView { list_select.add_item("[Create new]", None); for list in playlists.items().iter() { - list_select.add_item(list.meta.name.clone(), Some(list.meta.id.clone())); + list_select.add_item(list.name.clone(), Some(list.id.clone())); } list_select.set_on_submit(move |s, selected| { diff --git a/src/ui/search.rs b/src/ui/search.rs index 33ab7db..d5100eb 100644 --- a/src/ui/search.rs +++ b/src/ui/search.rs @@ -129,7 +129,7 @@ impl SearchView { .playlists .items .iter() - .map(|sp| Playlists::process_playlist(sp, &&spotify)) + .map(|sp| Playlists::process_simplified_playlist(sp, &&spotify)) .collect(); let mut r = playlists.write().unwrap(); *r = pls;