implement functionality to save queues to playlists

This commit is contained in:
Henrik Friedrichsen
2019-03-24 16:31:49 +01:00
parent 212edcb18c
commit e0f7b5c156
6 changed files with 113 additions and 11 deletions

View File

@@ -1,7 +1,7 @@
use std::iter::Iterator;
use std::ops::Deref;
use std::path::PathBuf;
use std::sync::{Arc, RwLock};
use std::sync::{Arc, RwLock, RwLockReadGuard};
use rspotify::spotify::model::playlist::SimplifiedPlaylist;
@@ -58,6 +58,12 @@ impl Playlists {
}
}
pub fn items(&self) -> RwLockReadGuard<Vec<Playlist>> {
self.store
.read()
.expect("could not readlock listview content")
}
pub fn load_cache(&self) {
if let Ok(contents) = std::fs::read_to_string(&self.cache_path) {
debug!(
@@ -142,6 +148,22 @@ impl Playlists {
store.len() - 1
}
pub fn overwrite_playlist(&self, id: &str, tracks: &Vec<Track>) {
debug!("saving {} tracks to {}", tracks.len(), id);
self.spotify.overwrite_playlist(id, &tracks);
self.fetch_playlists();
self.save_cache();
}
pub fn save_playlist(&self, name: &str, tracks: &Vec<Track>) {
debug!("saving {} tracks to new list {}", tracks.len(), name);
match self.spotify.create_playlist(name, None, None) {
Some(id) => self.overwrite_playlist(&id, &tracks),
None => error!("could not create new playlist.."),
}
}
pub fn fetch_playlists(&self) {
debug!("loading playlists");
let mut stale_lists = self.store.read().unwrap().clone();