diff --git a/src/library.rs b/src/library.rs index 8809dfe..0aec8d0 100644 --- a/src/library.rs +++ b/src/library.rs @@ -5,7 +5,7 @@ use std::path::PathBuf; use std::sync::{Arc, RwLock, RwLockReadGuard}; use std::thread; -use rspotify::spotify::model::playlist::{FullPlaylist, SimplifiedPlaylist}; +use rspotify::spotify::model::playlist::SimplifiedPlaylist; use serde::de::DeserializeOwned; use serde::Serialize; @@ -151,65 +151,6 @@ impl Library { } } - pub fn process_simplified_playlist(list: &SimplifiedPlaylist, spotify: &Spotify) -> Playlist { - Self::_process_playlist( - list.id.clone(), - list.name.clone(), - list.owner.id.clone(), - list.snapshot_id.clone(), - spotify, - ) - } - - pub fn process_full_playlist(list: &FullPlaylist, spotify: &Spotify) -> Playlist { - Self::_process_playlist( - list.id.clone(), - list.name.clone(), - list.owner.id.clone(), - list.snapshot_id.clone(), - spotify, - ) - } - - fn _process_playlist( - id: String, - name: String, - owner_id: String, - snapshot_id: String, - spotify: &Spotify, - ) -> Playlist { - let mut collected_tracks = Vec::new(); - - let mut tracks_result = spotify.user_playlist_tracks(&id, 100, 0); - while let Some(ref tracks) = tracks_result.clone() { - for listtrack in &tracks.items { - collected_tracks.push((&listtrack.track).into()); - } - debug!("got {} tracks", tracks.items.len()); - - // load next batch if necessary - tracks_result = match tracks.next { - Some(_) => { - debug!("requesting tracks again.."); - spotify.user_playlist_tracks( - &id, - 100, - tracks.offset + tracks.items.len() as u32, - ) - } - None => None, - } - } - - Playlist { - id, - name, - owner_id, - snapshot_id, - tracks: collected_tracks, - } - } - fn needs_download(&self, remote: &SimplifiedPlaylist) -> bool { for local in self .playlists @@ -291,7 +232,7 @@ impl Library { if self.needs_download(remote) { info!("updating playlist {}", remote.name); - let playlist = Self::process_simplified_playlist(remote, &self.spotify); + let playlist = Playlist::from_simplified_playlist(remote, &self.spotify); self.append_or_update(&playlist); // trigger redraw self.ev.trigger(); diff --git a/src/playlist.rs b/src/playlist.rs index 02dddc0..b9cf5f1 100644 --- a/src/playlist.rs +++ b/src/playlist.rs @@ -1,8 +1,11 @@ use std::iter::Iterator; use std::sync::Arc; +use rspotify::spotify::model::playlist::{FullPlaylist, SimplifiedPlaylist}; + use library::Library; use queue::Queue; +use spotify::Spotify; use track::Track; use traits::{IntoBoxedViewExt, ListItem, ViewExt}; use ui::playlist::PlaylistView; @@ -16,6 +19,67 @@ pub struct Playlist { pub tracks: Vec, } +impl Playlist { + pub fn from_simplified_playlist(list: &SimplifiedPlaylist, spotify: &Spotify) -> Playlist { + Self::_process_playlist( + list.id.clone(), + list.name.clone(), + list.owner.id.clone(), + list.snapshot_id.clone(), + spotify, + ) + } + + pub fn from_full_playlist(list: &FullPlaylist, spotify: &Spotify) -> Playlist { + Self::_process_playlist( + list.id.clone(), + list.name.clone(), + list.owner.id.clone(), + list.snapshot_id.clone(), + spotify, + ) + } + + fn _process_playlist( + id: String, + name: String, + owner_id: String, + snapshot_id: String, + spotify: &Spotify, + ) -> Playlist { + let mut collected_tracks = Vec::new(); + + let mut tracks_result = spotify.user_playlist_tracks(&id, 100, 0); + while let Some(ref tracks) = tracks_result.clone() { + for listtrack in &tracks.items { + collected_tracks.push((&listtrack.track).into()); + } + debug!("got {} tracks", tracks.items.len()); + + // load next batch if necessary + tracks_result = match tracks.next { + Some(_) => { + debug!("requesting tracks again.."); + spotify.user_playlist_tracks( + &id, + 100, + tracks.offset + tracks.items.len() as u32, + ) + } + None => None, + } + } + + Playlist { + id, + name, + owner_id, + snapshot_id, + tracks: collected_tracks, + } + } +} + impl ListItem for Playlist { fn is_playing(&self, queue: Arc) -> bool { let playing: Vec = queue diff --git a/src/ui/search.rs b/src/ui/search.rs index dc985e6..713bdda 100644 --- a/src/ui/search.rs +++ b/src/ui/search.rs @@ -225,7 +225,7 @@ impl SearchView { _append: bool, ) -> u32 { if let Some(results) = spotify.playlist(&query) { - let pls = vec![Library::process_full_playlist(&results, &&spotify)]; + let pls = vec![Playlist::from_full_playlist(&results, &&spotify)]; let mut r = playlists.write().unwrap(); *r = pls; return 1; @@ -245,7 +245,7 @@ impl SearchView { .playlists .items .iter() - .map(|sp| Library::process_simplified_playlist(sp, &&spotify)) + .map(|sp| Playlist::from_simplified_playlist(sp, &&spotify)) .collect(); let mut r = playlists.write().unwrap();