Added full support for managing the blocklist

This commit is contained in:
2024-02-15 16:20:03 -07:00
parent d869647dd8
commit 6cadf70c1e
42 changed files with 2004 additions and 123 deletions
@@ -0,0 +1,18 @@
#[cfg(test)]
mod tests {
use crate::models::servarr_data::radarr::radarr_data::{ActiveRadarrBlock, BLOCKLIST_BLOCKS};
use crate::ui::radarr_ui::blocklist::BlocklistUi;
use crate::ui::DrawUi;
use strum::IntoEnumIterator;
#[test]
fn test_blocklist_ui_accepts() {
ActiveRadarrBlock::iter().for_each(|active_radarr_block| {
if BLOCKLIST_BLOCKS.contains(&active_radarr_block) {
assert!(BlocklistUi::accepts(active_radarr_block.into()));
} else {
assert!(!BlocklistUi::accepts(active_radarr_block.into()));
}
});
}
}
+201
View File
@@ -0,0 +1,201 @@
use crate::app::App;
use crate::models::radarr_models::BlocklistItem;
use crate::models::servarr_data::radarr::radarr_data::{ActiveRadarrBlock, BLOCKLIST_BLOCKS};
use crate::models::Route;
use crate::ui::styles::ManagarrStyle;
use crate::ui::utils::{get_width_from_percentage, layout_block_top_border};
use crate::ui::widgets::confirmation_prompt::ConfirmationPrompt;
use crate::ui::widgets::managarr_table::ManagarrTable;
use crate::ui::widgets::message::Message;
use crate::ui::widgets::popup::{Popup, Size};
use crate::ui::DrawUi;
use ratatui::layout::{Alignment, Constraint, Rect};
use ratatui::style::{Style, Stylize};
use ratatui::text::{Line, Text};
use ratatui::widgets::{Cell, Row};
use ratatui::Frame;
#[cfg(test)]
#[path = "blocklist_ui_tests.rs"]
mod blocklist_ui_tests;
pub(super) struct BlocklistUi;
impl DrawUi for BlocklistUi {
fn accepts(route: Route) -> bool {
if let Route::Radarr(active_radarr_block, _) = route {
return BLOCKLIST_BLOCKS.contains(&active_radarr_block);
}
false
}
fn draw(f: &mut Frame<'_>, app: &mut App<'_>, area: Rect) {
if let Route::Radarr(active_radarr_block, _) = *app.get_current_route() {
match active_radarr_block {
ActiveRadarrBlock::Blocklist | ActiveRadarrBlock::BlocklistSortPrompt => {
draw_blocklist_table(f, app, area)
}
ActiveRadarrBlock::BlocklistItemDetails => {
draw_blocklist_table(f, app, area);
draw_blocklist_item_details_popup(f, app);
}
ActiveRadarrBlock::DeleteBlocklistItemPrompt => {
let prompt = format!(
"Do you want to remove this item from your blocklist: \n{}?",
app
.data
.radarr_data
.blocklist
.current_selection()
.source_title
);
let confirmation_prompt = ConfirmationPrompt::new()
.title("Remove Item from Blocklist")
.prompt(&prompt)
.yes_no_value(app.data.radarr_data.prompt_confirm);
draw_blocklist_table(f, app, area);
f.render_widget(Popup::new(confirmation_prompt).size(Size::Prompt), f.size());
}
ActiveRadarrBlock::BlocklistClearAllItemsPrompt => {
let confirmation_prompt = ConfirmationPrompt::new()
.title("Clear Blocklist")
.prompt("Do you want to clear your blocklist?")
.yes_no_value(app.data.radarr_data.prompt_confirm);
draw_blocklist_table(f, app, area);
f.render_widget(
Popup::new(confirmation_prompt).size(Size::SmallPrompt),
f.size(),
);
}
_ => (),
}
}
}
}
fn draw_blocklist_table(f: &mut Frame<'_>, app: &mut App<'_>, area: Rect) {
if let Route::Radarr(active_radarr_block, _) = *app.get_current_route() {
let current_selection = if app.data.radarr_data.blocklist.items.is_empty() {
BlocklistItem::default()
} else {
app.data.radarr_data.blocklist.current_selection().clone()
};
let blocklist_table_footer = app
.data
.radarr_data
.main_tabs
.get_active_tab_contextual_help();
let blocklist_row_mapping = |blocklist_item: &BlocklistItem| {
let BlocklistItem {
source_title,
languages,
quality,
custom_formats,
date,
movie,
..
} = blocklist_item;
movie.title.scroll_left_or_reset(
get_width_from_percentage(area, 20),
current_selection == *blocklist_item,
app.tick_count % app.ticks_until_scroll == 0,
);
let languages_string = languages
.iter()
.map(|lang| lang.name.to_owned())
.collect::<Vec<String>>()
.join(", ");
let custom_formats_string = if let Some(formats) = custom_formats.as_ref() {
formats
.iter()
.map(|cf| cf.name.to_owned())
.collect::<Vec<String>>()
.join(", ")
} else {
"".to_owned()
};
Row::new(vec![
Cell::from(movie.title.to_string()),
Cell::from(source_title.to_owned()),
Cell::from(languages_string),
Cell::from(quality.quality.name.to_owned()),
Cell::from(custom_formats_string),
Cell::from(date.to_string()),
])
.primary()
};
let blocklist_table = ManagarrTable::new(
Some(&mut app.data.radarr_data.blocklist),
blocklist_row_mapping,
)
.block(layout_block_top_border())
.loading(app.is_loading)
.footer(blocklist_table_footer)
.sorting(active_radarr_block == ActiveRadarrBlock::BlocklistSortPrompt)
.headers([
"Movie Title",
"Source Title",
"Languages",
"Quality",
"Formats",
"Date",
])
.constraints([
Constraint::Percentage(20),
Constraint::Percentage(35),
Constraint::Percentage(10),
Constraint::Percentage(10),
Constraint::Percentage(10),
Constraint::Percentage(15),
]);
f.render_widget(blocklist_table, area);
}
}
fn draw_blocklist_item_details_popup(f: &mut Frame<'_>, app: &mut App<'_>) {
let current_selection = if app.data.radarr_data.blocklist.items.is_empty() {
BlocklistItem::default()
} else {
app.data.radarr_data.blocklist.current_selection().clone()
};
let BlocklistItem {
source_title,
protocol,
indexer,
message,
..
} = current_selection;
let text = Text::from(vec![
Line::from(vec![
"Name: ".bold().secondary(),
source_title.to_owned().secondary(),
]),
Line::from(vec![
"Protocol: ".bold().secondary(),
protocol.to_owned().secondary(),
]),
Line::from(vec![
"Indexer: ".bold().secondary(),
indexer.to_owned().secondary(),
]),
Line::from(vec![
"Message: ".bold().secondary(),
message.to_owned().secondary(),
]),
]);
let message = Message::new(text)
.title("Details")
.style(Style::new().secondary())
.alignment(Alignment::Left);
f.render_widget(Popup::new(message).size(Size::NarrowMessage), f.size());
}
+3
View File
@@ -13,6 +13,7 @@ use crate::models::radarr_models::{DiskSpace, DownloadRecord, Movie, RootFolder}
use crate::models::servarr_data::radarr::radarr_data::RadarrData;
use crate::models::Route;
use crate::ui::draw_tabs;
use crate::ui::radarr_ui::blocklist::BlocklistUi;
use crate::ui::radarr_ui::collections::CollectionsUi;
use crate::ui::radarr_ui::downloads::DownloadsUi;
use crate::ui::radarr_ui::indexers::IndexersUi;
@@ -27,6 +28,7 @@ use crate::ui::widgets::loading_block::LoadingBlock;
use crate::ui::DrawUi;
use crate::utils::convert_to_gb;
mod blocklist;
mod collections;
mod downloads;
mod indexers;
@@ -57,6 +59,7 @@ impl DrawUi for RadarrUi {
_ if IndexersUi::accepts(route) => IndexersUi::draw(f, app, content_area),
_ if RootFoldersUi::accepts(route) => RootFoldersUi::draw(f, app, content_area),
_ if SystemUi::accepts(route) => SystemUi::draw(f, app, content_area),
_ if BlocklistUi::accepts(route) => BlocklistUi::draw(f, app, content_area),
_ => (),
}
}
+8 -1
View File
@@ -14,6 +14,7 @@ pub struct Message<'a> {
text: Text<'a>,
title: &'a str,
style: Style,
alignment: Alignment,
}
impl<'a> Message<'a> {
@@ -25,6 +26,7 @@ impl<'a> Message<'a> {
text: message.into(),
title: "Error",
style: Style::new().failure().bold(),
alignment: Alignment::Center,
}
}
@@ -38,10 +40,15 @@ impl<'a> Message<'a> {
self
}
pub fn alignment(mut self, alignment: Alignment) -> Self {
self.alignment = alignment;
self
}
fn render_message(self, area: Rect, buf: &mut Buffer) {
Paragraph::new(self.text)
.style(self.style)
.alignment(Alignment::Center)
.alignment(self.alignment)
.block(title_block_centered(self.title).style(self.style))
.wrap(Wrap { trim: true })
.render(area, buf);
+16
View File
@@ -3,6 +3,7 @@ mod tests {
use crate::ui::styles::ManagarrStyle;
use crate::ui::widgets::message::Message;
use pretty_assertions::{assert_eq, assert_str_eq};
use ratatui::layout::Alignment;
use ratatui::style::{Style, Stylize};
use ratatui::text::Text;
@@ -15,6 +16,7 @@ mod tests {
assert_eq!(message.text, Text::from(test_message));
assert_str_eq!(message.title, "Error");
assert_eq!(message.style, Style::new().failure().bold());
assert_eq!(message.alignment, Alignment::Center);
}
#[test]
@@ -27,6 +29,7 @@ mod tests {
assert_str_eq!(message.title, title);
assert_eq!(message.text, Text::from(test_message));
assert_eq!(message.style, Style::new().failure().bold());
assert_eq!(message.alignment, Alignment::Center);
}
#[test]
@@ -39,5 +42,18 @@ mod tests {
assert_eq!(message.style, style);
assert_eq!(message.text, Text::from(test_message));
assert_str_eq!(message.title, "Error");
assert_eq!(message.alignment, Alignment::Center);
}
#[test]
fn test_message_alignment() {
let test_message = "This is a message";
let message = Message::new(test_message).alignment(Alignment::Left);
assert_eq!(message.alignment, Alignment::Left);
assert_eq!(message.text, Text::from(test_message));
assert_str_eq!(message.title, "Error");
assert_eq!(message.style, Style::new().failure().bold());
}
}
+4
View File
@@ -10,9 +10,11 @@ use ratatui::widgets::{Block, Clear, Paragraph, Widget};
mod popup_tests;
pub enum Size {
SmallPrompt,
Prompt,
LargePrompt,
Message,
NarrowMessage,
LargeMessage,
InputBox,
Dropdown,
@@ -24,9 +26,11 @@ pub enum Size {
impl Size {
pub fn to_percent(&self) -> (u16, u16) {
match self {
Size::SmallPrompt => (20, 20),
Size::Prompt => (35, 35),
Size::LargePrompt => (70, 45),
Size::Message => (25, 8),
Size::NarrowMessage => (50, 20),
Size::LargeMessage => (25, 25),
Size::InputBox => (30, 13),
Size::Dropdown => (20, 30),
+2
View File
@@ -6,9 +6,11 @@ mod tests {
#[test]
fn test_dimensions_to_percent() {
assert_eq!(Size::SmallPrompt.to_percent(), (20, 20));
assert_eq!(Size::Prompt.to_percent(), (35, 35));
assert_eq!(Size::LargePrompt.to_percent(), (70, 45));
assert_eq!(Size::Message.to_percent(), (25, 8));
assert_eq!(Size::NarrowMessage.to_percent(), (50, 20));
assert_eq!(Size::LargeMessage.to_percent(), (25, 25));
assert_eq!(Size::InputBox.to_percent(), (30, 13));
assert_eq!(Size::Dropdown.to_percent(), (20, 30));