refactor: Migrated the handle_table_events macro into a trait for better IDE support, created a TableEventAdapter wrapper for the KeyEventHandlers to make it so that the trait can be used properly and a simple function to replace the previous call to the handle_table_events macro

This commit is contained in:
2025-12-04 16:03:58 -07:00
parent 71240373c0
commit 35dce0bf01
42 changed files with 1425 additions and 756 deletions
@@ -1,4 +1,4 @@
use crate::handlers::table_handler::TableHandlingConfig;
use crate::handlers::table_handler::{TableHandlingConfig, handle_table};
use crate::handlers::{KeyEventHandler, handle_prompt_toggle};
use crate::models::radarr_models::{
AddMovieBody, AddMovieOptions, AddMovieSearchResult, CollectionMovie,
@@ -9,9 +9,7 @@ use crate::models::servarr_data::radarr::radarr_data::{
};
use crate::models::{BlockSelectionState, Scrollable};
use crate::network::radarr_network::RadarrEvent;
use crate::{
App, Key, handle_table_events, handle_text_box_keys, handle_text_box_left_right_keys, matches_key,
};
use crate::{App, Key, handle_text_box_keys, handle_text_box_left_right_keys, matches_key};
#[cfg(test)]
#[path = "add_movie_handler_tests.rs"]
@@ -25,19 +23,6 @@ pub(super) struct AddMovieHandler<'a, 'b> {
}
impl AddMovieHandler<'_, '_> {
handle_table_events!(
self,
add_movie_search_results,
self
.app
.data
.radarr_data
.add_searched_movies
.as_mut()
.expect("add_searched_movies should be initialized"),
AddMovieSearchResult
);
fn build_add_movie_body(&mut self) -> AddMovieBody {
let add_movie_modal = self
.app
@@ -123,7 +108,18 @@ impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for AddMovieHandler<'a,
let add_movie_table_handling_config =
TableHandlingConfig::new(ActiveRadarrBlock::AddMovieSearchResults.into());
if !self.handle_add_movie_search_results_table_events(add_movie_table_handling_config) {
if !handle_table(
self,
|app| {
app
.data
.radarr_data
.add_searched_movies
.as_mut()
.expect("add_searched_movies should be initialized")
},
add_movie_table_handling_config,
) {
self.handle_key_event();
}
}
@@ -557,4 +553,12 @@ impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for AddMovieHandler<'a,
_ => (),
}
}
fn app_mut(&mut self) -> &mut App<'b> {
self.app
}
fn current_route(&self) -> crate::models::Route {
self.app.get_current_route()
}
}
@@ -136,4 +136,12 @@ impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for DeleteMovieHandler<'
self.app.pop_navigation_stack();
}
}
fn app_mut(&mut self) -> &mut App<'b> {
self.app
}
fn current_route(&self) -> crate::models::Route {
self.app.get_current_route()
}
}
@@ -392,4 +392,12 @@ impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for EditMovieHandler<'a,
_ => (),
}
}
fn app_mut(&mut self) -> &mut App<'b> {
self.app
}
fn current_route(&self) -> crate::models::Route {
self.app.get_current_route()
}
}
+16 -5
View File
@@ -7,15 +7,15 @@ use crate::handlers::radarr_handlers::library::edit_movie_handler::EditMovieHand
use crate::handlers::radarr_handlers::library::movie_details_handler::MovieDetailsHandler;
use crate::handlers::{KeyEventHandler, handle_clear_errors, handle_prompt_toggle};
use crate::handlers::table_handler::TableHandlingConfig;
use crate::handlers::table_handler::{TableHandlingConfig, handle_table};
use crate::matches_key;
use crate::models::radarr_models::Movie;
use crate::models::servarr_data::radarr::radarr_data::{
ActiveRadarrBlock, DELETE_MOVIE_SELECTION_BLOCKS, EDIT_MOVIE_SELECTION_BLOCKS, LIBRARY_BLOCKS,
};
use crate::models::stateful_table::SortOption;
use crate::models::{BlockSelectionState, HorizontallyScrollableText};
use crate::models::{BlockSelectionState, HorizontallyScrollableText, Route};
use crate::network::radarr_network::RadarrEvent;
use crate::{handle_table_events, matches_key};
mod add_movie_handler;
mod delete_movie_handler;
@@ -34,7 +34,6 @@ pub(super) struct LibraryHandler<'a, 'b> {
}
impl LibraryHandler<'_, '_> {
handle_table_events!(self, movies, self.app.data.radarr_data.movies, Movie);
fn extract_movie_id(&self) -> i64 {
self.app.data.radarr_data.movies.current_selection().id
}
@@ -52,7 +51,11 @@ impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for LibraryHandler<'a, '
.filter_error_block(ActiveRadarrBlock::FilterMoviesError.into())
.filter_field_fn(|movie| &movie.title.text);
if !self.handle_movies_table_events(movie_table_handling_config) {
if !handle_table(
self,
|app| &mut app.data.radarr_data.movies,
movie_table_handling_config,
) {
match self.active_radarr_block {
_ if AddMovieHandler::accepts(self.active_radarr_block) => {
AddMovieHandler::new(self.key, self.app, self.active_radarr_block, self.context).handle();
@@ -215,6 +218,14 @@ impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for LibraryHandler<'a, '
_ => (),
}
}
fn app_mut(&mut self) -> &mut App<'b> {
self.app
}
fn current_route(&self) -> Route {
self.app.get_current_route()
}
}
fn movies_sorting_options() -> Vec<SortOption<Movie>> {
@@ -2,11 +2,10 @@ use serde_json::Number;
use crate::app::App;
use crate::event::Key;
use crate::handlers::table_handler::TableHandlingConfig;
use crate::handlers::table_handler::{TableHandlingConfig, handle_table};
use crate::handlers::{KeyEventHandler, handle_prompt_toggle};
use crate::models::radarr_models::{
Credit, MovieHistoryItem, RadarrRelease, RadarrReleaseDownloadBody,
};
use crate::matches_key;
use crate::models::radarr_models::{RadarrRelease, RadarrReleaseDownloadBody};
use crate::models::servarr_data::radarr::radarr_data::{
ActiveRadarrBlock, EDIT_MOVIE_SELECTION_BLOCKS, MOVIE_DETAILS_BLOCKS,
};
@@ -14,7 +13,6 @@ use crate::models::servarr_models::Language;
use crate::models::stateful_table::SortOption;
use crate::models::{BlockSelectionState, Scrollable};
use crate::network::radarr_network::RadarrEvent;
use crate::{handle_table_events, matches_key};
#[cfg(test)]
#[path = "movie_details_handler_tests.rs"]
@@ -28,59 +26,6 @@ pub(super) struct MovieDetailsHandler<'a, 'b> {
}
impl MovieDetailsHandler<'_, '_> {
handle_table_events!(
self,
movie_releases,
self
.app
.data
.radarr_data
.movie_details_modal
.as_mut()
.unwrap()
.movie_releases,
RadarrRelease
);
handle_table_events!(
self,
movie_history,
self
.app
.data
.radarr_data
.movie_details_modal
.as_mut()
.unwrap()
.movie_history,
MovieHistoryItem
);
handle_table_events!(
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
);
fn build_radarr_release_download_body(&self) -> RadarrReleaseDownloadBody {
let movie_id = self.app.data.radarr_data.movies.current_selection().id;
let (guid, indexer_id) = {
@@ -122,11 +67,55 @@ impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for MovieDetailsHandler<
let movie_cast_table_handling_config = TableHandlingConfig::new(ActiveRadarrBlock::Cast.into());
let movie_crew_table_handling_config = TableHandlingConfig::new(ActiveRadarrBlock::Crew.into());
if !self.handle_movie_history_table_events(movie_history_table_handling_config)
&& !self.handle_movie_releases_table_events(movie_releases_table_handling_config)
&& !self.handle_movie_cast_table_events(movie_cast_table_handling_config)
&& !self.handle_movie_crew_table_events(movie_crew_table_handling_config)
{
if !handle_table(
self,
|app| {
&mut app
.data
.radarr_data
.movie_details_modal
.as_mut()
.unwrap()
.movie_history
},
movie_history_table_handling_config,
) && !handle_table(
self,
|app| {
&mut app
.data
.radarr_data
.movie_details_modal
.as_mut()
.unwrap()
.movie_releases
},
movie_releases_table_handling_config,
) && !handle_table(
self,
|app| {
&mut app
.data
.radarr_data
.movie_details_modal
.as_mut()
.unwrap()
.movie_cast
},
movie_cast_table_handling_config,
) && !handle_table(
self,
|app| {
&mut app
.data
.radarr_data
.movie_details_modal
.as_mut()
.unwrap()
.movie_crew
},
movie_crew_table_handling_config,
) {
self.handle_key_event();
}
}
@@ -385,6 +374,14 @@ impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for MovieDetailsHandler<
_ => (),
}
}
fn app_mut(&mut self) -> &mut App<'b> {
self.app
}
fn current_route(&self) -> crate::models::Route {
self.app.get_current_route()
}
}
fn releases_sorting_options() -> Vec<SortOption<RadarrRelease>> {