From 793ce9917cab22ed51751e968fe827515be5e340 Mon Sep 17 00:00:00 2001 From: Alexandre Trendel Date: Fri, 17 Feb 2023 10:09:36 -0500 Subject: [PATCH] fix nav + review fixes --- src/app/components/navigation/navigation.rs | 4 ++-- .../components/navigation/navigation_model.rs | 5 ----- src/app/components/sidebar/sidebar.rs | 21 +++++++++++-------- src/app/components/sidebar/sidebar_item.rs | 2 +- src/app/dispatch.rs | 7 +++++++ src/main.rs | 21 +++++++++++-------- 6 files changed, 34 insertions(+), 26 deletions(-) diff --git a/src/app/components/navigation/navigation.rs b/src/app/components/navigation/navigation.rs index e9ecb0a4..879350c5 100644 --- a/src/app/components/navigation/navigation.rs +++ b/src/app/components/navigation/navigation.rs @@ -64,9 +64,9 @@ impl Navigation { self.leaflet.navigate(NavigationDirection::Back); } - fn pop_to_home(&self) { + fn pop_to_home(&mut self) { self.leaflet.navigate(NavigationDirection::Forward); - self.model.go_home(); + self.pop_to(&ScreenName::Home); } fn push_screen(&mut self, name: &ScreenName) { diff --git a/src/app/components/navigation/navigation_model.rs b/src/app/components/navigation/navigation_model.rs index 761327a5..0051787d 100644 --- a/src/app/components/navigation/navigation_model.rs +++ b/src/app/components/navigation/navigation_model.rs @@ -16,11 +16,6 @@ impl NavigationModel { } } - pub fn go_home(&self) { - self.dispatcher - .dispatch(BrowserAction::NavigationPopTo(ScreenName::Home).into()) - } - pub fn visible_child_name(&self) -> impl Deref + '_ { self.app_model.map_state(|s| s.browser.current_screen()) } diff --git a/src/app/components/sidebar/sidebar.rs b/src/app/components/sidebar/sidebar.rs index 01ed8814..78dd2a28 100644 --- a/src/app/components/sidebar/sidebar.rs +++ b/src/app/components/sidebar/sidebar.rs @@ -62,16 +62,19 @@ impl SidebarModel { } fn navigate(&self, dest: SidebarDestination) { - let action = match dest { + let actions = match dest { SidebarDestination::Library | SidebarDestination::SavedTracks | SidebarDestination::NowPlaying | SidebarDestination::SavedPlaylists => { - BrowserAction::SetHomeVisiblePage(dest.id()).into() + vec![BrowserAction::SetHomeVisiblePage(dest.id()).into()] } - SidebarDestination::Playlist(PlaylistSummary { id, .. }) => AppAction::ViewPlaylist(id), + SidebarDestination::Playlist(PlaylistSummary { id, .. }) => vec![ + BrowserAction::SetHomeVisiblePage(SidebarDestination::SavedPlaylists.id()).into(), + AppAction::ViewPlaylist(id), + ], }; - self.dispatcher.dispatch(action); + self.dispatcher.dispatch_many(actions); } } @@ -88,16 +91,16 @@ impl Sidebar { let list_store = gio::ListStore::new(SidebarItem::static_type()); - list_store.append(&SidebarItem::for_destination(SidebarDestination::Library)); - list_store.append(&SidebarItem::for_destination( + list_store.append(&SidebarItem::from_destination(SidebarDestination::Library)); + list_store.append(&SidebarItem::from_destination( SidebarDestination::SavedTracks, )); - list_store.append(&SidebarItem::for_destination( + list_store.append(&SidebarItem::from_destination( SidebarDestination::NowPlaying, )); list_store.append(&SidebarItem::playlists_section()); list_store.append(&SidebarItem::create_playlist_item()); - list_store.append(&SidebarItem::for_destination( + list_store.append(&SidebarItem::from_destination( SidebarDestination::SavedPlaylists, )); @@ -169,7 +172,7 @@ impl Sidebar { .model .get_playlists() .into_iter() - .map(SidebarItem::for_destination) + .map(SidebarItem::from_destination) .collect(); self.list_store.splice( NUM_FIXED_ENTRIES, diff --git a/src/app/components/sidebar/sidebar_item.rs b/src/app/components/sidebar/sidebar_item.rs index e49d1140..7213ae16 100644 --- a/src/app/components/sidebar/sidebar_item.rs +++ b/src/app/components/sidebar/sidebar_item.rs @@ -59,7 +59,7 @@ impl SidebarDestination { } impl SidebarItem { - pub fn for_destination(dest: SidebarDestination) -> Self { + pub fn from_destination(dest: SidebarDestination) -> Self { let (id, data, title) = match dest { SidebarDestination::Playlist(PlaylistSummary { id, title }) => { (PLAYLIST, Some(id), title) diff --git a/src/app/dispatch.rs b/src/app/dispatch.rs index 28c6b81b..c54774b7 100644 --- a/src/app/dispatch.rs +++ b/src/app/dispatch.rs @@ -8,6 +8,7 @@ use super::AppAction; pub trait ActionDispatcher { fn dispatch(&self, action: AppAction); + fn dispatch_many(&self, actions: Vec); fn dispatch_local_async(&self, action: LocalBoxFuture<'static, Option>); fn dispatch_async(&self, action: BoxFuture<'static, Option>); fn dispatch_many_async(&self, actions: BoxFuture<'static, Vec>); @@ -31,6 +32,12 @@ impl ActionDispatcher for ActionDispatcherImpl { self.sender.unbounded_send(action).unwrap(); } + fn dispatch_many(&self, actions: Vec) { + for action in actions.into_iter() { + self.sender.unbounded_send(action).unwrap(); + } + } + fn dispatch_local_async(&self, action: LocalBoxFuture<'static, Option>) { let clone = self.sender.clone(); self.worker.send_local_task(async move { diff --git a/src/main.rs b/src/main.rs index 017fe005..089438ce 100644 --- a/src/main.rs +++ b/src/main.rs @@ -148,16 +148,19 @@ fn register_actions(app: >k::Application, sender: UnboundedSender) AppAction::BrowserAction(BrowserAction::NavigationPush(ScreenName::Search)), sender.clone(), )); - let action = SimpleAction::new("open_playlist", Option::from(glib::VariantTy::STRING)); - action.set_enabled(true); - action.connect_activate(move |_, playlist_id| { - // TODO: clean up unwraps - let id = playlist_id.unwrap().str().unwrap(); - sender - .unbounded_send(AppAction::ViewPlaylist(String::from(id))) - .unwrap(); + + app.add_action(&{ + let action = SimpleAction::new("open_playlist", Some(glib::VariantTy::STRING)); + action.set_enabled(true); + action.connect_activate(move |_, playlist_id| { + if let Some(id) = playlist_id.and_then(|s| s.str()) { + sender + .unbounded_send(AppAction::ViewPlaylist(id.to_owned())) + .unwrap(); + } + }); + action }); - app.add_action(&action); } fn make_action(