Implement following playlists

This commit is contained in:
KoffeinFlummi
2019-04-19 23:50:36 +02:00
parent e68ba60179
commit f320b953d6
3 changed files with 62 additions and 13 deletions

View File

@@ -140,6 +140,7 @@ impl Library {
Self::_process_playlist(
list.id.clone(),
list.name.clone(),
list.owner.id.clone(),
list.snapshot_id.clone(),
spotify,
)
@@ -149,6 +150,7 @@ impl Library {
Self::_process_playlist(
list.id.clone(),
list.name.clone(),
list.owner.id.clone(),
list.snapshot_id.clone(),
spotify,
)
@@ -157,6 +159,7 @@ impl Library {
fn _process_playlist(
id: String,
name: String,
owner_id: String,
snapshot_id: String,
spotify: &Spotify,
) -> Playlist {
@@ -182,10 +185,12 @@ impl Library {
None => None,
}
}
Playlist {
id: id.clone(),
name: name.clone(),
snapshot_id: snapshot_id.clone(),
id,
name,
owner_id,
snapshot_id,
tracks: collected_tracks,
}
}
@@ -212,6 +217,10 @@ impl Library {
}
pub fn delete_playlist(&self, id: &str) {
if !*self.is_done.read().unwrap() {
return;
}
let pos = {
let store = self.playlists.read().expect("can't readlock playlists");
store.iter().position(|ref i| i.id == id)
@@ -518,12 +527,14 @@ impl Library {
}
if api {
self.spotify.current_user_saved_tracks_add(
if self.spotify.current_user_saved_tracks_add(
tracks
.iter()
.map(|t| t.id.clone())
.collect()
);
).is_none() {
return;
}
}
{
@@ -551,12 +562,14 @@ impl Library {
}
if api {
self.spotify.current_user_saved_tracks_delete(
if self.spotify.current_user_saved_tracks_delete(
tracks
.iter()
.map(|t| t.id.clone())
.collect()
);
).is_none() {
return;
}
}
{
@@ -588,7 +601,9 @@ impl Library {
return;
}
self.spotify.current_user_saved_albums_add(vec![album.id.clone()]);
if self.spotify.current_user_saved_albums_add(vec![album.id.clone()]).is_none() {
return;
}
album.load_tracks(self.spotify.clone());
@@ -611,7 +626,9 @@ impl Library {
return;
}
self.spotify.current_user_saved_albums_delete(vec![album.id.clone()]);
if self.spotify.current_user_saved_albums_delete(vec![album.id.clone()]).is_none() {
return;
}
album.load_tracks(self.spotify.clone());
@@ -645,7 +662,9 @@ impl Library {
return;
}
self.spotify.user_follow_artists(vec![artist.id.clone()]);
if self.spotify.user_follow_artists(vec![artist.id.clone()]).is_none() {
return;
}
{
let mut store = self.artists.write().unwrap();
@@ -668,7 +687,9 @@ impl Library {
return;
}
self.spotify.user_unfollow_artists(vec![artist.id.clone()]);
if self.spotify.user_unfollow_artists(vec![artist.id.clone()]).is_none() {
return;
}
{
let mut store = self.artists.write().unwrap();
@@ -690,4 +711,23 @@ impl Library {
let playlists = self.playlists.read().unwrap();
playlists.iter().any(|p| p.id == playlist.id)
}
pub fn follow_playlist(&self, playlist: &Playlist) {
if !*self.is_done.read().unwrap() {
return;
}
if self.spotify.user_playlist_follow_playlist(playlist.owner_id.clone(), playlist.id.clone()).is_none() {
return;
}
{
let mut store = self.playlists.write().unwrap();
if !store.iter().any(|p| p.id == playlist.id) {
store.insert(0, playlist.clone());
}
}
self.save_cache(config::cache_path(CACHE_PLAYLISTS), self.playlists.clone());
}
}

View File

@@ -10,6 +10,7 @@ use traits::ListItem;
pub struct Playlist {
pub id: String,
pub name: String,
pub owner_id: String,
pub snapshot_id: String,
pub tracks: Vec<Track>,
}
@@ -55,7 +56,11 @@ impl ListItem for Playlist {
}
}
fn toggle_saved(&mut self, _library: Arc<Library>) {
// TODO
fn toggle_saved(&mut self, library: Arc<Library>) {
if library.is_saved_playlist(self) {
library.delete_playlist(&self.id);
} else {
library.follow_playlist(self);
}
}
}

View File

@@ -564,6 +564,10 @@ impl Spotify {
self.api_with_retry(|api| api.current_user_saved_tracks_delete(ids.clone()))
}
pub fn user_playlist_follow_playlist(&self, owner_id: String, id: String) -> Option<()> {
self.api_with_retry(|api| api.user_playlist_follow_playlist(&owner_id, &id, true))
}
pub fn load(&self, track: &Track) {
info!("loading track: {:?}", track);
self.channel