Refactored the UI module and the handlers module to do a more chain-of-responsibility method to manage the UI's and handlers for different key events. Also, initial work for indexer settings as well

This commit is contained in:
2023-08-08 10:50:07 -06:00
parent 718613d59f
commit cf11527fef
67 changed files with 5255 additions and 2216 deletions
@@ -1,6 +1,7 @@
use crate::app::key_binding::DEFAULT_KEYBINDINGS;
use crate::app::radarr::{
ActiveRadarrBlock, ADD_MOVIE_SELECTION_BLOCKS, EDIT_COLLECTION_SELECTION_BLOCKS,
ActiveRadarrBlock, ADD_MOVIE_SELECTION_BLOCKS, COLLECTION_DETAILS_BLOCKS,
EDIT_COLLECTION_SELECTION_BLOCKS,
};
use crate::app::App;
use crate::event::Key;
@@ -19,6 +20,10 @@ pub(super) struct CollectionDetailsHandler<'a, 'b> {
}
impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for CollectionDetailsHandler<'a, 'b> {
fn accepts(active_block: &'a ActiveRadarrBlock) -> bool {
COLLECTION_DETAILS_BLOCKS.contains(active_block)
}
fn with(
key: &'a Key,
app: &'a mut App<'b>,
@@ -1,12 +1,13 @@
#[cfg(test)]
mod tests {
use pretty_assertions::assert_str_eq;
use strum::IntoEnumIterator;
use crate::app::key_binding::DEFAULT_KEYBINDINGS;
use crate::app::radarr::ActiveRadarrBlock;
use crate::app::radarr::{ActiveRadarrBlock, COLLECTION_DETAILS_BLOCKS};
use crate::app::App;
use crate::event::Key;
use crate::handlers::radarr_handlers::collection_details_handler::CollectionDetailsHandler;
use crate::handlers::radarr_handlers::collections::collection_details_handler::CollectionDetailsHandler;
use crate::handlers::KeyEventHandler;
use crate::models::radarr_models::CollectionMovie;
use crate::models::HorizontallyScrollableText;
@@ -207,7 +208,6 @@ mod tests {
use crate::app::radarr::RadarrData;
use crate::app::radarr::EDIT_COLLECTION_SELECTION_BLOCKS;
use crate::models::radarr_models::{Collection, MinimumAvailability};
use crate::models::BlockSelectionState;
use crate::models::HorizontallyScrollableText;
use crate::models::StatefulTable;
use crate::test_edit_collection_key;
@@ -223,4 +223,15 @@ mod tests {
);
}
}
#[test]
fn test_collection_details_handler_accepts() {
ActiveRadarrBlock::iter().for_each(|active_radarr_block| {
if COLLECTION_DETAILS_BLOCKS.contains(&active_radarr_block) {
assert!(CollectionDetailsHandler::accepts(&active_radarr_block));
} else {
assert!(!CollectionDetailsHandler::accepts(&active_radarr_block));
}
});
}
}
@@ -0,0 +1,673 @@
#[cfg(test)]
mod tests {
use pretty_assertions::{assert_eq, assert_str_eq};
use rstest::rstest;
use strum::IntoEnumIterator;
use crate::app::key_binding::DEFAULT_KEYBINDINGS;
use crate::app::radarr::{
ActiveRadarrBlock, COLLECTIONS_BLOCKS, COLLECTION_DETAILS_BLOCKS, EDIT_COLLECTION_BLOCKS,
};
use crate::app::App;
use crate::event::Key;
use crate::handlers::radarr_handlers::collections::CollectionsHandler;
use crate::handlers::KeyEventHandler;
use crate::models::radarr_models::Collection;
use crate::models::HorizontallyScrollableText;
use crate::{extended_stateful_iterable_vec, test_handler_delegation};
mod test_handle_scroll_up_and_down {
use rstest::rstest;
use crate::{simple_stateful_iterable_vec, test_iterable_scroll};
use super::*;
test_iterable_scroll!(
test_collections_scroll,
CollectionsHandler,
collections,
simple_stateful_iterable_vec!(Collection, HorizontallyScrollableText),
ActiveRadarrBlock::Collections,
None,
title,
to_string
);
test_iterable_scroll!(
test_filtered_collections_scroll,
CollectionsHandler,
filtered_collections,
simple_stateful_iterable_vec!(Collection, HorizontallyScrollableText),
ActiveRadarrBlock::Collections,
None,
title,
to_string
);
}
mod test_handle_home_end {
use pretty_assertions::assert_eq;
use crate::{
extended_stateful_iterable_vec, test_iterable_home_and_end, test_text_box_home_end_keys,
};
use super::*;
test_iterable_home_and_end!(
test_collections_home_end,
CollectionsHandler,
collections,
extended_stateful_iterable_vec!(Collection, HorizontallyScrollableText),
ActiveRadarrBlock::Collections,
None,
title,
to_string
);
test_iterable_home_and_end!(
test_filtered_collections_home_end,
CollectionsHandler,
filtered_collections,
extended_stateful_iterable_vec!(Collection, HorizontallyScrollableText),
ActiveRadarrBlock::Collections,
None,
title,
to_string
);
#[test]
fn test_collection_search_box_home_end_keys() {
test_text_box_home_end_keys!(
CollectionsHandler,
ActiveRadarrBlock::SearchCollection,
search
);
}
#[test]
fn test_collection_filter_box_home_end_keys() {
test_text_box_home_end_keys!(
CollectionsHandler,
ActiveRadarrBlock::FilterCollections,
filter
);
}
}
mod test_handle_left_right_action {
use pretty_assertions::assert_eq;
use rstest::rstest;
use crate::test_text_box_left_right_keys;
use super::*;
#[test]
fn test_collections_tab_left() {
let mut app = App::default();
app.data.radarr_data.main_tabs.set_index(2);
CollectionsHandler::with(
&DEFAULT_KEYBINDINGS.left.key,
&mut app,
&ActiveRadarrBlock::Collections,
&None,
)
.handle();
assert_eq!(
app.data.radarr_data.main_tabs.get_active_route(),
&ActiveRadarrBlock::Downloads.into()
);
assert_eq!(
app.get_current_route(),
&ActiveRadarrBlock::Downloads.into()
);
}
#[test]
fn test_collections_tab_right() {
let mut app = App::default();
app.data.radarr_data.main_tabs.set_index(2);
CollectionsHandler::with(
&DEFAULT_KEYBINDINGS.right.key,
&mut app,
&ActiveRadarrBlock::Collections,
&None,
)
.handle();
assert_eq!(
app.data.radarr_data.main_tabs.get_active_route(),
&ActiveRadarrBlock::RootFolders.into()
);
assert_eq!(
app.get_current_route(),
&ActiveRadarrBlock::RootFolders.into()
);
}
#[rstest]
fn test_left_right_update_all_collections_prompt_toggle(
#[values(DEFAULT_KEYBINDINGS.left.key, DEFAULT_KEYBINDINGS.right.key)] key: Key,
) {
let mut app = App::default();
CollectionsHandler::with(
&key,
&mut app,
&ActiveRadarrBlock::UpdateAllCollectionsPrompt,
&None,
)
.handle();
assert!(app.data.radarr_data.prompt_confirm);
CollectionsHandler::with(
&key,
&mut app,
&ActiveRadarrBlock::UpdateAllCollectionsPrompt,
&None,
)
.handle();
assert!(!app.data.radarr_data.prompt_confirm);
}
#[test]
fn test_collection_search_box_left_right_keys() {
test_text_box_left_right_keys!(
CollectionsHandler,
ActiveRadarrBlock::SearchCollection,
search
);
}
#[test]
fn test_collection_filter_box_left_right_keys() {
test_text_box_left_right_keys!(
CollectionsHandler,
ActiveRadarrBlock::FilterCollections,
filter
);
}
}
mod test_handle_submit {
use pretty_assertions::assert_eq;
use crate::network::radarr_network::RadarrEvent;
use super::*;
const SUBMIT_KEY: Key = DEFAULT_KEYBINDINGS.submit.key;
#[test]
fn test_collections_submit() {
let mut app = App::default();
CollectionsHandler::with(
&SUBMIT_KEY,
&mut app,
&ActiveRadarrBlock::Collections,
&None,
)
.handle();
assert_eq!(
app.get_current_route(),
&ActiveRadarrBlock::CollectionDetails.into()
);
}
#[test]
fn test_search_collections_submit() {
let mut app = App::default();
app
.data
.radarr_data
.collections
.set_items(extended_stateful_iterable_vec!(
Collection,
HorizontallyScrollableText
));
app.data.radarr_data.search = "Test 2".to_owned().into();
CollectionsHandler::with(
&SUBMIT_KEY,
&mut app,
&ActiveRadarrBlock::SearchCollection,
&None,
)
.handle();
assert_str_eq!(
app
.data
.radarr_data
.collections
.current_selection()
.title
.text,
"Test 2"
);
}
#[test]
fn test_search_filtered_collections_submit() {
let mut app = App::default();
app
.data
.radarr_data
.filtered_collections
.set_items(extended_stateful_iterable_vec!(
Collection,
HorizontallyScrollableText
));
app.data.radarr_data.search = "Test 2".to_owned().into();
CollectionsHandler::with(
&SUBMIT_KEY,
&mut app,
&ActiveRadarrBlock::SearchCollection,
&None,
)
.handle();
assert_str_eq!(
app
.data
.radarr_data
.filtered_collections
.current_selection()
.title
.text,
"Test 2"
);
}
#[test]
fn test_filter_collections_submit() {
let mut app = App::default();
app
.data
.radarr_data
.collections
.set_items(extended_stateful_iterable_vec!(
Collection,
HorizontallyScrollableText
));
app.data.radarr_data.filter = "Test".to_owned().into();
CollectionsHandler::with(
&SUBMIT_KEY,
&mut app,
&ActiveRadarrBlock::FilterCollections,
&None,
)
.handle();
assert_eq!(app.data.radarr_data.filtered_collections.items.len(), 3);
assert_str_eq!(
app
.data
.radarr_data
.filtered_collections
.current_selection()
.title
.text,
"Test 1"
);
}
#[test]
fn test_update_all_collections_prompt_confirm_submit() {
let mut app = App::default();
app.data.radarr_data.prompt_confirm = true;
app.push_navigation_stack(ActiveRadarrBlock::Collections.into());
app.push_navigation_stack(ActiveRadarrBlock::UpdateAllCollectionsPrompt.into());
CollectionsHandler::with(
&SUBMIT_KEY,
&mut app,
&ActiveRadarrBlock::UpdateAllCollectionsPrompt,
&None,
)
.handle();
assert!(app.data.radarr_data.prompt_confirm);
assert_eq!(
app.data.radarr_data.prompt_confirm_action,
Some(RadarrEvent::UpdateCollections)
);
assert_eq!(
app.get_current_route(),
&ActiveRadarrBlock::Collections.into()
);
}
#[test]
fn test_update_all_collections_prompt_decline_submit() {
let mut app = App::default();
app.push_navigation_stack(ActiveRadarrBlock::Collections.into());
app.push_navigation_stack(ActiveRadarrBlock::UpdateAllCollectionsPrompt.into());
CollectionsHandler::with(
&SUBMIT_KEY,
&mut app,
&ActiveRadarrBlock::UpdateAllCollectionsPrompt,
&None,
)
.handle();
assert!(!app.data.radarr_data.prompt_confirm);
assert_eq!(app.data.radarr_data.prompt_confirm_action, None);
assert_eq!(
app.get_current_route(),
&ActiveRadarrBlock::Collections.into()
);
}
}
mod test_handle_esc {
use pretty_assertions::assert_eq;
use crate::app::radarr::radarr_test_utils::utils::create_test_radarr_data;
use crate::{assert_filter_reset, assert_search_reset};
use super::*;
const ESC_KEY: Key = DEFAULT_KEYBINDINGS.esc.key;
#[test]
fn test_search_collection_block_esc() {
let mut app = App::default();
app.should_ignore_quit_key = true;
app.push_navigation_stack(ActiveRadarrBlock::Collections.into());
app.push_navigation_stack(ActiveRadarrBlock::SearchCollection.into());
app.data.radarr_data = create_test_radarr_data();
CollectionsHandler::with(
&ESC_KEY,
&mut app,
&ActiveRadarrBlock::SearchCollection,
&None,
)
.handle();
assert_eq!(
app.get_current_route(),
&ActiveRadarrBlock::Collections.into()
);
assert!(!app.should_ignore_quit_key);
assert_search_reset!(app.data.radarr_data);
}
#[test]
fn test_filter_collections_block_esc() {
let mut app = App::default();
app.should_ignore_quit_key = true;
app.push_navigation_stack(ActiveRadarrBlock::Collections.into());
app.push_navigation_stack(ActiveRadarrBlock::FilterCollections.into());
app.data.radarr_data = create_test_radarr_data();
CollectionsHandler::with(
&ESC_KEY,
&mut app,
&ActiveRadarrBlock::FilterCollections,
&None,
)
.handle();
assert_eq!(
app.get_current_route(),
&ActiveRadarrBlock::Collections.into()
);
assert!(!app.should_ignore_quit_key);
assert_filter_reset!(app.data.radarr_data);
}
#[test]
fn test_update_all_collections_prompt_block_esc() {
let mut app = App::default();
app.push_navigation_stack(ActiveRadarrBlock::Collections.into());
app.push_navigation_stack(ActiveRadarrBlock::UpdateAllCollectionsPrompt.into());
app.data.radarr_data.prompt_confirm = true;
CollectionsHandler::with(
&ESC_KEY,
&mut app,
&ActiveRadarrBlock::UpdateAllCollectionsPrompt,
&None,
)
.handle();
assert_eq!(
app.get_current_route(),
&ActiveRadarrBlock::Collections.into()
);
assert!(!app.data.radarr_data.prompt_confirm);
}
#[test]
fn test_default_esc() {
let mut app = App::default();
app.error = "test error".to_owned().into();
app.push_navigation_stack(ActiveRadarrBlock::Collections.into());
app.push_navigation_stack(ActiveRadarrBlock::Collections.into());
app.data.radarr_data = create_test_radarr_data();
CollectionsHandler::with(&ESC_KEY, &mut app, &ActiveRadarrBlock::Collections, &None).handle();
assert_eq!(
app.get_current_route(),
&ActiveRadarrBlock::Collections.into()
);
assert!(app.error.text.is_empty());
assert_search_reset!(app.data.radarr_data);
assert_filter_reset!(app.data.radarr_data);
}
}
mod test_handle_key_char {
use bimap::BiMap;
use pretty_assertions::{assert_eq, assert_str_eq};
use serde_json::Number;
use strum::IntoEnumIterator;
use crate::app::radarr::radarr_test_utils::utils::create_test_radarr_data;
use crate::app::radarr::RadarrData;
use crate::app::radarr::EDIT_COLLECTION_SELECTION_BLOCKS;
use crate::models::radarr_models::MinimumAvailability;
use crate::models::HorizontallyScrollableText;
use crate::models::StatefulTable;
use crate::{assert_refresh_key, test_edit_collection_key};
use super::*;
#[test]
fn test_search_collections_key() {
let mut app = App::default();
CollectionsHandler::with(
&DEFAULT_KEYBINDINGS.search.key,
&mut app,
&ActiveRadarrBlock::Collections,
&None,
)
.handle();
assert_eq!(
app.get_current_route(),
&ActiveRadarrBlock::SearchCollection.into()
);
assert!(app.data.radarr_data.is_searching);
assert!(app.should_ignore_quit_key);
}
#[test]
fn test_filter_collections_key() {
let mut app = App::default();
CollectionsHandler::with(
&DEFAULT_KEYBINDINGS.filter.key,
&mut app,
&ActiveRadarrBlock::Collections,
&None,
)
.handle();
assert_eq!(
app.get_current_route(),
&ActiveRadarrBlock::FilterCollections.into()
);
assert!(app.data.radarr_data.is_filtering);
assert!(app.should_ignore_quit_key);
}
#[test]
fn test_collection_edit_key() {
test_edit_collection_key!(
CollectionsHandler,
ActiveRadarrBlock::Collections,
ActiveRadarrBlock::Collections
);
}
#[test]
fn test_update_key() {
let mut app = App::default();
CollectionsHandler::with(
&DEFAULT_KEYBINDINGS.update.key,
&mut app,
&ActiveRadarrBlock::Collections,
&None,
)
.handle();
assert_eq!(
app.get_current_route(),
&ActiveRadarrBlock::UpdateAllCollectionsPrompt.into()
);
}
#[test]
fn test_refresh_collections_key() {
assert_refresh_key!(CollectionsHandler, ActiveRadarrBlock::Collections);
}
#[test]
fn test_search_collections_box_backspace_key() {
let mut app = App::default();
app.data.radarr_data.search = "Test".to_owned().into();
CollectionsHandler::with(
&DEFAULT_KEYBINDINGS.backspace.key,
&mut app,
&ActiveRadarrBlock::SearchCollection,
&None,
)
.handle();
assert_str_eq!(app.data.radarr_data.search.text, "Tes");
}
#[test]
fn test_filter_collections_box_backspace_key() {
let mut app = App::default();
app.data.radarr_data.filter = "Test".to_owned().into();
CollectionsHandler::with(
&DEFAULT_KEYBINDINGS.backspace.key,
&mut app,
&ActiveRadarrBlock::FilterCollections,
&None,
)
.handle();
assert_str_eq!(app.data.radarr_data.filter.text, "Tes");
}
#[test]
fn test_search_collections_box_char_key() {
let mut app = App::default();
CollectionsHandler::with(
&Key::Char('h'),
&mut app,
&ActiveRadarrBlock::SearchCollection,
&None,
)
.handle();
assert_str_eq!(app.data.radarr_data.search.text, "h");
}
#[test]
fn test_filter_collections_box_char_key() {
let mut app = App::default();
CollectionsHandler::with(
&Key::Char('h'),
&mut app,
&ActiveRadarrBlock::FilterCollections,
&None,
)
.handle();
assert_str_eq!(app.data.radarr_data.filter.text, "h");
}
}
#[rstest]
fn test_delegate_collection_details_blocks_to_collection_details_handler(
#[values(
ActiveRadarrBlock::CollectionDetails,
ActiveRadarrBlock::ViewMovieOverview
)]
active_radarr_block: ActiveRadarrBlock,
) {
test_handler_delegation!(
CollectionsHandler,
ActiveRadarrBlock::Collections,
active_radarr_block
);
}
#[rstest]
fn test_delegate_edit_collection_blocks_to_edit_collection_handler(
#[values(
ActiveRadarrBlock::EditCollectionPrompt,
ActiveRadarrBlock::EditCollectionRootFolderPathInput,
ActiveRadarrBlock::EditCollectionSelectMinimumAvailability,
ActiveRadarrBlock::EditCollectionSelectQualityProfile
)]
active_radarr_block: ActiveRadarrBlock,
) {
test_handler_delegation!(
CollectionsHandler,
ActiveRadarrBlock::Collections,
active_radarr_block
);
}
#[test]
fn test_collections_handler_accepts() {
let mut collections_handler_blocks = Vec::new();
collections_handler_blocks.extend(COLLECTIONS_BLOCKS);
collections_handler_blocks.extend(COLLECTION_DETAILS_BLOCKS);
collections_handler_blocks.extend(EDIT_COLLECTION_BLOCKS);
ActiveRadarrBlock::iter().for_each(|active_radarr_block| {
if collections_handler_blocks.contains(&active_radarr_block) {
assert!(CollectionsHandler::accepts(&active_radarr_block));
} else {
assert!(!CollectionsHandler::accepts(&active_radarr_block));
}
});
}
}
@@ -1,5 +1,5 @@
use crate::app::key_binding::DEFAULT_KEYBINDINGS;
use crate::app::radarr::ActiveRadarrBlock;
use crate::app::radarr::{ActiveRadarrBlock, EDIT_COLLECTION_BLOCKS};
use crate::app::App;
use crate::event::Key;
use crate::handlers::{handle_prompt_toggle, KeyEventHandler};
@@ -19,6 +19,10 @@ pub(super) struct EditCollectionHandler<'a, 'b> {
}
impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for EditCollectionHandler<'a, 'b> {
fn accepts(active_block: &'a ActiveRadarrBlock) -> bool {
EDIT_COLLECTION_BLOCKS.contains(active_block)
}
fn with(
key: &'a Key,
app: &'a mut App<'b>,
@@ -1,12 +1,13 @@
#[cfg(test)]
mod tests {
use pretty_assertions::assert_str_eq;
use strum::IntoEnumIterator;
use crate::app::key_binding::DEFAULT_KEYBINDINGS;
use crate::app::radarr::ActiveRadarrBlock;
use crate::app::radarr::{ActiveRadarrBlock, EDIT_COLLECTION_BLOCKS};
use crate::app::App;
use crate::event::Key;
use crate::handlers::radarr_handlers::edit_collection_handler::EditCollectionHandler;
use crate::handlers::radarr_handlers::collections::edit_collection_handler::EditCollectionHandler;
use crate::handlers::KeyEventHandler;
use crate::models::radarr_models::MinimumAvailability;
@@ -500,4 +501,15 @@ mod tests {
assert_str_eq!(app.data.radarr_data.edit_path.text, "h");
}
}
#[test]
fn test_edit_collection_handler_accepts() {
ActiveRadarrBlock::iter().for_each(|active_radarr_block| {
if EDIT_COLLECTION_BLOCKS.contains(&active_radarr_block) {
assert!(EditCollectionHandler::accepts(&active_radarr_block));
} else {
assert!(!EditCollectionHandler::accepts(&active_radarr_block));
}
});
}
}
@@ -0,0 +1,311 @@
use crate::app::key_binding::DEFAULT_KEYBINDINGS;
use crate::app::radarr::{ActiveRadarrBlock, COLLECTIONS_BLOCKS, EDIT_COLLECTION_SELECTION_BLOCKS};
use crate::app::App;
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::{
filter_table, handle_change_tab_left_right_keys, search_table,
};
use crate::handlers::{handle_clear_errors, handle_prompt_toggle, KeyEventHandler};
use crate::models::{BlockSelectionState, Scrollable};
use crate::network::radarr_network::RadarrEvent;
use crate::{handle_text_box_keys, handle_text_box_left_right_keys};
mod collection_details_handler;
mod edit_collection_handler;
#[cfg(test)]
#[path = "collections_handler_tests.rs"]
mod collections_handler_tests;
pub(super) struct CollectionsHandler<'a, 'b> {
key: &'a Key,
app: &'a mut App<'b>,
active_radarr_block: &'a ActiveRadarrBlock,
context: &'a Option<ActiveRadarrBlock>,
}
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();
}
_ if EditCollectionHandler::accepts(self.active_radarr_block) => {
EditCollectionHandler::with(self.key, self.app, self.active_radarr_block, self.context)
.handle();
}
_ => self.handle_key_event(),
}
}
fn accepts(active_block: &'a ActiveRadarrBlock) -> bool {
CollectionDetailsHandler::accepts(active_block)
|| EditCollectionHandler::accepts(active_block)
|| COLLECTIONS_BLOCKS.contains(active_block)
}
fn with(
key: &'a Key,
app: &'a mut App<'b>,
active_block: &'a ActiveRadarrBlock,
context: &'a Option<ActiveRadarrBlock>,
) -> CollectionsHandler<'a, 'b> {
CollectionsHandler {
key,
app,
active_radarr_block: active_block,
context,
}
}
fn get_key(&self) -> &Key {
self.key
}
fn handle_scroll_up(&mut self) {
if self.active_radarr_block == &ActiveRadarrBlock::Collections {
if !self
.app
.data
.radarr_data
.filtered_collections
.items
.is_empty()
{
self.app.data.radarr_data.filtered_collections.scroll_up();
} else {
self.app.data.radarr_data.collections.scroll_up()
}
}
}
fn handle_scroll_down(&mut self) {
if self.active_radarr_block == &ActiveRadarrBlock::Collections {
if !self
.app
.data
.radarr_data
.filtered_collections
.items
.is_empty()
{
self.app.data.radarr_data.filtered_collections.scroll_down();
} else {
self.app.data.radarr_data.collections.scroll_down()
}
}
}
fn handle_home(&mut self) {
match self.active_radarr_block {
ActiveRadarrBlock::Collections => {
if !self
.app
.data
.radarr_data
.filtered_collections
.items
.is_empty()
{
self
.app
.data
.radarr_data
.filtered_collections
.scroll_to_top();
} else {
self.app.data.radarr_data.collections.scroll_to_top()
}
}
ActiveRadarrBlock::SearchCollection => self.app.data.radarr_data.search.scroll_home(),
ActiveRadarrBlock::FilterCollections => self.app.data.radarr_data.filter.scroll_home(),
_ => (),
}
}
fn handle_end(&mut self) {
match self.active_radarr_block {
ActiveRadarrBlock::Collections => {
if !self
.app
.data
.radarr_data
.filtered_collections
.items
.is_empty()
{
self
.app
.data
.radarr_data
.filtered_collections
.scroll_to_bottom();
} else {
self.app.data.radarr_data.collections.scroll_to_bottom()
}
}
ActiveRadarrBlock::SearchCollection => self.app.data.radarr_data.search.reset_offset(),
ActiveRadarrBlock::FilterCollections => self.app.data.radarr_data.filter.reset_offset(),
_ => (),
}
}
fn handle_delete(&mut self) {}
fn handle_left_right_action(&mut self) {
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.search)
}
ActiveRadarrBlock::FilterCollections => {
handle_text_box_left_right_keys!(self, self.key, self.app.data.radarr_data.filter)
}
_ => (),
}
}
fn handle_submit(&mut self) {
match self.active_radarr_block {
ActiveRadarrBlock::Collections => self
.app
.push_navigation_stack(ActiveRadarrBlock::CollectionDetails.into()),
ActiveRadarrBlock::SearchCollection => {
if self
.app
.data
.radarr_data
.filtered_collections
.items
.is_empty()
{
let selected_index = search_table(
self.app,
&self.app.data.radarr_data.collections.items.clone(),
|collection| &collection.title.text,
);
self
.app
.data
.radarr_data
.collections
.select_index(selected_index);
} else {
let selected_index = search_table(
self.app,
&self.app.data.radarr_data.filtered_collections.items.clone(),
|collection| &collection.title.text,
);
self
.app
.data
.radarr_data
.filtered_collections
.select_index(selected_index);
}
}
ActiveRadarrBlock::FilterCollections => {
let filtered_collections = filter_table(
self.app,
&self.app.data.radarr_data.collections.items.clone(),
|collection| &collection.title.text,
);
if !filtered_collections.is_empty() {
self
.app
.data
.radarr_data
.filtered_collections
.set_items(filtered_collections);
}
}
ActiveRadarrBlock::UpdateAllCollectionsPrompt => {
if self.app.data.radarr_data.prompt_confirm {
self.app.data.radarr_data.prompt_confirm_action = Some(RadarrEvent::UpdateCollections);
}
self.app.pop_navigation_stack();
}
_ => (),
}
}
fn handle_esc(&mut self) {
match self.active_radarr_block {
ActiveRadarrBlock::FilterCollections => {
self.app.pop_navigation_stack();
self.app.data.radarr_data.reset_filter();
self.app.should_ignore_quit_key = false;
}
ActiveRadarrBlock::SearchCollection => {
self.app.pop_navigation_stack();
self.app.data.radarr_data.reset_search();
self.app.should_ignore_quit_key = false;
}
ActiveRadarrBlock::UpdateAllCollectionsPrompt => {
self.app.pop_navigation_stack();
self.app.data.radarr_data.prompt_confirm = false;
}
_ => {
self.app.data.radarr_data.reset_search();
self.app.data.radarr_data.reset_filter();
handle_clear_errors(self.app);
}
}
}
fn handle_char_key_event(&mut self) {
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.is_searching = true;
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.is_filtering = true;
self.app.should_ignore_quit_key = true;
}
_ if *key == DEFAULT_KEYBINDINGS.edit.key => {
self.app.push_navigation_stack(
(
ActiveRadarrBlock::EditCollectionPrompt,
Some(ActiveRadarrBlock::Collections),
)
.into(),
);
self.app.data.radarr_data.populate_edit_collection_fields();
self.app.data.radarr_data.selected_block =
BlockSelectionState::new(&EDIT_COLLECTION_SELECTION_BLOCKS);
}
_ if *key == DEFAULT_KEYBINDINGS.update.key => {
self
.app
.push_navigation_stack(ActiveRadarrBlock::UpdateAllCollectionsPrompt.into());
}
_ if *key == DEFAULT_KEYBINDINGS.refresh.key => {
self.app.should_refresh = true;
}
_ => (),
},
ActiveRadarrBlock::SearchCollection => {
handle_text_box_keys!(self, key, self.app.data.radarr_data.search)
}
ActiveRadarrBlock::FilterCollections => {
handle_text_box_keys!(self, key, self.app.data.radarr_data.filter)
}
_ => (),
}
}
}
@@ -0,0 +1,282 @@
#[cfg(test)]
mod tests {
use pretty_assertions::assert_str_eq;
use strum::IntoEnumIterator;
use crate::app::key_binding::DEFAULT_KEYBINDINGS;
use crate::app::radarr::{ActiveRadarrBlock, DOWNLOADS_BLOCKS};
use crate::app::App;
use crate::event::Key;
use crate::handlers::radarr_handlers::downloads::DownloadsHandler;
use crate::handlers::KeyEventHandler;
mod test_handle_scroll_up_and_down {
use rstest::rstest;
use crate::models::radarr_models::DownloadRecord;
use crate::{simple_stateful_iterable_vec, test_iterable_scroll};
use super::*;
test_iterable_scroll!(
test_downloads_scroll,
DownloadsHandler,
downloads,
DownloadRecord,
ActiveRadarrBlock::Downloads,
None,
title
);
}
mod test_handle_home_end {
use crate::models::radarr_models::DownloadRecord;
use crate::{extended_stateful_iterable_vec, test_iterable_home_and_end};
use super::*;
test_iterable_home_and_end!(
test_downloads_home_end,
DownloadsHandler,
downloads,
DownloadRecord,
ActiveRadarrBlock::Downloads,
None,
title
);
}
mod test_handle_delete {
use pretty_assertions::assert_eq;
use crate::assert_delete_prompt;
use super::*;
const DELETE_KEY: Key = DEFAULT_KEYBINDINGS.delete.key;
#[test]
fn test_delete_download_prompt() {
assert_delete_prompt!(
DownloadsHandler,
ActiveRadarrBlock::Downloads,
ActiveRadarrBlock::DeleteDownloadPrompt
);
}
}
mod test_handle_left_right_action {
use pretty_assertions::assert_eq;
use rstest::rstest;
use super::*;
#[test]
fn test_downloads_tab_left() {
let mut app = App::default();
app.data.radarr_data.main_tabs.set_index(1);
DownloadsHandler::with(
&DEFAULT_KEYBINDINGS.left.key,
&mut app,
&ActiveRadarrBlock::Downloads,
&None,
)
.handle();
assert_eq!(
app.data.radarr_data.main_tabs.get_active_route(),
&ActiveRadarrBlock::Movies.into()
);
assert_eq!(app.get_current_route(), &ActiveRadarrBlock::Movies.into());
}
#[test]
fn test_downloads_tab_right() {
let mut app = App::default();
app.data.radarr_data.main_tabs.set_index(1);
DownloadsHandler::with(
&DEFAULT_KEYBINDINGS.right.key,
&mut app,
&ActiveRadarrBlock::Downloads,
&None,
)
.handle();
assert_eq!(
app.data.radarr_data.main_tabs.get_active_route(),
&ActiveRadarrBlock::Collections.into()
);
assert_eq!(
app.get_current_route(),
&ActiveRadarrBlock::Collections.into()
);
}
#[rstest]
fn test_downloads_left_right_prompt_toggle(
#[values(
ActiveRadarrBlock::DeleteDownloadPrompt,
ActiveRadarrBlock::UpdateDownloadsPrompt
)]
active_radarr_block: ActiveRadarrBlock,
#[values(DEFAULT_KEYBINDINGS.left.key, DEFAULT_KEYBINDINGS.right.key)] key: Key,
) {
let mut app = App::default();
DownloadsHandler::with(&key, &mut app, &active_radarr_block, &None).handle();
assert!(app.data.radarr_data.prompt_confirm);
DownloadsHandler::with(&key, &mut app, &active_radarr_block, &None).handle();
assert!(!app.data.radarr_data.prompt_confirm);
}
}
mod test_handle_submit {
use pretty_assertions::assert_eq;
use rstest::rstest;
use crate::network::radarr_network::RadarrEvent;
use super::*;
const SUBMIT_KEY: Key = DEFAULT_KEYBINDINGS.submit.key;
#[rstest]
#[case(
ActiveRadarrBlock::Downloads,
ActiveRadarrBlock::DeleteDownloadPrompt,
RadarrEvent::DeleteDownload
)]
#[case(
ActiveRadarrBlock::Downloads,
ActiveRadarrBlock::UpdateDownloadsPrompt,
RadarrEvent::UpdateDownloads
)]
fn test_downloads_prompt_confirm_submit(
#[case] base_route: ActiveRadarrBlock,
#[case] prompt_block: ActiveRadarrBlock,
#[case] expected_action: RadarrEvent,
) {
let mut app = App::default();
app.data.radarr_data.prompt_confirm = true;
app.push_navigation_stack(base_route.into());
app.push_navigation_stack(prompt_block.into());
DownloadsHandler::with(&SUBMIT_KEY, &mut app, &prompt_block, &None).handle();
assert!(app.data.radarr_data.prompt_confirm);
assert_eq!(
app.data.radarr_data.prompt_confirm_action,
Some(expected_action)
);
assert_eq!(app.get_current_route(), &base_route.into());
}
#[rstest]
#[case(ActiveRadarrBlock::Downloads, ActiveRadarrBlock::DeleteDownloadPrompt)]
#[case(ActiveRadarrBlock::Downloads, ActiveRadarrBlock::UpdateDownloadsPrompt)]
fn test_downloads_prompt_decline_submit(
#[case] base_route: ActiveRadarrBlock,
#[case] prompt_block: ActiveRadarrBlock,
) {
let mut app = App::default();
app.push_navigation_stack(base_route.into());
app.push_navigation_stack(prompt_block.into());
DownloadsHandler::with(&SUBMIT_KEY, &mut app, &prompt_block, &None).handle();
assert!(!app.data.radarr_data.prompt_confirm);
assert_eq!(app.data.radarr_data.prompt_confirm_action, None);
assert_eq!(app.get_current_route(), &base_route.into());
}
}
mod test_handle_esc {
use pretty_assertions::assert_eq;
use rstest::rstest;
use super::*;
const ESC_KEY: Key = DEFAULT_KEYBINDINGS.esc.key;
#[rstest]
#[case(ActiveRadarrBlock::Downloads, ActiveRadarrBlock::DeleteDownloadPrompt)]
#[case(ActiveRadarrBlock::Downloads, ActiveRadarrBlock::UpdateDownloadsPrompt)]
fn test_downloads_prompt_blocks_esc(
#[case] base_block: ActiveRadarrBlock,
#[case] prompt_block: ActiveRadarrBlock,
) {
let mut app = App::default();
app.push_navigation_stack(base_block.into());
app.push_navigation_stack(prompt_block.into());
app.data.radarr_data.prompt_confirm = true;
DownloadsHandler::with(&ESC_KEY, &mut app, &prompt_block, &None).handle();
assert_eq!(app.get_current_route(), &base_block.into());
assert!(!app.data.radarr_data.prompt_confirm);
}
#[test]
fn test_default_esc() {
let mut app = App::default();
app.error = "test error".to_owned().into();
app.push_navigation_stack(ActiveRadarrBlock::Downloads.into());
app.push_navigation_stack(ActiveRadarrBlock::Downloads.into());
DownloadsHandler::with(&ESC_KEY, &mut app, &ActiveRadarrBlock::Downloads, &None).handle();
assert_eq!(
app.get_current_route(),
&ActiveRadarrBlock::Downloads.into()
);
assert!(app.error.text.is_empty());
}
}
mod test_handle_key_char {
use pretty_assertions::assert_eq;
use crate::assert_refresh_key;
use super::*;
#[test]
fn test_update_downloads_key() {
let mut app = App::default();
DownloadsHandler::with(
&DEFAULT_KEYBINDINGS.update.key,
&mut app,
&ActiveRadarrBlock::Downloads,
&None,
)
.handle();
assert_eq!(
app.get_current_route(),
&ActiveRadarrBlock::UpdateDownloadsPrompt.into()
);
}
#[test]
fn test_refresh_downloads_key() {
assert_refresh_key!(DownloadsHandler, ActiveRadarrBlock::Downloads);
}
}
#[test]
fn test_downloads_handler_accepts() {
ActiveRadarrBlock::iter().for_each(|active_radarr_block| {
if DOWNLOADS_BLOCKS.contains(&active_radarr_block) {
assert!(DownloadsHandler::accepts(&active_radarr_block));
} else {
assert!(!DownloadsHandler::accepts(&active_radarr_block));
}
})
}
}
@@ -0,0 +1,132 @@
use crate::app::key_binding::DEFAULT_KEYBINDINGS;
use crate::app::radarr::{ActiveRadarrBlock, DOWNLOADS_BLOCKS};
use crate::app::App;
use crate::event::Key;
use crate::handlers::radarr_handlers::handle_change_tab_left_right_keys;
use crate::handlers::{handle_clear_errors, handle_prompt_toggle, KeyEventHandler};
use crate::models::Scrollable;
use crate::network::radarr_network::RadarrEvent;
#[cfg(test)]
#[path = "downloads_handler_tests.rs"]
mod downloads_handler_tests;
pub(super) struct DownloadsHandler<'a, 'b> {
key: &'a Key,
app: &'a mut App<'b>,
active_radarr_block: &'a ActiveRadarrBlock,
_context: &'a Option<ActiveRadarrBlock>,
}
impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for DownloadsHandler<'a, 'b> {
fn accepts(active_block: &'a ActiveRadarrBlock) -> bool {
DOWNLOADS_BLOCKS.contains(active_block)
}
fn with(
key: &'a Key,
app: &'a mut App<'b>,
active_block: &'a ActiveRadarrBlock,
_context: &'a Option<ActiveRadarrBlock>,
) -> DownloadsHandler<'a, 'b> {
DownloadsHandler {
key,
app,
active_radarr_block: active_block,
_context,
}
}
fn get_key(&self) -> &Key {
self.key
}
fn handle_scroll_up(&mut self) {
if self.active_radarr_block == &ActiveRadarrBlock::Downloads {
self.app.data.radarr_data.downloads.scroll_up()
}
}
fn handle_scroll_down(&mut self) {
if self.active_radarr_block == &ActiveRadarrBlock::Downloads {
self.app.data.radarr_data.downloads.scroll_down()
}
}
fn handle_home(&mut self) {
if self.active_radarr_block == &ActiveRadarrBlock::Downloads {
self.app.data.radarr_data.downloads.scroll_to_top()
}
}
fn handle_end(&mut self) {
if self.active_radarr_block == &ActiveRadarrBlock::Downloads {
self.app.data.radarr_data.downloads.scroll_to_bottom()
}
}
fn handle_delete(&mut self) {
if self.active_radarr_block == &ActiveRadarrBlock::Downloads {
self
.app
.push_navigation_stack(ActiveRadarrBlock::DeleteDownloadPrompt.into())
}
}
fn handle_left_right_action(&mut self) {
match self.active_radarr_block {
ActiveRadarrBlock::Downloads => handle_change_tab_left_right_keys(self.app, self.key),
ActiveRadarrBlock::DeleteDownloadPrompt | ActiveRadarrBlock::UpdateDownloadsPrompt => {
handle_prompt_toggle(self.app, self.key)
}
_ => (),
}
}
fn handle_submit(&mut self) {
match self.active_radarr_block {
ActiveRadarrBlock::DeleteDownloadPrompt => {
if self.app.data.radarr_data.prompt_confirm {
self.app.data.radarr_data.prompt_confirm_action = Some(RadarrEvent::DeleteDownload);
}
self.app.pop_navigation_stack();
}
ActiveRadarrBlock::UpdateDownloadsPrompt => {
if self.app.data.radarr_data.prompt_confirm {
self.app.data.radarr_data.prompt_confirm_action = Some(RadarrEvent::UpdateDownloads);
}
self.app.pop_navigation_stack();
}
_ => (),
}
}
fn handle_esc(&mut self) {
match self.active_radarr_block {
ActiveRadarrBlock::DeleteDownloadPrompt | ActiveRadarrBlock::UpdateDownloadsPrompt => {
self.app.pop_navigation_stack();
self.app.data.radarr_data.prompt_confirm = false;
}
_ => handle_clear_errors(self.app),
}
}
fn handle_char_key_event(&mut self) {
let key = self.key;
if self.active_radarr_block == &ActiveRadarrBlock::Downloads {
match self.key {
_ if *key == DEFAULT_KEYBINDINGS.update.key => {
self
.app
.push_navigation_stack(ActiveRadarrBlock::UpdateDownloadsPrompt.into());
}
_ if *key == DEFAULT_KEYBINDINGS.refresh.key => {
self.app.should_refresh = true;
}
_ => (),
}
}
}
}
@@ -0,0 +1,74 @@
use crate::app::radarr::{ActiveRadarrBlock, INDEXER_SETTINGS_BLOCKS};
use crate::app::App;
use crate::event::Key;
use crate::handlers::KeyEventHandler;
#[cfg(test)]
#[path = "./edit_indexer_settings_handler_tests.rs"]
mod edit_indexer_settings_handler_tests;
pub(super) struct IndexerSettingsHandler<'a, 'b> {
key: &'a Key,
app: &'a mut App<'b>,
active_radarr_block: &'a ActiveRadarrBlock,
_context: &'a Option<ActiveRadarrBlock>,
}
impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for IndexerSettingsHandler<'a, 'b> {
fn accepts(active_block: &'a ActiveRadarrBlock) -> bool {
INDEXER_SETTINGS_BLOCKS.contains(active_block)
}
fn with(
key: &'a Key,
app: &'a mut App<'b>,
active_block: &'a ActiveRadarrBlock,
_context: &'a Option<ActiveRadarrBlock>,
) -> IndexerSettingsHandler<'a, 'b> {
IndexerSettingsHandler {
key,
app,
active_radarr_block: active_block,
_context,
}
}
fn get_key(&self) -> &Key {
self.key
}
fn handle_scroll_up(&mut self) {
if self.active_radarr_block == &ActiveRadarrBlock::IndexerSettingsPrompt {
self.app.data.radarr_data.selected_block.previous()
}
}
fn handle_scroll_down(&mut self) {
if self.active_radarr_block == &ActiveRadarrBlock::IndexerSettingsPrompt {
self.app.data.radarr_data.selected_block.next()
}
}
fn handle_home(&mut self) {}
fn handle_end(&mut self) {}
fn handle_delete(&mut self) {}
fn handle_left_right_action(&mut self) {}
fn handle_submit(&mut self) {}
fn handle_esc(&mut self) {
match self.active_radarr_block {
ActiveRadarrBlock::IndexerSettingsPrompt => {
self.app.pop_navigation_stack();
self.app.data.radarr_data.prompt_confirm = false;
self.app.data.radarr_data.indexer_settings = None;
}
_ => self.app.pop_navigation_stack(), // Need to tweak this still and add unit tests
}
}
fn handle_char_key_event(&mut self) {}
}
@@ -0,0 +1,98 @@
#[cfg(test)]
mod tests {
use strum::IntoEnumIterator;
use crate::app::key_binding::DEFAULT_KEYBINDINGS;
use crate::app::radarr::{ActiveRadarrBlock, INDEXER_SETTINGS_BLOCKS};
use crate::app::App;
use crate::event::Key;
use crate::handlers::radarr_handlers::indexers::edit_indexer_settings_handler::IndexerSettingsHandler;
use crate::handlers::KeyEventHandler;
mod test_handle_scroll_up_and_down {
use pretty_assertions::assert_eq;
use rstest::rstest;
use crate::app::radarr::INDEXER_SETTINGS_SELECTION_BLOCKS;
use crate::models::BlockSelectionState;
use super::*;
#[rstest]
fn test_edit_indexer_settings_prompt_scroll(#[values(Key::Up, Key::Down)] key: Key) {
let mut app = App::default();
app.data.radarr_data.selected_block =
BlockSelectionState::new(&INDEXER_SETTINGS_SELECTION_BLOCKS);
app.data.radarr_data.selected_block.next();
IndexerSettingsHandler::with(
&key,
&mut app,
&ActiveRadarrBlock::IndexerSettingsPrompt,
&None,
)
.handle();
if key == Key::Up {
assert_eq!(
app.data.radarr_data.selected_block.get_active_block(),
&ActiveRadarrBlock::IndexerSettingsMinimumAgeInput
);
} else {
assert_eq!(
app.data.radarr_data.selected_block.get_active_block(),
&ActiveRadarrBlock::IndexerSettingsMaximumSizeInput
);
}
}
}
mod test_handle_home_end {}
mod test_handle_left_right_action {}
mod test_handle_submit {}
mod test_handle_esc {
use pretty_assertions::assert_eq;
use crate::app::radarr::radarr_test_utils::utils::create_test_radarr_data;
use super::*;
const ESC_KEY: Key = DEFAULT_KEYBINDINGS.esc.key;
#[test]
fn test_edit_indexer_settings_prompt_esc() {
let mut app = App::default();
app.push_navigation_stack(ActiveRadarrBlock::Indexers.into());
app.push_navigation_stack(ActiveRadarrBlock::EditMoviePrompt.into());
app.data.radarr_data = create_test_radarr_data();
IndexerSettingsHandler::with(
&ESC_KEY,
&mut app,
&ActiveRadarrBlock::IndexerSettingsPrompt,
&None,
)
.handle();
assert_eq!(app.get_current_route(), &ActiveRadarrBlock::Indexers.into());
assert!(!app.data.radarr_data.prompt_confirm);
assert_eq!(app.data.radarr_data.indexer_settings, None);
}
}
mod test_handle_key_char {}
#[test]
fn test_indexer_settings_handler_accepts() {
ActiveRadarrBlock::iter().for_each(|active_radarr_block| {
if INDEXER_SETTINGS_BLOCKS.contains(&active_radarr_block) {
assert!(IndexerSettingsHandler::accepts(&active_radarr_block));
} else {
assert!(!IndexerSettingsHandler::accepts(&active_radarr_block));
}
})
}
}
@@ -0,0 +1,342 @@
#[cfg(test)]
mod tests {
use pretty_assertions::{assert_eq, assert_str_eq};
use rstest::rstest;
use strum::IntoEnumIterator;
use crate::app::key_binding::DEFAULT_KEYBINDINGS;
use crate::app::radarr::{ActiveRadarrBlock, INDEXERS_BLOCKS, INDEXER_SETTINGS_BLOCKS};
use crate::app::App;
use crate::event::Key;
use crate::handlers::radarr_handlers::indexers::IndexersHandler;
use crate::handlers::KeyEventHandler;
use crate::test_handler_delegation;
mod test_handle_scroll_up_and_down {
use rstest::rstest;
use crate::models::radarr_models::Indexer;
use crate::{simple_stateful_iterable_vec, test_iterable_scroll};
use super::*;
test_iterable_scroll!(
test_indexers_scroll,
IndexersHandler,
indexers,
simple_stateful_iterable_vec!(Indexer, String, protocol),
ActiveRadarrBlock::Indexers,
None,
protocol
);
}
mod test_handle_home_end {
use crate::models::radarr_models::Indexer;
use crate::{extended_stateful_iterable_vec, test_iterable_home_and_end};
use super::*;
test_iterable_home_and_end!(
test_indexers_home_end,
IndexersHandler,
indexers,
extended_stateful_iterable_vec!(Indexer, String, protocol),
ActiveRadarrBlock::Indexers,
None,
protocol
);
}
mod test_handle_delete {
use pretty_assertions::assert_eq;
use crate::assert_delete_prompt;
use super::*;
const DELETE_KEY: Key = DEFAULT_KEYBINDINGS.delete.key;
#[test]
fn test_delete_indexer_prompt() {
assert_delete_prompt!(
IndexersHandler,
ActiveRadarrBlock::Indexers,
ActiveRadarrBlock::DeleteIndexerPrompt
);
}
}
mod test_handle_left_right_action {
use pretty_assertions::assert_eq;
use rstest::rstest;
use super::*;
#[test]
fn test_indexers_tab_left() {
let mut app = App::default();
app.data.radarr_data.main_tabs.set_index(4);
IndexersHandler::with(
&DEFAULT_KEYBINDINGS.left.key,
&mut app,
&ActiveRadarrBlock::Indexers,
&None,
)
.handle();
assert_eq!(
app.data.radarr_data.main_tabs.get_active_route(),
&ActiveRadarrBlock::RootFolders.into()
);
assert_eq!(
app.get_current_route(),
&ActiveRadarrBlock::RootFolders.into()
);
}
#[test]
fn test_indexers_tab_right() {
let mut app = App::default();
app.data.radarr_data.main_tabs.set_index(4);
IndexersHandler::with(
&DEFAULT_KEYBINDINGS.right.key,
&mut app,
&ActiveRadarrBlock::Indexers,
&None,
)
.handle();
assert_eq!(
app.data.radarr_data.main_tabs.get_active_route(),
&ActiveRadarrBlock::System.into()
);
assert_eq!(app.get_current_route(), &ActiveRadarrBlock::System.into());
}
#[rstest]
fn test_left_right_delete_indexer_prompt_toggle(
#[values(DEFAULT_KEYBINDINGS.left.key, DEFAULT_KEYBINDINGS.right.key)] key: Key,
) {
let mut app = App::default();
IndexersHandler::with(
&key,
&mut app,
&ActiveRadarrBlock::DeleteIndexerPrompt,
&None,
)
.handle();
assert!(app.data.radarr_data.prompt_confirm);
IndexersHandler::with(
&key,
&mut app,
&ActiveRadarrBlock::DeleteIndexerPrompt,
&None,
)
.handle();
assert!(!app.data.radarr_data.prompt_confirm);
}
}
mod test_handle_submit {
use pretty_assertions::assert_eq;
use crate::network::radarr_network::RadarrEvent;
use super::*;
const SUBMIT_KEY: Key = DEFAULT_KEYBINDINGS.submit.key;
#[test]
fn test_indexer_submit_aka_edit() {
let mut app = App::default();
IndexersHandler::with(&SUBMIT_KEY, &mut app, &ActiveRadarrBlock::Indexers, &None).handle();
assert_eq!(
app.get_current_route(),
&ActiveRadarrBlock::EditIndexer.into()
);
}
#[test]
fn test_delete_indexer_prompt_confirm_submit() {
let mut app = App::default();
app.data.radarr_data.prompt_confirm = true;
app.push_navigation_stack(ActiveRadarrBlock::Indexers.into());
app.push_navigation_stack(ActiveRadarrBlock::DeleteIndexerPrompt.into());
IndexersHandler::with(
&SUBMIT_KEY,
&mut app,
&ActiveRadarrBlock::DeleteIndexerPrompt,
&None,
)
.handle();
assert!(app.data.radarr_data.prompt_confirm);
assert_eq!(
app.data.radarr_data.prompt_confirm_action,
Some(RadarrEvent::DeleteIndexer)
);
assert_eq!(app.get_current_route(), &ActiveRadarrBlock::Indexers.into());
}
#[test]
fn test_prompt_decline_submit() {
let mut app = App::default();
app.push_navigation_stack(ActiveRadarrBlock::Indexers.into());
app.push_navigation_stack(ActiveRadarrBlock::DeleteIndexerPrompt.into());
IndexersHandler::with(
&SUBMIT_KEY,
&mut app,
&ActiveRadarrBlock::DeleteIndexerPrompt,
&None,
)
.handle();
assert!(!app.data.radarr_data.prompt_confirm);
assert_eq!(app.data.radarr_data.prompt_confirm_action, None);
assert_eq!(app.get_current_route(), &ActiveRadarrBlock::Indexers.into());
}
}
mod test_handle_esc {
use pretty_assertions::assert_eq;
use super::*;
const ESC_KEY: Key = DEFAULT_KEYBINDINGS.esc.key;
#[test]
fn test_delete_indexer_prompt_block_esc() {
let mut app = App::default();
app.push_navigation_stack(ActiveRadarrBlock::Indexers.into());
app.push_navigation_stack(ActiveRadarrBlock::DeleteIndexerPrompt.into());
app.data.radarr_data.prompt_confirm = true;
IndexersHandler::with(
&ESC_KEY,
&mut app,
&ActiveRadarrBlock::DeleteIndexerPrompt,
&None,
)
.handle();
assert_eq!(app.get_current_route(), &ActiveRadarrBlock::Indexers.into());
assert!(!app.data.radarr_data.prompt_confirm);
}
#[test]
fn test_default_esc() {
let mut app = App::default();
app.error = "test error".to_owned().into();
app.push_navigation_stack(ActiveRadarrBlock::Indexers.into());
app.push_navigation_stack(ActiveRadarrBlock::Indexers.into());
IndexersHandler::with(&ESC_KEY, &mut app, &ActiveRadarrBlock::Indexers, &None).handle();
assert_eq!(app.get_current_route(), &ActiveRadarrBlock::Indexers.into());
assert!(app.error.text.is_empty());
}
}
mod test_handle_key_char {
use pretty_assertions::assert_eq;
use crate::app::radarr::INDEXER_SETTINGS_SELECTION_BLOCKS;
use crate::assert_refresh_key;
use super::*;
#[test]
fn test_indexer_add() {
let mut app = App::default();
IndexersHandler::with(
&DEFAULT_KEYBINDINGS.add.key,
&mut app,
&ActiveRadarrBlock::Indexers,
&None,
)
.handle();
assert_eq!(
app.get_current_route(),
&ActiveRadarrBlock::AddIndexer.into()
);
}
#[test]
fn test_refresh_indexers_key() {
assert_refresh_key!(IndexersHandler, ActiveRadarrBlock::Indexers);
}
#[test]
fn test_indexer_settings_key() {
let mut app = App::default();
IndexersHandler::with(
&DEFAULT_KEYBINDINGS.settings.key,
&mut app,
&ActiveRadarrBlock::Indexers,
&None,
)
.handle();
assert_eq!(
app.get_current_route(),
&ActiveRadarrBlock::IndexerSettingsPrompt.into()
);
assert_eq!(
app.data.radarr_data.selected_block.blocks,
&INDEXER_SETTINGS_SELECTION_BLOCKS
);
}
}
#[rstest]
fn test_delegates_indexer_settings_blocks_to_indexer_settings_handler(
#[values(
ActiveRadarrBlock::IndexerSettingsPrompt,
ActiveRadarrBlock::IndexerSettingsAvailabilityDelayInput,
ActiveRadarrBlock::IndexerSettingsConfirmPrompt,
ActiveRadarrBlock::IndexerSettingsMaximumSizeInput,
ActiveRadarrBlock::IndexerSettingsMinimumAgeInput,
ActiveRadarrBlock::IndexerSettingsRetentionInput,
ActiveRadarrBlock::IndexerSettingsRssSyncIntervalInput,
ActiveRadarrBlock::IndexerSettingsToggleAllowHardcodedSubs,
ActiveRadarrBlock::IndexerSettingsTogglePreferIndexerFlags,
ActiveRadarrBlock::IndexerSettingsWhitelistedSubtitleTagsInput
)]
active_radarr_block: ActiveRadarrBlock,
) {
test_handler_delegation!(
IndexersHandler,
ActiveRadarrBlock::Indexers,
active_radarr_block
);
}
#[test]
fn test_indexers_handler_accepts() {
let mut indexers_blocks = Vec::new();
indexers_blocks.extend(INDEXERS_BLOCKS);
indexers_blocks.extend(INDEXER_SETTINGS_BLOCKS);
ActiveRadarrBlock::iter().for_each(|active_radarr_block| {
if indexers_blocks.contains(&active_radarr_block) {
assert!(IndexersHandler::accepts(&active_radarr_block));
} else {
assert!(!IndexersHandler::accepts(&active_radarr_block));
}
})
}
}
@@ -0,0 +1,148 @@
use crate::app::key_binding::DEFAULT_KEYBINDINGS;
use crate::app::radarr::{ActiveRadarrBlock, INDEXERS_BLOCKS, INDEXER_SETTINGS_SELECTION_BLOCKS};
use crate::app::App;
use crate::event::Key;
use crate::handlers::radarr_handlers::handle_change_tab_left_right_keys;
use crate::handlers::radarr_handlers::indexers::edit_indexer_settings_handler::IndexerSettingsHandler;
use crate::handlers::{handle_clear_errors, handle_prompt_toggle, KeyEventHandler};
use crate::models::{BlockSelectionState, Scrollable};
use crate::network::radarr_network::RadarrEvent;
mod edit_indexer_settings_handler;
#[cfg(test)]
#[path = "indexers_handler_tests.rs"]
mod indexers_handler_tests;
pub(super) struct IndexersHandler<'a, 'b> {
key: &'a Key,
app: &'a mut App<'b>,
active_radarr_block: &'a ActiveRadarrBlock,
context: &'a Option<ActiveRadarrBlock>,
}
impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for IndexersHandler<'a, 'b> {
fn handle(&mut self) {
match self.active_radarr_block {
_ if IndexerSettingsHandler::accepts(self.active_radarr_block) => {
IndexerSettingsHandler::with(self.key, self.app, self.active_radarr_block, self.context)
.handle()
}
_ => self.handle_key_event(),
}
}
fn accepts(active_block: &'a ActiveRadarrBlock) -> bool {
IndexerSettingsHandler::accepts(active_block) || INDEXERS_BLOCKS.contains(active_block)
}
fn with(
key: &'a Key,
app: &'a mut App<'b>,
active_block: &'a ActiveRadarrBlock,
context: &'a Option<ActiveRadarrBlock>,
) -> IndexersHandler<'a, 'b> {
IndexersHandler {
key,
app,
active_radarr_block: active_block,
context,
}
}
fn get_key(&self) -> &Key {
self.key
}
fn handle_scroll_up(&mut self) {
if self.active_radarr_block == &ActiveRadarrBlock::Indexers {
self.app.data.radarr_data.indexers.scroll_up();
}
}
fn handle_scroll_down(&mut self) {
if self.active_radarr_block == &ActiveRadarrBlock::Indexers {
self.app.data.radarr_data.indexers.scroll_down();
}
}
fn handle_home(&mut self) {
if self.active_radarr_block == &ActiveRadarrBlock::Indexers {
self.app.data.radarr_data.indexers.scroll_to_top();
}
}
fn handle_end(&mut self) {
if self.active_radarr_block == &ActiveRadarrBlock::Indexers {
self.app.data.radarr_data.indexers.scroll_to_bottom();
}
}
fn handle_delete(&mut self) {
if self.active_radarr_block == &ActiveRadarrBlock::Indexers {
self
.app
.push_navigation_stack(ActiveRadarrBlock::DeleteIndexerPrompt.into());
}
}
fn handle_left_right_action(&mut self) {
match self.active_radarr_block {
ActiveRadarrBlock::Indexers => handle_change_tab_left_right_keys(self.app, self.key),
ActiveRadarrBlock::DeleteIndexerPrompt => handle_prompt_toggle(self.app, self.key),
_ => (),
}
}
fn handle_submit(&mut self) {
match self.active_radarr_block {
ActiveRadarrBlock::DeleteIndexerPrompt => {
if self.app.data.radarr_data.prompt_confirm {
self.app.data.radarr_data.prompt_confirm_action = Some(RadarrEvent::DeleteIndexer);
}
self.app.pop_navigation_stack();
}
ActiveRadarrBlock::Indexers => {
self
.app
.push_navigation_stack(ActiveRadarrBlock::EditIndexer.into());
}
_ => (),
}
}
fn handle_esc(&mut self) {
match self.active_radarr_block {
ActiveRadarrBlock::DeleteIndexerPrompt => {
self.app.pop_navigation_stack();
self.app.data.radarr_data.prompt_confirm = false;
}
_ => handle_clear_errors(self.app),
}
}
fn handle_char_key_event(&mut self) {
let key = self.key;
if self.active_radarr_block == &ActiveRadarrBlock::Indexers {
match self.key {
_ if *key == DEFAULT_KEYBINDINGS.add.key => {
self
.app
.push_navigation_stack(ActiveRadarrBlock::AddIndexer.into());
}
_ if *key == DEFAULT_KEYBINDINGS.refresh.key => {
self.app.should_refresh = true;
}
_ if *key == DEFAULT_KEYBINDINGS.settings.key => {
self
.app
.push_navigation_stack(ActiveRadarrBlock::IndexerSettingsPrompt.into());
self.app.data.radarr_data.selected_block =
BlockSelectionState::new(&INDEXER_SETTINGS_SELECTION_BLOCKS);
}
_ => (),
}
}
}
}
@@ -1,5 +1,5 @@
use crate::app::key_binding::DEFAULT_KEYBINDINGS;
use crate::app::radarr::{ActiveRadarrBlock, ADD_MOVIE_SELECTION_BLOCKS};
use crate::app::radarr::{ActiveRadarrBlock, ADD_MOVIE_BLOCKS, ADD_MOVIE_SELECTION_BLOCKS};
use crate::handlers::{handle_prompt_toggle, KeyEventHandler};
use crate::models::{BlockSelectionState, Scrollable, StatefulTable};
use crate::network::radarr_network::RadarrEvent;
@@ -17,6 +17,10 @@ pub(super) struct AddMovieHandler<'a, 'b> {
}
impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for AddMovieHandler<'a, 'b> {
fn accepts(active_block: &'a ActiveRadarrBlock) -> bool {
ADD_MOVIE_BLOCKS.contains(active_block)
}
fn with(
key: &'a Key,
app: &'a mut App<'b>,
@@ -1,12 +1,13 @@
#[cfg(test)]
mod tests {
use pretty_assertions::assert_str_eq;
use strum::IntoEnumIterator;
use crate::app::key_binding::DEFAULT_KEYBINDINGS;
use crate::app::radarr::ActiveRadarrBlock;
use crate::app::radarr::{ActiveRadarrBlock, ADD_MOVIE_BLOCKS};
use crate::app::App;
use crate::event::Key;
use crate::handlers::radarr_handlers::add_movie_handler::AddMovieHandler;
use crate::handlers::radarr_handlers::library::add_movie_handler::AddMovieHandler;
use crate::handlers::KeyEventHandler;
use crate::models::radarr_models::{
AddMovieSearchResult, MinimumAvailability, Monitor, RootFolder,
@@ -724,4 +725,15 @@ mod tests {
assert_str_eq!(app.data.radarr_data.edit_tags.text, "h");
}
}
#[test]
fn test_add_movie_handler_accepts() {
ActiveRadarrBlock::iter().for_each(|active_radarr_block| {
if ADD_MOVIE_BLOCKS.contains(&active_radarr_block) {
assert!(AddMovieHandler::accepts(&active_radarr_block));
} else {
assert!(!AddMovieHandler::accepts(&active_radarr_block));
}
});
}
}
@@ -1,4 +1,4 @@
use crate::app::radarr::ActiveRadarrBlock;
use crate::app::radarr::{ActiveRadarrBlock, DELETE_MOVIE_BLOCKS};
use crate::app::App;
use crate::event::Key;
use crate::handlers::{handle_prompt_toggle, KeyEventHandler};
@@ -16,6 +16,10 @@ pub(super) struct DeleteMovieHandler<'a, 'b> {
}
impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for DeleteMovieHandler<'a, 'b> {
fn accepts(active_block: &'a ActiveRadarrBlock) -> bool {
DELETE_MOVIE_BLOCKS.contains(active_block)
}
fn with(
key: &'a Key,
app: &'a mut App<'b>,
@@ -1,10 +1,12 @@
#[cfg(test)]
mod tests {
use strum::IntoEnumIterator;
use crate::app::key_binding::DEFAULT_KEYBINDINGS;
use crate::app::radarr::ActiveRadarrBlock;
use crate::app::radarr::{ActiveRadarrBlock, DELETE_MOVIE_BLOCKS};
use crate::app::App;
use crate::event::Key;
use crate::handlers::radarr_handlers::delete_movie_handler::DeleteMovieHandler;
use crate::handlers::radarr_handlers::library::delete_movie_handler::DeleteMovieHandler;
use crate::handlers::KeyEventHandler;
mod test_handle_scroll_up_and_down {
@@ -197,4 +199,15 @@ mod tests {
assert!(!app.data.radarr_data.add_list_exclusion);
}
}
#[test]
fn test_delete_movie_handler_accepts() {
ActiveRadarrBlock::iter().for_each(|active_radarr_block| {
if DELETE_MOVIE_BLOCKS.contains(&active_radarr_block) {
assert!(DeleteMovieHandler::accepts(&active_radarr_block));
} else {
assert!(!DeleteMovieHandler::accepts(&active_radarr_block));
}
});
}
}
@@ -1,5 +1,5 @@
use crate::app::key_binding::DEFAULT_KEYBINDINGS;
use crate::app::radarr::ActiveRadarrBlock;
use crate::app::radarr::{ActiveRadarrBlock, EDIT_MOVIE_BLOCKS};
use crate::app::App;
use crate::event::Key;
use crate::handlers::{handle_prompt_toggle, KeyEventHandler};
@@ -19,6 +19,10 @@ pub(super) struct EditMovieHandler<'a, 'b> {
}
impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for EditMovieHandler<'a, 'b> {
fn accepts(active_block: &'a ActiveRadarrBlock) -> bool {
EDIT_MOVIE_BLOCKS.contains(active_block)
}
fn with(
key: &'a Key,
app: &'a mut App<'b>,
@@ -1,12 +1,13 @@
#[cfg(test)]
mod tests {
use pretty_assertions::assert_str_eq;
use strum::IntoEnumIterator;
use crate::app::key_binding::DEFAULT_KEYBINDINGS;
use crate::app::radarr::ActiveRadarrBlock;
use crate::app::radarr::{ActiveRadarrBlock, EDIT_MOVIE_BLOCKS};
use crate::app::App;
use crate::event::Key;
use crate::handlers::radarr_handlers::edit_movie_handler::EditMovieHandler;
use crate::handlers::radarr_handlers::library::edit_movie_handler::EditMovieHandler;
use crate::handlers::KeyEventHandler;
use crate::models::radarr_models::MinimumAvailability;
@@ -503,4 +504,15 @@ mod tests {
assert_str_eq!(app.data.radarr_data.edit_tags.text, "h");
}
}
#[test]
fn test_edit_movie_handler_accepts() {
ActiveRadarrBlock::iter().for_each(|active_radarr_block| {
if EDIT_MOVIE_BLOCKS.contains(&active_radarr_block) {
assert!(EditMovieHandler::accepts(&active_radarr_block));
} else {
assert!(!EditMovieHandler::accepts(&active_radarr_block));
}
});
}
}
@@ -0,0 +1,699 @@
#[cfg(test)]
mod tests {
use pretty_assertions::assert_str_eq;
use rstest::rstest;
use strum::IntoEnumIterator;
use crate::app::key_binding::DEFAULT_KEYBINDINGS;
use crate::app::radarr::{
ActiveRadarrBlock, ADD_MOVIE_BLOCKS, DELETE_MOVIE_BLOCKS, EDIT_MOVIE_BLOCKS, LIBRARY_BLOCKS,
MOVIE_DETAILS_BLOCKS,
};
use crate::app::App;
use crate::event::Key;
use crate::handlers::radarr_handlers::library::LibraryHandler;
use crate::handlers::KeyEventHandler;
use crate::models::radarr_models::Movie;
use crate::models::HorizontallyScrollableText;
use crate::test_handler_delegation;
mod test_handle_scroll_up_and_down {
use crate::{simple_stateful_iterable_vec, test_iterable_scroll};
use super::*;
test_iterable_scroll!(
test_movies_scroll,
LibraryHandler,
movies,
simple_stateful_iterable_vec!(Movie, HorizontallyScrollableText),
ActiveRadarrBlock::Movies,
None,
title,
to_string
);
test_iterable_scroll!(
test_filtered_movies_scroll,
LibraryHandler,
filtered_movies,
simple_stateful_iterable_vec!(Movie, HorizontallyScrollableText),
ActiveRadarrBlock::Movies,
None,
title,
to_string
);
}
mod test_handle_home_end {
use pretty_assertions::assert_eq;
use crate::{
extended_stateful_iterable_vec, test_iterable_home_and_end, test_text_box_home_end_keys,
};
use super::*;
test_iterable_home_and_end!(
test_movies_home_end,
LibraryHandler,
movies,
extended_stateful_iterable_vec!(Movie, HorizontallyScrollableText),
ActiveRadarrBlock::Movies,
None,
title,
to_string
);
test_iterable_home_and_end!(
test_filtered_movies_home_end,
LibraryHandler,
filtered_movies,
extended_stateful_iterable_vec!(Movie, HorizontallyScrollableText),
ActiveRadarrBlock::Movies,
None,
title,
to_string
);
#[test]
fn test_movie_search_box_home_end_keys() {
test_text_box_home_end_keys!(LibraryHandler, ActiveRadarrBlock::SearchMovie, search);
}
#[test]
fn test_movie_filter_box_home_end_keys() {
test_text_box_home_end_keys!(LibraryHandler, ActiveRadarrBlock::FilterMovies, filter);
}
}
mod test_handle_delete {
use pretty_assertions::assert_eq;
use crate::app::radarr::DELETE_MOVIE_SELECTION_BLOCKS;
use crate::assert_delete_prompt;
use super::*;
const DELETE_KEY: Key = DEFAULT_KEYBINDINGS.delete.key;
#[test]
fn test_movies_delete() {
let mut app = App::default();
assert_delete_prompt!(
LibraryHandler,
app,
ActiveRadarrBlock::Movies,
ActiveRadarrBlock::DeleteMoviePrompt
);
assert_eq!(
app.data.radarr_data.selected_block.blocks,
&DELETE_MOVIE_SELECTION_BLOCKS
);
}
}
mod test_handle_left_right_action {
use pretty_assertions::assert_eq;
use rstest::rstest;
use crate::test_text_box_left_right_keys;
use super::*;
#[test]
fn test_movie_tab_left() {
let mut app = App::default();
app.data.radarr_data.main_tabs.set_index(0);
LibraryHandler::with(
&DEFAULT_KEYBINDINGS.left.key,
&mut app,
&ActiveRadarrBlock::Movies,
&None,
)
.handle();
assert_eq!(
app.data.radarr_data.main_tabs.get_active_route(),
&ActiveRadarrBlock::System.into()
);
assert_eq!(app.get_current_route(), &ActiveRadarrBlock::System.into());
}
#[test]
fn test_movie_tab_right() {
let mut app = App::default();
app.data.radarr_data.main_tabs.set_index(0);
LibraryHandler::with(
&DEFAULT_KEYBINDINGS.right.key,
&mut app,
&ActiveRadarrBlock::Movies,
&None,
)
.handle();
assert_eq!(
app.data.radarr_data.main_tabs.get_active_route(),
&ActiveRadarrBlock::Downloads.into()
);
assert_eq!(
app.get_current_route(),
&ActiveRadarrBlock::Downloads.into()
);
}
#[rstest]
fn test_left_right_update_all_movies_prompt_toggle(
#[values(DEFAULT_KEYBINDINGS.left.key, DEFAULT_KEYBINDINGS.right.key)] key: Key,
) {
let mut app = App::default();
LibraryHandler::with(
&key,
&mut app,
&ActiveRadarrBlock::UpdateAllMoviesPrompt,
&None,
)
.handle();
assert!(app.data.radarr_data.prompt_confirm);
LibraryHandler::with(
&key,
&mut app,
&ActiveRadarrBlock::UpdateAllMoviesPrompt,
&None,
)
.handle();
assert!(!app.data.radarr_data.prompt_confirm);
}
#[test]
fn test_movie_search_box_left_right_keys() {
test_text_box_left_right_keys!(LibraryHandler, ActiveRadarrBlock::SearchMovie, search);
}
#[test]
fn test_movie_filter_box_left_right_keys() {
test_text_box_left_right_keys!(LibraryHandler, ActiveRadarrBlock::FilterMovies, filter);
}
}
mod test_handle_submit {
use pretty_assertions::assert_eq;
use crate::extended_stateful_iterable_vec;
use crate::network::radarr_network::RadarrEvent;
use super::*;
const SUBMIT_KEY: Key = DEFAULT_KEYBINDINGS.submit.key;
#[test]
fn test_movie_details_submit() {
let mut app = App::default();
LibraryHandler::with(&SUBMIT_KEY, &mut app, &ActiveRadarrBlock::Movies, &None).handle();
assert_eq!(
app.get_current_route(),
&ActiveRadarrBlock::MovieDetails.into()
);
}
#[test]
fn test_search_movie_submit() {
let mut app = App::default();
app
.data
.radarr_data
.movies
.set_items(extended_stateful_iterable_vec!(
Movie,
HorizontallyScrollableText
));
app.data.radarr_data.search = "Test 2".to_owned().into();
LibraryHandler::with(
&SUBMIT_KEY,
&mut app,
&ActiveRadarrBlock::SearchMovie,
&None,
)
.handle();
assert_str_eq!(
app.data.radarr_data.movies.current_selection().title.text,
"Test 2"
);
}
#[test]
fn test_search_filtered_movies_submit() {
let mut app = App::default();
app
.data
.radarr_data
.filtered_movies
.set_items(extended_stateful_iterable_vec!(
Movie,
HorizontallyScrollableText
));
app.data.radarr_data.search = "Test 2".to_owned().into();
LibraryHandler::with(
&SUBMIT_KEY,
&mut app,
&ActiveRadarrBlock::SearchMovie,
&None,
)
.handle();
assert_str_eq!(
app
.data
.radarr_data
.filtered_movies
.current_selection()
.title
.text,
"Test 2"
);
}
#[test]
fn test_filter_movies_submit() {
let mut app = App::default();
app
.data
.radarr_data
.movies
.set_items(extended_stateful_iterable_vec!(
Movie,
HorizontallyScrollableText
));
app.data.radarr_data.filter = "Test".to_owned().into();
LibraryHandler::with(
&SUBMIT_KEY,
&mut app,
&ActiveRadarrBlock::FilterMovies,
&None,
)
.handle();
assert_eq!(app.data.radarr_data.filtered_movies.items.len(), 3);
assert_str_eq!(
app
.data
.radarr_data
.filtered_movies
.current_selection()
.title
.text,
"Test 1"
);
}
#[test]
fn test_update_all_movies_prompt_confirm_submit() {
let mut app = App::default();
app.data.radarr_data.prompt_confirm = true;
app.push_navigation_stack(ActiveRadarrBlock::Movies.into());
app.push_navigation_stack(ActiveRadarrBlock::UpdateAllMoviesPrompt.into());
LibraryHandler::with(
&SUBMIT_KEY,
&mut app,
&ActiveRadarrBlock::UpdateAllMoviesPrompt,
&None,
)
.handle();
assert!(app.data.radarr_data.prompt_confirm);
assert_eq!(
app.data.radarr_data.prompt_confirm_action,
Some(RadarrEvent::UpdateAllMovies)
);
assert_eq!(app.get_current_route(), &ActiveRadarrBlock::Movies.into());
}
#[test]
fn test_update_all_movies_prompt_decline_submit() {
let mut app = App::default();
app.push_navigation_stack(ActiveRadarrBlock::Movies.into());
app.push_navigation_stack(ActiveRadarrBlock::UpdateAllMoviesPrompt.into());
LibraryHandler::with(
&SUBMIT_KEY,
&mut app,
&ActiveRadarrBlock::UpdateAllMoviesPrompt,
&None,
)
.handle();
assert!(!app.data.radarr_data.prompt_confirm);
assert_eq!(app.data.radarr_data.prompt_confirm_action, None);
assert_eq!(app.get_current_route(), &ActiveRadarrBlock::Movies.into());
}
}
mod test_handle_esc {
use pretty_assertions::assert_eq;
use crate::app::radarr::radarr_test_utils::utils::create_test_radarr_data;
use crate::{assert_filter_reset, assert_search_reset};
use super::*;
const ESC_KEY: Key = DEFAULT_KEYBINDINGS.esc.key;
#[test]
fn test_search_movie_block_esc() {
let mut app = App::default();
app.should_ignore_quit_key = true;
app.push_navigation_stack(ActiveRadarrBlock::Movies.into());
app.push_navigation_stack(ActiveRadarrBlock::SearchMovie.into());
app.data.radarr_data = create_test_radarr_data();
LibraryHandler::with(&ESC_KEY, &mut app, &ActiveRadarrBlock::SearchMovie, &None).handle();
assert_eq!(app.get_current_route(), &ActiveRadarrBlock::Movies.into());
assert!(!app.should_ignore_quit_key);
assert_search_reset!(app.data.radarr_data);
}
#[test]
fn test_filter_movies_block_esc() {
let mut app = App::default();
app.should_ignore_quit_key = true;
app.push_navigation_stack(ActiveRadarrBlock::Movies.into());
app.push_navigation_stack(ActiveRadarrBlock::FilterMovies.into());
app.data.radarr_data = create_test_radarr_data();
LibraryHandler::with(&ESC_KEY, &mut app, &ActiveRadarrBlock::FilterMovies, &None).handle();
assert_eq!(app.get_current_route(), &ActiveRadarrBlock::Movies.into());
assert!(!app.should_ignore_quit_key);
assert_filter_reset!(app.data.radarr_data);
}
#[test]
fn test_update_all_movies_prompt_blocks_esc() {
let mut app = App::default();
app.push_navigation_stack(ActiveRadarrBlock::Movies.into());
app.push_navigation_stack(ActiveRadarrBlock::UpdateAllMoviesPrompt.into());
app.data.radarr_data.prompt_confirm = true;
LibraryHandler::with(
&ESC_KEY,
&mut app,
&ActiveRadarrBlock::UpdateAllMoviesPrompt,
&None,
)
.handle();
assert_eq!(app.get_current_route(), &ActiveRadarrBlock::Movies.into());
assert!(!app.data.radarr_data.prompt_confirm);
}
#[test]
fn test_default_esc() {
let mut app = App::default();
app.error = "test error".to_owned().into();
app.push_navigation_stack(ActiveRadarrBlock::Movies.into());
app.push_navigation_stack(ActiveRadarrBlock::Movies.into());
app.data.radarr_data = create_test_radarr_data();
LibraryHandler::with(&ESC_KEY, &mut app, &ActiveRadarrBlock::Movies, &None).handle();
assert_eq!(app.get_current_route(), &ActiveRadarrBlock::Movies.into());
assert!(app.error.text.is_empty());
assert_search_reset!(app.data.radarr_data);
assert_filter_reset!(app.data.radarr_data);
}
}
mod test_handle_key_char {
use bimap::BiMap;
use pretty_assertions::{assert_eq, assert_str_eq};
use serde_json::Number;
use strum::IntoEnumIterator;
use crate::app::radarr::radarr_test_utils::utils::create_test_radarr_data;
use crate::app::radarr::RadarrData;
use crate::app::radarr::EDIT_MOVIE_SELECTION_BLOCKS;
use crate::models::radarr_models::MinimumAvailability;
use crate::models::HorizontallyScrollableText;
use crate::models::StatefulTable;
use crate::{assert_refresh_key, test_edit_movie_key};
use super::*;
#[test]
fn test_search_movies_key() {
let mut app = App::default();
LibraryHandler::with(
&DEFAULT_KEYBINDINGS.search.key,
&mut app,
&ActiveRadarrBlock::Movies,
&None,
)
.handle();
assert_eq!(
app.get_current_route(),
&ActiveRadarrBlock::SearchMovie.into()
);
assert!(app.data.radarr_data.is_searching);
assert!(app.should_ignore_quit_key);
}
#[test]
fn test_filter_movies_key() {
let mut app = App::default();
LibraryHandler::with(
&DEFAULT_KEYBINDINGS.filter.key,
&mut app,
&ActiveRadarrBlock::Movies,
&None,
)
.handle();
assert_eq!(
app.get_current_route(),
&ActiveRadarrBlock::FilterMovies.into()
);
assert!(app.data.radarr_data.is_filtering);
assert!(app.should_ignore_quit_key);
}
#[test]
fn test_movie_add() {
let mut app = App::default();
LibraryHandler::with(
&DEFAULT_KEYBINDINGS.add.key,
&mut app,
&ActiveRadarrBlock::Movies,
&None,
)
.handle();
assert_eq!(
app.get_current_route(),
&ActiveRadarrBlock::AddMovieSearchInput.into()
);
assert!(app.should_ignore_quit_key);
}
#[test]
fn test_movie_edit_key() {
test_edit_movie_key!(
LibraryHandler,
ActiveRadarrBlock::Movies,
ActiveRadarrBlock::Movies
);
}
#[test]
fn test_update_all_movies_key() {
let mut app = App::default();
LibraryHandler::with(
&DEFAULT_KEYBINDINGS.update.key,
&mut app,
&ActiveRadarrBlock::Movies,
&None,
)
.handle();
assert_eq!(
app.get_current_route(),
&ActiveRadarrBlock::UpdateAllMoviesPrompt.into()
);
}
#[test]
fn test_refresh_movies_key() {
assert_refresh_key!(LibraryHandler, ActiveRadarrBlock::Movies);
}
#[test]
fn test_search_movies_box_backspace_key() {
let mut app = App::default();
app.data.radarr_data.search = "Test".to_owned().into();
LibraryHandler::with(
&DEFAULT_KEYBINDINGS.backspace.key,
&mut app,
&ActiveRadarrBlock::SearchMovie,
&None,
)
.handle();
assert_str_eq!(app.data.radarr_data.search.text, "Tes");
}
#[test]
fn test_filter_movies_box_backspace_key() {
let mut app = App::default();
app.data.radarr_data.filter = "Test".to_owned().into();
LibraryHandler::with(
&DEFAULT_KEYBINDINGS.backspace.key,
&mut app,
&ActiveRadarrBlock::FilterMovies,
&None,
)
.handle();
assert_str_eq!(app.data.radarr_data.filter.text, "Tes");
}
#[test]
fn test_search_movies_box_char_key() {
let mut app = App::default();
LibraryHandler::with(
&Key::Char('h'),
&mut app,
&ActiveRadarrBlock::SearchMovie,
&None,
)
.handle();
assert_str_eq!(app.data.radarr_data.search.text, "h");
}
#[test]
fn test_filter_movies_box_char_key() {
let mut app = App::default();
LibraryHandler::with(
&Key::Char('h'),
&mut app,
&ActiveRadarrBlock::FilterMovies,
&None,
)
.handle();
assert_str_eq!(app.data.radarr_data.filter.text, "h");
}
}
#[rstest]
fn test_delegates_add_movie_blocks_to_add_movie_handler(
#[values(
ActiveRadarrBlock::AddMovieSearchInput,
ActiveRadarrBlock::AddMovieSearchResults,
ActiveRadarrBlock::AddMoviePrompt,
ActiveRadarrBlock::AddMovieSelectMonitor,
ActiveRadarrBlock::AddMovieSelectMinimumAvailability,
ActiveRadarrBlock::AddMovieSelectQualityProfile,
ActiveRadarrBlock::AddMovieSelectRootFolder,
ActiveRadarrBlock::AddMovieAlreadyInLibrary,
ActiveRadarrBlock::AddMovieTagsInput
)]
active_radarr_block: ActiveRadarrBlock,
) {
test_handler_delegation!(
LibraryHandler,
ActiveRadarrBlock::Movies,
active_radarr_block
);
}
#[rstest]
fn test_delegates_movie_details_blocks_to_movie_details_handler(
#[values(
ActiveRadarrBlock::MovieDetails,
ActiveRadarrBlock::MovieHistory,
ActiveRadarrBlock::FileInfo,
ActiveRadarrBlock::Cast,
ActiveRadarrBlock::Crew,
ActiveRadarrBlock::AutomaticallySearchMoviePrompt,
ActiveRadarrBlock::UpdateAndScanPrompt,
ActiveRadarrBlock::ManualSearch,
ActiveRadarrBlock::ManualSearchConfirmPrompt
)]
active_radarr_block: ActiveRadarrBlock,
) {
test_handler_delegation!(
LibraryHandler,
ActiveRadarrBlock::Movies,
active_radarr_block
);
}
#[rstest]
fn test_delegates_edit_movie_blocks_to_edit_movie_handler(
#[values(
ActiveRadarrBlock::EditMoviePrompt,
ActiveRadarrBlock::EditMoviePathInput,
ActiveRadarrBlock::EditMovieSelectMinimumAvailability,
ActiveRadarrBlock::EditMovieSelectQualityProfile,
ActiveRadarrBlock::EditMovieTagsInput
)]
active_radarr_block: ActiveRadarrBlock,
) {
test_handler_delegation!(
LibraryHandler,
ActiveRadarrBlock::Movies,
active_radarr_block
);
}
#[test]
fn test_delegates_delete_movie_blocks_to_delete_movie_handler() {
test_handler_delegation!(
LibraryHandler,
ActiveRadarrBlock::Movies,
ActiveRadarrBlock::DeleteMoviePrompt
);
}
#[test]
fn test_library_handler_accepts() {
let mut library_handler_blocks = Vec::new();
library_handler_blocks.extend(LIBRARY_BLOCKS);
library_handler_blocks.extend(ADD_MOVIE_BLOCKS);
library_handler_blocks.extend(DELETE_MOVIE_BLOCKS);
library_handler_blocks.extend(EDIT_MOVIE_BLOCKS);
library_handler_blocks.extend(MOVIE_DETAILS_BLOCKS);
ActiveRadarrBlock::iter().for_each(|active_radarr_block| {
if library_handler_blocks.contains(&active_radarr_block) {
assert!(LibraryHandler::accepts(&active_radarr_block));
} else {
assert!(!LibraryHandler::accepts(&active_radarr_block));
}
});
}
}
+298
View File
@@ -0,0 +1,298 @@
use crate::app::key_binding::DEFAULT_KEYBINDINGS;
use crate::app::radarr::{
ActiveRadarrBlock, DELETE_MOVIE_SELECTION_BLOCKS, EDIT_MOVIE_SELECTION_BLOCKS, LIBRARY_BLOCKS,
};
use crate::app::App;
use crate::event::Key;
use crate::handlers::radarr_handlers::library::add_movie_handler::AddMovieHandler;
use crate::handlers::radarr_handlers::library::delete_movie_handler::DeleteMovieHandler;
use crate::handlers::radarr_handlers::library::edit_movie_handler::EditMovieHandler;
use crate::handlers::radarr_handlers::library::movie_details_handler::MovieDetailsHandler;
use crate::handlers::radarr_handlers::{
filter_table, handle_change_tab_left_right_keys, search_table,
};
use crate::handlers::{handle_clear_errors, handle_prompt_toggle, KeyEventHandler};
use crate::models::{BlockSelectionState, Scrollable};
use crate::network::radarr_network::RadarrEvent;
use crate::{handle_text_box_keys, handle_text_box_left_right_keys};
mod add_movie_handler;
mod delete_movie_handler;
mod edit_movie_handler;
mod movie_details_handler;
#[cfg(test)]
#[path = "library_handler_tests.rs"]
mod library_handler_tests;
pub(super) struct LibraryHandler<'a, 'b> {
key: &'a Key,
app: &'a mut App<'b>,
active_radarr_block: &'a ActiveRadarrBlock,
context: &'a Option<ActiveRadarrBlock>,
}
impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for LibraryHandler<'a, 'b> {
fn handle(&mut self) {
match self.active_radarr_block {
_ if AddMovieHandler::accepts(self.active_radarr_block) => {
AddMovieHandler::with(self.key, self.app, self.active_radarr_block, self.context).handle();
}
_ if DeleteMovieHandler::accepts(self.active_radarr_block) => {
DeleteMovieHandler::with(self.key, self.app, self.active_radarr_block, self.context)
.handle();
}
_ if EditMovieHandler::accepts(self.active_radarr_block) => {
EditMovieHandler::with(self.key, self.app, self.active_radarr_block, self.context).handle();
}
_ if MovieDetailsHandler::accepts(self.active_radarr_block) => {
MovieDetailsHandler::with(self.key, self.app, self.active_radarr_block, self.context)
.handle();
}
_ => self.handle_key_event(),
}
}
fn accepts(active_block: &'a ActiveRadarrBlock) -> bool {
AddMovieHandler::accepts(active_block)
|| DeleteMovieHandler::accepts(active_block)
|| EditMovieHandler::accepts(active_block)
|| MovieDetailsHandler::accepts(active_block)
|| LIBRARY_BLOCKS.contains(active_block)
}
fn with(
key: &'a Key,
app: &'a mut App<'b>,
active_block: &'a ActiveRadarrBlock,
context: &'a Option<ActiveRadarrBlock>,
) -> LibraryHandler<'a, 'b> {
LibraryHandler {
key,
app,
active_radarr_block: active_block,
context,
}
}
fn get_key(&self) -> &Key {
self.key
}
fn handle_scroll_up(&mut self) {
if self.active_radarr_block == &ActiveRadarrBlock::Movies {
if !self.app.data.radarr_data.filtered_movies.items.is_empty() {
self.app.data.radarr_data.filtered_movies.scroll_up();
} else {
self.app.data.radarr_data.movies.scroll_up()
}
}
}
fn handle_scroll_down(&mut self) {
if self.active_radarr_block == &ActiveRadarrBlock::Movies {
if !self.app.data.radarr_data.filtered_movies.items.is_empty() {
self.app.data.radarr_data.filtered_movies.scroll_down();
} else {
self.app.data.radarr_data.movies.scroll_down()
}
}
}
fn handle_home(&mut self) {
match self.active_radarr_block {
ActiveRadarrBlock::Movies => {
if !self.app.data.radarr_data.filtered_movies.items.is_empty() {
self.app.data.radarr_data.filtered_movies.scroll_to_top();
} else {
self.app.data.radarr_data.movies.scroll_to_top()
}
}
ActiveRadarrBlock::SearchMovie => {
self.app.data.radarr_data.search.scroll_home();
}
ActiveRadarrBlock::FilterMovies => {
self.app.data.radarr_data.filter.scroll_home();
}
_ => (),
}
}
fn handle_end(&mut self) {
match self.active_radarr_block {
ActiveRadarrBlock::Movies => {
if !self.app.data.radarr_data.filtered_movies.items.is_empty() {
self.app.data.radarr_data.filtered_movies.scroll_to_bottom();
} else {
self.app.data.radarr_data.movies.scroll_to_bottom()
}
}
ActiveRadarrBlock::SearchMovie => self.app.data.radarr_data.search.reset_offset(),
ActiveRadarrBlock::FilterMovies => self.app.data.radarr_data.filter.reset_offset(),
_ => (),
}
}
fn handle_delete(&mut self) {
if self.active_radarr_block == &ActiveRadarrBlock::Movies {
self
.app
.push_navigation_stack(ActiveRadarrBlock::DeleteMoviePrompt.into());
self.app.data.radarr_data.selected_block =
BlockSelectionState::new(&DELETE_MOVIE_SELECTION_BLOCKS);
}
}
fn handle_left_right_action(&mut self) {
match self.active_radarr_block {
ActiveRadarrBlock::Movies => handle_change_tab_left_right_keys(self.app, self.key),
ActiveRadarrBlock::UpdateAllMoviesPrompt => handle_prompt_toggle(self.app, self.key),
ActiveRadarrBlock::SearchMovie => {
handle_text_box_left_right_keys!(self, self.key, self.app.data.radarr_data.search)
}
ActiveRadarrBlock::FilterMovies => {
handle_text_box_left_right_keys!(self, self.key, self.app.data.radarr_data.filter)
}
_ => (),
}
}
fn handle_submit(&mut self) {
match self.active_radarr_block {
ActiveRadarrBlock::Movies => self
.app
.push_navigation_stack(ActiveRadarrBlock::MovieDetails.into()),
ActiveRadarrBlock::SearchMovie => {
if self.app.data.radarr_data.filtered_movies.items.is_empty() {
let selected_index = search_table(
self.app,
&self.app.data.radarr_data.movies.items.clone(),
|movie| &movie.title.text,
);
self
.app
.data
.radarr_data
.movies
.select_index(selected_index);
} else {
let selected_index = search_table(
self.app,
&self.app.data.radarr_data.filtered_movies.items.clone(),
|movie| &movie.title.text,
);
self
.app
.data
.radarr_data
.filtered_movies
.select_index(selected_index);
};
}
ActiveRadarrBlock::FilterMovies => {
let filtered_movies = filter_table(
self.app,
&self.app.data.radarr_data.movies.items.clone(),
|movie| &movie.title.text,
);
if !filtered_movies.is_empty() {
self
.app
.data
.radarr_data
.filtered_movies
.set_items(filtered_movies);
}
}
ActiveRadarrBlock::UpdateAllMoviesPrompt => {
if self.app.data.radarr_data.prompt_confirm {
self.app.data.radarr_data.prompt_confirm_action = Some(RadarrEvent::UpdateAllMovies);
}
self.app.pop_navigation_stack();
}
_ => (),
}
}
fn handle_esc(&mut self) {
match self.active_radarr_block {
ActiveRadarrBlock::FilterMovies => {
self.app.pop_navigation_stack();
self.app.data.radarr_data.reset_filter();
self.app.should_ignore_quit_key = false;
}
ActiveRadarrBlock::SearchMovie => {
self.app.pop_navigation_stack();
self.app.data.radarr_data.reset_search();
self.app.should_ignore_quit_key = false;
}
ActiveRadarrBlock::UpdateAllMoviesPrompt => {
self.app.pop_navigation_stack();
self.app.data.radarr_data.prompt_confirm = false;
}
_ => {
self.app.data.radarr_data.reset_search();
self.app.data.radarr_data.reset_filter();
handle_clear_errors(self.app);
}
}
}
fn handle_char_key_event(&mut self) {
let key = self.key;
match self.active_radarr_block {
ActiveRadarrBlock::Movies => match self.key {
_ if *key == DEFAULT_KEYBINDINGS.search.key => {
self
.app
.push_navigation_stack(ActiveRadarrBlock::SearchMovie.into());
self.app.data.radarr_data.is_searching = true;
self.app.should_ignore_quit_key = true;
}
_ if *key == DEFAULT_KEYBINDINGS.filter.key => {
self
.app
.push_navigation_stack(ActiveRadarrBlock::FilterMovies.into());
self.app.data.radarr_data.is_filtering = true;
self.app.should_ignore_quit_key = true;
}
_ if *key == DEFAULT_KEYBINDINGS.edit.key => {
self.app.push_navigation_stack(
(
ActiveRadarrBlock::EditMoviePrompt,
Some(ActiveRadarrBlock::Movies),
)
.into(),
);
self.app.data.radarr_data.populate_edit_movie_fields();
self.app.data.radarr_data.selected_block =
BlockSelectionState::new(&EDIT_MOVIE_SELECTION_BLOCKS);
}
_ if *key == DEFAULT_KEYBINDINGS.add.key => {
self
.app
.push_navigation_stack(ActiveRadarrBlock::AddMovieSearchInput.into());
self.app.should_ignore_quit_key = true;
}
_ if *key == DEFAULT_KEYBINDINGS.update.key => {
self
.app
.push_navigation_stack(ActiveRadarrBlock::UpdateAllMoviesPrompt.into());
}
_ if *key == DEFAULT_KEYBINDINGS.refresh.key => {
self.app.should_refresh = true;
}
_ => (),
},
ActiveRadarrBlock::SearchMovie => {
handle_text_box_keys!(self, key, self.app.data.radarr_data.search)
}
ActiveRadarrBlock::FilterMovies => {
handle_text_box_keys!(self, key, self.app.data.radarr_data.filter)
}
_ => (),
}
}
}
@@ -4,7 +4,7 @@ use serde_json::Number;
use strum::IntoEnumIterator;
use crate::app::key_binding::DEFAULT_KEYBINDINGS;
use crate::app::radarr::{ActiveRadarrBlock, EDIT_MOVIE_SELECTION_BLOCKS};
use crate::app::radarr::{ActiveRadarrBlock, EDIT_MOVIE_SELECTION_BLOCKS, MOVIE_DETAILS_BLOCKS};
use crate::app::App;
use crate::event::Key;
use crate::handlers::{handle_prompt_toggle, KeyEventHandler};
@@ -24,6 +24,10 @@ pub(super) struct MovieDetailsHandler<'a, 'b> {
}
impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for MovieDetailsHandler<'a, 'b> {
fn accepts(active_block: &'a ActiveRadarrBlock) -> bool {
MOVIE_DETAILS_BLOCKS.contains(active_block)
}
fn with(
key: &'a Key,
app: &'a mut App<'b>,
@@ -292,7 +296,7 @@ fn sort_releases_by_selected_field(
.cmp(release_b.size.as_u64().as_ref().unwrap())
},
ReleaseField::Peers => |release_a, release_b| {
let default_number = Number::from(i64::MAX);
let default_number = Number::from(i64::max_value());
let seeder_a = release_a
.seeders
.as_ref()
@@ -3,12 +3,13 @@ mod tests {
use pretty_assertions::assert_str_eq;
use rstest::rstest;
use serde_json::Number;
use strum::IntoEnumIterator;
use crate::app::key_binding::DEFAULT_KEYBINDINGS;
use crate::app::radarr::ActiveRadarrBlock;
use crate::app::radarr::{ActiveRadarrBlock, MOVIE_DETAILS_BLOCKS};
use crate::app::App;
use crate::event::Key;
use crate::handlers::radarr_handlers::movie_details_handler::{
use crate::handlers::radarr_handlers::library::movie_details_handler::{
sort_releases_by_selected_field, MovieDetailsHandler,
};
use crate::handlers::KeyEventHandler;
@@ -415,7 +416,6 @@ mod tests {
use crate::app::radarr::RadarrData;
use crate::app::radarr::EDIT_MOVIE_SELECTION_BLOCKS;
use crate::models::radarr_models::{MinimumAvailability, Movie};
use crate::models::BlockSelectionState;
use crate::models::HorizontallyScrollableText;
use crate::models::StatefulTable;
use crate::test_edit_movie_key;
@@ -643,4 +643,15 @@ mod tests {
vec![release_a, release_b, release_c]
}
#[test]
fn test_movie_details_handler_accepts() {
ActiveRadarrBlock::iter().for_each(|active_radarr_block| {
if MOVIE_DETAILS_BLOCKS.contains(&active_radarr_block) {
assert!(MovieDetailsHandler::accepts(&active_radarr_block));
} else {
assert!(!MovieDetailsHandler::accepts(&active_radarr_block));
}
});
}
}
+90 -632
View File
@@ -1,30 +1,21 @@
use crate::app::key_binding::DEFAULT_KEYBINDINGS;
use crate::app::radarr::{
ActiveRadarrBlock, ADD_MOVIE_BLOCKS, COLLECTION_DETAILS_BLOCKS, DELETE_MOVIE_BLOCKS,
DELETE_MOVIE_SELECTION_BLOCKS, EDIT_COLLECTION_BLOCKS, EDIT_COLLECTION_SELECTION_BLOCKS,
EDIT_MOVIE_BLOCKS, EDIT_MOVIE_SELECTION_BLOCKS, FILTER_BLOCKS, MOVIE_DETAILS_BLOCKS,
SEARCH_BLOCKS, SYSTEM_DETAILS_BLOCKS,
};
use crate::handlers::radarr_handlers::add_movie_handler::AddMovieHandler;
use crate::handlers::radarr_handlers::collection_details_handler::CollectionDetailsHandler;
use crate::handlers::radarr_handlers::delete_movie_handler::DeleteMovieHandler;
use crate::handlers::radarr_handlers::edit_collection_handler::EditCollectionHandler;
use crate::handlers::radarr_handlers::edit_movie_handler::EditMovieHandler;
use crate::handlers::radarr_handlers::movie_details_handler::MovieDetailsHandler;
use crate::handlers::radarr_handlers::system_details_handler::SystemDetailsHandler;
use crate::handlers::{handle_clear_errors, handle_prompt_toggle, KeyEventHandler};
use crate::models::{BlockSelectionState, HorizontallyScrollableText, Scrollable};
use crate::network::radarr_network::RadarrEvent;
use crate::app::radarr::ActiveRadarrBlock;
use crate::handlers::radarr_handlers::collections::CollectionsHandler;
use crate::handlers::radarr_handlers::downloads::DownloadsHandler;
use crate::handlers::radarr_handlers::indexers::IndexersHandler;
use crate::handlers::radarr_handlers::library::LibraryHandler;
use crate::handlers::radarr_handlers::root_folders::RootFoldersHandler;
use crate::handlers::radarr_handlers::system::SystemHandler;
use crate::handlers::KeyEventHandler;
use crate::utils::strip_non_search_characters;
use crate::{handle_text_box_keys, handle_text_box_left_right_keys, App, Key};
use crate::{App, Key};
mod add_movie_handler;
mod collection_details_handler;
mod delete_movie_handler;
mod edit_collection_handler;
mod edit_movie_handler;
mod movie_details_handler;
mod system_details_handler;
mod collections;
mod downloads;
mod indexers;
mod library;
mod root_folders;
mod system;
#[cfg(test)]
#[path = "radarr_handler_tests.rs"]
@@ -44,36 +35,34 @@ pub(super) struct RadarrHandler<'a, 'b> {
impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for RadarrHandler<'a, 'b> {
fn handle(&mut self) {
match self.active_radarr_block {
_ if MOVIE_DETAILS_BLOCKS.contains(self.active_radarr_block) => {
MovieDetailsHandler::with(self.key, self.app, self.active_radarr_block, self.context)
_ if LibraryHandler::accepts(self.active_radarr_block) => {
LibraryHandler::with(self.key, self.app, self.active_radarr_block, self.context).handle();
}
_ if CollectionsHandler::accepts(self.active_radarr_block) => {
CollectionsHandler::with(self.key, self.app, self.active_radarr_block, self.context)
.handle()
}
_ if COLLECTION_DETAILS_BLOCKS.contains(self.active_radarr_block) => {
CollectionDetailsHandler::with(self.key, self.app, self.active_radarr_block, self.context)
_ if IndexersHandler::accepts(self.active_radarr_block) => {
IndexersHandler::with(self.key, self.app, self.active_radarr_block, self.context).handle()
}
_ if SystemHandler::accepts(self.active_radarr_block) => {
SystemHandler::with(self.key, self.app, self.active_radarr_block, self.context).handle()
}
_ if DownloadsHandler::accepts(self.active_radarr_block) => {
DownloadsHandler::with(self.key, self.app, self.active_radarr_block, self.context).handle()
}
_ if RootFoldersHandler::accepts(self.active_radarr_block) => {
RootFoldersHandler::with(self.key, self.app, self.active_radarr_block, self.context)
.handle()
}
_ if ADD_MOVIE_BLOCKS.contains(self.active_radarr_block) => {
AddMovieHandler::with(self.key, self.app, self.active_radarr_block, self.context).handle()
}
_ if EDIT_MOVIE_BLOCKS.contains(self.active_radarr_block) => {
EditMovieHandler::with(self.key, self.app, self.active_radarr_block, self.context).handle()
}
_ if DELETE_MOVIE_BLOCKS.contains(self.active_radarr_block) => {
DeleteMovieHandler::with(self.key, self.app, self.active_radarr_block, self.context)
.handle()
}
_ if EDIT_COLLECTION_BLOCKS.contains(self.active_radarr_block) => {
EditCollectionHandler::with(self.key, self.app, self.active_radarr_block, self.context)
.handle()
}
_ if SYSTEM_DETAILS_BLOCKS.contains(self.active_radarr_block) => {
SystemDetailsHandler::with(self.key, self.app, self.active_radarr_block, self.context)
.handle();
}
_ => self.handle_key_event(),
}
}
fn accepts(_active_block: &'a ActiveRadarrBlock) -> bool {
true
}
fn with(
key: &'a Key,
app: &'a mut App<'b>,
@@ -92,608 +81,77 @@ impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for RadarrHandler<'a, 'b
self.key
}
fn handle_scroll_up(&mut self) {
match self.active_radarr_block {
ActiveRadarrBlock::Collections => {
if !self
.app
.data
.radarr_data
.filtered_collections
.items
.is_empty()
{
self.app.data.radarr_data.filtered_collections.scroll_up();
} else {
self.app.data.radarr_data.collections.scroll_up()
}
}
ActiveRadarrBlock::Movies => {
if !self.app.data.radarr_data.filtered_movies.items.is_empty() {
self.app.data.radarr_data.filtered_movies.scroll_up();
} else {
self.app.data.radarr_data.movies.scroll_up()
}
}
ActiveRadarrBlock::Downloads => self.app.data.radarr_data.downloads.scroll_up(),
ActiveRadarrBlock::Indexers => self.app.data.radarr_data.indexers.scroll_up(),
ActiveRadarrBlock::RootFolders => self.app.data.radarr_data.root_folders.scroll_up(),
_ => (),
}
}
fn handle_scroll_up(&mut self) {}
fn handle_scroll_down(&mut self) {
match self.active_radarr_block {
ActiveRadarrBlock::Collections => {
if !self
.app
.data
.radarr_data
.filtered_collections
.items
.is_empty()
{
self.app.data.radarr_data.filtered_collections.scroll_down();
} else {
self.app.data.radarr_data.collections.scroll_down()
}
}
ActiveRadarrBlock::Movies => {
if !self.app.data.radarr_data.filtered_movies.items.is_empty() {
self.app.data.radarr_data.filtered_movies.scroll_down();
} else {
self.app.data.radarr_data.movies.scroll_down()
}
}
ActiveRadarrBlock::Downloads => self.app.data.radarr_data.downloads.scroll_down(),
ActiveRadarrBlock::Indexers => self.app.data.radarr_data.indexers.scroll_down(),
ActiveRadarrBlock::RootFolders => self.app.data.radarr_data.root_folders.scroll_down(),
_ => (),
}
}
fn handle_scroll_down(&mut self) {}
fn handle_home(&mut self) {
match self.active_radarr_block {
ActiveRadarrBlock::Collections => {
if !self
.app
.data
.radarr_data
.filtered_collections
.items
.is_empty()
{
self
.app
.data
.radarr_data
.filtered_collections
.scroll_to_top();
} else {
self.app.data.radarr_data.collections.scroll_to_top()
}
}
ActiveRadarrBlock::Movies => {
if !self.app.data.radarr_data.filtered_movies.items.is_empty() {
self.app.data.radarr_data.filtered_movies.scroll_to_top();
} else {
self.app.data.radarr_data.movies.scroll_to_top()
}
}
ActiveRadarrBlock::Downloads => self.app.data.radarr_data.downloads.scroll_to_top(),
ActiveRadarrBlock::Indexers => self.app.data.radarr_data.indexers.scroll_to_top(),
ActiveRadarrBlock::RootFolders => self.app.data.radarr_data.root_folders.scroll_to_top(),
ActiveRadarrBlock::SearchMovie | ActiveRadarrBlock::SearchCollection => {
self.app.data.radarr_data.search.scroll_home()
}
ActiveRadarrBlock::FilterMovies | ActiveRadarrBlock::FilterCollections => {
self.app.data.radarr_data.filter.scroll_home()
}
ActiveRadarrBlock::AddRootFolderPrompt => self.app.data.radarr_data.edit_path.scroll_home(),
_ => (),
}
}
fn handle_home(&mut self) {}
fn handle_end(&mut self) {
match self.active_radarr_block {
ActiveRadarrBlock::Collections => {
if !self
.app
.data
.radarr_data
.filtered_collections
.items
.is_empty()
{
self
.app
.data
.radarr_data
.filtered_collections
.scroll_to_bottom();
} else {
self.app.data.radarr_data.collections.scroll_to_bottom()
}
}
ActiveRadarrBlock::Movies => {
if !self.app.data.radarr_data.filtered_movies.items.is_empty() {
self.app.data.radarr_data.filtered_movies.scroll_to_bottom();
} else {
self.app.data.radarr_data.movies.scroll_to_bottom()
}
}
ActiveRadarrBlock::Downloads => self.app.data.radarr_data.downloads.scroll_to_bottom(),
ActiveRadarrBlock::Indexers => self.app.data.radarr_data.indexers.scroll_to_bottom(),
ActiveRadarrBlock::RootFolders => self.app.data.radarr_data.root_folders.scroll_to_bottom(),
ActiveRadarrBlock::SearchMovie | ActiveRadarrBlock::SearchCollection => {
self.app.data.radarr_data.search.reset_offset()
}
ActiveRadarrBlock::FilterMovies | ActiveRadarrBlock::FilterCollections => {
self.app.data.radarr_data.filter.reset_offset()
}
ActiveRadarrBlock::AddRootFolderPrompt => self.app.data.radarr_data.edit_path.reset_offset(),
_ => (),
}
}
fn handle_end(&mut self) {}
fn handle_delete(&mut self) {
match self.active_radarr_block {
ActiveRadarrBlock::Movies => {
self
.app
.push_navigation_stack(ActiveRadarrBlock::DeleteMoviePrompt.into());
self.app.data.radarr_data.selected_block =
BlockSelectionState::new(&DELETE_MOVIE_SELECTION_BLOCKS);
}
ActiveRadarrBlock::Downloads => self
.app
.push_navigation_stack(ActiveRadarrBlock::DeleteDownloadPrompt.into()),
ActiveRadarrBlock::RootFolders => self
.app
.push_navigation_stack(ActiveRadarrBlock::DeleteRootFolderPrompt.into()),
ActiveRadarrBlock::Indexers => self
.app
.push_navigation_stack(ActiveRadarrBlock::DeleteIndexerPrompt.into()),
_ => (),
}
}
fn handle_delete(&mut self) {}
fn handle_left_right_action(&mut self) {
match self.active_radarr_block {
ActiveRadarrBlock::Movies
| ActiveRadarrBlock::Downloads
| ActiveRadarrBlock::Collections
| ActiveRadarrBlock::RootFolders
| ActiveRadarrBlock::Indexers
| ActiveRadarrBlock::System => match self.key {
_ if *self.key == DEFAULT_KEYBINDINGS.left.key => {
self.app.data.radarr_data.main_tabs.previous();
self
.app
.pop_and_push_navigation_stack(*self.app.data.radarr_data.main_tabs.get_active_route());
}
_ if *self.key == DEFAULT_KEYBINDINGS.right.key => {
self.app.data.radarr_data.main_tabs.next();
self
.app
.pop_and_push_navigation_stack(*self.app.data.radarr_data.main_tabs.get_active_route());
}
_ => (),
},
ActiveRadarrBlock::DeleteDownloadPrompt
| ActiveRadarrBlock::DeleteIndexerPrompt
| ActiveRadarrBlock::DeleteRootFolderPrompt
| ActiveRadarrBlock::UpdateAllMoviesPrompt
| ActiveRadarrBlock::UpdateAllCollectionsPrompt
| ActiveRadarrBlock::UpdateDownloadsPrompt => handle_prompt_toggle(self.app, self.key),
ActiveRadarrBlock::AddRootFolderPrompt => {
handle_text_box_left_right_keys!(self, self.key, self.app.data.radarr_data.edit_path)
}
ActiveRadarrBlock::SearchMovie | ActiveRadarrBlock::SearchCollection => {
handle_text_box_left_right_keys!(self, self.key, self.app.data.radarr_data.search)
}
ActiveRadarrBlock::FilterMovies | ActiveRadarrBlock::FilterCollections => {
handle_text_box_left_right_keys!(self, self.key, self.app.data.radarr_data.filter)
}
_ => (),
}
}
fn handle_left_right_action(&mut self) {}
fn handle_submit(&mut self) {
match self.active_radarr_block {
ActiveRadarrBlock::Movies => self
.app
.push_navigation_stack(ActiveRadarrBlock::MovieDetails.into()),
ActiveRadarrBlock::Collections => self
.app
.push_navigation_stack(ActiveRadarrBlock::CollectionDetails.into()),
ActiveRadarrBlock::SearchMovie => {
if self.app.data.radarr_data.filtered_movies.items.is_empty() {
let selected_index = self
.search_table(&self.app.data.radarr_data.movies.items.clone(), |movie| {
&movie.title.text
});
self
.app
.data
.radarr_data
.movies
.select_index(selected_index);
} else {
let selected_index = self.search_table(
&self.app.data.radarr_data.filtered_movies.items.clone(),
|movie| &movie.title.text,
);
self
.app
.data
.radarr_data
.filtered_movies
.select_index(selected_index);
};
}
ActiveRadarrBlock::SearchCollection => {
if self
.app
.data
.radarr_data
.filtered_collections
.items
.is_empty()
{
let selected_index = self.search_table(
&self.app.data.radarr_data.collections.items.clone(),
|collection| &collection.title.text,
);
self
.app
.data
.radarr_data
.collections
.select_index(selected_index);
} else {
let selected_index = self.search_table(
&self.app.data.radarr_data.filtered_collections.items.clone(),
|collection| &collection.title.text,
);
self
.app
.data
.radarr_data
.filtered_collections
.select_index(selected_index);
}
}
ActiveRadarrBlock::FilterMovies => {
let filtered_movies = self
.filter_table(&self.app.data.radarr_data.movies.items.clone(), |movie| {
&movie.title.text
});
fn handle_submit(&mut self) {}
if !filtered_movies.is_empty() {
self
.app
.data
.radarr_data
.filtered_movies
.set_items(filtered_movies);
}
}
ActiveRadarrBlock::FilterCollections => {
let filtered_collections = self.filter_table(
&self.app.data.radarr_data.collections.items.clone(),
|collection| &collection.title.text,
);
fn handle_esc(&mut self) {}
if !filtered_collections.is_empty() {
self
.app
.data
.radarr_data
.filtered_collections
.set_items(filtered_collections);
}
}
ActiveRadarrBlock::DeleteDownloadPrompt => {
if self.app.data.radarr_data.prompt_confirm {
self.app.data.radarr_data.prompt_confirm_action = Some(RadarrEvent::DeleteDownload);
}
self.app.pop_navigation_stack();
}
ActiveRadarrBlock::DeleteRootFolderPrompt => {
if self.app.data.radarr_data.prompt_confirm {
self.app.data.radarr_data.prompt_confirm_action = Some(RadarrEvent::DeleteRootFolder);
}
self.app.pop_navigation_stack();
}
ActiveRadarrBlock::DeleteIndexerPrompt => {
if self.app.data.radarr_data.prompt_confirm {
self.app.data.radarr_data.prompt_confirm_action = Some(RadarrEvent::DeleteIndexer);
}
self.app.pop_navigation_stack();
}
ActiveRadarrBlock::UpdateAllMoviesPrompt => {
if self.app.data.radarr_data.prompt_confirm {
self.app.data.radarr_data.prompt_confirm_action = Some(RadarrEvent::UpdateAllMovies);
}
self.app.pop_navigation_stack();
}
ActiveRadarrBlock::UpdateDownloadsPrompt => {
if self.app.data.radarr_data.prompt_confirm {
self.app.data.radarr_data.prompt_confirm_action = Some(RadarrEvent::UpdateDownloads);
}
self.app.pop_navigation_stack();
}
ActiveRadarrBlock::UpdateAllCollectionsPrompt => {
if self.app.data.radarr_data.prompt_confirm {
self.app.data.radarr_data.prompt_confirm_action = Some(RadarrEvent::UpdateCollections);
}
self.app.pop_navigation_stack();
}
ActiveRadarrBlock::AddRootFolderPrompt => {
self.app.data.radarr_data.prompt_confirm_action = Some(RadarrEvent::AddRootFolder);
self.app.data.radarr_data.prompt_confirm = true;
self.app.should_ignore_quit_key = false;
self.app.pop_navigation_stack();
}
ActiveRadarrBlock::Indexers => {
self
.app
.push_navigation_stack(ActiveRadarrBlock::EditIndexer.into());
}
_ => (),
}
}
fn handle_esc(&mut self) {
match self.active_radarr_block {
_ if FILTER_BLOCKS.contains(self.active_radarr_block) => {
self.app.pop_navigation_stack();
self.app.data.radarr_data.reset_filter();
self.app.should_ignore_quit_key = false;
}
_ if SEARCH_BLOCKS.contains(self.active_radarr_block) => {
self.app.pop_navigation_stack();
self.app.data.radarr_data.reset_search();
self.app.should_ignore_quit_key = false;
}
ActiveRadarrBlock::AddRootFolderPrompt => {
self.app.pop_navigation_stack();
self.app.data.radarr_data.edit_path = HorizontallyScrollableText::default();
self.app.data.radarr_data.prompt_confirm = false;
self.app.should_ignore_quit_key = false;
}
ActiveRadarrBlock::DeleteDownloadPrompt
| ActiveRadarrBlock::DeleteIndexerPrompt
| ActiveRadarrBlock::DeleteRootFolderPrompt
| ActiveRadarrBlock::UpdateAllMoviesPrompt
| ActiveRadarrBlock::UpdateAllCollectionsPrompt
| ActiveRadarrBlock::UpdateDownloadsPrompt => {
self.app.pop_navigation_stack();
self.app.data.radarr_data.prompt_confirm = false;
}
_ => {
self.app.data.radarr_data.reset_search();
self.app.data.radarr_data.reset_filter();
handle_clear_errors(self.app);
}
}
}
fn handle_char_key_event(&mut self) {
let key = self.key;
match *self.active_radarr_block {
ActiveRadarrBlock::Movies => match self.key {
_ if *key == DEFAULT_KEYBINDINGS.search.key => {
self
.app
.push_navigation_stack(ActiveRadarrBlock::SearchMovie.into());
self.app.data.radarr_data.is_searching = true;
self.app.should_ignore_quit_key = true;
}
_ if *key == DEFAULT_KEYBINDINGS.filter.key => {
self
.app
.push_navigation_stack(ActiveRadarrBlock::FilterMovies.into());
self.app.data.radarr_data.is_filtering = true;
self.app.should_ignore_quit_key = true;
}
_ if *key == DEFAULT_KEYBINDINGS.edit.key => {
self.app.push_navigation_stack(
(
ActiveRadarrBlock::EditMoviePrompt,
Some(ActiveRadarrBlock::Movies),
)
.into(),
);
self.app.data.radarr_data.populate_edit_movie_fields();
self.app.data.radarr_data.selected_block =
BlockSelectionState::new(&EDIT_MOVIE_SELECTION_BLOCKS);
}
_ if *key == DEFAULT_KEYBINDINGS.add.key => {
self
.app
.push_navigation_stack(ActiveRadarrBlock::AddMovieSearchInput.into());
self.app.should_ignore_quit_key = true;
}
_ if *key == DEFAULT_KEYBINDINGS.update.key => {
self
.app
.push_navigation_stack(ActiveRadarrBlock::UpdateAllMoviesPrompt.into());
}
_ if *key == DEFAULT_KEYBINDINGS.refresh.key => {
self.app.should_refresh = true;
}
_ => (),
},
ActiveRadarrBlock::Downloads => match self.key {
_ if *key == DEFAULT_KEYBINDINGS.update.key => {
self
.app
.push_navigation_stack(ActiveRadarrBlock::UpdateDownloadsPrompt.into());
}
_ if *key == DEFAULT_KEYBINDINGS.refresh.key => {
self.app.should_refresh = true;
}
_ => (),
},
ActiveRadarrBlock::Indexers => match self.key {
_ if *key == DEFAULT_KEYBINDINGS.add.key => {
self
.app
.push_navigation_stack(ActiveRadarrBlock::AddIndexer.into());
}
_ if *key == DEFAULT_KEYBINDINGS.refresh.key => {
self.app.should_refresh = true;
}
_ if *key == DEFAULT_KEYBINDINGS.settings.key => {
self
.app
.push_navigation_stack(ActiveRadarrBlock::IndexerSettings.into());
}
_ => (),
},
ActiveRadarrBlock::Collections => match self.key {
_ if *key == DEFAULT_KEYBINDINGS.search.key => {
self
.app
.push_navigation_stack(ActiveRadarrBlock::SearchCollection.into());
self.app.data.radarr_data.is_searching = true;
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.is_filtering = true;
self.app.should_ignore_quit_key = true;
}
_ if *key == DEFAULT_KEYBINDINGS.edit.key => {
self.app.push_navigation_stack(
(
ActiveRadarrBlock::EditCollectionPrompt,
Some(ActiveRadarrBlock::Collections),
)
.into(),
);
self.app.data.radarr_data.populate_edit_collection_fields();
self.app.data.radarr_data.selected_block =
BlockSelectionState::new(&EDIT_COLLECTION_SELECTION_BLOCKS);
}
_ if *key == DEFAULT_KEYBINDINGS.update.key => {
self
.app
.push_navigation_stack(ActiveRadarrBlock::UpdateAllCollectionsPrompt.into());
}
_ if *key == DEFAULT_KEYBINDINGS.refresh.key => {
self.app.should_refresh = true;
}
_ => (),
},
ActiveRadarrBlock::RootFolders => match self.key {
_ if *key == DEFAULT_KEYBINDINGS.refresh.key => {
self.app.should_refresh = true;
}
_ if *key == DEFAULT_KEYBINDINGS.add.key => {
self
.app
.push_navigation_stack(ActiveRadarrBlock::AddRootFolderPrompt.into());
self.app.should_ignore_quit_key = true;
}
_ => (),
},
ActiveRadarrBlock::System => match self.key {
_ if *key == DEFAULT_KEYBINDINGS.refresh.key => {
self.app.should_refresh = true;
}
_ if *key == DEFAULT_KEYBINDINGS.events.key => {
self
.app
.push_navigation_stack(ActiveRadarrBlock::SystemQueuedEvents.into());
}
_ if *key == DEFAULT_KEYBINDINGS.logs.key => {
self
.app
.push_navigation_stack(ActiveRadarrBlock::SystemLogs.into());
self
.app
.data
.radarr_data
.log_details
.set_items(self.app.data.radarr_data.logs.items.to_vec());
self.app.data.radarr_data.log_details.scroll_to_bottom();
}
_ if *key == DEFAULT_KEYBINDINGS.tasks.key => {
self
.app
.push_navigation_stack(ActiveRadarrBlock::SystemTasks.into());
}
_ if *key == DEFAULT_KEYBINDINGS.update.key => {
self
.app
.push_navigation_stack(ActiveRadarrBlock::SystemUpdates.into());
}
_ => (),
},
ActiveRadarrBlock::AddRootFolderPrompt => {
handle_text_box_keys!(self, key, self.app.data.radarr_data.edit_path)
}
_ if SEARCH_BLOCKS.contains(self.active_radarr_block) => {
handle_text_box_keys!(self, key, self.app.data.radarr_data.search)
}
_ if FILTER_BLOCKS.contains(self.active_radarr_block) => {
handle_text_box_keys!(self, key, self.app.data.radarr_data.filter)
}
_ => (),
}
}
fn handle_char_key_event(&mut self) {}
}
impl<'a, 'b> RadarrHandler<'a, 'b> {
fn search_table<T, F>(&mut self, rows: &[T], field_selection_fn: F) -> Option<usize>
where
F: Fn(&T) -> &str,
{
let search_string = self.app.data.radarr_data.search.drain().to_lowercase();
let search_index = rows.iter().position(|item| {
strip_non_search_characters(field_selection_fn(item)).contains(&search_string)
});
pub fn search_table<T, F>(app: &mut App<'_>, rows: &[T], field_selection_fn: F) -> Option<usize>
where
F: Fn(&T) -> &str,
{
let search_string = app.data.radarr_data.search.drain().to_lowercase();
let search_index = rows.iter().position(|item| {
strip_non_search_characters(field_selection_fn(item)).contains(&search_string)
});
self.app.data.radarr_data.is_searching = false;
self.app.should_ignore_quit_key = false;
app.data.radarr_data.is_searching = false;
app.should_ignore_quit_key = false;
if search_index.is_some() {
self.app.pop_navigation_stack();
}
search_index
if search_index.is_some() {
app.pop_navigation_stack();
}
fn filter_table<T, F>(&mut self, rows: &[T], field_selection_fn: F) -> Vec<T>
where
F: Fn(&T) -> &str,
T: Clone,
{
let filter = strip_non_search_characters(&self.app.data.radarr_data.filter.drain());
let filter_matches: Vec<T> = rows
.iter()
.filter(|&item| strip_non_search_characters(field_selection_fn(item)).contains(&filter))
.cloned()
.collect();
search_index
}
self.app.data.radarr_data.is_filtering = false;
self.app.should_ignore_quit_key = false;
pub fn filter_table<T, F>(app: &mut App<'_>, rows: &[T], field_selection_fn: F) -> Vec<T>
where
F: Fn(&T) -> &str,
T: Clone,
{
let filter = strip_non_search_characters(&app.data.radarr_data.filter.drain());
let filter_matches: Vec<T> = rows
.iter()
.filter(|&item| strip_non_search_characters(field_selection_fn(item)).contains(&filter))
.cloned()
.collect();
if !filter_matches.is_empty() {
self.app.pop_navigation_stack();
app.data.radarr_data.is_filtering = false;
app.should_ignore_quit_key = false;
if !filter_matches.is_empty() {
app.pop_navigation_stack();
}
filter_matches
}
pub fn handle_change_tab_left_right_keys(app: &mut App<'_>, key: &Key) {
let key_ref = key;
match key_ref {
_ if *key == DEFAULT_KEYBINDINGS.left.key => {
app.data.radarr_data.main_tabs.previous();
app.pop_and_push_navigation_stack(*app.data.radarr_data.main_tabs.get_active_route());
}
filter_matches
_ if *key == DEFAULT_KEYBINDINGS.right.key => {
app.data.radarr_data.main_tabs.next();
app.pop_and_push_navigation_stack(*app.data.radarr_data.main_tabs.get_active_route());
}
_ => (),
}
}
@@ -26,7 +26,6 @@ mod utils {
tags: vec![Number::from(1)],
..Movie::default()
}]);
radarr_data.selected_block = BlockSelectionState::new(&EDIT_MOVIE_SELECTION_BLOCKS);
app.data.radarr_data = radarr_data;
$handler::with(&DEFAULT_KEYBINDINGS.edit.key, &mut app, &$block, &None).handle();
@@ -66,6 +65,10 @@ mod utils {
assert_str_eq!(app.data.radarr_data.edit_path.text, "/nfs/movies/Test");
assert_str_eq!(app.data.radarr_data.edit_tags.text, "test");
assert_eq!(app.data.radarr_data.edit_monitored, Some(true));
assert_eq!(
app.data.radarr_data.selected_block.blocks,
&EDIT_MOVIE_SELECTION_BLOCKS
);
};
}
@@ -93,7 +96,6 @@ mod utils {
minimum_availability: MinimumAvailability::Released,
..Collection::default()
}]);
radarr_data.selected_block = BlockSelectionState::new(&EDIT_COLLECTION_SELECTION_BLOCKS);
app.data.radarr_data = radarr_data;
$handler::with(&DEFAULT_KEYBINDINGS.edit.key, &mut app, &$block, &None).handle();
@@ -133,6 +135,10 @@ mod utils {
assert_str_eq!(app.data.radarr_data.edit_path.text, "/nfs/movies/Test");
assert_eq!(app.data.radarr_data.edit_monitored, Some(true));
assert_eq!(app.data.radarr_data.edit_search_on_add, Some(true));
assert_eq!(
app.data.radarr_data.selected_block.blocks,
&EDIT_COLLECTION_SELECTION_BLOCKS
);
};
}
@@ -146,10 +152,37 @@ mod utils {
assert_eq!(app.get_current_route(), &$expected_block.into());
};
($handler:ident, $block:expr, $expected_block:expr) => {
let mut app = App::default();
$handler::with(&DELETE_KEY, &mut app, &$block, &None).handle();
assert_eq!(app.get_current_route(), &$expected_block.into());
};
($app:expr, $block:expr, $expected_block:expr) => {
RadarrHandler::with(&DELETE_KEY, &mut $app, &$block, &None).handle();
assert_eq!($app.get_current_route(), &$expected_block.into());
};
($handler:ident, $app:expr, $block:expr, $expected_block:expr) => {
$handler::with(&DELETE_KEY, &mut $app, &$block, &None).handle();
assert_eq!($app.get_current_route(), &$expected_block.into());
};
}
#[macro_export]
macro_rules! assert_refresh_key {
($handler:ident, $block:expr) => {
let mut app = App::default();
app.push_navigation_stack($block.into());
$handler::with(&DEFAULT_KEYBINDINGS.refresh.key, &mut app, &$block, &None).handle();
assert_eq!(app.get_current_route(), &$block.into());
assert!(app.should_refresh);
};
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,148 @@
use crate::app::key_binding::DEFAULT_KEYBINDINGS;
use crate::app::radarr::{ActiveRadarrBlock, ROOT_FOLDERS_BLOCKS};
use crate::app::App;
use crate::event::Key;
use crate::handlers::radarr_handlers::handle_change_tab_left_right_keys;
use crate::handlers::{handle_clear_errors, handle_prompt_toggle, KeyEventHandler};
use crate::models::{HorizontallyScrollableText, Scrollable};
use crate::network::radarr_network::RadarrEvent;
use crate::{handle_text_box_keys, handle_text_box_left_right_keys};
#[cfg(test)]
#[path = "root_folders_handler_tests.rs"]
mod root_folders_handler_tests;
pub(super) struct RootFoldersHandler<'a, 'b> {
key: &'a Key,
app: &'a mut App<'b>,
active_radarr_block: &'a ActiveRadarrBlock,
_context: &'a Option<ActiveRadarrBlock>,
}
impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for RootFoldersHandler<'a, 'b> {
fn accepts(active_block: &'a ActiveRadarrBlock) -> bool {
ROOT_FOLDERS_BLOCKS.contains(active_block)
}
fn with(
key: &'a Key,
app: &'a mut App<'b>,
active_block: &'a ActiveRadarrBlock,
_context: &'a Option<ActiveRadarrBlock>,
) -> RootFoldersHandler<'a, 'b> {
RootFoldersHandler {
key,
app,
active_radarr_block: active_block,
_context,
}
}
fn get_key(&self) -> &Key {
self.key
}
fn handle_scroll_up(&mut self) {
if self.active_radarr_block == &ActiveRadarrBlock::RootFolders {
self.app.data.radarr_data.root_folders.scroll_up()
}
}
fn handle_scroll_down(&mut self) {
if self.active_radarr_block == &ActiveRadarrBlock::RootFolders {
self.app.data.radarr_data.root_folders.scroll_down()
}
}
fn handle_home(&mut self) {
match self.active_radarr_block {
ActiveRadarrBlock::RootFolders => self.app.data.radarr_data.root_folders.scroll_to_top(),
ActiveRadarrBlock::AddRootFolderPrompt => self.app.data.radarr_data.edit_path.scroll_home(),
_ => (),
}
}
fn handle_end(&mut self) {
match self.active_radarr_block {
ActiveRadarrBlock::RootFolders => self.app.data.radarr_data.root_folders.scroll_to_bottom(),
ActiveRadarrBlock::AddRootFolderPrompt => self.app.data.radarr_data.edit_path.reset_offset(),
_ => (),
}
}
fn handle_delete(&mut self) {
if self.active_radarr_block == &ActiveRadarrBlock::RootFolders {
self
.app
.push_navigation_stack(ActiveRadarrBlock::DeleteRootFolderPrompt.into())
}
}
fn handle_left_right_action(&mut self) {
match self.active_radarr_block {
ActiveRadarrBlock::RootFolders => handle_change_tab_left_right_keys(self.app, self.key),
ActiveRadarrBlock::DeleteRootFolderPrompt => handle_prompt_toggle(self.app, self.key),
ActiveRadarrBlock::AddRootFolderPrompt => {
handle_text_box_left_right_keys!(self, self.key, self.app.data.radarr_data.edit_path)
}
_ => (),
}
}
fn handle_submit(&mut self) {
match self.active_radarr_block {
ActiveRadarrBlock::DeleteRootFolderPrompt => {
if self.app.data.radarr_data.prompt_confirm {
self.app.data.radarr_data.prompt_confirm_action = Some(RadarrEvent::DeleteRootFolder);
}
self.app.pop_navigation_stack();
}
ActiveRadarrBlock::AddRootFolderPrompt => {
self.app.data.radarr_data.prompt_confirm_action = Some(RadarrEvent::AddRootFolder);
self.app.data.radarr_data.prompt_confirm = true;
self.app.should_ignore_quit_key = false;
self.app.pop_navigation_stack();
}
_ => (),
}
}
fn handle_esc(&mut self) {
match self.active_radarr_block {
ActiveRadarrBlock::AddRootFolderPrompt => {
self.app.pop_navigation_stack();
self.app.data.radarr_data.edit_path = HorizontallyScrollableText::default();
self.app.data.radarr_data.prompt_confirm = false;
self.app.should_ignore_quit_key = false;
}
ActiveRadarrBlock::DeleteRootFolderPrompt => {
self.app.pop_navigation_stack();
self.app.data.radarr_data.prompt_confirm = false;
}
_ => handle_clear_errors(self.app),
}
}
fn handle_char_key_event(&mut self) {
let key = self.key;
match self.active_radarr_block {
ActiveRadarrBlock::RootFolders => match self.key {
_ if *key == DEFAULT_KEYBINDINGS.refresh.key => {
self.app.should_refresh = true;
}
_ if *key == DEFAULT_KEYBINDINGS.add.key => {
self
.app
.push_navigation_stack(ActiveRadarrBlock::AddRootFolderPrompt.into());
self.app.should_ignore_quit_key = true;
}
_ => (),
},
ActiveRadarrBlock::AddRootFolderPrompt => {
handle_text_box_keys!(self, key, self.app.data.radarr_data.edit_path)
}
_ => (),
}
}
}
@@ -0,0 +1,401 @@
#[cfg(test)]
mod tests {
use pretty_assertions::assert_str_eq;
use strum::IntoEnumIterator;
use crate::app::key_binding::DEFAULT_KEYBINDINGS;
use crate::app::radarr::{ActiveRadarrBlock, ROOT_FOLDERS_BLOCKS};
use crate::app::App;
use crate::event::Key;
use crate::handlers::radarr_handlers::root_folders::RootFoldersHandler;
use crate::handlers::KeyEventHandler;
use crate::models::HorizontallyScrollableText;
mod test_handle_scroll_up_and_down {
use rstest::rstest;
use crate::models::radarr_models::RootFolder;
use crate::{simple_stateful_iterable_vec, test_iterable_scroll};
use super::*;
test_iterable_scroll!(
test_root_folders_scroll,
RootFoldersHandler,
root_folders,
simple_stateful_iterable_vec!(RootFolder, String, path),
ActiveRadarrBlock::RootFolders,
None,
path
);
}
mod test_handle_home_end {
use pretty_assertions::assert_eq;
use crate::models::radarr_models::RootFolder;
use crate::{
extended_stateful_iterable_vec, test_iterable_home_and_end, test_text_box_home_end_keys,
};
use super::*;
test_iterable_home_and_end!(
test_root_folders_home_end,
RootFoldersHandler,
root_folders,
extended_stateful_iterable_vec!(RootFolder, String, path),
ActiveRadarrBlock::RootFolders,
None,
path
);
#[test]
fn test_add_root_folder_prompt_home_end_keys() {
test_text_box_home_end_keys!(
RootFoldersHandler,
ActiveRadarrBlock::AddRootFolderPrompt,
edit_path
);
}
}
mod test_handle_delete {
use pretty_assertions::assert_eq;
use crate::assert_delete_prompt;
use super::*;
const DELETE_KEY: Key = DEFAULT_KEYBINDINGS.delete.key;
#[test]
fn test_delete_root_folder_prompt() {
assert_delete_prompt!(
RootFoldersHandler,
ActiveRadarrBlock::RootFolders,
ActiveRadarrBlock::DeleteRootFolderPrompt
);
}
}
mod test_handle_left_right_action {
use pretty_assertions::assert_eq;
use rstest::rstest;
use crate::test_text_box_left_right_keys;
use super::*;
#[test]
fn test_root_folders_tab_left() {
let mut app = App::default();
app.data.radarr_data.main_tabs.set_index(3);
RootFoldersHandler::with(
&DEFAULT_KEYBINDINGS.left.key,
&mut app,
&ActiveRadarrBlock::RootFolders,
&None,
)
.handle();
assert_eq!(
app.data.radarr_data.main_tabs.get_active_route(),
&ActiveRadarrBlock::Collections.into()
);
assert_eq!(
app.get_current_route(),
&ActiveRadarrBlock::Collections.into()
);
}
#[test]
fn test_root_folders_tab_right() {
let mut app = App::default();
app.data.radarr_data.main_tabs.set_index(3);
RootFoldersHandler::with(
&DEFAULT_KEYBINDINGS.right.key,
&mut app,
&ActiveRadarrBlock::RootFolders,
&None,
)
.handle();
assert_eq!(
app.data.radarr_data.main_tabs.get_active_route(),
&ActiveRadarrBlock::Indexers.into()
);
assert_eq!(app.get_current_route(), &ActiveRadarrBlock::Indexers.into());
}
#[rstest]
fn test_left_right_delete_root_folder_prompt_toggle(
#[values(DEFAULT_KEYBINDINGS.left.key, DEFAULT_KEYBINDINGS.right.key)] key: Key,
) {
let mut app = App::default();
RootFoldersHandler::with(
&key,
&mut app,
&ActiveRadarrBlock::DeleteRootFolderPrompt,
&None,
)
.handle();
assert!(app.data.radarr_data.prompt_confirm);
RootFoldersHandler::with(
&key,
&mut app,
&ActiveRadarrBlock::DeleteRootFolderPrompt,
&None,
)
.handle();
assert!(!app.data.radarr_data.prompt_confirm);
}
#[test]
fn test_add_root_folder_prompt_left_right_keys() {
test_text_box_left_right_keys!(
RootFoldersHandler,
ActiveRadarrBlock::AddRootFolderPrompt,
edit_path
);
}
}
mod test_handle_submit {
use pretty_assertions::assert_eq;
use crate::network::radarr_network::RadarrEvent;
use super::*;
const SUBMIT_KEY: Key = DEFAULT_KEYBINDINGS.submit.key;
#[test]
fn test_add_root_folder_prompt_confirm_submit() {
let mut app = App::default();
app.data.radarr_data.prompt_confirm = true;
app.should_ignore_quit_key = true;
app.push_navigation_stack(ActiveRadarrBlock::RootFolders.into());
app.push_navigation_stack(ActiveRadarrBlock::AddRootFolderPrompt.into());
RootFoldersHandler::with(
&SUBMIT_KEY,
&mut app,
&ActiveRadarrBlock::AddRootFolderPrompt,
&None,
)
.handle();
assert!(app.data.radarr_data.prompt_confirm);
assert!(!app.should_ignore_quit_key);
assert_eq!(
app.data.radarr_data.prompt_confirm_action,
Some(RadarrEvent::AddRootFolder)
);
assert_eq!(
app.get_current_route(),
&ActiveRadarrBlock::RootFolders.into()
);
}
#[test]
fn test_delete_root_folder_prompt_confirm_submit() {
let mut app = App::default();
app.data.radarr_data.prompt_confirm = true;
app.push_navigation_stack(ActiveRadarrBlock::RootFolders.into());
app.push_navigation_stack(ActiveRadarrBlock::DeleteRootFolderPrompt.into());
RootFoldersHandler::with(
&SUBMIT_KEY,
&mut app,
&ActiveRadarrBlock::DeleteRootFolderPrompt,
&None,
)
.handle();
assert!(app.data.radarr_data.prompt_confirm);
assert_eq!(
app.data.radarr_data.prompt_confirm_action,
Some(RadarrEvent::DeleteRootFolder)
);
assert_eq!(
app.get_current_route(),
&ActiveRadarrBlock::RootFolders.into()
);
}
#[test]
fn test_delete_root_folder_prompt_decline_submit() {
let mut app = App::default();
app.push_navigation_stack(ActiveRadarrBlock::RootFolders.into());
app.push_navigation_stack(ActiveRadarrBlock::DeleteRootFolderPrompt.into());
RootFoldersHandler::with(
&SUBMIT_KEY,
&mut app,
&ActiveRadarrBlock::DeleteRootFolderPrompt,
&None,
)
.handle();
assert!(!app.data.radarr_data.prompt_confirm);
assert_eq!(app.data.radarr_data.prompt_confirm_action, None);
assert_eq!(
app.get_current_route(),
&ActiveRadarrBlock::RootFolders.into()
);
}
}
mod test_handle_esc {
use pretty_assertions::assert_eq;
use super::*;
const ESC_KEY: Key = DEFAULT_KEYBINDINGS.esc.key;
#[test]
fn test_delete_root_folder_prompt_block_esc() {
let mut app = App::default();
app.push_navigation_stack(ActiveRadarrBlock::RootFolders.into());
app.push_navigation_stack(ActiveRadarrBlock::DeleteRootFolderPrompt.into());
app.data.radarr_data.prompt_confirm = true;
RootFoldersHandler::with(
&ESC_KEY,
&mut app,
&ActiveRadarrBlock::DeleteRootFolderPrompt,
&None,
)
.handle();
assert_eq!(
app.get_current_route(),
&ActiveRadarrBlock::RootFolders.into()
);
assert!(!app.data.radarr_data.prompt_confirm);
}
#[test]
fn test_add_root_folder_prompt_esc() {
let mut app = App::default();
app.push_navigation_stack(ActiveRadarrBlock::RootFolders.into());
app.push_navigation_stack(ActiveRadarrBlock::AddRootFolderPrompt.into());
app.data.radarr_data.edit_path = HorizontallyScrollableText::from("/nfs/test");
app.should_ignore_quit_key = true;
RootFoldersHandler::with(
&ESC_KEY,
&mut app,
&ActiveRadarrBlock::AddRootFolderPrompt,
&None,
)
.handle();
assert_eq!(
app.get_current_route(),
&ActiveRadarrBlock::RootFolders.into()
);
assert!(app.data.radarr_data.edit_path.text.is_empty());
assert!(!app.data.radarr_data.prompt_confirm);
assert!(!app.should_ignore_quit_key);
}
#[test]
fn test_default_esc() {
let mut app = App::default();
app.error = "test error".to_owned().into();
app.push_navigation_stack(ActiveRadarrBlock::RootFolders.into());
app.push_navigation_stack(ActiveRadarrBlock::RootFolders.into());
RootFoldersHandler::with(&ESC_KEY, &mut app, &ActiveRadarrBlock::RootFolders, &None).handle();
assert_eq!(
app.get_current_route(),
&ActiveRadarrBlock::RootFolders.into()
);
assert!(app.error.text.is_empty());
}
}
mod test_handle_key_char {
use pretty_assertions::{assert_eq, assert_str_eq};
use crate::assert_refresh_key;
use super::*;
#[test]
fn test_root_folder_add() {
let mut app = App::default();
RootFoldersHandler::with(
&DEFAULT_KEYBINDINGS.add.key,
&mut app,
&ActiveRadarrBlock::RootFolders,
&None,
)
.handle();
assert_eq!(
app.get_current_route(),
&ActiveRadarrBlock::AddRootFolderPrompt.into()
);
assert!(app.should_ignore_quit_key);
}
#[test]
fn test_refresh_root_folders_key() {
assert_refresh_key!(RootFoldersHandler, ActiveRadarrBlock::RootFolders);
}
#[test]
fn test_add_root_folder_prompt_backspace_key() {
let mut app = App::default();
app.data.radarr_data.edit_path = "/nfs/test".to_owned().into();
RootFoldersHandler::with(
&DEFAULT_KEYBINDINGS.backspace.key,
&mut app,
&ActiveRadarrBlock::AddRootFolderPrompt,
&None,
)
.handle();
assert_str_eq!(app.data.radarr_data.edit_path.text, "/nfs/tes");
}
#[test]
fn test_add_root_folder_prompt_char_key() {
let mut app = App::default();
RootFoldersHandler::with(
&Key::Char('h'),
&mut app,
&ActiveRadarrBlock::AddRootFolderPrompt,
&None,
)
.handle();
assert_str_eq!(app.data.radarr_data.edit_path.text, "h");
}
}
#[test]
fn test_root_folders_handler_accepts() {
ActiveRadarrBlock::iter().for_each(|active_radarr_block| {
if ROOT_FOLDERS_BLOCKS.contains(&active_radarr_block) {
assert!(RootFoldersHandler::accepts(&active_radarr_block));
} else {
assert!(!RootFoldersHandler::accepts(&active_radarr_block));
}
})
}
}
+116
View File
@@ -0,0 +1,116 @@
use crate::app::key_binding::DEFAULT_KEYBINDINGS;
use crate::app::radarr::ActiveRadarrBlock;
use crate::app::App;
use crate::event::Key;
use crate::handlers::radarr_handlers::handle_change_tab_left_right_keys;
use crate::handlers::radarr_handlers::system::system_details_handler::SystemDetailsHandler;
use crate::handlers::{handle_clear_errors, KeyEventHandler};
use crate::models::Scrollable;
mod system_details_handler;
#[cfg(test)]
#[path = "system_handler_tests.rs"]
mod system_handler_tests;
pub(super) struct SystemHandler<'a, 'b> {
key: &'a Key,
app: &'a mut App<'b>,
active_radarr_block: &'a ActiveRadarrBlock,
context: &'a Option<ActiveRadarrBlock>,
}
impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for SystemHandler<'a, 'b> {
fn handle(&mut self) {
match self.active_radarr_block {
_ if SystemDetailsHandler::accepts(self.active_radarr_block) => {
SystemDetailsHandler::with(self.key, self.app, self.active_radarr_block, self.context)
.handle()
}
_ => self.handle_key_event(),
}
}
fn accepts(active_block: &'a ActiveRadarrBlock) -> bool {
SystemDetailsHandler::accepts(active_block) || active_block == &ActiveRadarrBlock::System
}
fn with(
key: &'a Key,
app: &'a mut App<'b>,
active_block: &'a ActiveRadarrBlock,
context: &'a Option<ActiveRadarrBlock>,
) -> SystemHandler<'a, 'b> {
SystemHandler {
key,
app,
active_radarr_block: active_block,
context,
}
}
fn get_key(&self) -> &Key {
self.key
}
fn handle_scroll_up(&mut self) {}
fn handle_scroll_down(&mut self) {}
fn handle_home(&mut self) {}
fn handle_end(&mut self) {}
fn handle_delete(&mut self) {}
fn handle_left_right_action(&mut self) {
if self.active_radarr_block == &ActiveRadarrBlock::System {
handle_change_tab_left_right_keys(self.app, self.key);
}
}
fn handle_submit(&mut self) {}
fn handle_esc(&mut self) {
handle_clear_errors(self.app)
}
fn handle_char_key_event(&mut self) {
if self.active_radarr_block == &ActiveRadarrBlock::System {
let key = self.key;
match self.key {
_ if *key == DEFAULT_KEYBINDINGS.refresh.key => {
self.app.should_refresh = true;
}
_ if *key == DEFAULT_KEYBINDINGS.events.key => {
self
.app
.push_navigation_stack(ActiveRadarrBlock::SystemQueuedEvents.into());
}
_ if *key == DEFAULT_KEYBINDINGS.logs.key => {
self
.app
.push_navigation_stack(ActiveRadarrBlock::SystemLogs.into());
self
.app
.data
.radarr_data
.log_details
.set_items(self.app.data.radarr_data.logs.items.to_vec());
self.app.data.radarr_data.log_details.scroll_to_bottom();
}
_ if *key == DEFAULT_KEYBINDINGS.tasks.key => {
self
.app
.push_navigation_stack(ActiveRadarrBlock::SystemTasks.into());
}
_ if *key == DEFAULT_KEYBINDINGS.update.key => {
self
.app
.push_navigation_stack(ActiveRadarrBlock::SystemUpdates.into());
}
_ => (),
}
}
}
}
@@ -18,6 +18,10 @@ pub(super) struct SystemDetailsHandler<'a, 'b> {
}
impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for SystemDetailsHandler<'a, 'b> {
fn accepts(active_block: &'a ActiveRadarrBlock) -> bool {
SYSTEM_DETAILS_BLOCKS.contains(active_block)
}
fn with(
key: &'a Key,
app: &'a mut App<'b>,
@@ -1,12 +1,13 @@
#[cfg(test)]
mod tests {
use pretty_assertions::assert_str_eq;
use strum::IntoEnumIterator;
use crate::app::key_binding::DEFAULT_KEYBINDINGS;
use crate::app::radarr::ActiveRadarrBlock;
use crate::app::radarr::{ActiveRadarrBlock, SYSTEM_DETAILS_BLOCKS};
use crate::app::App;
use crate::event::Key;
use crate::handlers::radarr_handlers::system_details_handler::SystemDetailsHandler;
use crate::handlers::radarr_handlers::system::system_details_handler::SystemDetailsHandler;
use crate::handlers::KeyEventHandler;
use crate::models::radarr_models::{QueueEvent, Task};
@@ -448,4 +449,15 @@ mod tests {
assert!(app.should_refresh);
}
}
#[test]
fn test_system_details_handler_accepts() {
ActiveRadarrBlock::iter().for_each(|active_radarr_block| {
if SYSTEM_DETAILS_BLOCKS.contains(&active_radarr_block) {
assert!(SystemDetailsHandler::accepts(&active_radarr_block));
} else {
assert!(!SystemDetailsHandler::accepts(&active_radarr_block));
}
})
}
}
@@ -0,0 +1,211 @@
#[cfg(test)]
mod tests {
use pretty_assertions::assert_eq;
use rstest::rstest;
use strum::IntoEnumIterator;
use crate::app::key_binding::DEFAULT_KEYBINDINGS;
use crate::app::radarr::{ActiveRadarrBlock, SYSTEM_DETAILS_BLOCKS};
use crate::app::App;
use crate::event::Key;
use crate::handlers::radarr_handlers::system::SystemHandler;
use crate::handlers::KeyEventHandler;
use crate::test_handler_delegation;
mod test_handle_left_right_action {
use pretty_assertions::assert_eq;
use super::*;
#[test]
fn test_system_tab_left() {
let mut app = App::default();
app.data.radarr_data.main_tabs.set_index(5);
SystemHandler::with(
&DEFAULT_KEYBINDINGS.left.key,
&mut app,
&ActiveRadarrBlock::System,
&None,
)
.handle();
assert_eq!(
app.data.radarr_data.main_tabs.get_active_route(),
&ActiveRadarrBlock::Indexers.into()
);
assert_eq!(app.get_current_route(), &ActiveRadarrBlock::Indexers.into());
}
#[test]
fn test_system_tab_right() {
let mut app = App::default();
app.data.radarr_data.main_tabs.set_index(5);
SystemHandler::with(
&DEFAULT_KEYBINDINGS.right.key,
&mut app,
&ActiveRadarrBlock::System,
&None,
)
.handle();
assert_eq!(
app.data.radarr_data.main_tabs.get_active_route(),
&ActiveRadarrBlock::Movies.into()
);
assert_eq!(app.get_current_route(), &ActiveRadarrBlock::Movies.into());
}
}
mod test_handle_esc {
use pretty_assertions::assert_eq;
use super::*;
const ESC_KEY: Key = DEFAULT_KEYBINDINGS.esc.key;
#[test]
fn test_default_esc() {
let mut app = App::default();
app.error = "test error".to_owned().into();
app.push_navigation_stack(ActiveRadarrBlock::System.into());
app.push_navigation_stack(ActiveRadarrBlock::System.into());
SystemHandler::with(&ESC_KEY, &mut app, &ActiveRadarrBlock::System, &None).handle();
assert_eq!(app.get_current_route(), &ActiveRadarrBlock::System.into());
assert!(app.error.text.is_empty());
}
}
mod test_handle_key_char {
use pretty_assertions::{assert_eq, assert_str_eq};
use crate::assert_refresh_key;
use crate::models::HorizontallyScrollableText;
use super::*;
#[test]
fn test_update_system_key() {
let mut app = App::default();
SystemHandler::with(
&DEFAULT_KEYBINDINGS.update.key,
&mut app,
&ActiveRadarrBlock::System,
&None,
)
.handle();
assert_eq!(
app.get_current_route(),
&ActiveRadarrBlock::SystemUpdates.into()
);
}
#[test]
fn test_queued_events_key() {
let mut app = App::default();
SystemHandler::with(
&DEFAULT_KEYBINDINGS.events.key,
&mut app,
&ActiveRadarrBlock::System,
&None,
)
.handle();
assert_eq!(
app.get_current_route(),
&ActiveRadarrBlock::SystemQueuedEvents.into()
);
}
#[test]
fn test_refresh_system_key() {
assert_refresh_key!(SystemHandler, ActiveRadarrBlock::System);
}
#[test]
fn test_logs_key() {
let mut app = App::default();
app.data.radarr_data.logs.set_items(vec![
HorizontallyScrollableText::from("test 1"),
HorizontallyScrollableText::from("test 2"),
]);
SystemHandler::with(
&DEFAULT_KEYBINDINGS.logs.key,
&mut app,
&ActiveRadarrBlock::System,
&None,
)
.handle();
assert_eq!(
app.get_current_route(),
&ActiveRadarrBlock::SystemLogs.into()
);
assert_eq!(
app.data.radarr_data.log_details.items,
app.data.radarr_data.logs.items
);
assert_str_eq!(
app.data.radarr_data.log_details.current_selection().text,
"test 2"
);
}
#[test]
fn test_tasks_key() {
let mut app = App::default();
SystemHandler::with(
&DEFAULT_KEYBINDINGS.tasks.key,
&mut app,
&ActiveRadarrBlock::System,
&None,
)
.handle();
assert_eq!(
app.get_current_route(),
&ActiveRadarrBlock::SystemTasks.into()
);
}
}
#[rstest]
fn test_delegates_system_details_blocks_to_system_details_handler(
#[values(
ActiveRadarrBlock::SystemLogs,
ActiveRadarrBlock::SystemQueuedEvents,
ActiveRadarrBlock::SystemTasks,
ActiveRadarrBlock::SystemTaskStartConfirmPrompt,
ActiveRadarrBlock::SystemUpdates
)]
active_radarr_block: ActiveRadarrBlock,
) {
test_handler_delegation!(
SystemHandler,
ActiveRadarrBlock::System,
active_radarr_block
);
}
#[test]
fn test_system_handler_accepts() {
let mut system_blocks = vec![ActiveRadarrBlock::System];
system_blocks.extend(SYSTEM_DETAILS_BLOCKS);
ActiveRadarrBlock::iter().for_each(|active_radarr_block| {
if system_blocks.contains(&active_radarr_block) {
assert!(SystemHandler::accepts(&active_radarr_block));
} else {
assert!(!SystemHandler::accepts(&active_radarr_block));
}
})
}
}