Refactored handlers and UI to have a separate radarr module, and implemented movie search functionality for existing movies.

This commit is contained in:
2023-08-08 10:50:04 -06:00
parent 3f378fb25a
commit a304367e0e
14 changed files with 1263 additions and 929 deletions
+384
View File
@@ -0,0 +1,384 @@
use std::iter;
use std::ops::Sub;
use chrono::{Duration, Utc};
use tui::backend::Backend;
use tui::layout::{Alignment, Constraint, Rect};
use tui::style::{Color, Style};
use tui::text::Text;
use tui::widgets::{Cell, Paragraph, Row};
use tui::Frame;
use crate::app::radarr::{ActiveRadarrBlock, RadarrData};
use crate::app::App;
use crate::logos::RADARR_LOGO;
use crate::models::radarr_models::{DiskSpace, DownloadRecord, Movie};
use crate::models::Route;
use crate::ui::radarr_ui::collection_details_ui::draw_collection_details_popup;
use crate::ui::radarr_ui::movie_details_ui::draw_movie_info;
use crate::ui::utils::{
borderless_block, horizontal_chunks, layout_block_top_border, line_gauge_with_label,
line_gauge_with_title, style_bold, style_default, style_failure, style_primary, style_success,
style_warning, title_block, vertical_chunks_with_margin,
};
use crate::ui::{
draw_large_popup_over, draw_popup_over, draw_table, draw_tabs, loading, TableProps,
};
use crate::utils::{convert_runtime, convert_to_gb};
mod collection_details_ui;
mod movie_details_ui;
pub(super) fn draw_radarr_ui<B: Backend>(f: &mut Frame<'_, B>, app: &mut App, area: Rect) {
let (content_rect, _) = draw_tabs(f, area, " Movies ", &app.data.radarr_data.main_tabs);
if let Route::Radarr(active_radarr_block) = app.get_current_route().clone() {
match active_radarr_block {
ActiveRadarrBlock::Movies => draw_library(f, app, content_rect),
ActiveRadarrBlock::SearchMovie => {
draw_popup_over(f, app, content_rect, draw_library, draw_search_box, 30, 10)
}
ActiveRadarrBlock::Downloads => draw_downloads(f, app, content_rect),
ActiveRadarrBlock::Collections => draw_collections(f, app, content_rect),
ActiveRadarrBlock::MovieDetails
| ActiveRadarrBlock::MovieHistory
| ActiveRadarrBlock::FileInfo
| ActiveRadarrBlock::Cast
| ActiveRadarrBlock::Crew => {
draw_large_popup_over(f, app, content_rect, draw_library, draw_movie_info)
}
ActiveRadarrBlock::CollectionDetails | ActiveRadarrBlock::ViewMovieOverview => {
draw_large_popup_over(
f,
app,
content_rect,
draw_collections,
draw_collection_details_popup,
)
}
_ => (),
}
}
}
pub(super) fn draw_radarr_context_row<B: Backend>(f: &mut Frame<'_, B>, app: &App, area: Rect) {
let chunks = horizontal_chunks(vec![Constraint::Ratio(1, 2), Constraint::Ratio(1, 2)], area);
draw_stats_context(f, app, chunks[0]);
draw_downloads_context(f, app, chunks[1]);
}
fn draw_library<B: Backend>(f: &mut Frame<'_, B>, app: &mut App, area: Rect) {
let quality_profile_map = &app.data.radarr_data.quality_profile_map;
let downloads_vec = &app.data.radarr_data.downloads.items;
draw_table(
f,
area,
layout_block_top_border(),
TableProps {
content: &mut app.data.radarr_data.movies,
table_headers: vec![
"Title",
"Year",
"Runtime",
"Rating",
"Language",
"Size",
"Quality Profile",
],
constraints: vec![
Constraint::Percentage(25),
Constraint::Percentage(12),
Constraint::Percentage(12),
Constraint::Percentage(12),
Constraint::Percentage(12),
Constraint::Percentage(12),
Constraint::Percentage(12),
],
},
|movie| {
let (hours, minutes) = convert_runtime(movie.runtime.as_u64().unwrap());
let file_size: f64 = convert_to_gb(movie.size_on_disk.as_u64().unwrap());
let certification = movie.certification.clone().unwrap_or_else(|| "".to_owned());
Row::new(vec![
Cell::from(movie.title.to_owned()),
Cell::from(movie.year.to_string()),
Cell::from(format!("{}h {}m", hours, minutes)),
Cell::from(certification),
Cell::from(movie.original_language.name.to_owned()),
Cell::from(format!("{:.2} GB", file_size)),
Cell::from(
quality_profile_map
.get(&movie.quality_profile_id.as_u64().unwrap())
.unwrap()
.to_owned(),
),
])
.style(determine_row_style(downloads_vec, movie))
},
app.is_loading,
);
}
fn draw_search_box<B: Backend>(f: &mut Frame<'_, B>, app: &mut App, area: Rect) {
let chunks = vertical_chunks_with_margin(vec![Constraint::Length(3)], area, 1);
if !app.data.radarr_data.is_searching {
let input = Paragraph::new("Movie not found!")
.style(style_failure())
.block(title_block("Search"));
f.set_cursor(
chunks[0].x + app.data.radarr_data.search.len() as u16 + 1,
chunks[0].y + 1,
);
f.render_widget(input, chunks[0]);
} else {
let input = Paragraph::new(app.data.radarr_data.search.as_ref())
.style(style_default())
.block(title_block("Search"));
f.set_cursor(
chunks[0].x + app.data.radarr_data.search.len() as u16 + 1,
chunks[0].y + 1,
);
f.render_widget(input, chunks[0]);
}
}
fn draw_downloads_context<B: Backend>(f: &mut Frame<'_, B>, app: &App, area: Rect) {
let block = title_block("Downloads");
let downloads_vec = &app.data.radarr_data.downloads.items;
if !downloads_vec.is_empty() {
f.render_widget(block, area);
let constraints = iter::repeat(Constraint::Min(2))
.take(downloads_vec.len())
.collect::<Vec<Constraint>>();
let chunks = vertical_chunks_with_margin(constraints, area, 1);
for i in 0..downloads_vec.len() {
let DownloadRecord {
title,
sizeleft,
size,
..
} = &downloads_vec[i];
let percent = 1f64 - (sizeleft.as_f64().unwrap() / size.as_f64().unwrap());
let download_gague = line_gauge_with_title(title, percent);
f.render_widget(download_gague, chunks[i]);
}
} else {
loading(f, block, area, app.is_loading);
}
}
fn draw_downloads<B: Backend>(f: &mut Frame<'_, B>, app: &mut App, area: Rect) {
let current_selection = if app.data.radarr_data.downloads.items.is_empty() {
DownloadRecord::default()
} else {
app.data.radarr_data.downloads.current_selection_clone()
};
let width = (area.width as f32 * 0.30) as usize;
draw_table(
f,
area,
layout_block_top_border(),
TableProps {
content: &mut app.data.radarr_data.downloads,
table_headers: vec![
"Title",
"Percent Complete",
"Size",
"Output Path",
"Indexer",
"Download Client",
],
constraints: vec![
Constraint::Percentage(30),
Constraint::Percentage(11),
Constraint::Percentage(11),
Constraint::Percentage(18),
Constraint::Percentage(17),
Constraint::Percentage(13),
],
},
|download_record| {
let DownloadRecord {
title,
size,
sizeleft,
download_client,
indexer,
output_path,
..
} = download_record;
if current_selection == *download_record && output_path.text.len() > width {
output_path.scroll_text()
} else {
output_path.reset_offset();
}
let percent = 1f64 - (sizeleft.as_f64().unwrap() / size.as_f64().unwrap());
let file_size: f64 = convert_to_gb(size.as_u64().unwrap());
Row::new(vec![
Cell::from(title.to_owned()),
Cell::from(format!("{:.0}%", percent * 100.0)),
Cell::from(format!("{:.2} GB", file_size)),
Cell::from(output_path.to_string()),
Cell::from(indexer.to_owned()),
Cell::from(download_client.to_owned()),
])
.style(style_primary())
},
app.is_loading,
);
}
fn draw_collections<B: Backend>(f: &mut Frame<'_, B>, app: &mut App, area: Rect) {
let quality_profile_map = &app.data.radarr_data.quality_profile_map;
draw_table(
f,
area,
layout_block_top_border(),
TableProps {
content: &mut app.data.radarr_data.collections,
table_headers: vec![
"Collection",
"Search on Add?",
"Number of Movies",
"Root Folder Path",
"Quality Profile",
],
constraints: iter::repeat(Constraint::Ratio(1, 5)).take(5).collect(),
},
|collection| {
let number_of_movies = collection.movies.clone().unwrap_or_default().len();
Row::new(vec![
Cell::from(collection.title.to_owned()),
Cell::from(collection.search_on_add.to_string()),
Cell::from(number_of_movies.to_string()),
Cell::from(collection.root_folder_path.clone().unwrap_or_default()),
Cell::from(
quality_profile_map
.get(&collection.quality_profile_id.as_u64().unwrap())
.unwrap()
.to_owned(),
),
])
.style(style_primary())
},
app.is_loading,
);
}
fn draw_stats_context<B: Backend>(f: &mut Frame<'_, B>, app: &App, area: Rect) {
let block = title_block("Stats");
if !app.data.radarr_data.version.is_empty() {
f.render_widget(block, area);
let RadarrData {
disk_space_vec,
start_time,
..
} = &app.data.radarr_data;
let mut constraints = vec![
Constraint::Percentage(60),
Constraint::Length(1),
Constraint::Length(1),
Constraint::Length(1),
];
constraints.append(
&mut iter::repeat(Constraint::Min(2))
.take(disk_space_vec.len())
.collect(),
);
let chunks = vertical_chunks_with_margin(constraints, area, 1);
let version_paragraph = Paragraph::new(Text::from(format!(
"Radarr Version: {}",
app.data.radarr_data.version
)))
.block(borderless_block());
let uptime = Utc::now().sub(start_time.to_owned());
let days = uptime.num_days();
let day_difference = uptime.sub(Duration::days(days));
let hours = day_difference.num_hours();
let hour_difference = day_difference.sub(Duration::hours(hours));
let minutes = hour_difference.num_minutes();
let seconds = hour_difference
.sub(Duration::minutes(minutes))
.num_seconds();
let uptime_paragraph = Paragraph::new(Text::from(format!(
"Uptime: {}d {:0width$}:{:0width$}:{:0width$}",
days,
hours,
minutes,
seconds,
width = 2
)))
.block(borderless_block());
let mut logo_text = Text::from(RADARR_LOGO);
logo_text.patch_style(Style::default().fg(Color::LightYellow));
let logo = Paragraph::new(logo_text)
.block(borderless_block())
.alignment(Alignment::Center);
let storage =
Paragraph::new(Text::from("Storage:")).block(borderless_block().style(style_bold()));
f.render_widget(logo, chunks[0]);
f.render_widget(version_paragraph, chunks[1]);
f.render_widget(uptime_paragraph, chunks[2]);
f.render_widget(storage, chunks[3]);
for i in 0..disk_space_vec.len() {
let DiskSpace {
free_space,
total_space,
} = &disk_space_vec[i];
let title = format!("Disk {}", i + 1);
let ratio = if total_space.as_u64().unwrap() == 0 {
0f64
} else {
1f64 - (free_space.as_u64().unwrap() as f64 / total_space.as_u64().unwrap() as f64)
};
let space_gauge = line_gauge_with_label(title.as_str(), ratio);
f.render_widget(space_gauge, chunks[i + 4]);
}
} else {
loading(f, block, area, app.is_loading);
}
}
fn determine_row_style(downloads_vec: &[DownloadRecord], movie: &Movie) -> Style {
if !movie.has_file {
if let Some(download) = downloads_vec
.iter()
.find(|&download| download.movie_id == movie.id)
{
if download.status == "downloading" {
return style_warning();
}
}
return style_failure();
}
style_success()
}