diff --git a/amadeus/src/app.rs b/amadeus/src/app.rs index 2d9295484095808fc5cf1a45c3c40e2519784b63..c2fcfea5ec11a6f5171a55a9d465b89877cdfd49 100644 --- a/amadeus/src/app.rs +++ b/amadeus/src/app.rs @@ -409,6 +409,7 @@ impl Amadeus { ) -> Command<<Self as Application>::Message> { let cfg = self.connect_config.clone(); let store = self.kara_store.clone(); + log::error!("{req:?}"); match req { RefreshRequest::Playlists => Command::perform(get_playlists(cfg), |res| { res.map_err(|err| log::error!("{err}")) diff --git a/amadeus/src/components/mainpanel/playlists.rs b/amadeus/src/components/mainpanel/playlists.rs index 9191fa98f706bfb5b68daa748071ebbaa9475845..7394354143cee8f5f68d71628d12a6c8d2b4f5b7 100644 --- a/amadeus/src/components/mainpanel/playlists.rs +++ b/amadeus/src/components/mainpanel/playlists.rs @@ -1,4 +1,5 @@ use crate::components::{self, icon, karalist, tip}; +use hashbrown::HashMap; use iced::{widget::row, Command, Element}; use lektor_payloads::{KId, Kara, PlaylistInfo}; use lektor_utils::log; @@ -6,7 +7,7 @@ use std::sync::Arc; #[derive(Default)] pub struct State { - playlists: Vec<(Arc<str>, PlaylistInfo, karalist::State)>, + playlists: HashMap<Arc<str>, (Option<PlaylistInfo>, karalist::State)>, to_show: Option<Arc<str>>, } @@ -58,43 +59,41 @@ pub enum Request { impl State { /// Get a mut view to a playlist info, content, etc. If the playlist was not found returns /// [None], otherwise returns [Some]. - fn get_mut( + fn get_mut_or_insert( &mut self, - name: impl AsRef<str>, - ) -> Option<&mut (Arc<str>, PlaylistInfo, karalist::State)> { - self.playlists - .iter_mut() - .find(|(plt, ..)| plt.as_ref().eq(name.as_ref())) + name: Arc<str>, + ) -> &mut (Option<PlaylistInfo>, karalist::State) { + self.playlists.entry(name).or_default() } /// Get a const view to a playlist info, content, etc. If the playlist was not found returns /// [None], otherwise returns [Some]. - fn get(&self, name: impl AsRef<str>) -> Option<&(Arc<str>, PlaylistInfo, karalist::State)> { + fn get( + &self, + name: impl AsRef<str>, + ) -> Option<(&Arc<str>, Option<&PlaylistInfo>, &karalist::State)> { self.playlists - .iter() - .find(|(plt, ..)| plt.as_ref().eq(name.as_ref())) + .get_key_value(name.as_ref()) + .map(|(k, (v1, v2))| (k, v1.as_ref(), v2)) } /// Remove playlists from the list of playlists, returns whever the playlist was found and was /// removed from the vector. fn remove(&mut self, names: &[impl AsRef<str>]) -> bool { - let count = self.playlists.len(); - self.playlists - .retain(|(name, ..)| names.iter().any(|n| n.as_ref().ne(name.as_ref()))); - count != self.playlists.len() + !names.iter().fold(false, |not, name| { + not || self.playlists.remove(name.as_ref()).is_none() + }) } /// Keep playlists from the list of playlists. fn keep(&mut self, names: &[impl AsRef<str>]) { self.playlists - .retain(|(name, ..)| names.iter().any(|n| n.as_ref().eq(name.as_ref()))); + .retain(|k, _| names.iter().any(|name| name.as_ref().eq(k.as_ref()))); } /// Returns whever the container contains the asked playlist or not. fn contains(&self, name: impl AsRef<str>) -> bool { - self.playlists - .iter() - .any(|(plt, ..)| name.as_ref().eq(plt.as_ref())) + self.playlists.contains_key(name.as_ref()) } pub fn update(&mut self, message: Message) -> Command<Request> { @@ -110,26 +109,20 @@ impl State { } Message::Reload(plt, karas) => { - match self.get_mut(&plt) { - Some((.., plt)) => plt.update(karalist::Message::Reload(karas)), - None => log::error!("failed to update playlist {plt}"), - } + let (.., plt) = self.get_mut_or_insert(plt); + plt.update(karalist::Message::Reload(karas)); Command::none() } Message::UpdatePlaylistInfos(plt, infos) => { - match self.get_mut(&plt) { - Some((_, old, _)) => *old = infos, - None => log::error!("failed to update playlist {plt}"), - } + let (old, _) = self.get_mut_or_insert(plt); + *old = Some(infos); Command::none() } Message::RemoveKaraFromPlaylist(plt, id) => { - match self.get_mut(&plt) { - Some((.., plt)) => plt.update(karalist::Message::RemoveId(id)), - None => log::error!("failed to delete kara {id:?} from playlist {plt}"), - } + let (.., plt) = self.get_mut_or_insert(plt); + plt.update(karalist::Message::RemoveId(id)); Command::none() } @@ -150,20 +143,19 @@ impl State { } Message::AddKaraToPlaylist(plt, kara) => { - match self.get_mut(&plt) { - Some((.., plt)) => plt.update(karalist::Message::Add(kara)), - None => log::error!("can't add kara {} to playlist {plt}", kara.id), - } + let (.., plt) = self.get_mut_or_insert(plt); + plt.update(karalist::Message::Add(kara)); Command::none() } Message::ShowPlaylist(name) => { if self.contains(&name) { self.to_show = Some(name); + Command::none() } else { - log::error!("asked to show playlist {name}, but it wasn't found"); + log::info!("asked to show playlist {name}, but it wasn't found"); + Command::perform(async {}, |_| Request::Refresh(name)) } - Command::none() } } }