Refactor: separate Spotify API from player logic
The separation is not perfect yet, but it's a start and makes the entire codebase much easier to read.
This commit is contained in:
@@ -42,7 +42,7 @@ pub struct Library {
|
||||
|
||||
impl Library {
|
||||
pub fn new(ev: &EventManager, spotify: Spotify, cfg: Arc<Config>) -> Self {
|
||||
let current_user = spotify.current_user();
|
||||
let current_user = spotify.api.current_user();
|
||||
let user_id = current_user.as_ref().map(|u| u.id.clone());
|
||||
let display_name = current_user.as_ref().and_then(|u| u.display_name.clone());
|
||||
|
||||
@@ -131,7 +131,7 @@ impl Library {
|
||||
};
|
||||
|
||||
if let Some(position) = pos {
|
||||
if self.spotify.delete_playlist(id) {
|
||||
if self.spotify.api.delete_playlist(id) {
|
||||
{
|
||||
let mut store = self.playlists.write().expect("can't writelock playlists");
|
||||
store.remove(position);
|
||||
@@ -143,7 +143,7 @@ impl Library {
|
||||
|
||||
pub fn overwrite_playlist(&self, id: &str, tracks: &[Playable]) {
|
||||
debug!("saving {} tracks to list {}", tracks.len(), id);
|
||||
self.spotify.overwrite_playlist(id, tracks);
|
||||
self.spotify.api.overwrite_playlist(id, tracks);
|
||||
|
||||
self.fetch_playlists();
|
||||
self.save_cache(config::cache_path(CACHE_PLAYLISTS), self.playlists.clone());
|
||||
@@ -151,7 +151,7 @@ impl Library {
|
||||
|
||||
pub fn save_playlist(&self, name: &str, tracks: &[Playable]) {
|
||||
debug!("saving {} tracks to new list {}", tracks.len(), name);
|
||||
match self.spotify.create_playlist(name, None, None) {
|
||||
match self.spotify.api.create_playlist(name, None, None) {
|
||||
Some(id) => self.overwrite_playlist(&id, tracks),
|
||||
None => error!("could not create new playlist.."),
|
||||
}
|
||||
@@ -231,7 +231,7 @@ impl Library {
|
||||
debug!("loading shows");
|
||||
|
||||
let mut saved_shows: Vec<Show> = Vec::new();
|
||||
let mut shows_result = self.spotify.get_saved_shows(0);
|
||||
let mut shows_result = self.spotify.api.get_saved_shows(0);
|
||||
|
||||
while let Some(shows) = shows_result.as_ref() {
|
||||
saved_shows.extend(shows.items.iter().map(|show| (&show.show).into()));
|
||||
@@ -241,6 +241,7 @@ impl Library {
|
||||
Some(_) => {
|
||||
debug!("requesting shows again..");
|
||||
self.spotify
|
||||
.api
|
||||
.get_saved_shows(shows.offset + shows.items.len() as u32)
|
||||
}
|
||||
None => None,
|
||||
@@ -255,7 +256,7 @@ impl Library {
|
||||
let mut stale_lists = self.playlists.read().unwrap().clone();
|
||||
let mut list_order = Vec::new();
|
||||
|
||||
let lists_page = self.spotify.current_user_playlist();
|
||||
let lists_page = self.spotify.api.current_user_playlist();
|
||||
let mut lists_batch = Some(lists_page.items.read().unwrap().clone());
|
||||
while let Some(lists) = &lists_batch {
|
||||
for (index, remote) in lists.iter().enumerate() {
|
||||
@@ -311,7 +312,7 @@ impl Library {
|
||||
let mut i: u32 = 0;
|
||||
|
||||
loop {
|
||||
let page = self.spotify.current_user_followed_artists(last);
|
||||
let page = self.spotify.api.current_user_followed_artists(last);
|
||||
debug!("artists page: {}", i);
|
||||
i += 1;
|
||||
if page.is_none() {
|
||||
@@ -363,7 +364,10 @@ impl Library {
|
||||
let mut i: u32 = 0;
|
||||
|
||||
loop {
|
||||
let page = self.spotify.current_user_saved_albums(albums.len() as u32);
|
||||
let page = self
|
||||
.spotify
|
||||
.api
|
||||
.current_user_saved_albums(albums.len() as u32);
|
||||
debug!("albums page: {}", i);
|
||||
i += 1;
|
||||
if page.is_none() {
|
||||
@@ -409,7 +413,10 @@ impl Library {
|
||||
let mut i: u32 = 0;
|
||||
|
||||
loop {
|
||||
let page = self.spotify.current_user_saved_tracks(tracks.len() as u32);
|
||||
let page = self
|
||||
.spotify
|
||||
.api
|
||||
.current_user_saved_tracks(tracks.len() as u32);
|
||||
|
||||
debug!("tracks page: {}", i);
|
||||
i += 1;
|
||||
@@ -539,6 +546,7 @@ impl Library {
|
||||
if api
|
||||
&& self
|
||||
.spotify
|
||||
.api
|
||||
.current_user_saved_tracks_add(tracks.iter().filter_map(|t| t.id.clone()).collect())
|
||||
.is_none()
|
||||
{
|
||||
@@ -572,6 +580,7 @@ impl Library {
|
||||
if api
|
||||
&& self
|
||||
.spotify
|
||||
.api
|
||||
.current_user_saved_tracks_delete(
|
||||
tracks.iter().filter_map(|t| t.id.clone()).collect(),
|
||||
)
|
||||
@@ -612,6 +621,7 @@ impl Library {
|
||||
if let Some(ref album_id) = album.id {
|
||||
if self
|
||||
.spotify
|
||||
.api
|
||||
.current_user_saved_albums_add(vec![album_id.clone()])
|
||||
.is_none()
|
||||
{
|
||||
@@ -637,6 +647,7 @@ impl Library {
|
||||
if let Some(ref album_id) = album.id {
|
||||
if self
|
||||
.spotify
|
||||
.api
|
||||
.current_user_saved_albums_delete(vec![album_id.clone()])
|
||||
.is_none()
|
||||
{
|
||||
@@ -669,6 +680,7 @@ impl Library {
|
||||
if let Some(ref artist_id) = artist.id {
|
||||
if self
|
||||
.spotify
|
||||
.api
|
||||
.user_follow_artists(vec![artist_id.clone()])
|
||||
.is_none()
|
||||
{
|
||||
@@ -700,6 +712,7 @@ impl Library {
|
||||
if let Some(ref artist_id) = artist.id {
|
||||
if self
|
||||
.spotify
|
||||
.api
|
||||
.user_unfollow_artists(vec![artist_id.clone()])
|
||||
.is_none()
|
||||
{
|
||||
@@ -742,6 +755,7 @@ impl Library {
|
||||
|
||||
if self
|
||||
.spotify
|
||||
.api
|
||||
.user_playlist_follow_playlist(playlist.owner_id.clone(), playlist.id.clone())
|
||||
.is_none()
|
||||
{
|
||||
@@ -775,7 +789,7 @@ impl Library {
|
||||
return;
|
||||
}
|
||||
|
||||
if self.spotify.save_shows(vec![show.id.clone()]) {
|
||||
if self.spotify.api.save_shows(vec![show.id.clone()]) {
|
||||
{
|
||||
let mut store = self.shows.write().unwrap();
|
||||
if !store.iter().any(|s| s.id == show.id) {
|
||||
@@ -790,7 +804,7 @@ impl Library {
|
||||
return;
|
||||
}
|
||||
|
||||
if self.spotify.unsave_shows(vec![show.id.clone()]) {
|
||||
if self.spotify.api.unsave_shows(vec![show.id.clone()]) {
|
||||
{
|
||||
let mut store = self.shows.write().unwrap();
|
||||
*store = store.iter().filter(|s| s.id != show.id).cloned().collect();
|
||||
|
||||
Reference in New Issue
Block a user