Handle local tracks in playlist

- Show error on deletion, as we currently do not have a Uri we can pass to the
  Spotify API to delete local files in playlists
- Mark local files in `ListView`

fixes #1213
This commit is contained in:
Henrik Friedrichsen
2023-07-22 15:38:37 +02:00
parent 90b4560a2d
commit 92cd4f1c8b
4 changed files with 29 additions and 20 deletions

View File

@@ -4,7 +4,7 @@ use std::{cmp::Ordering, iter::Iterator};
use rand::{seq::IteratorRandom, thread_rng};
use log::debug;
use log::{debug, warn};
use rspotify::model::playlist::{FullPlaylist, SimplifiedPlaylist};
use rspotify::model::Id;
@@ -56,11 +56,17 @@ impl Playlist {
}
pub fn delete_track(&mut self, index: usize, spotify: Spotify, library: &Library) -> bool {
let track = self.tracks.as_ref().unwrap()[index].clone();
debug!("deleting track: {} {:?}", index, track);
let playable = self.tracks.as_ref().unwrap()[index].clone();
debug!("deleting track: {} {:?}", index, playable);
if playable.track().map(|t| t.is_local) == Some(true) {
warn!("track is a local file, can't delete");
return false;
}
match spotify
.api
.delete_tracks(&self.id, &self.snapshot_id, &[track])
.delete_tracks(&self.id, &self.snapshot_id, &[playable])
{
false => false,
true => {

View File

@@ -16,7 +16,7 @@ use crate::queue::Queue;
use crate::traits::{IntoBoxedViewExt, ListItem, ViewExt};
use crate::ui::listview::ListView;
#[derive(Clone, Deserialize, Serialize)]
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Track {
pub id: Option<String>,
pub uri: String,
@@ -33,6 +33,7 @@ pub struct Track {
pub url: String,
pub added_at: Option<DateTime<Utc>>,
pub list_index: usize,
pub is_local: bool,
}
impl Track {
@@ -69,6 +70,7 @@ impl Track {
url: track.id.as_ref().map(|id| id.url()).unwrap_or_default(),
added_at: None,
list_index: 0,
is_local: track.is_local,
}
}
@@ -106,6 +108,7 @@ impl From<&SimplifiedTrack> for Track {
url: track.id.as_ref().map(|id| id.url()).unwrap_or_default(),
added_at: None,
list_index: 0,
is_local: track.is_local,
}
}
}
@@ -145,6 +148,7 @@ impl From<&FullTrack> for Track {
url: track.id.as_ref().map(|id| id.url()).unwrap_or_default(),
added_at: None,
list_index: 0,
is_local: track.is_local,
}
}
}
@@ -163,18 +167,6 @@ impl fmt::Display for Track {
}
}
impl fmt::Debug for Track {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"({} - {} ({:?}))",
self.artists.join(", "),
self.title,
self.id
)
}
}
impl ListItem for Track {
fn is_playing(&self, queue: &Queue) -> bool {
let current = queue.get_current();