refactor(collection_details_handler): use the new handle_table_events macro

This commit is contained in:
2024-12-08 14:22:59 -07:00
parent 87a652d911
commit 048877bbb6
9 changed files with 129 additions and 78 deletions
@@ -1,7 +1,10 @@
use crate::app::key_binding::DEFAULT_KEYBINDINGS; use crate::app::key_binding::DEFAULT_KEYBINDINGS;
use crate::app::App; use crate::app::App;
use crate::event::Key; use crate::event::Key;
use crate::handle_table_events;
use crate::handlers::table_handler::TableHandlingProps;
use crate::handlers::KeyEventHandler; use crate::handlers::KeyEventHandler;
use crate::models::radarr_models::CollectionMovie;
use crate::models::servarr_data::radarr::radarr_data::{ use crate::models::servarr_data::radarr::radarr_data::{
ActiveRadarrBlock, ADD_MOVIE_SELECTION_BLOCKS, COLLECTION_DETAILS_BLOCKS, ActiveRadarrBlock, ADD_MOVIE_SELECTION_BLOCKS, COLLECTION_DETAILS_BLOCKS,
EDIT_COLLECTION_SELECTION_BLOCKS, EDIT_COLLECTION_SELECTION_BLOCKS,
@@ -20,7 +23,25 @@ pub(super) struct CollectionDetailsHandler<'a, 'b> {
_context: Option<ActiveRadarrBlock>, _context: Option<ActiveRadarrBlock>,
} }
impl<'a, 'b> CollectionDetailsHandler<'a, 'b> {
handle_table_events!(
self,
collection_movies,
self.app.data.radarr_data.collection_movies,
CollectionMovie
);
}
impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for CollectionDetailsHandler<'a, 'b> { impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for CollectionDetailsHandler<'a, 'b> {
fn handle(&mut self) {
let collection_movies_table_handling_props =
TableHandlingProps::new(ActiveRadarrBlock::CollectionDetails.into());
if !self.handle_collection_movies_table_events(collection_movies_table_handling_props) {
self.handle_key_event();
}
}
fn accepts(active_block: ActiveRadarrBlock) -> bool { fn accepts(active_block: ActiveRadarrBlock) -> bool {
COLLECTION_DETAILS_BLOCKS.contains(&active_block) COLLECTION_DETAILS_BLOCKS.contains(&active_block)
} }
+20 -13
View File
@@ -1,6 +1,7 @@
use crate::app::key_binding::DEFAULT_KEYBINDINGS; use crate::app::key_binding::DEFAULT_KEYBINDINGS;
use crate::app::App; use crate::app::App;
use crate::event::Key; use crate::event::Key;
use crate::handle_table_events;
use crate::handlers::radarr_handlers::collections::collection_details_handler::CollectionDetailsHandler; use crate::handlers::radarr_handlers::collections::collection_details_handler::CollectionDetailsHandler;
use crate::handlers::radarr_handlers::collections::edit_collection_handler::EditCollectionHandler; use crate::handlers::radarr_handlers::collections::edit_collection_handler::EditCollectionHandler;
use crate::handlers::radarr_handlers::handle_change_tab_left_right_keys; use crate::handlers::radarr_handlers::handle_change_tab_left_right_keys;
@@ -11,9 +12,8 @@ use crate::models::servarr_data::radarr::radarr_data::{
ActiveRadarrBlock, COLLECTIONS_BLOCKS, EDIT_COLLECTION_SELECTION_BLOCKS, ActiveRadarrBlock, COLLECTIONS_BLOCKS, EDIT_COLLECTION_SELECTION_BLOCKS,
}; };
use crate::models::stateful_table::SortOption; use crate::models::stateful_table::SortOption;
use crate::models::{BlockSelectionState, HorizontallyScrollableText, Scrollable}; use crate::models::{BlockSelectionState, Scrollable};
use crate::network::radarr_network::RadarrEvent; use crate::network::radarr_network::RadarrEvent;
use crate::handle_table_events;
mod collection_details_handler; mod collection_details_handler;
mod edit_collection_handler; mod edit_collection_handler;
@@ -30,12 +30,18 @@ pub(super) struct CollectionsHandler<'a, 'b> {
} }
impl<'a, 'b> CollectionsHandler<'a, 'b> { impl<'a, 'b> CollectionsHandler<'a, 'b> {
handle_table_events!(self, collections, self.app.data.radarr_data.collections, Collection); handle_table_events!(
self,
collections,
self.app.data.radarr_data.collections,
Collection
);
} }
impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for CollectionsHandler<'a, 'b> { impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for CollectionsHandler<'a, 'b> {
fn handle(&mut self) { fn handle(&mut self) {
let collections_table_handling_props = TableHandlingProps::new(ActiveRadarrBlock::Collections.into()) let collections_table_handling_props =
TableHandlingProps::new(ActiveRadarrBlock::Collections.into())
.sorting_block(ActiveRadarrBlock::CollectionsSortPrompt.into()) .sorting_block(ActiveRadarrBlock::CollectionsSortPrompt.into())
.sort_by_fn(|a: &Collection, b: &Collection| a.id.cmp(&b.id)) .sort_by_fn(|a: &Collection, b: &Collection| a.id.cmp(&b.id))
.sort_options(collections_sorting_options()) .sort_options(collections_sorting_options())
@@ -49,7 +55,12 @@ impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for CollectionsHandler<'
if !self.handle_collections_table_events(collections_table_handling_props) { if !self.handle_collections_table_events(collections_table_handling_props) {
match self.active_radarr_block { match self.active_radarr_block {
_ if CollectionDetailsHandler::accepts(self.active_radarr_block) => { _ if CollectionDetailsHandler::accepts(self.active_radarr_block) => {
CollectionDetailsHandler::with(self.key, self.app, self.active_radarr_block, self.context) CollectionDetailsHandler::with(
self.key,
self.app,
self.active_radarr_block,
self.context,
)
.handle(); .handle();
} }
_ if EditCollectionHandler::accepts(self.active_radarr_block) => { _ if EditCollectionHandler::accepts(self.active_radarr_block) => {
@@ -89,17 +100,13 @@ impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for CollectionsHandler<'
!self.app.is_loading && !self.app.data.radarr_data.collections.is_empty() !self.app.is_loading && !self.app.data.radarr_data.collections.is_empty()
} }
fn handle_scroll_up(&mut self) { fn handle_scroll_up(&mut self) {}
}
fn handle_scroll_down(&mut self) { fn handle_scroll_down(&mut self) {}
}
fn handle_home(&mut self) { fn handle_home(&mut self) {}
}
fn handle_end(&mut self) { fn handle_end(&mut self) {}
}
fn handle_delete(&mut self) {} fn handle_delete(&mut self) {}
@@ -1,4 +1,3 @@
use crate::models::HorizontallyScrollableText;
use serde_json::Number; use serde_json::Number;
use crate::app::key_binding::DEFAULT_KEYBINDINGS; use crate::app::key_binding::DEFAULT_KEYBINDINGS;
@@ -54,8 +53,32 @@ impl<'a, 'b> MovieDetailsHandler<'a, 'b> {
.movie_history, .movie_history,
MovieHistoryItem MovieHistoryItem
); );
handle_table_events!(self, movie_cast, self.app.data.radarr_data.movie_details_modal.as_mut().unwrap().movie_cast, Credit); handle_table_events!(
handle_table_events!(self, movie_crew, self.app.data.radarr_data.movie_details_modal.as_mut().unwrap().movie_crew, Credit); self,
movie_cast,
self
.app
.data
.radarr_data
.movie_details_modal
.as_mut()
.unwrap()
.movie_cast,
Credit
);
handle_table_events!(
self,
movie_crew,
self
.app
.data
.radarr_data
.movie_details_modal
.as_mut()
.unwrap()
.movie_crew,
Credit
);
} }
impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for MovieDetailsHandler<'a, 'b> { impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for MovieDetailsHandler<'a, 'b> {
@@ -66,8 +89,7 @@ impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for MovieDetailsHandler<
TableHandlingProps::new(ActiveRadarrBlock::ManualSearch.into()) TableHandlingProps::new(ActiveRadarrBlock::ManualSearch.into())
.sorting_block(ActiveRadarrBlock::ManualSearchSortPrompt.into()) .sorting_block(ActiveRadarrBlock::ManualSearchSortPrompt.into())
.sort_options(releases_sorting_options()); .sort_options(releases_sorting_options());
let movie_cast_table_handling_props = let movie_cast_table_handling_props = TableHandlingProps::new(ActiveRadarrBlock::Cast.into());
TableHandlingProps::new(ActiveRadarrBlock::Cast.into());
let movie_crew_table_handling_props = TableHandlingProps::new(ActiveRadarrBlock::Crew.into()); let movie_crew_table_handling_props = TableHandlingProps::new(ActiveRadarrBlock::Crew.into());
if !self.handle_movie_history_table_events(movie_history_table_handling_props) if !self.handle_movie_history_table_events(movie_history_table_handling_props)
@@ -127,8 +149,8 @@ impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for MovieDetailsHandler<
} }
fn handle_scroll_up(&mut self) { fn handle_scroll_up(&mut self) {
match self.active_radarr_block { if self.active_radarr_block == ActiveRadarrBlock::MovieDetails {
ActiveRadarrBlock::MovieDetails => self self
.app .app
.data .data
.radarr_data .radarr_data
@@ -136,14 +158,13 @@ impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for MovieDetailsHandler<
.as_mut() .as_mut()
.unwrap() .unwrap()
.movie_details .movie_details
.scroll_up(), .scroll_up()
_ => (),
} }
} }
fn handle_scroll_down(&mut self) { fn handle_scroll_down(&mut self) {
match self.active_radarr_block { if self.active_radarr_block == ActiveRadarrBlock::MovieDetails {
ActiveRadarrBlock::MovieDetails => self self
.app .app
.data .data
.radarr_data .radarr_data
@@ -151,14 +172,13 @@ impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for MovieDetailsHandler<
.as_mut() .as_mut()
.unwrap() .unwrap()
.movie_details .movie_details
.scroll_down(), .scroll_down()
_ => (),
} }
} }
fn handle_home(&mut self) { fn handle_home(&mut self) {
match self.active_radarr_block { if self.active_radarr_block == ActiveRadarrBlock::MovieDetails {
ActiveRadarrBlock::MovieDetails => self self
.app .app
.data .data
.radarr_data .radarr_data
@@ -166,14 +186,13 @@ impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for MovieDetailsHandler<
.as_mut() .as_mut()
.unwrap() .unwrap()
.movie_details .movie_details
.scroll_to_top(), .scroll_to_top()
_ => (),
} }
} }
fn handle_end(&mut self) { fn handle_end(&mut self) {
match self.active_radarr_block { if let ActiveRadarrBlock::MovieDetails = self.active_radarr_block {
ActiveRadarrBlock::MovieDetails => self self
.app .app
.data .data
.radarr_data .radarr_data
@@ -181,8 +200,7 @@ impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for MovieDetailsHandler<
.as_mut() .as_mut()
.unwrap() .unwrap()
.movie_details .movie_details
.scroll_to_bottom(), .scroll_to_bottom()
_ => (),
} }
} }
+10 -4
View File
@@ -3,12 +3,12 @@ use crate::app::App;
use crate::event::Key; use crate::event::Key;
use crate::handle_table_events; use crate::handle_table_events;
use crate::handlers::sonarr_handlers::handle_change_tab_left_right_keys; use crate::handlers::sonarr_handlers::handle_change_tab_left_right_keys;
use crate::handlers::{handle_clear_errors, handle_prompt_toggle, KeyEventHandler};
use crate::handlers::table_handler::TableHandlingProps; use crate::handlers::table_handler::TableHandlingProps;
use crate::handlers::{handle_clear_errors, handle_prompt_toggle, KeyEventHandler};
use crate::models::servarr_data::sonarr::sonarr_data::{ActiveSonarrBlock, BLOCKLIST_BLOCKS}; use crate::models::servarr_data::sonarr::sonarr_data::{ActiveSonarrBlock, BLOCKLIST_BLOCKS};
use crate::models::sonarr_models::BlocklistItem; use crate::models::sonarr_models::BlocklistItem;
use crate::models::stateful_table::SortOption; use crate::models::stateful_table::SortOption;
use crate::models::{Scrollable, HorizontallyScrollableText}; use crate::models::Scrollable;
use crate::network::sonarr_network::SonarrEvent; use crate::network::sonarr_network::SonarrEvent;
#[cfg(test)] #[cfg(test)]
@@ -23,12 +23,18 @@ pub(super) struct BlocklistHandler<'a, 'b> {
} }
impl<'a, 'b> BlocklistHandler<'a, 'b> { impl<'a, 'b> BlocklistHandler<'a, 'b> {
handle_table_events!(self, blocklist, self.app.data.sonarr_data.blocklist, BlocklistItem); handle_table_events!(
self,
blocklist,
self.app.data.sonarr_data.blocklist,
BlocklistItem
);
} }
impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveSonarrBlock> for BlocklistHandler<'a, 'b> { impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveSonarrBlock> for BlocklistHandler<'a, 'b> {
fn handle(&mut self) { fn handle(&mut self) {
let blocklist_table_handling_props = TableHandlingProps::new(ActiveSonarrBlock::Blocklist.into()) let blocklist_table_handling_props =
TableHandlingProps::new(ActiveSonarrBlock::Blocklist.into())
.sorting_block(ActiveSonarrBlock::BlocklistSortPrompt.into()) .sorting_block(ActiveSonarrBlock::BlocklistSortPrompt.into())
.sort_by_fn(|a: &BlocklistItem, b: &BlocklistItem| a.id.cmp(&b.id)) .sort_by_fn(|a: &BlocklistItem, b: &BlocklistItem| a.id.cmp(&b.id))
.sort_options(blocklist_sorting_options()); .sort_options(blocklist_sorting_options());
+14 -13
View File
@@ -1,14 +1,13 @@
use crate::models::HorizontallyScrollableText;
use crate::app::key_binding::DEFAULT_KEYBINDINGS; use crate::app::key_binding::DEFAULT_KEYBINDINGS;
use crate::app::App; use crate::app::App;
use crate::event::Key; use crate::event::Key;
use crate::handle_table_events; use crate::handle_table_events;
use crate::handlers::sonarr_handlers::handle_change_tab_left_right_keys; use crate::handlers::sonarr_handlers::handle_change_tab_left_right_keys;
use crate::handlers::{handle_clear_errors, handle_prompt_toggle, KeyEventHandler};
use crate::handlers::table_handler::TableHandlingProps; use crate::handlers::table_handler::TableHandlingProps;
use crate::handlers::{handle_clear_errors, handle_prompt_toggle, KeyEventHandler};
use crate::models::servarr_data::sonarr::sonarr_data::{ActiveSonarrBlock, DOWNLOADS_BLOCKS}; use crate::models::servarr_data::sonarr::sonarr_data::{ActiveSonarrBlock, DOWNLOADS_BLOCKS};
use crate::models::Scrollable;
use crate::models::sonarr_models::DownloadRecord; use crate::models::sonarr_models::DownloadRecord;
use crate::models::Scrollable;
use crate::network::sonarr_network::SonarrEvent; use crate::network::sonarr_network::SonarrEvent;
#[cfg(test)] #[cfg(test)]
@@ -23,12 +22,18 @@ pub(super) struct DownloadsHandler<'a, 'b> {
} }
impl<'a, 'b> DownloadsHandler<'a, 'b> { impl<'a, 'b> DownloadsHandler<'a, 'b> {
handle_table_events!(self, downloads, self.app.data.sonarr_data.downloads, DownloadRecord); handle_table_events!(
self,
downloads,
self.app.data.sonarr_data.downloads,
DownloadRecord
);
} }
impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveSonarrBlock> for DownloadsHandler<'a, 'b> { impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveSonarrBlock> for DownloadsHandler<'a, 'b> {
fn handle(&mut self) { fn handle(&mut self) {
let download_table_handling_props = TableHandlingProps::new(ActiveSonarrBlock::Downloads.into()); let download_table_handling_props =
TableHandlingProps::new(ActiveSonarrBlock::Downloads.into());
if !self.handle_downloads_table_events(download_table_handling_props) { if !self.handle_downloads_table_events(download_table_handling_props) {
self.handle_key_event(); self.handle_key_event();
@@ -61,17 +66,13 @@ impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveSonarrBlock> for DownloadsHandler<'a,
!self.app.is_loading && !self.app.data.sonarr_data.downloads.is_empty() !self.app.is_loading && !self.app.data.sonarr_data.downloads.is_empty()
} }
fn handle_scroll_up(&mut self) { fn handle_scroll_up(&mut self) {}
}
fn handle_scroll_down(&mut self) { fn handle_scroll_down(&mut self) {}
}
fn handle_home(&mut self) { fn handle_home(&mut self) {}
}
fn handle_end(&mut self) { fn handle_end(&mut self) {}
}
fn handle_delete(&mut self) { fn handle_delete(&mut self) {
if self.active_sonarr_block == ActiveSonarrBlock::Downloads { if self.active_sonarr_block == ActiveSonarrBlock::Downloads {
+4 -5
View File
@@ -1,14 +1,14 @@
use crate::app::key_binding::DEFAULT_KEYBINDINGS; use crate::app::key_binding::DEFAULT_KEYBINDINGS;
use crate::app::App; use crate::app::App;
use crate::event::Key; use crate::event::Key;
use crate::handle_table_events;
use crate::handlers::sonarr_handlers::handle_change_tab_left_right_keys; use crate::handlers::sonarr_handlers::handle_change_tab_left_right_keys;
use crate::handlers::table_handler::TableHandlingProps; use crate::handlers::table_handler::TableHandlingProps;
use crate::handlers::{handle_clear_errors, KeyEventHandler}; use crate::handlers::{handle_clear_errors, KeyEventHandler};
use crate::models::servarr_data::sonarr::sonarr_data::{ActiveSonarrBlock, HISTORY_BLOCKS}; use crate::models::servarr_data::sonarr::sonarr_data::{ActiveSonarrBlock, HISTORY_BLOCKS};
use crate::models::sonarr_models::SonarrHistoryItem; use crate::models::sonarr_models::SonarrHistoryItem;
use crate::models::stateful_table::SortOption; use crate::models::stateful_table::SortOption;
use crate::models::{HorizontallyScrollableText, Scrollable}; use crate::models::Scrollable;
use crate::handle_table_events;
#[cfg(test)] #[cfg(test)]
#[path = "history_handler_tests.rs"] #[path = "history_handler_tests.rs"]
@@ -85,9 +85,8 @@ impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveSonarrBlock> for HistoryHandler<'a, '
fn handle_delete(&mut self) {} fn handle_delete(&mut self) {}
fn handle_left_right_action(&mut self) { fn handle_left_right_action(&mut self) {
match self.active_sonarr_block { if self.active_sonarr_block == ActiveSonarrBlock::History {
ActiveSonarrBlock::History => handle_change_tab_left_right_keys(self.app, self.key), handle_change_tab_left_right_keys(self.app, self.key)
_ => {}
} }
} }
@@ -14,7 +14,6 @@ use crate::models::servarr_data::sonarr::sonarr_data::{
}; };
use crate::models::servarr_models::Indexer; use crate::models::servarr_models::Indexer;
use crate::models::BlockSelectionState; use crate::models::BlockSelectionState;
use crate::models::HorizontallyScrollableText;
use crate::models::Scrollable; use crate::models::Scrollable;
use crate::network::sonarr_network::SonarrEvent; use crate::network::sonarr_network::SonarrEvent;
@@ -9,7 +9,7 @@ use crate::models::servarr_data::sonarr::sonarr_data::{
ActiveSonarrBlock, EDIT_SERIES_SELECTION_BLOCKS, SERIES_DETAILS_BLOCKS, ActiveSonarrBlock, EDIT_SERIES_SELECTION_BLOCKS, SERIES_DETAILS_BLOCKS,
}; };
use crate::models::sonarr_models::{Season, SonarrHistoryItem}; use crate::models::sonarr_models::{Season, SonarrHistoryItem};
use crate::models::{BlockSelectionState, HorizontallyScrollableText, Scrollable}; use crate::models::{BlockSelectionState, Scrollable};
use crate::network::sonarr_network::SonarrEvent; use crate::network::sonarr_network::SonarrEvent;
#[cfg(test)] #[cfg(test)]
+2 -2
View File
@@ -319,7 +319,7 @@ macro_rules! handle_table_events {
.app .app
.push_navigation_stack(props.filtering_block.expect("Filtering block is undefined").into()); .push_navigation_stack(props.filtering_block.expect("Filtering block is undefined").into());
$table.reset_filter(); $table.reset_filter();
$table.filter = Some(HorizontallyScrollableText::default()); $table.filter = Some($crate::models::HorizontallyScrollableText::default());
$self.app.should_ignore_quit_key = true; $self.app.should_ignore_quit_key = true;
true true
@@ -333,7 +333,7 @@ macro_rules! handle_table_events {
$self $self
.app .app
.push_navigation_stack(props.searching_block.expect("Searching block is undefined")); .push_navigation_stack(props.searching_block.expect("Searching block is undefined"));
$table.search = Some(HorizontallyScrollableText::default()); $table.search = Some($crate::models::HorizontallyScrollableText::default());
$self.app.should_ignore_quit_key = true; $self.app.should_ignore_quit_key = true;
true true