refactor(collections_handler): Use the new handle_table_events macro
This commit is contained in:
@@ -218,6 +218,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_collection_search_box_home_end_keys() {
|
||||
let mut app = App::default();
|
||||
app.push_navigation_stack(ActiveRadarrBlock::SearchCollection.into());
|
||||
app
|
||||
.data
|
||||
.radarr_data
|
||||
@@ -271,6 +272,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_collection_filter_box_home_end_keys() {
|
||||
let mut app = App::default();
|
||||
app.push_navigation_stack(ActiveRadarrBlock::FilterCollections.into());
|
||||
app
|
||||
.data
|
||||
.radarr_data
|
||||
@@ -447,6 +449,8 @@ mod tests {
|
||||
#[test]
|
||||
fn test_collection_search_box_left_right_keys() {
|
||||
let mut app = App::default();
|
||||
app.data.radarr_data.collections.set_items(vec![Collection::default()]);
|
||||
app.push_navigation_stack(ActiveRadarrBlock::SearchCollection.into());
|
||||
app.data.radarr_data.collections.search = Some("Test".into());
|
||||
|
||||
CollectionsHandler::with(
|
||||
@@ -495,6 +499,8 @@ mod tests {
|
||||
#[test]
|
||||
fn test_collection_filter_box_left_right_keys() {
|
||||
let mut app = App::default();
|
||||
app.data.radarr_data.collections.set_items(vec![Collection::default()]);
|
||||
app.push_navigation_stack(ActiveRadarrBlock::FilterCollections.into());
|
||||
app.data.radarr_data.collections.filter = Some("Test".into());
|
||||
|
||||
CollectionsHandler::with(
|
||||
@@ -932,6 +938,7 @@ mod tests {
|
||||
filtered_state: Some(TableState::default()),
|
||||
..StatefulTable::default()
|
||||
};
|
||||
app.data.radarr_data.collections.set_items(vec![Collection::default()]);
|
||||
|
||||
CollectionsHandler::with(ESC_KEY, &mut app, active_radarr_block, None).handle();
|
||||
|
||||
@@ -970,6 +977,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_collections_sort_prompt_block_esc() {
|
||||
let mut app = App::default();
|
||||
app.data.radarr_data.collections.set_items(vec![Collection::default()]);
|
||||
app.push_navigation_stack(ActiveRadarrBlock::Collections.into());
|
||||
app.push_navigation_stack(ActiveRadarrBlock::CollectionsSortPrompt.into());
|
||||
|
||||
@@ -995,13 +1003,6 @@ mod tests {
|
||||
app.push_navigation_stack(ActiveRadarrBlock::Collections.into());
|
||||
app.push_navigation_stack(ActiveRadarrBlock::Collections.into());
|
||||
app.data.radarr_data = create_test_radarr_data();
|
||||
app.data.radarr_data.collections = StatefulTable {
|
||||
search: Some("Test".into()),
|
||||
filter: Some("Test".into()),
|
||||
filtered_items: Some(Vec::new()),
|
||||
filtered_state: Some(TableState::default()),
|
||||
..StatefulTable::default()
|
||||
};
|
||||
|
||||
CollectionsHandler::with(ESC_KEY, &mut app, ActiveRadarrBlock::Collections, None).handle();
|
||||
|
||||
@@ -1010,10 +1011,6 @@ mod tests {
|
||||
ActiveRadarrBlock::Collections.into()
|
||||
);
|
||||
assert!(app.error.text.is_empty());
|
||||
assert_eq!(app.data.radarr_data.collections.search, None);
|
||||
assert_eq!(app.data.radarr_data.collections.filter, None);
|
||||
assert_eq!(app.data.radarr_data.collections.filtered_items, None);
|
||||
assert_eq!(app.data.radarr_data.collections.filtered_state, None);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1035,6 +1032,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_search_collections_key() {
|
||||
let mut app = App::default();
|
||||
app.push_navigation_stack(ActiveRadarrBlock::Collections.into());
|
||||
app
|
||||
.data
|
||||
.radarr_data
|
||||
@@ -1090,6 +1088,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_filter_collections_key() {
|
||||
let mut app = App::default();
|
||||
app.push_navigation_stack(ActiveRadarrBlock::Collections.into());
|
||||
app
|
||||
.data
|
||||
.radarr_data
|
||||
@@ -1315,6 +1314,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_search_collections_box_backspace_key() {
|
||||
let mut app = App::default();
|
||||
app.push_navigation_stack(ActiveRadarrBlock::SearchCollection.into());
|
||||
app
|
||||
.data
|
||||
.radarr_data
|
||||
@@ -1346,6 +1346,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_filter_collections_box_backspace_key() {
|
||||
let mut app = App::default();
|
||||
app.push_navigation_stack(ActiveRadarrBlock::FilterCollections.into());
|
||||
app
|
||||
.data
|
||||
.radarr_data
|
||||
@@ -1377,6 +1378,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_search_collections_box_char_key() {
|
||||
let mut app = App::default();
|
||||
app.push_navigation_stack(ActiveRadarrBlock::SearchCollection.into());
|
||||
app
|
||||
.data
|
||||
.radarr_data
|
||||
@@ -1408,6 +1410,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_filter_collections_box_char_key() {
|
||||
let mut app = App::default();
|
||||
app.push_navigation_stack(ActiveRadarrBlock::FilterCollections.into());
|
||||
app
|
||||
.data
|
||||
.radarr_data
|
||||
@@ -1439,6 +1442,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_sort_key() {
|
||||
let mut app = App::default();
|
||||
app.push_navigation_stack(ActiveRadarrBlock::Collections.into());
|
||||
app
|
||||
.data
|
||||
.radarr_data
|
||||
|
||||
@@ -4,6 +4,7 @@ use crate::event::Key;
|
||||
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::handle_change_tab_left_right_keys;
|
||||
use crate::handlers::table_handler::TableHandlingProps;
|
||||
use crate::handlers::{handle_clear_errors, handle_prompt_toggle, KeyEventHandler};
|
||||
use crate::models::radarr_models::Collection;
|
||||
use crate::models::servarr_data::radarr::radarr_data::{
|
||||
@@ -12,7 +13,7 @@ use crate::models::servarr_data::radarr::radarr_data::{
|
||||
use crate::models::stateful_table::SortOption;
|
||||
use crate::models::{BlockSelectionState, HorizontallyScrollableText, Scrollable};
|
||||
use crate::network::radarr_network::RadarrEvent;
|
||||
use crate::{handle_text_box_keys, handle_text_box_left_right_keys};
|
||||
use crate::handle_table_events;
|
||||
|
||||
mod collection_details_handler;
|
||||
mod edit_collection_handler;
|
||||
@@ -28,18 +29,35 @@ pub(super) struct CollectionsHandler<'a, 'b> {
|
||||
context: Option<ActiveRadarrBlock>,
|
||||
}
|
||||
|
||||
impl<'a, 'b> CollectionsHandler<'a, 'b> {
|
||||
handle_table_events!(self, collections, self.app.data.radarr_data.collections, Collection);
|
||||
}
|
||||
|
||||
impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for CollectionsHandler<'a, 'b> {
|
||||
fn handle(&mut self) {
|
||||
match self.active_radarr_block {
|
||||
_ if CollectionDetailsHandler::accepts(self.active_radarr_block) => {
|
||||
CollectionDetailsHandler::with(self.key, self.app, self.active_radarr_block, self.context)
|
||||
.handle();
|
||||
let collections_table_handling_props = TableHandlingProps::new(ActiveRadarrBlock::Collections.into())
|
||||
.sorting_block(ActiveRadarrBlock::CollectionsSortPrompt.into())
|
||||
.sort_by_fn(|a: &Collection, b: &Collection| a.id.cmp(&b.id))
|
||||
.sort_options(collections_sorting_options())
|
||||
.searching_block(ActiveRadarrBlock::SearchCollection.into())
|
||||
.search_error_block(ActiveRadarrBlock::SearchCollectionError.into())
|
||||
.search_field_fn(|collection| &collection.title.text)
|
||||
.filtering_block(ActiveRadarrBlock::FilterCollections.into())
|
||||
.filter_error_block(ActiveRadarrBlock::FilterCollectionsError.into())
|
||||
.filter_field_fn(|collection| &collection.title.text);
|
||||
|
||||
if !self.handle_collections_table_events(collections_table_handling_props) {
|
||||
match self.active_radarr_block {
|
||||
_ if CollectionDetailsHandler::accepts(self.active_radarr_block) => {
|
||||
CollectionDetailsHandler::with(self.key, self.app, self.active_radarr_block, self.context)
|
||||
.handle();
|
||||
}
|
||||
_ if EditCollectionHandler::accepts(self.active_radarr_block) => {
|
||||
EditCollectionHandler::with(self.key, self.app, self.active_radarr_block, self.context)
|
||||
.handle();
|
||||
}
|
||||
_ => self.handle_key_event(),
|
||||
}
|
||||
_ if EditCollectionHandler::accepts(self.active_radarr_block) => {
|
||||
EditCollectionHandler::with(self.key, self.app, self.active_radarr_block, self.context)
|
||||
.handle();
|
||||
}
|
||||
_ => self.handle_key_event(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,103 +90,15 @@ impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for CollectionsHandler<'
|
||||
}
|
||||
|
||||
fn handle_scroll_up(&mut self) {
|
||||
match self.active_radarr_block {
|
||||
ActiveRadarrBlock::Collections => self.app.data.radarr_data.collections.scroll_up(),
|
||||
ActiveRadarrBlock::CollectionsSortPrompt => self
|
||||
.app
|
||||
.data
|
||||
.radarr_data
|
||||
.collections
|
||||
.sort
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.scroll_up(),
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_scroll_down(&mut self) {
|
||||
match self.active_radarr_block {
|
||||
ActiveRadarrBlock::Collections => self.app.data.radarr_data.collections.scroll_down(),
|
||||
ActiveRadarrBlock::CollectionsSortPrompt => self
|
||||
.app
|
||||
.data
|
||||
.radarr_data
|
||||
.collections
|
||||
.sort
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.scroll_down(),
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_home(&mut self) {
|
||||
match self.active_radarr_block {
|
||||
ActiveRadarrBlock::Collections => self.app.data.radarr_data.collections.scroll_to_top(),
|
||||
ActiveRadarrBlock::SearchCollection => self
|
||||
.app
|
||||
.data
|
||||
.radarr_data
|
||||
.collections
|
||||
.search
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.scroll_home(),
|
||||
ActiveRadarrBlock::FilterCollections => self
|
||||
.app
|
||||
.data
|
||||
.radarr_data
|
||||
.collections
|
||||
.filter
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.scroll_home(),
|
||||
ActiveRadarrBlock::CollectionsSortPrompt => self
|
||||
.app
|
||||
.data
|
||||
.radarr_data
|
||||
.collections
|
||||
.sort
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.scroll_to_top(),
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_end(&mut self) {
|
||||
match self.active_radarr_block {
|
||||
ActiveRadarrBlock::Collections => self.app.data.radarr_data.collections.scroll_to_bottom(),
|
||||
ActiveRadarrBlock::SearchCollection => self
|
||||
.app
|
||||
.data
|
||||
.radarr_data
|
||||
.collections
|
||||
.search
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.reset_offset(),
|
||||
ActiveRadarrBlock::FilterCollections => self
|
||||
.app
|
||||
.data
|
||||
.radarr_data
|
||||
.collections
|
||||
.filter
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.reset_offset(),
|
||||
ActiveRadarrBlock::CollectionsSortPrompt => self
|
||||
.app
|
||||
.data
|
||||
.radarr_data
|
||||
.collections
|
||||
.sort
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.scroll_to_bottom(),
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_delete(&mut self) {}
|
||||
@@ -177,34 +107,6 @@ impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for CollectionsHandler<'
|
||||
match self.active_radarr_block {
|
||||
ActiveRadarrBlock::Collections => handle_change_tab_left_right_keys(self.app, self.key),
|
||||
ActiveRadarrBlock::UpdateAllCollectionsPrompt => handle_prompt_toggle(self.app, self.key),
|
||||
ActiveRadarrBlock::SearchCollection => {
|
||||
handle_text_box_left_right_keys!(
|
||||
self,
|
||||
self.key,
|
||||
self
|
||||
.app
|
||||
.data
|
||||
.radarr_data
|
||||
.collections
|
||||
.search
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
)
|
||||
}
|
||||
ActiveRadarrBlock::FilterCollections => {
|
||||
handle_text_box_left_right_keys!(
|
||||
self,
|
||||
self.key,
|
||||
self
|
||||
.app
|
||||
.data
|
||||
.radarr_data
|
||||
.collections
|
||||
.filter
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
)
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
@@ -214,44 +116,6 @@ impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for CollectionsHandler<'
|
||||
ActiveRadarrBlock::Collections => self
|
||||
.app
|
||||
.push_navigation_stack(ActiveRadarrBlock::CollectionDetails.into()),
|
||||
ActiveRadarrBlock::SearchCollection => {
|
||||
self.app.pop_navigation_stack();
|
||||
self.app.should_ignore_quit_key = false;
|
||||
|
||||
if self.app.data.radarr_data.collections.search.is_some() {
|
||||
let has_match = self
|
||||
.app
|
||||
.data
|
||||
.radarr_data
|
||||
.collections
|
||||
.apply_search(|collection| &collection.title.text);
|
||||
|
||||
if !has_match {
|
||||
self
|
||||
.app
|
||||
.push_navigation_stack(ActiveRadarrBlock::SearchCollectionError.into());
|
||||
}
|
||||
}
|
||||
}
|
||||
ActiveRadarrBlock::FilterCollections => {
|
||||
self.app.pop_navigation_stack();
|
||||
self.app.should_ignore_quit_key = false;
|
||||
|
||||
if self.app.data.radarr_data.collections.filter.is_some() {
|
||||
let has_matches = self
|
||||
.app
|
||||
.data
|
||||
.radarr_data
|
||||
.collections
|
||||
.apply_filter(|collection| &collection.title.text);
|
||||
|
||||
if !has_matches {
|
||||
self
|
||||
.app
|
||||
.push_navigation_stack(ActiveRadarrBlock::FilterCollectionsError.into());
|
||||
}
|
||||
}
|
||||
}
|
||||
ActiveRadarrBlock::UpdateAllCollectionsPrompt => {
|
||||
if self.app.data.radarr_data.prompt_confirm {
|
||||
self.app.data.radarr_data.prompt_confirm_action = Some(RadarrEvent::UpdateCollections);
|
||||
@@ -259,44 +123,17 @@ impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for CollectionsHandler<'
|
||||
|
||||
self.app.pop_navigation_stack();
|
||||
}
|
||||
ActiveRadarrBlock::CollectionsSortPrompt => {
|
||||
self
|
||||
.app
|
||||
.data
|
||||
.radarr_data
|
||||
.collections
|
||||
.items
|
||||
.sort_by(|a, b| a.id.cmp(&b.id));
|
||||
self.app.data.radarr_data.collections.apply_sorting();
|
||||
|
||||
self.app.pop_navigation_stack();
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_esc(&mut self) {
|
||||
match self.active_radarr_block {
|
||||
ActiveRadarrBlock::FilterCollections | ActiveRadarrBlock::FilterCollectionsError => {
|
||||
self.app.pop_navigation_stack();
|
||||
self.app.data.radarr_data.collections.reset_filter();
|
||||
self.app.should_ignore_quit_key = false;
|
||||
}
|
||||
ActiveRadarrBlock::SearchCollection | ActiveRadarrBlock::SearchCollectionError => {
|
||||
self.app.pop_navigation_stack();
|
||||
self.app.data.radarr_data.collections.reset_search();
|
||||
self.app.should_ignore_quit_key = false;
|
||||
}
|
||||
ActiveRadarrBlock::UpdateAllCollectionsPrompt => {
|
||||
self.app.pop_navigation_stack();
|
||||
self.app.data.radarr_data.prompt_confirm = false;
|
||||
}
|
||||
ActiveRadarrBlock::CollectionsSortPrompt => {
|
||||
self.app.pop_navigation_stack();
|
||||
}
|
||||
_ => {
|
||||
self.app.data.radarr_data.collections.reset_search();
|
||||
self.app.data.radarr_data.collections.reset_filter();
|
||||
handle_clear_errors(self.app);
|
||||
}
|
||||
}
|
||||
@@ -306,23 +143,6 @@ impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for CollectionsHandler<'
|
||||
let key = self.key;
|
||||
match self.active_radarr_block {
|
||||
ActiveRadarrBlock::Collections => match self.key {
|
||||
_ if key == DEFAULT_KEYBINDINGS.search.key => {
|
||||
self
|
||||
.app
|
||||
.push_navigation_stack(ActiveRadarrBlock::SearchCollection.into());
|
||||
self.app.data.radarr_data.collections.search =
|
||||
Some(HorizontallyScrollableText::default());
|
||||
self.app.should_ignore_quit_key = true;
|
||||
}
|
||||
_ if key == DEFAULT_KEYBINDINGS.filter.key => {
|
||||
self
|
||||
.app
|
||||
.push_navigation_stack(ActiveRadarrBlock::FilterCollections.into());
|
||||
self.app.data.radarr_data.collections.reset_filter();
|
||||
self.app.data.radarr_data.collections.filter =
|
||||
Some(HorizontallyScrollableText::default());
|
||||
self.app.should_ignore_quit_key = true;
|
||||
}
|
||||
_ if key == DEFAULT_KEYBINDINGS.edit.key => {
|
||||
self.app.push_navigation_stack(
|
||||
(
|
||||
@@ -344,47 +164,8 @@ impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for CollectionsHandler<'
|
||||
_ if key == DEFAULT_KEYBINDINGS.refresh.key => {
|
||||
self.app.should_refresh = true;
|
||||
}
|
||||
_ if key == DEFAULT_KEYBINDINGS.sort.key => {
|
||||
self
|
||||
.app
|
||||
.data
|
||||
.radarr_data
|
||||
.collections
|
||||
.sorting(collections_sorting_options());
|
||||
self
|
||||
.app
|
||||
.push_navigation_stack(ActiveRadarrBlock::CollectionsSortPrompt.into());
|
||||
}
|
||||
_ => (),
|
||||
},
|
||||
ActiveRadarrBlock::SearchCollection => {
|
||||
handle_text_box_keys!(
|
||||
self,
|
||||
key,
|
||||
self
|
||||
.app
|
||||
.data
|
||||
.radarr_data
|
||||
.collections
|
||||
.search
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
)
|
||||
}
|
||||
ActiveRadarrBlock::FilterCollections => {
|
||||
handle_text_box_keys!(
|
||||
self,
|
||||
key,
|
||||
self
|
||||
.app
|
||||
.data
|
||||
.radarr_data
|
||||
.collections
|
||||
.filter
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
)
|
||||
}
|
||||
ActiveRadarrBlock::UpdateAllCollectionsPrompt => {
|
||||
if key == DEFAULT_KEYBINDINGS.confirm.key {
|
||||
self.app.data.radarr_data.prompt_confirm = true;
|
||||
|
||||
Reference in New Issue
Block a user