refactor(handlers): Refactored the handlers to all use the handle_table_events macro when appropriate and created tests for the macro so tests don't have to be duplicated across each handler
This commit is contained in:
1 parent
c09950d0af
commit
ed2211586e
44 files changed
+1592
-9288
No files matched your search
@@ -4,8 +4,12 @@ use derive_setters::Setters;
|
||||
use std::cmp::Ordering;
|
||||
use std::fmt::Debug;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "table_handler_tests.rs"]
|
||||
mod table_handler_tests;
|
||||
|
||||
#[derive(Setters)]
|
||||
pub struct TableHandlingProps<T>
|
||||
pub struct TableHandlingConfig<T>
|
||||
where
|
||||
T: Clone + PartialEq + Eq + Debug + Default,
|
||||
{
|
||||
@@ -35,36 +39,36 @@ where
|
||||
macro_rules! handle_table_events {
|
||||
($self:expr, $name:ty, $table:expr, $row:ident) => {
|
||||
paste::paste! {
|
||||
fn [<handle_ $name _table_events>](&mut $self, props: $crate::handlers::table_handler::TableHandlingProps<$row>) -> bool {
|
||||
fn [<handle_ $name _table_events>](&mut $self, config: $crate::handlers::table_handler::TableHandlingConfig<$row>) -> bool {
|
||||
if $self.is_ready() {
|
||||
match $self.key {
|
||||
_ if $self.key == $crate::app::key_binding::DEFAULT_KEYBINDINGS.up.key => $self.[<handle_ $name _table_scroll_up>](props),
|
||||
_ if $self.key == $crate::app::key_binding::DEFAULT_KEYBINDINGS.down.key => $self.[<handle_ $name _table_scroll_down>](props),
|
||||
_ if $self.key == $crate::app::key_binding::DEFAULT_KEYBINDINGS.home.key => $self.[<handle_ $name _table_home>](props),
|
||||
_ if $self.key == $crate::app::key_binding::DEFAULT_KEYBINDINGS.end.key => $self.[<handle_ $name _table_end>](props),
|
||||
_ if $self.key == $crate::app::key_binding::DEFAULT_KEYBINDINGS.up.key => $self.[<handle_ $name _table_scroll_up>](config),
|
||||
_ if $self.key == $crate::app::key_binding::DEFAULT_KEYBINDINGS.down.key => $self.[<handle_ $name _table_scroll_down>](config),
|
||||
_ if $self.key == $crate::app::key_binding::DEFAULT_KEYBINDINGS.home.key => $self.[<handle_ $name _table_home>](config),
|
||||
_ if $self.key == $crate::app::key_binding::DEFAULT_KEYBINDINGS.end.key => $self.[<handle_ $name _table_end>](config),
|
||||
_ if $self.key == $crate::app::key_binding::DEFAULT_KEYBINDINGS.left.key
|
||||
|| $self.key == $crate::app::key_binding::DEFAULT_KEYBINDINGS.right.key =>
|
||||
{
|
||||
$self.[<handle_ $name _table_left_right>](props)
|
||||
$self.[<handle_ $name _table_left_right>](config)
|
||||
}
|
||||
_ if $self.key == $crate::app::key_binding::DEFAULT_KEYBINDINGS.submit.key => $self.[<handle_ $name _table_submit>](props),
|
||||
_ if $self.key == $crate::app::key_binding::DEFAULT_KEYBINDINGS.esc.key => $self.[<handle_ $name _table_esc>](props),
|
||||
_ if props.searching_block.is_some()
|
||||
&& $self.app.get_current_route() == *props.searching_block.as_ref().unwrap() =>
|
||||
_ if $self.key == $crate::app::key_binding::DEFAULT_KEYBINDINGS.submit.key => $self.[<handle_ $name _table_submit>](config),
|
||||
_ if $self.key == $crate::app::key_binding::DEFAULT_KEYBINDINGS.esc.key => $self.[<handle_ $name _table_esc>](config),
|
||||
_ if config.searching_block.is_some()
|
||||
&& $self.app.get_current_route() == *config.searching_block.as_ref().unwrap() =>
|
||||
{
|
||||
$self.[<handle_ $name _table_search_box_input>]()
|
||||
}
|
||||
_ if props.filtering_block.is_some()
|
||||
&& $self.app.get_current_route() == *props.filtering_block.as_ref().unwrap() =>
|
||||
_ if config.filtering_block.is_some()
|
||||
&& $self.app.get_current_route() == *config.filtering_block.as_ref().unwrap() =>
|
||||
{
|
||||
$self.[<handle_ $name _table_filter_box_input>]()
|
||||
}
|
||||
_ if $self.key == $crate::app::key_binding::DEFAULT_KEYBINDINGS.filter.key
|
||||
&& props.filtering_block.is_some() => $self.[<handle_ $name _table_filter_key>](props),
|
||||
&& config.filtering_block.is_some() => $self.[<handle_ $name _table_filter_key>](config),
|
||||
_ if $self.key == $crate::app::key_binding::DEFAULT_KEYBINDINGS.search.key
|
||||
&& props.searching_block.is_some() => $self.[<handle_ $name _table_search_key>](props),
|
||||
&& config.searching_block.is_some() => $self.[<handle_ $name _table_search_key>](config),
|
||||
_ if $self.key == $crate::app::key_binding::DEFAULT_KEYBINDINGS.sort.key
|
||||
&& props.sorting_block.is_some() => $self.[<handle_ $name _table_sort_key>](props),
|
||||
&& config.sorting_block.is_some() => $self.[<handle_ $name _table_sort_key>](config),
|
||||
_ => false,
|
||||
}
|
||||
} else {
|
||||
@@ -72,14 +76,16 @@ macro_rules! handle_table_events {
|
||||
}
|
||||
}
|
||||
|
||||
fn [<handle_ $name _table_scroll_up>](&mut $self, props: $crate::handlers::table_handler::TableHandlingProps<$row>) -> bool {
|
||||
fn [<handle_ $name _table_scroll_up>](&mut $self, config: $crate::handlers::table_handler::TableHandlingConfig<$row>) -> bool {
|
||||
use $crate::models::Scrollable;
|
||||
|
||||
match $self.app.get_current_route() {
|
||||
_ if props.table_block == $self.app.get_current_route() => {
|
||||
_ if config.table_block == $self.app.get_current_route() => {
|
||||
$table.scroll_up();
|
||||
true
|
||||
}
|
||||
_ if props.sorting_block.is_some()
|
||||
&& $self.app.get_current_route() == *props.sorting_block.as_ref().unwrap() =>
|
||||
_ if config.sorting_block.is_some()
|
||||
&& $self.app.get_current_route() == *config.sorting_block.as_ref().unwrap() =>
|
||||
{
|
||||
$table.sort.as_mut().unwrap().scroll_up();
|
||||
true
|
||||
@@ -88,14 +94,16 @@ macro_rules! handle_table_events {
|
||||
}
|
||||
}
|
||||
|
||||
fn [<handle_ $name _table_scroll_down>](&mut $self, props: $crate::handlers::table_handler::TableHandlingProps<$row>) -> bool {
|
||||
fn [<handle_ $name _table_scroll_down>](&mut $self, config: $crate::handlers::table_handler::TableHandlingConfig<$row>) -> bool {
|
||||
use $crate::models::Scrollable;
|
||||
|
||||
match $self.app.get_current_route() {
|
||||
_ if props.table_block == $self.app.get_current_route() => {
|
||||
_ if config.table_block == $self.app.get_current_route() => {
|
||||
$table.scroll_down();
|
||||
true
|
||||
}
|
||||
_ if props.sorting_block.is_some()
|
||||
&& $self.app.get_current_route() == *props.sorting_block.as_ref().unwrap() =>
|
||||
_ if config.sorting_block.is_some()
|
||||
&& $self.app.get_current_route() == *config.sorting_block.as_ref().unwrap() =>
|
||||
{
|
||||
$table
|
||||
.sort
|
||||
@@ -108,14 +116,16 @@ macro_rules! handle_table_events {
|
||||
}
|
||||
}
|
||||
|
||||
fn [<handle_ $name _table_home>](&mut $self, props: $crate::handlers::table_handler::TableHandlingProps<$row>) -> bool {
|
||||
fn [<handle_ $name _table_home>](&mut $self, config: $crate::handlers::table_handler::TableHandlingConfig<$row>) -> bool {
|
||||
use $crate::models::Scrollable;
|
||||
|
||||
match $self.app.get_current_route() {
|
||||
_ if props.table_block == $self.app.get_current_route() => {
|
||||
_ if config.table_block == $self.app.get_current_route() => {
|
||||
$table.scroll_to_top();
|
||||
true
|
||||
}
|
||||
_ if props.sorting_block.is_some()
|
||||
&& $self.app.get_current_route() == *props.sorting_block.as_ref().unwrap() =>
|
||||
_ if config.sorting_block.is_some()
|
||||
&& $self.app.get_current_route() == *config.sorting_block.as_ref().unwrap() =>
|
||||
{
|
||||
$table
|
||||
.sort
|
||||
@@ -124,8 +134,8 @@ macro_rules! handle_table_events {
|
||||
.scroll_to_top();
|
||||
true
|
||||
}
|
||||
_ if props.searching_block.is_some()
|
||||
&& $self.app.get_current_route() == *props.searching_block.as_ref().unwrap() =>
|
||||
_ if config.searching_block.is_some()
|
||||
&& $self.app.get_current_route() == *config.searching_block.as_ref().unwrap() =>
|
||||
{
|
||||
$table
|
||||
.search
|
||||
@@ -134,8 +144,8 @@ macro_rules! handle_table_events {
|
||||
.scroll_home();
|
||||
true
|
||||
}
|
||||
_ if props.filtering_block.is_some()
|
||||
&& $self.app.get_current_route() == *props.filtering_block.as_ref().unwrap() =>
|
||||
_ if config.filtering_block.is_some()
|
||||
&& $self.app.get_current_route() == *config.filtering_block.as_ref().unwrap() =>
|
||||
{
|
||||
$table
|
||||
.filter
|
||||
@@ -148,14 +158,16 @@ macro_rules! handle_table_events {
|
||||
}
|
||||
}
|
||||
|
||||
fn [<handle_ $name _table_end>](&mut $self, props: $crate::handlers::table_handler::TableHandlingProps<$row>) -> bool {
|
||||
fn [<handle_ $name _table_end>](&mut $self, config: $crate::handlers::table_handler::TableHandlingConfig<$row>) -> bool {
|
||||
use $crate::models::Scrollable;
|
||||
|
||||
match $self.app.get_current_route() {
|
||||
_ if props.table_block == $self.app.get_current_route() => {
|
||||
_ if config.table_block == $self.app.get_current_route() => {
|
||||
$table.scroll_to_bottom();
|
||||
true
|
||||
}
|
||||
_ if props.sorting_block.is_some()
|
||||
&& $self.app.get_current_route() == *props.sorting_block.as_ref().unwrap() =>
|
||||
_ if config.sorting_block.is_some()
|
||||
&& $self.app.get_current_route() == *config.sorting_block.as_ref().unwrap() =>
|
||||
{
|
||||
$table
|
||||
.sort
|
||||
@@ -164,8 +176,8 @@ macro_rules! handle_table_events {
|
||||
.scroll_to_bottom();
|
||||
true
|
||||
}
|
||||
_ if props.searching_block.is_some()
|
||||
&& $self.app.get_current_route() == *props.searching_block.as_ref().unwrap() =>
|
||||
_ if config.searching_block.is_some()
|
||||
&& $self.app.get_current_route() == *config.searching_block.as_ref().unwrap() =>
|
||||
{
|
||||
$table
|
||||
.search
|
||||
@@ -174,8 +186,8 @@ macro_rules! handle_table_events {
|
||||
.reset_offset();
|
||||
true
|
||||
}
|
||||
_ if props.filtering_block.is_some()
|
||||
&& $self.app.get_current_route() == *props.filtering_block.as_ref().unwrap() =>
|
||||
_ if config.filtering_block.is_some()
|
||||
&& $self.app.get_current_route() == *config.filtering_block.as_ref().unwrap() =>
|
||||
{
|
||||
$table
|
||||
.filter
|
||||
@@ -188,10 +200,10 @@ macro_rules! handle_table_events {
|
||||
}
|
||||
}
|
||||
|
||||
fn [<handle_ $name _table_left_right>](&mut $self, props: $crate::handlers::table_handler::TableHandlingProps<$row>) -> bool {
|
||||
fn [<handle_ $name _table_left_right>](&mut $self, config: $crate::handlers::table_handler::TableHandlingConfig<$row>) -> bool {
|
||||
match $self.app.get_current_route() {
|
||||
_ if props.searching_block.is_some()
|
||||
&& $self.app.get_current_route() == *props.searching_block.as_ref().unwrap() =>
|
||||
_ if config.searching_block.is_some()
|
||||
&& $self.app.get_current_route() == *config.searching_block.as_ref().unwrap() =>
|
||||
{
|
||||
$crate::handle_text_box_left_right_keys!(
|
||||
$self,
|
||||
@@ -200,8 +212,8 @@ macro_rules! handle_table_events {
|
||||
);
|
||||
true
|
||||
}
|
||||
_ if props.filtering_block.is_some()
|
||||
&& $self.app.get_current_route() == *props.filtering_block.as_ref().unwrap() =>
|
||||
_ if config.filtering_block.is_some()
|
||||
&& $self.app.get_current_route() == *config.filtering_block.as_ref().unwrap() =>
|
||||
{
|
||||
$crate::handle_text_box_left_right_keys!(
|
||||
$self,
|
||||
@@ -214,12 +226,12 @@ macro_rules! handle_table_events {
|
||||
}
|
||||
}
|
||||
|
||||
fn [<handle _$name _table_submit>](&mut $self, props: $crate::handlers::table_handler::TableHandlingProps<$row>) -> bool {
|
||||
fn [<handle _$name _table_submit>](&mut $self, config: $crate::handlers::table_handler::TableHandlingConfig<$row>) -> bool {
|
||||
match $self.app.get_current_route() {
|
||||
_ if props.sorting_block.is_some()
|
||||
&& $self.app.get_current_route() == *props.sorting_block.as_ref().unwrap() =>
|
||||
_ if config.sorting_block.is_some()
|
||||
&& $self.app.get_current_route() == *config.sorting_block.as_ref().unwrap() =>
|
||||
{
|
||||
if let Some(sort_by_fn) = props.sort_by_fn {
|
||||
if let Some(sort_by_fn) = config.sort_by_fn {
|
||||
$table.items.sort_by(sort_by_fn);
|
||||
}
|
||||
|
||||
@@ -228,21 +240,21 @@ macro_rules! handle_table_events {
|
||||
|
||||
true
|
||||
}
|
||||
_ if props.searching_block.is_some()
|
||||
&& $self.app.get_current_route() == *props.searching_block.as_ref().unwrap() =>
|
||||
_ if config.searching_block.is_some()
|
||||
&& $self.app.get_current_route() == *config.searching_block.as_ref().unwrap() =>
|
||||
{
|
||||
$self.app.pop_navigation_stack();
|
||||
$self.app.should_ignore_quit_key = false;
|
||||
|
||||
if $table.search.is_some() {
|
||||
let search_field_fn = props
|
||||
let search_field_fn = config
|
||||
.search_field_fn
|
||||
.expect("Search field function is required");
|
||||
let has_match = $table.apply_search(search_field_fn);
|
||||
|
||||
if !has_match {
|
||||
$self.app.push_navigation_stack(
|
||||
props
|
||||
config
|
||||
.search_error_block
|
||||
.expect("Search error block is undefined"),
|
||||
);
|
||||
@@ -251,21 +263,21 @@ macro_rules! handle_table_events {
|
||||
|
||||
true
|
||||
}
|
||||
_ if props.filtering_block.is_some()
|
||||
&& $self.app.get_current_route() == *props.filtering_block.as_ref().unwrap() =>
|
||||
_ if config.filtering_block.is_some()
|
||||
&& $self.app.get_current_route() == *config.filtering_block.as_ref().unwrap() =>
|
||||
{
|
||||
$self.app.pop_navigation_stack();
|
||||
$self.app.should_ignore_quit_key = false;
|
||||
|
||||
if $table.filter.is_some() {
|
||||
let filter_field_fn = props
|
||||
let filter_field_fn = config
|
||||
.filter_field_fn
|
||||
.expect("Search field function is required");
|
||||
let has_match = $table.apply_filter(filter_field_fn);
|
||||
|
||||
if !has_match {
|
||||
$self.app.push_navigation_stack(
|
||||
props
|
||||
config
|
||||
.filter_error_block
|
||||
.expect("Search error block is undefined"),
|
||||
);
|
||||
@@ -278,35 +290,35 @@ macro_rules! handle_table_events {
|
||||
}
|
||||
}
|
||||
|
||||
fn [<handle_ $name _table_esc>](&mut $self, props: $crate::handlers::table_handler::TableHandlingProps<$row>) -> bool {
|
||||
fn [<handle_ $name _table_esc>](&mut $self, config: $crate::handlers::table_handler::TableHandlingConfig<$row>) -> bool {
|
||||
match $self.app.get_current_route() {
|
||||
_ if props.sorting_block.is_some()
|
||||
&& $self.app.get_current_route() == *props.sorting_block.as_ref().unwrap() =>
|
||||
_ if config.sorting_block.is_some()
|
||||
&& $self.app.get_current_route() == *config.sorting_block.as_ref().unwrap() =>
|
||||
{
|
||||
$self.app.pop_navigation_stack();
|
||||
true
|
||||
}
|
||||
_ if (props.searching_block.is_some()
|
||||
&& $self.app.get_current_route() == *props.searching_block.as_ref().unwrap())
|
||||
|| (props.search_error_block.is_some()
|
||||
&& $self.app.get_current_route() == *props.search_error_block.as_ref().unwrap()) =>
|
||||
_ if (config.searching_block.is_some()
|
||||
&& $self.app.get_current_route() == *config.searching_block.as_ref().unwrap())
|
||||
|| (config.search_error_block.is_some()
|
||||
&& $self.app.get_current_route() == *config.search_error_block.as_ref().unwrap()) =>
|
||||
{
|
||||
$self.app.pop_navigation_stack();
|
||||
$table.reset_search();
|
||||
$self.app.should_ignore_quit_key = false;
|
||||
true
|
||||
}
|
||||
_ if (props.filtering_block.is_some()
|
||||
&& $self.app.get_current_route() == *props.filtering_block.as_ref().unwrap())
|
||||
|| (props.filter_error_block.is_some()
|
||||
&& $self.app.get_current_route() == *props.filter_error_block.as_ref().unwrap()) =>
|
||||
_ if (config.filtering_block.is_some()
|
||||
&& $self.app.get_current_route() == *config.filtering_block.as_ref().unwrap())
|
||||
|| (config.filter_error_block.is_some()
|
||||
&& $self.app.get_current_route() == *config.filter_error_block.as_ref().unwrap()) =>
|
||||
{
|
||||
$self.app.pop_navigation_stack();
|
||||
$table.reset_filter();
|
||||
$self.app.should_ignore_quit_key = false;
|
||||
true
|
||||
}
|
||||
_ if props.table_block == $self.app.get_current_route()
|
||||
_ if config.table_block == $self.app.get_current_route()
|
||||
&& $table.filtered_items.is_some() =>
|
||||
{
|
||||
$table.reset_filter();
|
||||
@@ -316,11 +328,11 @@ macro_rules! handle_table_events {
|
||||
}
|
||||
}
|
||||
|
||||
fn [<handle_ $name _table_filter_key>](&mut $self, props: $crate::handlers::table_handler::TableHandlingProps<$row>) -> bool {
|
||||
if matches!($self.app.get_current_route(), _ if props.table_block == $self.app.get_current_route()) {
|
||||
fn [<handle_ $name _table_filter_key>](&mut $self, config: $crate::handlers::table_handler::TableHandlingConfig<$row>) -> bool {
|
||||
if matches!($self.app.get_current_route(), _ if config.table_block == $self.app.get_current_route()) {
|
||||
$self
|
||||
.app
|
||||
.push_navigation_stack(props.filtering_block.expect("Filtering block is undefined").into());
|
||||
.push_navigation_stack(config.filtering_block.expect("Filtering block is undefined").into());
|
||||
$table.reset_filter();
|
||||
$table.filter = Some($crate::models::HorizontallyScrollableText::default());
|
||||
$self.app.should_ignore_quit_key = true;
|
||||
@@ -331,11 +343,11 @@ macro_rules! handle_table_events {
|
||||
}
|
||||
}
|
||||
|
||||
fn [<handle_ $name _table_search_key>](&mut $self, props: $crate::handlers::table_handler::TableHandlingProps<$row>) -> bool {
|
||||
if matches!($self.app.get_current_route(), _ if props.table_block == $self.app.get_current_route()) {
|
||||
fn [<handle_ $name _table_search_key>](&mut $self, config: $crate::handlers::table_handler::TableHandlingConfig<$row>) -> bool {
|
||||
if matches!($self.app.get_current_route(), _ if config.table_block == $self.app.get_current_route()) {
|
||||
$self
|
||||
.app
|
||||
.push_navigation_stack(props.searching_block.expect("Searching block is undefined"));
|
||||
.push_navigation_stack(config.searching_block.expect("Searching block is undefined"));
|
||||
$table.search = Some($crate::models::HorizontallyScrollableText::default());
|
||||
$self.app.should_ignore_quit_key = true;
|
||||
|
||||
@@ -345,10 +357,10 @@ macro_rules! handle_table_events {
|
||||
}
|
||||
}
|
||||
|
||||
fn [<handle_ $name _table_sort_key>](&mut $self, props: $crate::handlers::table_handler::TableHandlingProps<$row>) -> bool {
|
||||
if matches!($self.app.get_current_route(), _ if props.table_block == $self.app.get_current_route()) {
|
||||
fn [<handle_ $name _table_sort_key>](&mut $self, config: $crate::handlers::table_handler::TableHandlingConfig<$row>) -> bool {
|
||||
if matches!($self.app.get_current_route(), _ if config.table_block == $self.app.get_current_route()) {
|
||||
$table.sorting(
|
||||
props
|
||||
config
|
||||
.sort_options
|
||||
.as_ref()
|
||||
.expect("Sort options are undefined")
|
||||
@@ -356,7 +368,7 @@ macro_rules! handle_table_events {
|
||||
);
|
||||
$self
|
||||
.app
|
||||
.push_navigation_stack(props.sorting_block.expect("Sorting block is undefined"));
|
||||
.push_navigation_stack(config.sorting_block.expect("Sorting block is undefined"));
|
||||
true
|
||||
} else {
|
||||
false
|
||||
@@ -384,12 +396,12 @@ macro_rules! handle_table_events {
|
||||
};
|
||||
}
|
||||
|
||||
impl<T> TableHandlingProps<T>
|
||||
impl<T> TableHandlingConfig<T>
|
||||
where
|
||||
T: Clone + PartialEq + Eq + Debug + Default,
|
||||
{
|
||||
pub fn new(table_block: Route) -> Self {
|
||||
TableHandlingProps {
|
||||
TableHandlingConfig {
|
||||
sorting_block: None,
|
||||
sort_options: None,
|
||||
sort_by_fn: None,
|
||||
|
||||
Reference in new issue
Block a user