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:
@@ -4,7 +4,7 @@ use std::{cmp::Ordering, iter::Iterator};
|
|||||||
|
|
||||||
use rand::{seq::IteratorRandom, thread_rng};
|
use rand::{seq::IteratorRandom, thread_rng};
|
||||||
|
|
||||||
use log::debug;
|
use log::{debug, warn};
|
||||||
use rspotify::model::playlist::{FullPlaylist, SimplifiedPlaylist};
|
use rspotify::model::playlist::{FullPlaylist, SimplifiedPlaylist};
|
||||||
use rspotify::model::Id;
|
use rspotify::model::Id;
|
||||||
|
|
||||||
@@ -56,11 +56,17 @@ impl Playlist {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn delete_track(&mut self, index: usize, spotify: Spotify, library: &Library) -> bool {
|
pub fn delete_track(&mut self, index: usize, spotify: Spotify, library: &Library) -> bool {
|
||||||
let track = self.tracks.as_ref().unwrap()[index].clone();
|
let playable = self.tracks.as_ref().unwrap()[index].clone();
|
||||||
debug!("deleting track: {} {:?}", index, track);
|
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
|
match spotify
|
||||||
.api
|
.api
|
||||||
.delete_tracks(&self.id, &self.snapshot_id, &[track])
|
.delete_tracks(&self.id, &self.snapshot_id, &[playable])
|
||||||
{
|
{
|
||||||
false => false,
|
false => false,
|
||||||
true => {
|
true => {
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ use crate::queue::Queue;
|
|||||||
use crate::traits::{IntoBoxedViewExt, ListItem, ViewExt};
|
use crate::traits::{IntoBoxedViewExt, ListItem, ViewExt};
|
||||||
use crate::ui::listview::ListView;
|
use crate::ui::listview::ListView;
|
||||||
|
|
||||||
#[derive(Clone, Deserialize, Serialize)]
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||||
pub struct Track {
|
pub struct Track {
|
||||||
pub id: Option<String>,
|
pub id: Option<String>,
|
||||||
pub uri: String,
|
pub uri: String,
|
||||||
@@ -33,6 +33,7 @@ pub struct Track {
|
|||||||
pub url: String,
|
pub url: String,
|
||||||
pub added_at: Option<DateTime<Utc>>,
|
pub added_at: Option<DateTime<Utc>>,
|
||||||
pub list_index: usize,
|
pub list_index: usize,
|
||||||
|
pub is_local: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Track {
|
impl Track {
|
||||||
@@ -69,6 +70,7 @@ impl Track {
|
|||||||
url: track.id.as_ref().map(|id| id.url()).unwrap_or_default(),
|
url: track.id.as_ref().map(|id| id.url()).unwrap_or_default(),
|
||||||
added_at: None,
|
added_at: None,
|
||||||
list_index: 0,
|
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(),
|
url: track.id.as_ref().map(|id| id.url()).unwrap_or_default(),
|
||||||
added_at: None,
|
added_at: None,
|
||||||
list_index: 0,
|
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(),
|
url: track.id.as_ref().map(|id| id.url()).unwrap_or_default(),
|
||||||
added_at: None,
|
added_at: None,
|
||||||
list_index: 0,
|
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 {
|
impl ListItem for Track {
|
||||||
fn is_playing(&self, queue: &Queue) -> bool {
|
fn is_playing(&self, queue: &Queue) -> bool {
|
||||||
let current = queue.get_current();
|
let current = queue.get_current();
|
||||||
|
|||||||
@@ -220,6 +220,7 @@ impl<I: ListItem + Clone> View for ListView<I> {
|
|||||||
let item = &content[i];
|
let item = &content[i];
|
||||||
let currently_playing =
|
let currently_playing =
|
||||||
item.is_playing(&self.queue) && self.queue.get_current_index() == Some(i);
|
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 {
|
let style = if self.selected == i {
|
||||||
if currently_playing {
|
if currently_playing {
|
||||||
@@ -227,6 +228,11 @@ impl<I: ListItem + Clone> View for ListView<I> {
|
|||||||
*printer.theme.palette.custom("playing_selected").unwrap(),
|
*printer.theme.palette.custom("playing_selected").unwrap(),
|
||||||
ColorType::Palette(PaletteColor::Highlight),
|
ColorType::Palette(PaletteColor::Highlight),
|
||||||
)
|
)
|
||||||
|
} else if is_local {
|
||||||
|
ColorStyle::new(
|
||||||
|
ColorType::Palette(PaletteColor::Secondary),
|
||||||
|
ColorType::Palette(PaletteColor::Highlight),
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
ColorStyle::highlight()
|
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").unwrap()),
|
||||||
ColorType::Color(*printer.theme.palette.custom("playing_bg").unwrap()),
|
ColorType::Color(*printer.theme.palette.custom("playing_bg").unwrap()),
|
||||||
)
|
)
|
||||||
|
} else if is_local {
|
||||||
|
ColorStyle::secondary()
|
||||||
} else {
|
} else {
|
||||||
ColorStyle::primary()
|
ColorStyle::primary()
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -80,13 +80,16 @@ impl ViewExt for PlaylistView {
|
|||||||
fn on_command(&mut self, s: &mut Cursive, cmd: &Command) -> Result<CommandResult, String> {
|
fn on_command(&mut self, s: &mut Cursive, cmd: &Command) -> Result<CommandResult, String> {
|
||||||
if let Command::Delete = cmd {
|
if let Command::Delete = cmd {
|
||||||
let pos = self.list.get_selected_index();
|
let pos = self.list.get_selected_index();
|
||||||
if self
|
|
||||||
|
return if self
|
||||||
.playlist
|
.playlist
|
||||||
.delete_track(pos, self.spotify.clone(), &self.library)
|
.delete_track(pos, self.spotify.clone(), &self.library)
|
||||||
{
|
{
|
||||||
self.list.remove(pos);
|
self.list.remove(pos);
|
||||||
}
|
Ok(CommandResult::Consumed(None))
|
||||||
return Ok(CommandResult::Consumed(None));
|
} else {
|
||||||
|
Err("Could not delete track.".to_string())
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Command::Sort(key, direction) = cmd {
|
if let Command::Sort(key, direction) = cmd {
|
||||||
|
|||||||
Reference in New Issue
Block a user