feat(ui): Sonarr support for viewing season details

This commit is contained in:
2024-12-10 18:23:09 -07:00
parent 7bf3311102
commit e9a30382a3
14 changed files with 737 additions and 92 deletions
+4 -5
View File
@@ -2,7 +2,7 @@
mod tests {
use crate::models::servarr_data::sonarr::sonarr_data::{
ActiveSonarrBlock, ADD_SERIES_BLOCKS, DELETE_SERIES_BLOCKS, EDIT_SERIES_BLOCKS,
SERIES_DETAILS_BLOCKS,
SEASON_DETAILS_BLOCKS, SERIES_DETAILS_BLOCKS,
};
use crate::models::{
servarr_data::sonarr::sonarr_data::LIBRARY_BLOCKS, sonarr_models::SeriesStatus,
@@ -16,8 +16,7 @@ mod tests {
use crate::models::sonarr_models::{Season, SeasonStatistics};
use crate::{
models::sonarr_models::Series,
ui::sonarr_ui::library::decorate_series_row_with_style,
models::sonarr_models::Series, ui::sonarr_ui::library::decorate_series_row_with_style,
};
#[test]
@@ -28,6 +27,7 @@ mod tests {
library_ui_blocks.extend(DELETE_SERIES_BLOCKS);
library_ui_blocks.extend(EDIT_SERIES_BLOCKS);
library_ui_blocks.extend(SERIES_DETAILS_BLOCKS);
library_ui_blocks.extend(SEASON_DETAILS_BLOCKS);
ActiveSonarrBlock::iter().for_each(|active_sonarr_block| {
if library_ui_blocks.contains(&active_sonarr_block) {
@@ -39,8 +39,7 @@ mod tests {
}
#[test]
fn test_decorate_row_with_style_downloaded_when_ended_and_all_monitored_episodes_are_present(
) {
fn test_decorate_row_with_style_downloaded_when_ended_and_all_monitored_episodes_are_present() {
let seasons = vec![
Season {
monitored: false,
+5 -1
View File
@@ -35,6 +35,7 @@ mod series_details_ui;
#[cfg(test)]
#[path = "library_ui_tests.rs"]
mod library_ui_tests;
mod season_details_ui;
pub(super) struct LibraryUi;
@@ -80,7 +81,10 @@ impl DrawUi for LibraryUi {
_ if AddSeriesUi::accepts(route) => AddSeriesUi::draw(f, app, area),
_ if DeleteSeriesUi::accepts(route) => DeleteSeriesUi::draw(f, app, area),
_ if EditSeriesUi::accepts(route) => EditSeriesUi::draw(f, app, area),
_ if SeriesDetailsUi::accepts(route) => SeriesDetailsUi::draw(f, app, area),
_ if SeriesDetailsUi::accepts(route) => {
draw_library(f, app, area);
SeriesDetailsUi::draw(f, app, area)
},
Route::Sonarr(active_sonarr_block, _) if LIBRARY_BLOCKS.contains(&active_sonarr_block) => {
series_ui_matchers(active_sonarr_block)
}
@@ -0,0 +1,535 @@
use crate::app::App;
use crate::models::servarr_data::sonarr::sonarr_data::{ActiveSonarrBlock, SEASON_DETAILS_BLOCKS};
use crate::models::sonarr_models::{
DownloadRecord, DownloadStatus, Episode, SonarrHistoryItem, SonarrRelease,
};
use crate::models::Route;
use crate::ui::styles::ManagarrStyle;
use crate::ui::utils::{
borderless_block, decorate_peer_style, get_width_from_percentage, layout_block_top_border,
};
use crate::ui::widgets::confirmation_prompt::ConfirmationPrompt;
use crate::ui::widgets::loading_block::LoadingBlock;
use crate::ui::widgets::managarr_table::ManagarrTable;
use crate::ui::widgets::popup::{Popup, Size};
use crate::ui::{draw_popup, draw_tabs, DrawUi};
use crate::utils::convert_to_gb;
use chrono::Utc;
use ratatui::layout::{Constraint, Rect};
use ratatui::prelude::{Line, Stylize, Text};
use ratatui::widgets::{Cell, Paragraph, Row, Wrap};
use ratatui::Frame;
#[cfg(test)]
#[path = "season_details_ui_tests.rs"]
mod season_details_ui_tests;
pub(super) struct SeasonDetailsUi;
impl DrawUi for SeasonDetailsUi {
fn accepts(route: Route) -> bool {
if let Route::Sonarr(active_sonarr_block, _) = route {
return SEASON_DETAILS_BLOCKS.contains(&active_sonarr_block);
}
false
}
fn draw(f: &mut Frame<'_>, app: &mut App<'_>, _area: Rect) {
if app.data.sonarr_data.season_details_modal.is_some() {
if let Route::Sonarr(active_sonarr_block, _) = app
.data
.sonarr_data
.season_details_modal
.as_ref()
.unwrap()
.season_details_tabs
.get_active_route()
{
let draw_season_details_popup = |f: &mut Frame<'_>, app: &mut App<'_>, popup_area: Rect| {
let content_area = draw_tabs(
f,
popup_area,
"Season Details",
&app
.data
.sonarr_data
.season_details_modal
.as_ref()
.unwrap()
.season_details_tabs,
);
draw_season_details(f, app, content_area);
match active_sonarr_block {
ActiveSonarrBlock::AutomaticallySearchSeasonPrompt => {
let prompt = format!(
"Do you want to trigger an automatic search of your indexers for season packs for the season: Season {}",
app.data.sonarr_data.seasons.current_selection().season_number
);
let confirmation_prompt = ConfirmationPrompt::new()
.title("Automatic Season Search")
.prompt(&prompt)
.yes_no_value(app.data.sonarr_data.prompt_confirm);
f.render_widget(
Popup::new(confirmation_prompt).size(Size::MediumPrompt),
f.area(),
);
}
ActiveSonarrBlock::DeleteEpisodeFilePrompt => {
let prompt = format!(
"Do you really want to delete this episode: \n{}?",
app
.data
.sonarr_data
.season_details_modal
.as_ref()
.unwrap()
.episodes
.current_selection()
.title
.as_ref()
.unwrap_or(&String::new())
);
let confirmation_prompt = ConfirmationPrompt::new()
.title("Delete Episode")
.prompt(&prompt)
.yes_no_value(app.data.sonarr_data.prompt_confirm);
f.render_widget(
Popup::new(confirmation_prompt).size(Size::MediumPrompt),
f.area(),
);
}
ActiveSonarrBlock::ManualSeasonSearchConfirmPrompt => {
draw_manual_season_search_confirm_prompt(f, app);
}
_ => (),
}
};
draw_popup(f, app, draw_season_details_popup, Size::Large);
}
}
}
}
pub fn draw_season_details(f: &mut Frame<'_>, app: &mut App<'_>, area: Rect) {
if let Some(season_details_modal) = app.data.sonarr_data.season_details_modal.as_ref() {
if let Route::Sonarr(active_sonarr_block, _) =
season_details_modal.season_details_tabs.get_active_route()
{
match active_sonarr_block {
ActiveSonarrBlock::SeasonDetails => draw_episodes_table(f, app, area),
ActiveSonarrBlock::SeasonHistory => draw_episode_history_table(f, app, area),
ActiveSonarrBlock::ManualSeasonSearch => draw_season_releases(f, app, area),
_ => (),
}
}
}
}
fn draw_episodes_table(f: &mut Frame<'_>, app: &mut App<'_>, area: Rect) {
if let Route::Sonarr(active_sonarr_block, _) = app.get_current_route() {
let help_footer = app
.data
.sonarr_data
.season_details_modal
.as_ref()
.expect("Season details modal is unpopulated")
.season_details_tabs
.get_active_tab_contextual_help();
let episode_files = app
.data
.sonarr_data
.season_details_modal
.as_ref()
.expect("Season details modal is unpopulated")
.episode_files
.items
.clone();
let content = Some(
&mut app
.data
.sonarr_data
.season_details_modal
.as_mut()
.expect("Season details modal is unpopulated")
.episodes,
);
let downloads_vec = &app.data.sonarr_data.downloads.items;
let episode_row_mapping = |episode: &Episode| {
let Episode {
episode_number,
title,
air_date_utc,
episode_file_id,
..
} = episode;
let episode_file = episode_files
.iter()
.find(|episode_file| episode_file.id == *episode_file_id);
let (quality_profile, size_on_disk) = if let Some(episode_file) = episode_file {
(
episode_file.quality.quality.name.to_owned(),
episode_file.size,
)
} else {
(String::new(), 0)
};
let episode_monitored = if episode.monitored { "🏷" } else { "" };
let size = convert_to_gb(size_on_disk);
let air_date = if let Some(air_date) = air_date_utc.as_ref() {
air_date.to_string()
} else {
String::new()
};
decorate_with_row_style(
downloads_vec,
episode,
Row::new(vec![
Cell::from(episode_monitored.to_owned()),
Cell::from(episode_number.to_string()),
Cell::from(title.clone().unwrap_or_default()),
Cell::from(air_date),
Cell::from(format!("{size:.2} GB")),
Cell::from(quality_profile),
]),
)
};
let is_searching = active_sonarr_block == ActiveSonarrBlock::SearchSeason;
let season_table = ManagarrTable::new(content, episode_row_mapping)
.block(layout_block_top_border())
.loading(app.is_loading)
.footer(help_footer)
.searching(active_sonarr_block == ActiveSonarrBlock::SearchSeason)
.search_produced_empty_results(active_sonarr_block == ActiveSonarrBlock::SearchSeasonError)
.headers([
"🏷",
"#",
"Title",
"Air Date",
"Size on Disk",
"Quality Profile",
])
.constraints([
Constraint::Percentage(4),
Constraint::Percentage(4),
Constraint::Percentage(50),
Constraint::Percentage(19),
Constraint::Percentage(10),
Constraint::Percentage(12),
]);
if is_searching {
season_table.show_cursor(f, area);
}
f.render_widget(season_table, area);
}
}
fn draw_episode_history_table(f: &mut Frame<'_>, app: &mut App<'_>, area: Rect) {
match app.data.sonarr_data.season_details_modal.as_ref() {
Some(season_details_modal) if !app.is_loading => {
let current_selection = if season_details_modal.season_history.is_empty() {
SonarrHistoryItem::default()
} else {
season_details_modal
.season_history
.current_selection()
.clone()
};
let season_history_table_footer = season_details_modal
.season_details_tabs
.get_active_tab_contextual_help();
if let Route::Sonarr(active_sonarr_block, _) = app.get_current_route() {
let history_row_mapping = |history_item: &SonarrHistoryItem| {
let SonarrHistoryItem {
source_title,
language,
quality,
event_type,
date,
..
} = history_item;
source_title.scroll_left_or_reset(
get_width_from_percentage(area, 40),
current_selection == *history_item,
app.tick_count % app.ticks_until_scroll == 0,
);
Row::new(vec![
Cell::from(source_title.to_string()),
Cell::from(event_type.to_string()),
Cell::from(language.name.to_owned()),
Cell::from(quality.quality.name.to_owned()),
Cell::from(date.to_string()),
])
.primary()
};
let mut season_history_table = &mut app
.data
.sonarr_data
.season_details_modal
.as_mut()
.unwrap()
.season_history;
let history_table =
ManagarrTable::new(Some(&mut season_history_table), history_row_mapping)
.block(layout_block_top_border())
.loading(app.is_loading)
.footer(season_history_table_footer)
.sorting(active_sonarr_block == ActiveSonarrBlock::SeasonHistorySortPrompt)
.searching(active_sonarr_block == ActiveSonarrBlock::SearchSeasonHistory)
.search_produced_empty_results(
active_sonarr_block == ActiveSonarrBlock::SearchSeasonHistoryError,
)
.filtering(active_sonarr_block == ActiveSonarrBlock::FilterSeasonHistory)
.filter_produced_empty_results(
active_sonarr_block == ActiveSonarrBlock::FilterSeasonHistoryError,
)
.headers(["Source Title", "Event Type", "Language", "Quality", "Date"])
.constraints([
Constraint::Percentage(40),
Constraint::Percentage(15),
Constraint::Percentage(12),
Constraint::Percentage(13),
Constraint::Percentage(20),
]);
if [
ActiveSonarrBlock::SearchSeriesHistory,
ActiveSonarrBlock::FilterSeriesHistory,
]
.contains(&active_sonarr_block)
{
history_table.show_cursor(f, area);
}
f.render_widget(history_table, area);
}
}
_ => f.render_widget(
LoadingBlock::new(
app.is_loading || app.data.sonarr_data.season_details_modal.is_none(),
layout_block_top_border(),
),
area,
),
}
}
fn draw_season_releases(f: &mut Frame<'_>, app: &mut App<'_>, area: Rect) {
match app.data.sonarr_data.season_details_modal.as_ref() {
Some(season_details_modal) if !app.is_loading => {
let current_selection = if season_details_modal.season_releases.is_empty() {
SonarrRelease::default()
} else {
season_details_modal
.season_releases
.current_selection()
.clone()
};
let season_release_table_footer = season_details_modal
.season_details_tabs
.get_active_tab_contextual_help();
if let Route::Sonarr(active_sonarr_block, _) = app.get_current_route() {
let season_release_row_mapping = |release: &SonarrRelease| {
let SonarrRelease {
protocol,
age,
title,
indexer,
size,
rejected,
seeders,
leechers,
languages,
quality,
..
} = release;
let age = format!("{age} days");
title.scroll_left_or_reset(
get_width_from_percentage(area, 30),
current_selection == *release
&& active_sonarr_block != ActiveSonarrBlock::ManualSeasonSearchConfirmPrompt,
app.tick_count % app.ticks_until_scroll == 0,
);
let size = convert_to_gb(*size);
let rejected_str = if *rejected { "" } else { "" };
let peers = if seeders.is_none() || leechers.is_none() {
Text::from("")
} else {
let seeders = seeders.clone().unwrap().as_u64().unwrap();
let leechers = leechers.clone().unwrap().as_u64().unwrap();
decorate_peer_style(
seeders,
leechers,
Text::from(format!("{seeders} / {leechers}")),
)
};
let language = if languages.is_some() {
languages.clone().unwrap()[0].name.clone()
} else {
String::new()
};
let quality = quality.quality.name.clone();
Row::new(vec![
Cell::from(protocol.clone()),
Cell::from(age),
Cell::from(rejected_str),
Cell::from(title.to_string()),
Cell::from(indexer.clone()),
Cell::from(format!("{size:.1} GB")),
Cell::from(peers),
Cell::from(language),
Cell::from(quality),
])
.primary()
};
let mut season_release_table = &mut app
.data
.sonarr_data
.season_details_modal
.as_mut()
.unwrap()
.season_releases;
let release_table =
ManagarrTable::new(Some(&mut season_release_table), season_release_row_mapping)
.block(layout_block_top_border())
.loading(app.is_loading)
.footer(season_release_table_footer)
.sorting(active_sonarr_block == ActiveSonarrBlock::ManualSeasonSearchSortPrompt)
.headers(["Source Title", "Event Type", "Language", "Quality", "Date"])
.constraints([
Constraint::Percentage(40),
Constraint::Percentage(15),
Constraint::Percentage(12),
Constraint::Percentage(13),
Constraint::Percentage(20),
]);
f.render_widget(release_table, area);
}
}
_ => f.render_widget(
LoadingBlock::new(
app.is_loading || app.data.sonarr_data.season_details_modal.is_none(),
layout_block_top_border(),
),
area,
),
}
}
fn draw_manual_season_search_confirm_prompt(f: &mut Frame<'_>, app: &mut App<'_>) {
let current_selection = app
.data
.sonarr_data
.season_details_modal
.as_ref()
.unwrap()
.season_releases
.current_selection();
let title = if current_selection.rejected {
"Download Rejected Release"
} else {
"Download Release"
};
let prompt = if current_selection.rejected {
format!(
"Do you really want to download the rejected release: {}?",
&current_selection.title.text
)
} else {
format!(
"Do you want to download the release: {}?",
&current_selection.title.text
)
};
if current_selection.rejected {
let mut lines_vec = vec![Line::from("Rejection reasons: ".primary().bold())];
let mut rejections_spans = current_selection
.rejections
.clone()
.unwrap_or_default()
.iter()
.map(|item| Line::from(format!("{item}").primary().bold()))
.collect::<Vec<Line<'_>>>();
lines_vec.append(&mut rejections_spans);
let content_paragraph = Paragraph::new(lines_vec)
.block(borderless_block())
.wrap(Wrap { trim: false })
.left_aligned();
let confirmation_prompt = ConfirmationPrompt::new()
.title(title)
.prompt(&prompt)
.content(content_paragraph)
.yes_no_value(app.data.radarr_data.prompt_confirm);
f.render_widget(Popup::new(confirmation_prompt).size(Size::Small), f.area());
} else {
let confirmation_prompt = ConfirmationPrompt::new()
.title(title)
.prompt(&prompt)
.yes_no_value(app.data.radarr_data.prompt_confirm);
f.render_widget(
Popup::new(confirmation_prompt).size(Size::MediumPrompt),
f.area(),
);
}
}
fn decorate_with_row_style<'a>(
downloads_vec: &[DownloadRecord],
episode: &Episode,
row: Row<'a>,
) -> Row<'a> {
if !episode.has_file {
if let Some(download) = downloads_vec
.iter()
.find(|&download| download.episode_id == episode.id)
{
if download.status == DownloadStatus::Downloading {
return row.downloading();
}
if download.status == DownloadStatus::Completed {
return row.awaiting_import();
}
}
if !episode.monitored {
return row.unmonitored_missing();
}
if let Some(air_date) = episode.air_date_utc.as_ref() {
if air_date > &Utc::now() {
return row.unreleased();
}
}
return row.missing();
}
if !episode.monitored {
row.unmonitored()
} else {
row.downloaded()
}
}
@@ -0,0 +1,21 @@
#[cfg(test)]
mod tests {
use strum::IntoEnumIterator;
use crate::models::servarr_data::sonarr::sonarr_data::{
ActiveSonarrBlock, SEASON_DETAILS_BLOCKS,
};
use crate::ui::sonarr_ui::library::season_details_ui::SeasonDetailsUi;
use crate::ui::DrawUi;
#[test]
fn test_season_details_ui_accepts() {
ActiveSonarrBlock::iter().for_each(|active_sonarr_block| {
if SEASON_DETAILS_BLOCKS.contains(&active_sonarr_block) {
assert!(SeasonDetailsUi::accepts(active_sonarr_block.into()));
} else {
assert!(!SeasonDetailsUi::accepts(active_sonarr_block.into()));
}
});
}
}
+23 -12
View File
@@ -28,7 +28,8 @@ use crate::ui::widgets::loading_block::LoadingBlock;
use crate::ui::widgets::managarr_table::ManagarrTable;
use crate::ui::widgets::message::Message;
use crate::ui::widgets::popup::{Popup, Size};
use crate::ui::{draw_popup_over, draw_tabs, DrawUi};
use crate::ui::{draw_popup, draw_popup_over, draw_tabs, DrawUi};
use crate::ui::sonarr_ui::library::season_details_ui::SeasonDetailsUi;
use crate::utils::convert_to_gb;
use super::draw_library;
@@ -42,14 +43,15 @@ pub(super) struct SeriesDetailsUi;
impl DrawUi for SeriesDetailsUi {
fn accepts(route: Route) -> bool {
if let Route::Sonarr(active_sonarr_block, _) = route {
return SERIES_DETAILS_BLOCKS.contains(&active_sonarr_block);
return SeasonDetailsUi::accepts(route) || SERIES_DETAILS_BLOCKS.contains(&active_sonarr_block);
}
false
}
fn draw(f: &mut Frame<'_>, app: &mut App<'_>, area: Rect) {
if let Route::Sonarr(active_sonarr_block, _) = app.get_current_route() {
let route = app.get_current_route();
if let Route::Sonarr(active_sonarr_block, _) = route {
let draw_series_details_popup = |f: &mut Frame<'_>, app: &mut App<'_>, popup_area: Rect| {
f.render_widget(
title_block(&app.data.sonarr_data.series.current_selection().title.text),
@@ -105,14 +107,23 @@ impl DrawUi for SeriesDetailsUi {
};
};
draw_popup_over(
f,
app,
area,
draw_library,
draw_series_details_popup,
Size::XXLarge,
);
match route {
_ if SeasonDetailsUi::accepts(route) => {
draw_popup(f, app, draw_series_details_popup, Size::XXLarge);
SeasonDetailsUi::draw(f, app, area);
},
Route::Sonarr(active_sonarr_block, _) if SERIES_DETAILS_BLOCKS.contains(&active_sonarr_block) => {
draw_popup_over(
f,
app,
area,
draw_library,
draw_series_details_popup,
Size::XXLarge,
);
}
_ => (),
}
}
}
}
@@ -364,7 +375,7 @@ fn draw_series_history_table(f: &mut Frame<'_>, app: &mut App<'_>, area: Rect) {
}
_ => f.render_widget(
LoadingBlock::new(
app.is_loading || app.data.radarr_data.movie_details_modal.is_none(),
app.is_loading || app.data.sonarr_data.seasons.is_empty(),
layout_block_top_border(),
),
area,
@@ -2,16 +2,17 @@
mod tests {
use strum::IntoEnumIterator;
use crate::models::servarr_data::sonarr::sonarr_data::{
ActiveSonarrBlock, SERIES_DETAILS_BLOCKS,
};
use crate::models::servarr_data::sonarr::sonarr_data::{ActiveSonarrBlock, SEASON_DETAILS_BLOCKS, SERIES_DETAILS_BLOCKS};
use crate::ui::sonarr_ui::library::series_details_ui::SeriesDetailsUi;
use crate::ui::DrawUi;
#[test]
fn test_series_details_ui_accepts() {
let mut blocks = SERIES_DETAILS_BLOCKS.clone().to_vec();
blocks.extend(SEASON_DETAILS_BLOCKS);
ActiveSonarrBlock::iter().for_each(|active_sonarr_block| {
if SERIES_DETAILS_BLOCKS.contains(&active_sonarr_block) {
if blocks.contains(&active_sonarr_block) {
assert!(SeriesDetailsUi::accepts(active_sonarr_block.into()));
} else {
assert!(!SeriesDetailsUi::accepts(active_sonarr_block.into()));