Implement (un)saving tracks, albums; (un)following artists
This commit is contained in:
@@ -22,7 +22,7 @@ failure = "0.1.3"
|
|||||||
fern = "0.5"
|
fern = "0.5"
|
||||||
futures = "0.1"
|
futures = "0.1"
|
||||||
log = "0.4.0"
|
log = "0.4.0"
|
||||||
rspotify = "0.4.0"
|
#rspotify = "0.4.0"
|
||||||
serde = "1.0"
|
serde = "1.0"
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
toml = "0.4"
|
toml = "0.4"
|
||||||
@@ -34,6 +34,10 @@ dbus = { version = "0.6.4", optional = true }
|
|||||||
rand = "0.6.5"
|
rand = "0.6.5"
|
||||||
webbrowser = "0.5"
|
webbrowser = "0.5"
|
||||||
|
|
||||||
|
[dependencies.rspotify]
|
||||||
|
git = "https://github.com/KoffeinFlummi/rspotify"
|
||||||
|
rev = "1a30afc"
|
||||||
|
|
||||||
[dependencies.librespot]
|
[dependencies.librespot]
|
||||||
git = "https://github.com/librespot-org/librespot.git"
|
git = "https://github.com/librespot-org/librespot.git"
|
||||||
rev = "14721f4"
|
rev = "14721f4"
|
||||||
|
|||||||
@@ -160,4 +160,12 @@ impl ListItem for Album {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn toggle_saved(&mut self, library: Arc<Library>) {
|
||||||
|
if library.is_saved_album(self) {
|
||||||
|
library.unsave_album(self);
|
||||||
|
} else {
|
||||||
|
library.save_album(self);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -160,4 +160,12 @@ impl ListItem for Artist {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn toggle_saved(&mut self, library: Arc<Library>) {
|
||||||
|
if library.is_followed_artist(self) {
|
||||||
|
library.unfollow_artist(self);
|
||||||
|
} else {
|
||||||
|
library.follow_artist(self);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
186
src/library.rs
186
src/library.rs
@@ -5,7 +5,6 @@ use std::path::PathBuf;
|
|||||||
use std::sync::{Arc, RwLock, RwLockReadGuard};
|
use std::sync::{Arc, RwLock, RwLockReadGuard};
|
||||||
use std::thread;
|
use std::thread;
|
||||||
|
|
||||||
use rspotify::spotify::model::artist::SimplifiedArtist;
|
|
||||||
use rspotify::spotify::model::playlist::{FullPlaylist, SimplifiedPlaylist};
|
use rspotify::spotify::model::playlist::{FullPlaylist, SimplifiedPlaylist};
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
@@ -311,22 +310,31 @@ impl Library {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only play saved tracks
|
|
||||||
artist.albums = Some(Vec::new());
|
|
||||||
artist.tracks = Some(Vec::new());
|
|
||||||
artist.is_followed = true;
|
artist.is_followed = true;
|
||||||
|
|
||||||
store.push(artist.clone());
|
store.push(artist.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn insert_artist(&self, artist: &SimplifiedArtist) {
|
fn insert_artist(&self, track: &Track) {
|
||||||
let mut artists = self.artists.write().unwrap();
|
let mut artists = self.artists.write().unwrap();
|
||||||
if artists.iter().any(|a| a.id == artist.id) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
artists.push(artist.into());
|
for (id, name) in track.artist_ids.iter().zip(track.artists.iter()) {
|
||||||
|
let artist = Artist {
|
||||||
|
id: id.clone(),
|
||||||
|
name: name.clone(),
|
||||||
|
url: "".into(),
|
||||||
|
albums: Some(Vec::new()),
|
||||||
|
tracks: Some(Vec::new()),
|
||||||
|
is_followed: false,
|
||||||
|
};
|
||||||
|
|
||||||
|
if artists.iter().any(|a| a.id == artist.id) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
artists.push(artist.into());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fetch_albums(&self) {
|
fn fetch_albums(&self) {
|
||||||
@@ -387,12 +395,7 @@ impl Library {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for track in page.items.iter() {
|
tracks.extend(page.items.iter().map(|t| t.into()));
|
||||||
for artist in track.track.artists.iter() {
|
|
||||||
self.insert_artist(artist);
|
|
||||||
}
|
|
||||||
tracks.push(track.into());
|
|
||||||
}
|
|
||||||
|
|
||||||
if page.next.is_none() {
|
if page.next.is_none() {
|
||||||
break;
|
break;
|
||||||
@@ -403,11 +406,35 @@ impl Library {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn populate_artists(&self) {
|
fn populate_artists(&self) {
|
||||||
|
// Remove old unfollowed artists
|
||||||
|
{
|
||||||
|
let mut artists = self.artists.write().unwrap();
|
||||||
|
*artists = artists
|
||||||
|
.iter()
|
||||||
|
.filter(|a| a.is_followed)
|
||||||
|
.cloned()
|
||||||
|
.collect();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add artists that aren't followed but have saved tracks/albums
|
||||||
|
{
|
||||||
|
let tracks = self.tracks.read().unwrap();
|
||||||
|
for track in tracks.iter() {
|
||||||
|
self.insert_artist(track);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let mut artists = self.artists.write().unwrap();
|
let mut artists = self.artists.write().unwrap();
|
||||||
let mut lookup: HashMap<String, Option<usize>> = HashMap::new();
|
let mut lookup: HashMap<String, Option<usize>> = HashMap::new();
|
||||||
|
|
||||||
|
for artist in artists.iter_mut() {
|
||||||
|
artist.albums = Some(Vec::new());
|
||||||
|
artist.tracks = Some(Vec::new());
|
||||||
|
}
|
||||||
|
|
||||||
artists.sort_unstable_by(|a, b| a.name.partial_cmp(&b.name).unwrap());
|
artists.sort_unstable_by(|a, b| a.name.partial_cmp(&b.name).unwrap());
|
||||||
|
|
||||||
|
// Add saved albums to artists
|
||||||
{
|
{
|
||||||
let albums = self.albums.read().unwrap();
|
let albums = self.albums.read().unwrap();
|
||||||
for album in albums.iter() {
|
for album in albums.iter() {
|
||||||
@@ -438,6 +465,7 @@ impl Library {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add saved tracks to artists
|
||||||
{
|
{
|
||||||
let tracks = self.tracks.read().unwrap();
|
let tracks = self.tracks.read().unwrap();
|
||||||
for track in tracks.iter() {
|
for track in tracks.iter() {
|
||||||
@@ -474,16 +502,144 @@ impl Library {
|
|||||||
tracks.iter().any(|t| t.id == track.id)
|
tracks.iter().any(|t| t.id == track.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn save_tracks(&self, tracks: Vec<&Track>, api: bool) {
|
||||||
|
if api {
|
||||||
|
self.spotify.current_user_saved_tracks_add(
|
||||||
|
tracks
|
||||||
|
.iter()
|
||||||
|
.map(|t| t.id.clone())
|
||||||
|
.collect()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut store = self.tracks.write().unwrap();
|
||||||
|
let mut i = 0;
|
||||||
|
for track in tracks {
|
||||||
|
if store.iter().any(|t| t.id == track.id) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
store.insert(i, track.clone());
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self.populate_artists();
|
||||||
|
|
||||||
|
self.save_cache(config::cache_path(CACHE_TRACKS), self.tracks.clone());
|
||||||
|
self.save_cache(config::cache_path(CACHE_ARTISTS), self.artists.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn unsave_tracks(&self, tracks: Vec<&Track>, api: bool) {
|
||||||
|
if api {
|
||||||
|
self.spotify.current_user_saved_tracks_delete(
|
||||||
|
tracks
|
||||||
|
.iter()
|
||||||
|
.map(|t| t.id.clone())
|
||||||
|
.collect()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut store = self.tracks.write().unwrap();
|
||||||
|
*store = store
|
||||||
|
.iter()
|
||||||
|
.filter(|t| !tracks.iter().any(|tt| t.id == tt.id))
|
||||||
|
.cloned()
|
||||||
|
.collect();
|
||||||
|
}
|
||||||
|
|
||||||
|
self.populate_artists();
|
||||||
|
|
||||||
|
self.save_cache(config::cache_path(CACHE_TRACKS), self.tracks.clone());
|
||||||
|
self.save_cache(config::cache_path(CACHE_ARTISTS), self.artists.clone());
|
||||||
|
}
|
||||||
|
|
||||||
pub fn is_saved_album(&self, album: &Album) -> bool {
|
pub fn is_saved_album(&self, album: &Album) -> bool {
|
||||||
let albums = self.albums.read().unwrap();
|
let albums = self.albums.read().unwrap();
|
||||||
albums.iter().any(|a| a.id == album.id)
|
albums.iter().any(|a| a.id == album.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn save_album(&self, album: &mut Album) {
|
||||||
|
self.spotify.current_user_saved_albums_add(vec![album.id.clone()]);
|
||||||
|
|
||||||
|
album.load_tracks(self.spotify.clone());
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut store = self.albums.write().unwrap();
|
||||||
|
if !store.iter().any(|a| a.id == album.id) {
|
||||||
|
store.insert(0, album.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(tracks) = album.tracks.as_ref() {
|
||||||
|
self.save_tracks(tracks.iter().collect(), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.save_cache(config::cache_path(CACHE_ALBUMS), self.albums.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn unsave_album(&self, album: &mut Album) {
|
||||||
|
self.spotify.current_user_saved_albums_delete(vec![album.id.clone()]);
|
||||||
|
|
||||||
|
album.load_tracks(self.spotify.clone());
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut store = self.albums.write().unwrap();
|
||||||
|
*store = store
|
||||||
|
.iter()
|
||||||
|
.filter(|a| a.id != album.id)
|
||||||
|
.cloned()
|
||||||
|
.collect();
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(tracks) = album.tracks.as_ref() {
|
||||||
|
self.unsave_tracks(tracks.iter().collect(), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.save_cache(config::cache_path(CACHE_ALBUMS), self.albums.clone());
|
||||||
|
}
|
||||||
|
|
||||||
pub fn is_followed_artist(&self, artist: &Artist) -> bool {
|
pub fn is_followed_artist(&self, artist: &Artist) -> bool {
|
||||||
let artists = self.artists.read().unwrap();
|
let artists = self.artists.read().unwrap();
|
||||||
artists.iter().any(|a| a.id == artist.id && a.is_followed)
|
artists.iter().any(|a| a.id == artist.id && a.is_followed)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn follow_artist(&self, artist: &Artist) {
|
||||||
|
self.spotify.user_follow_artists(vec![artist.id.clone()]);
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut store = self.artists.write().unwrap();
|
||||||
|
if let Some(i) = store.iter().position(|a| a.id == artist.id) {
|
||||||
|
store[i].is_followed = true;
|
||||||
|
} else {
|
||||||
|
let mut artist = artist.clone();
|
||||||
|
artist.is_followed = true;
|
||||||
|
store.push(artist);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self.populate_artists();
|
||||||
|
|
||||||
|
self.save_cache(config::cache_path(CACHE_ARTISTS), self.artists.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn unfollow_artist(&self, artist: &Artist) {
|
||||||
|
self.spotify.user_unfollow_artists(vec![artist.id.clone()]);
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut store = self.artists.write().unwrap();
|
||||||
|
if let Some(i) = store.iter().position(|a| a.id == artist.id) {
|
||||||
|
store[i].is_followed = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self.populate_artists();
|
||||||
|
|
||||||
|
self.save_cache(config::cache_path(CACHE_ARTISTS), self.artists.clone());
|
||||||
|
}
|
||||||
|
|
||||||
pub fn is_saved_playlist(&self, playlist: &Playlist) -> bool {
|
pub fn is_saved_playlist(&self, playlist: &Playlist) -> bool {
|
||||||
let playlists = self.playlists.read().unwrap();
|
let playlists = self.playlists.read().unwrap();
|
||||||
playlists.iter().any(|p| p.id == playlist.id)
|
playlists.iter().any(|p| p.id == playlist.id)
|
||||||
|
|||||||
@@ -54,4 +54,8 @@ impl ListItem for Playlist {
|
|||||||
queue.append(track);
|
queue.append(track);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn toggle_saved(&mut self, _library: Arc<Library>) {
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -532,14 +532,38 @@ impl Spotify {
|
|||||||
.map(|cp| cp.artists)
|
.map(|cp| cp.artists)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn user_follow_artists(&self, ids: Vec<String>) -> Option<()> {
|
||||||
|
self.api_with_retry(|api| api.user_follow_artists(&ids))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn user_unfollow_artists(&self, ids: Vec<String>) -> Option<()> {
|
||||||
|
self.api_with_retry(|api| api.user_unfollow_artists(&ids))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn current_user_saved_albums(&self, offset: u32) -> Option<Page<SavedAlbum>> {
|
pub fn current_user_saved_albums(&self, offset: u32) -> Option<Page<SavedAlbum>> {
|
||||||
self.api_with_retry(|api| api.current_user_saved_albums(50, offset))
|
self.api_with_retry(|api| api.current_user_saved_albums(50, offset))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn current_user_saved_albums_add(&self, ids: Vec<String>) -> Option<()> {
|
||||||
|
self.api_with_retry(|api| api.current_user_saved_albums_add(&ids))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn current_user_saved_albums_delete(&self, ids: Vec<String>) -> Option<()> {
|
||||||
|
self.api_with_retry(|api| api.current_user_saved_albums_delete(&ids))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn current_user_saved_tracks(&self, offset: u32) -> Option<Page<SavedTrack>> {
|
pub fn current_user_saved_tracks(&self, offset: u32) -> Option<Page<SavedTrack>> {
|
||||||
self.api_with_retry(|api| api.current_user_saved_tracks(50, offset))
|
self.api_with_retry(|api| api.current_user_saved_tracks(50, offset))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn current_user_saved_tracks_add(&self, ids: Vec<String>) -> Option<()> {
|
||||||
|
self.api_with_retry(|api| api.current_user_saved_tracks_add(&ids))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn current_user_saved_tracks_delete(&self, ids: Vec<String>) -> Option<()> {
|
||||||
|
self.api_with_retry(|api| api.current_user_saved_tracks_delete(ids.clone()))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn load(&self, track: &Track) {
|
pub fn load(&self, track: &Track) {
|
||||||
info!("loading track: {:?}", track);
|
info!("loading track: {:?}", track);
|
||||||
self.channel
|
self.channel
|
||||||
|
|||||||
@@ -169,4 +169,12 @@ impl ListItem for Track {
|
|||||||
fn queue(&mut self, queue: Arc<Queue>) {
|
fn queue(&mut self, queue: Arc<Queue>) {
|
||||||
queue.append(self);
|
queue.append(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn toggle_saved(&mut self, library: Arc<Library>) {
|
||||||
|
if library.is_saved_track(self) {
|
||||||
|
library.unsave_tracks(vec![self], true);
|
||||||
|
} else {
|
||||||
|
library.save_tracks(vec![self], true);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ pub trait ListItem: Sync + Send + 'static {
|
|||||||
fn display_right(&self, library: Arc<Library>) -> String;
|
fn display_right(&self, library: Arc<Library>) -> String;
|
||||||
fn play(&mut self, queue: Arc<Queue>);
|
fn play(&mut self, queue: Arc<Queue>);
|
||||||
fn queue(&mut self, queue: Arc<Queue>);
|
fn queue(&mut self, queue: Arc<Queue>);
|
||||||
|
fn toggle_saved(&mut self, library: Arc<Library>);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait ViewExt: View {
|
pub trait ViewExt: View {
|
||||||
|
|||||||
@@ -273,7 +273,7 @@ impl<I: ListItem> View for ListView<I> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<I: ListItem> ViewExt for ListView<I> {
|
impl<I: ListItem + Clone> ViewExt for ListView<I> {
|
||||||
fn on_command(
|
fn on_command(
|
||||||
&mut self,
|
&mut self,
|
||||||
_s: &mut Cursive,
|
_s: &mut Cursive,
|
||||||
@@ -299,6 +299,17 @@ impl<I: ListItem> ViewExt for ListView<I> {
|
|||||||
return Ok(CommandResult::Consumed(None));
|
return Ok(CommandResult::Consumed(None));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if cmd == "save" {
|
||||||
|
let mut item = {
|
||||||
|
let content = self.content.read().unwrap();
|
||||||
|
content.get(self.selected).cloned()
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(item) = item.as_mut() {
|
||||||
|
item.toggle_saved(self.library.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if cmd == "move" {
|
if cmd == "move" {
|
||||||
if let Some(dir) = args.get(0) {
|
if let Some(dir) = args.get(0) {
|
||||||
let amount: usize = args
|
let amount: usize = args
|
||||||
|
|||||||
Reference in New Issue
Block a user