add methods and api usage to delete track from playlist (#211)

* wip: add methods and api usage to delete track from playlist

* enh: add ability to remove track from playlist with immediate visual feedback

* minor cosmetic changes

Co-authored-by: Henrik Friedrichsen <henrik@affekt.org>
This commit is contained in:
Thomas Storey
2020-07-01 14:52:39 -04:00
committed by GitHub
parent ca1e8df361
commit e5419dae2d
4 changed files with 62 additions and 2 deletions

View File

@@ -154,6 +154,11 @@ impl<I: ListItem> ListView<I> {
false
}
}
pub fn remove(&self, index: usize) {
let mut c = self.content.write().unwrap();
c.remove(index);
}
}
impl<I: ListItem> View for ListView<I> {

View File

@@ -8,6 +8,7 @@ use crate::commands::CommandResult;
use crate::library::Library;
use crate::playlist::Playlist;
use crate::queue::Queue;
use crate::spotify::Spotify;
use crate::track::Track;
use crate::traits::ViewExt;
use crate::ui::listview::ListView;
@@ -15,12 +16,12 @@ use crate::ui::listview::ListView;
pub struct PlaylistView {
playlist: Playlist,
list: ListView<Track>,
spotify: Arc<Spotify>,
}
impl PlaylistView {
pub fn new(queue: Arc<Queue>, library: Arc<Library>, playlist: &Playlist) -> Self {
let mut playlist = playlist.clone();
playlist.load_tracks(queue.get_spotify());
let tracks = if let Some(t) = playlist.tracks.as_ref() {
@@ -29,9 +30,14 @@ impl PlaylistView {
Vec::new()
};
let spotify = queue.get_spotify();
let list = ListView::new(Arc::new(RwLock::new(tracks)), queue, library);
Self { playlist, list }
Self {
playlist,
list,
spotify,
}
}
}
@@ -45,6 +51,22 @@ 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();
let tracks = if let Some(t) = self.playlist.tracks.as_ref() {
t.clone()
} else {
Vec::new()
};
let track = tracks.get(pos);
if let Some(t) = track {
self.playlist
.delete_tracks(&[(t.clone(), pos)], self.spotify.clone());
self.list.remove(pos);
}
return Ok(CommandResult::Consumed(None));
}
self.list.on_command(s, cmd)
}
}