From 316c129b995c4c0e4b5361ee9d0004e69a493d2e Mon Sep 17 00:00:00 2001 From: Dark-Alex-17 Date: Tue, 8 Aug 2023 10:50:05 -0600 Subject: [PATCH] Fixed UI bug that didn't alert users when they tried to add a movie that couldn't be found --- Cargo.toml | 2 +- .../radarr_handlers/add_movie_handler.rs | 32 +++- src/ui/radarr_ui/add_movie_ui.rs | 145 +++++++++--------- 3 files changed, 108 insertions(+), 71 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index be907f2..160cbd0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,7 @@ indoc = "2.0.0" log = "0.4.17" log4rs = { version = "1.2.0", features = ["file_appender"] } regex = "1.7.1" -reqwest = { version = "0.11.13", features = ["json"] } +reqwest = { version = "0.11.14", features = ["json"] } serde_yaml = "0.9.16" serde_json = "1.0.91" serde = { version = "1.0", features = ["derive"] } diff --git a/src/handlers/radarr_handlers/add_movie_handler.rs b/src/handlers/radarr_handlers/add_movie_handler.rs index b6270a2..49a34e2 100644 --- a/src/handlers/radarr_handlers/add_movie_handler.rs +++ b/src/handlers/radarr_handlers/add_movie_handler.rs @@ -175,7 +175,15 @@ impl<'a> KeyEventHandler<'a, ActiveRadarrBlock> for AddMovieHandler<'a> { .push_navigation_stack(ActiveRadarrBlock::AddMovieSearchResults.into()); self.app.should_ignore_quit_key = false; } - ActiveRadarrBlock::AddMovieSearchResults => { + _ if *self.active_radarr_block == ActiveRadarrBlock::AddMovieSearchResults + && !self + .app + .data + .radarr_data + .add_searched_movies + .items + .is_empty() => + { self .app .push_navigation_stack(ActiveRadarrBlock::AddMoviePrompt.into()); @@ -439,6 +447,11 @@ mod tests { #[test] fn test_add_movie_search_results_submit() { let mut app = App::default(); + app + .data + .radarr_data + .add_searched_movies + .set_items(vec![AddMovieSearchResult::default()]); app.data.radarr_data.quality_profile_map = HashMap::from([(1, "B - Test 2".to_owned()), (0, "A - Test 1".to_owned())]); @@ -476,6 +489,23 @@ mod tests { ); } + #[test] + fn test_add_movie_search_results_submit_does_nothing_on_empty_table() { + let mut app = App::default(); + app.push_navigation_stack(ActiveRadarrBlock::AddMovieSearchResults.into()); + AddMovieHandler::with( + &SUBMIT_KEY, + &mut app, + &ActiveRadarrBlock::AddMovieSearchResults, + ) + .handle(); + + assert_eq!( + app.get_current_route(), + &ActiveRadarrBlock::AddMovieSearchResults.into() + ); + } + #[test] fn test_add_movie_prompt_prompt_decline() { let mut app = App::default(); diff --git a/src/ui/radarr_ui/add_movie_ui.rs b/src/ui/radarr_ui/add_movie_ui.rs index b4deec0..bccb8c2 100644 --- a/src/ui/radarr_ui/add_movie_ui.rs +++ b/src/ui/radarr_ui/add_movie_ui.rs @@ -9,8 +9,8 @@ use crate::models::radarr_models::AddMovieSearchResult; use crate::models::Route; use crate::ui::utils::{ borderless_block, get_width_with_margin, horizontal_chunks, layout_block, - layout_paragraph_borderless, show_cursor, style_default, style_help, style_primary, - title_block_centered, vertical_chunks_with_margin, + layout_paragraph_borderless, show_cursor, style_default, style_failure, style_help, + style_primary, title_block_centered, vertical_chunks_with_margin, }; use crate::ui::{ draw_button, draw_drop_down_list, draw_drop_down_menu_button, draw_drop_down_popup, @@ -91,75 +91,82 @@ fn draw_add_movie_search(f: &mut Frame<'_, B>, app: &mut App, area: .alignment(Alignment::Center); f.render_widget(help_paragraph, chunks[2]); - draw_table( - f, - chunks[1], - layout_block(), - TableProps { - content: &mut app.data.radarr_data.add_searched_movies, - table_headers: vec![ - "Title", - "Year", - "Runtime", - "IMDB", - "Rotten Tomatoes", - "Genres", - ], - constraints: vec![ - Constraint::Percentage(27), - Constraint::Percentage(8), - Constraint::Percentage(10), - Constraint::Percentage(8), - Constraint::Percentage(14), - Constraint::Percentage(30), - ], - help: None, - }, - |movie| { - let (hours, minutes) = convert_runtime(movie.runtime.as_u64().unwrap()); - let imdb_rating = movie - .ratings - .imdb - .clone() - .unwrap_or_default() - .value - .as_f64() - .unwrap(); - let rotten_tomatoes_rating = movie - .ratings - .rotten_tomatoes - .clone() - .unwrap_or_default() - .value - .as_u64() - .unwrap(); - let imdb_rating = if imdb_rating == 0.0 { - String::default() - } else { - format!("{:.1}", imdb_rating) - }; - let rotten_tomatoes_rating = if rotten_tomatoes_rating == 0 { - String::default() - } else { - format!("{}%", rotten_tomatoes_rating) - }; + if app.data.radarr_data.add_searched_movies.items.is_empty() && !app.is_loading { + let mut error_text = Text::from("No movies found matching your query!"); + error_text.patch_style(style_failure()); + let error_paragraph = Paragraph::new(error_text).block(layout_block()); + f.render_widget(error_paragraph, chunks[1]); + } else { + draw_table( + f, + chunks[1], + layout_block(), + TableProps { + content: &mut app.data.radarr_data.add_searched_movies, + table_headers: vec![ + "Title", + "Year", + "Runtime", + "IMDB", + "Rotten Tomatoes", + "Genres", + ], + constraints: vec![ + Constraint::Percentage(27), + Constraint::Percentage(8), + Constraint::Percentage(10), + Constraint::Percentage(8), + Constraint::Percentage(14), + Constraint::Percentage(30), + ], + help: None, + }, + |movie| { + let (hours, minutes) = convert_runtime(movie.runtime.as_u64().unwrap()); + let imdb_rating = movie + .ratings + .imdb + .clone() + .unwrap_or_default() + .value + .as_f64() + .unwrap(); + let rotten_tomatoes_rating = movie + .ratings + .rotten_tomatoes + .clone() + .unwrap_or_default() + .value + .as_u64() + .unwrap(); + let imdb_rating = if imdb_rating == 0.0 { + String::default() + } else { + format!("{:.1}", imdb_rating) + }; + let rotten_tomatoes_rating = if rotten_tomatoes_rating == 0 { + String::default() + } else { + format!("{}%", rotten_tomatoes_rating) + }; - movie - .title - .scroll_or_reset(get_width_with_margin(area), *movie == current_selection); + movie + .title + .scroll_or_reset(get_width_with_margin(area), *movie == current_selection); - Row::new(vec![ - Cell::from(movie.title.to_string()), - Cell::from(movie.year.as_u64().unwrap().to_string()), - Cell::from(format!("{}h {}m", hours, minutes)), - Cell::from(imdb_rating), - Cell::from(rotten_tomatoes_rating), - Cell::from(movie.genres.join(", ")), - ]) - .style(style_primary()) - }, - app.is_loading, - ); + Row::new(vec![ + Cell::from(movie.title.to_string()), + Cell::from(movie.year.as_u64().unwrap().to_string()), + Cell::from(format!("{}h {}m", hours, minutes)), + Cell::from(imdb_rating), + Cell::from(rotten_tomatoes_rating), + Cell::from(movie.genres.join(", ")), + ]) + .style(style_primary()) + }, + app.is_loading, + ); + } } _ => (), }