Refactored unnecessary data fields into Options to make the code cleaner, and to reduce the memory usage of the application

This commit is contained in:
2023-08-14 13:02:50 -06:00
parent 23ad489b15
commit 907fa9a2ec
34 changed files with 932 additions and 545 deletions
@@ -234,7 +234,6 @@ mod tests {
RadarrData, EDIT_COLLECTION_SELECTION_BLOCKS,
};
use crate::models::StatefulTable;
use crate::test_edit_collection_key;
use super::*;
@@ -19,6 +19,7 @@ mod tests {
mod test_handle_scroll_up_and_down {
use rstest::rstest;
use crate::models::StatefulTable;
use crate::{simple_stateful_iterable_vec, test_iterable_scroll};
use super::*;
@@ -34,21 +35,54 @@ mod tests {
to_string
);
test_iterable_scroll!(
test_filtered_collections_scroll,
CollectionsHandler,
filtered_collections,
simple_stateful_iterable_vec!(Collection, HorizontallyScrollableText),
ActiveRadarrBlock::Collections,
None,
title,
to_string
);
#[rstest]
fn test_filtered_collections_scroll(
#[values(DEFAULT_KEYBINDINGS.up.key, DEFAULT_KEYBINDINGS.down.key)] key: Key,
) {
let mut app = App::default();
let mut filtered_collections = StatefulTable::default();
filtered_collections.set_items(simple_stateful_iterable_vec!(
Collection,
HorizontallyScrollableText
));
app.data.radarr_data.filtered_collections = Some(filtered_collections);
CollectionsHandler::with(&key, &mut app, &ActiveRadarrBlock::Collections, &None).handle();
assert_str_eq!(
app
.data
.radarr_data
.filtered_collections
.as_ref()
.unwrap()
.current_selection()
.title
.to_string(),
"Test 2"
);
CollectionsHandler::with(&key, &mut app, &ActiveRadarrBlock::Collections, &None).handle();
assert_str_eq!(
app
.data
.radarr_data
.filtered_collections
.as_ref()
.unwrap()
.current_selection()
.title
.to_string(),
"Test 1"
);
}
}
mod test_handle_home_end {
use pretty_assertions::assert_eq;
use crate::models::StatefulTable;
use crate::{extended_stateful_iterable_vec, test_iterable_home_and_end};
use super::*;
@@ -64,16 +98,58 @@ mod tests {
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_filtered_collections_home_end() {
let mut app = App::default();
let mut filtered_collections = StatefulTable::default();
filtered_collections.set_items(extended_stateful_iterable_vec!(
Collection,
HorizontallyScrollableText
));
app.data.radarr_data.filtered_collections = Some(filtered_collections);
CollectionsHandler::with(
&DEFAULT_KEYBINDINGS.end.key,
&mut app,
&ActiveRadarrBlock::Collections,
&None,
)
.handle();
assert_str_eq!(
app
.data
.radarr_data
.filtered_collections
.as_ref()
.unwrap()
.current_selection()
.title
.to_string(),
"Test 3"
);
CollectionsHandler::with(
&DEFAULT_KEYBINDINGS.home.key,
&mut app,
&ActiveRadarrBlock::Collections,
&None,
)
.handle();
assert_str_eq!(
app
.data
.radarr_data
.filtered_collections
.as_ref()
.unwrap()
.current_selection()
.title
.to_string(),
"Test 1"
);
}
#[test]
fn test_collection_search_box_home_end_keys() {
@@ -341,6 +417,7 @@ mod tests {
}
mod test_handle_submit {
use crate::models::StatefulTable;
use pretty_assertions::assert_eq;
use crate::network::radarr_network::RadarrEvent;
@@ -450,14 +527,12 @@ mod tests {
let mut app = App::default();
app.push_navigation_stack(ActiveRadarrBlock::Collections.into());
app.push_navigation_stack(ActiveRadarrBlock::SearchCollection.into());
app
.data
.radarr_data
.filtered_collections
.set_items(extended_stateful_iterable_vec!(
Collection,
HorizontallyScrollableText
));
let mut filtered_collections = StatefulTable::default();
filtered_collections.set_items(extended_stateful_iterable_vec!(
Collection,
HorizontallyScrollableText
));
app.data.radarr_data.filtered_collections = Some(filtered_collections);
app.data.radarr_data.search = Some("Test 2".into());
CollectionsHandler::with(
@@ -473,6 +548,8 @@ mod tests {
.data
.radarr_data
.filtered_collections
.as_ref()
.unwrap()
.current_selection()
.title
.text,
@@ -507,12 +584,25 @@ mod tests {
)
.handle();
assert_eq!(app.data.radarr_data.filtered_collections.items.len(), 3);
assert!(app.data.radarr_data.filtered_collections.is_some());
assert_eq!(
app
.data
.radarr_data
.filtered_collections
.as_ref()
.unwrap()
.items
.len(),
3
);
assert_str_eq!(
app
.data
.radarr_data
.filtered_collections
.as_ref()
.unwrap()
.current_selection()
.title
.text,
@@ -547,7 +637,7 @@ mod tests {
)
.handle();
assert!(app.data.radarr_data.filtered_collections.items.is_empty());
assert!(app.data.radarr_data.filtered_collections.is_none());
assert_eq!(
app.get_current_route(),
&ActiveRadarrBlock::FilterCollectionsError.into()
@@ -715,7 +805,6 @@ mod tests {
RadarrData, EDIT_COLLECTION_SELECTION_BLOCKS,
};
use crate::models::StatefulTable;
use crate::{assert_refresh_key, test_edit_collection_key};
use super::*;
@@ -784,7 +873,7 @@ mod tests {
assert!(app.data.radarr_data.is_filtering);
assert!(app.should_ignore_quit_key);
assert!(app.data.radarr_data.filter.is_some());
assert!(app.data.radarr_data.filtered_collections.items.is_empty());
assert!(app.data.radarr_data.filtered_collections.is_none());
}
#[test]
+14 -56
View File
@@ -8,7 +8,7 @@ use crate::handlers::{handle_clear_errors, handle_prompt_toggle, KeyEventHandler
use crate::models::servarr_data::radarr::radarr_data::{
ActiveRadarrBlock, COLLECTIONS_BLOCKS, EDIT_COLLECTION_SELECTION_BLOCKS,
};
use crate::models::{BlockSelectionState, HorizontallyScrollableText, Scrollable};
use crate::models::{BlockSelectionState, HorizontallyScrollableText, Scrollable, StatefulTable};
use crate::network::radarr_network::RadarrEvent;
use crate::utils::strip_non_search_characters;
use crate::{filter_table, handle_text_box_keys, handle_text_box_left_right_keys, search_table};
@@ -68,15 +68,8 @@ impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for CollectionsHandler<'
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();
if let Some(filtered_collections) = self.app.data.radarr_data.filtered_collections.as_mut() {
filtered_collections.scroll_up();
} else {
self.app.data.radarr_data.collections.scroll_up()
}
@@ -85,15 +78,8 @@ impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for CollectionsHandler<'
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();
if let Some(filtered_collections) = self.app.data.radarr_data.filtered_collections.as_mut() {
filtered_collections.scroll_down();
} else {
self.app.data.radarr_data.collections.scroll_down()
}
@@ -103,20 +89,9 @@ impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for CollectionsHandler<'
fn handle_home(&mut self) {
match self.active_radarr_block {
ActiveRadarrBlock::Collections => {
if !self
.app
.data
.radarr_data
.filtered_collections
.items
.is_empty()
if let Some(filtered_collections) = self.app.data.radarr_data.filtered_collections.as_mut()
{
self
.app
.data
.radarr_data
.filtered_collections
.scroll_to_top();
filtered_collections.scroll_to_top();
} else {
self.app.data.radarr_data.collections.scroll_to_top()
}
@@ -144,20 +119,9 @@ impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for CollectionsHandler<'
fn handle_end(&mut self) {
match self.active_radarr_block {
ActiveRadarrBlock::Collections => {
if !self
.app
.data
.radarr_data
.filtered_collections
.items
.is_empty()
if let Some(filtered_collections) = self.app.data.radarr_data.filtered_collections.as_mut()
{
self
.app
.data
.radarr_data
.filtered_collections
.scroll_to_bottom();
filtered_collections.scroll_to_bottom();
} else {
self.app.data.radarr_data.collections.scroll_to_bottom()
}
@@ -212,23 +176,17 @@ impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveRadarrBlock> for CollectionsHandler<'
.app
.push_navigation_stack(ActiveRadarrBlock::CollectionDetails.into()),
ActiveRadarrBlock::SearchCollection => {
if self
.app
.data
.radarr_data
.filtered_collections
.items
.is_empty()
{
if self.app.data.radarr_data.filtered_collections.is_some() {
search_table!(
self.app,
collections,
ActiveRadarrBlock::SearchCollectionError
filtered_collections,
ActiveRadarrBlock::SearchCollectionError,
true
);
} else {
search_table!(
self.app,
filtered_collections,
collections,
ActiveRadarrBlock::SearchCollectionError
);
}