fix: update playlist changes in local store

Move playlist change logic out of the library while we're at it and notify
the library of changes instead.

fixes #302
This commit is contained in:
Henrik Friedrichsen
2020-10-25 00:09:23 +02:00
parent 6587efdfd2
commit 5fb4eb7af2
6 changed files with 85 additions and 52 deletions

View File

@@ -4,7 +4,6 @@ use cursive::view::{Margins, ViewWrapper};
use cursive::views::{Dialog, NamedView, ScrollView, SelectView};
use cursive::Cursive;
use crate::command::{Command, MoveAmount, MoveMode};
use crate::commands::CommandResult;
use crate::library::Library;
use crate::queue::Queue;
@@ -12,6 +11,11 @@ use crate::track::Track;
use crate::traits::{ListItem, ViewExt};
use crate::ui::layout::Layout;
use crate::ui::modal::Modal;
use crate::{
command::{Command, MoveAmount, MoveMode},
playlist::Playlist,
spotify::Spotify,
};
#[cfg(feature = "share_clipboard")]
use clipboard::{ClipboardContext, ClipboardProvider};
use cursive::traits::{Finder, Nameable};
@@ -28,23 +32,33 @@ enum ContextMenuAction {
}
impl ContextMenu {
pub fn add_track_dialog(library: Arc<Library>, track: Track) -> Modal<Dialog> {
let mut list_select: SelectView<String> = SelectView::new().autojump();
pub fn add_track_dialog(
library: Arc<Library>,
spotify: Arc<Spotify>,
track: Track,
) -> Modal<Dialog> {
let mut list_select: SelectView<Playlist> = SelectView::new().autojump();
for list in library.items().iter() {
list_select.add_item(list.name.clone(), list.id.clone());
list_select.add_item(list.name.clone(), list.clone());
}
list_select.set_on_submit(move |s, selected| {
let track = track.clone();
let mut playlist = selected.clone();
let spotify = spotify.clone();
let library = library.clone();
if library.playlist_has_track(selected, &track.id.as_ref().unwrap_or(&"".to_string())) {
if playlist.has_track(track.id.as_ref().unwrap_or(&String::new())) {
let mut already_added_dialog = Self::track_already_added();
let lib = Arc::clone(&library);
let selected = selected.to_string();
already_added_dialog.add_button("Add anyway", move |c| {
lib.playlist_append_tracks(&selected.clone(), &[track.clone()]);
let mut playlist = playlist.clone();
let spotify = spotify.clone();
let library = library.clone();
playlist.append_tracks(&[track.clone()], spotify, library);
// playlist.map(|p| p.append_tracks(&[track.clone()], spotify, library));
c.pop_layer();
// Close add_track_dialog too
@@ -55,10 +69,10 @@ impl ContextMenu {
s.add_layer(modal);
return;
} else {
playlist.append_tracks(&[track.clone()], spotify, library);
s.pop_layer();
}
library.playlist_append_tracks(selected, &[track]);
s.pop_layer();
});
let dialog = Dialog::new()
@@ -118,7 +132,8 @@ impl ContextMenu {
.ok();
}
ContextMenuAction::AddToPlaylist(track) => {
let dialog = Self::add_track_dialog(library, *track.clone());
let dialog =
Self::add_track_dialog(library, queue.get_spotify(), *track.clone());
s.add_layer(dialog);
}
ContextMenuAction::ShowRecommentations(item) => {

View File

@@ -17,6 +17,7 @@ pub struct PlaylistView {
playlist: Playlist,
list: ListView<Track>,
spotify: Arc<Spotify>,
library: Arc<Library>,
}
impl PlaylistView {
@@ -31,12 +32,13 @@ impl PlaylistView {
};
let spotify = queue.get_spotify();
let list = ListView::new(Arc::new(RwLock::new(tracks)), queue, library);
let list = ListView::new(Arc::new(RwLock::new(tracks)), queue, library.clone());
Self {
playlist,
list,
spotify,
library,
}
}
}
@@ -60,8 +62,11 @@ impl ViewExt for PlaylistView {
};
let track = tracks.get(pos);
if let Some(t) = track {
self.playlist
.delete_tracks(&[(t.clone(), pos)], self.spotify.clone());
self.playlist.delete_tracks(
&[(t.clone(), pos)],
self.spotify.clone(),
self.library.clone(),
);
self.list.remove(pos);
}
return Ok(CommandResult::Consumed(None));