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();

View File

@@ -220,6 +220,7 @@ impl<I: ListItem + Clone> View for ListView<I> {
let item = &content[i];
let currently_playing =
item.is_playing(&self.queue) && self.queue.get_current_index() == Some(i);
let is_local = item.track().map(|t| t.is_local).unwrap_or_default();
let style = if self.selected == i {
if currently_playing {
@@ -227,6 +228,11 @@ impl<I: ListItem + Clone> View for ListView<I> {
*printer.theme.palette.custom("playing_selected").unwrap(),
ColorType::Palette(PaletteColor::Highlight),
)
} else if is_local {
ColorStyle::new(
ColorType::Palette(PaletteColor::Secondary),
ColorType::Palette(PaletteColor::Highlight),
)
} else {
ColorStyle::highlight()
}
@@ -235,6 +241,8 @@ impl<I: ListItem + Clone> View for ListView<I> {
ColorType::Color(*printer.theme.palette.custom("playing").unwrap()),
ColorType::Color(*printer.theme.palette.custom("playing_bg").unwrap()),
)
} else if is_local {
ColorStyle::secondary()
} else {
ColorStyle::primary()
};

View File

@@ -80,13 +80,16 @@ impl ViewExt for PlaylistView {
fn on_command(&mut self, s: &mut Cursive, cmd: &Command) -> Result<CommandResult, String> {
if let Command::Delete = cmd {
let pos = self.list.get_selected_index();
if self
return if self
.playlist
.delete_track(pos, self.spotify.clone(), &self.library)
{
self.list.remove(pos);
}
return Ok(CommandResult::Consumed(None));
Ok(CommandResult::Consumed(None))
} else {
Err("Could not delete track.".to_string())
};
}
if let Command::Sort(key, direction) = cmd {