Skip to content

Terminal interface to find, download, and stream movies, TV shows, and live TV using local media players.

refactor(tui): encapsulate async request handles and modal surface rendering

Samir Hossainsac6854868f8f
diff --git a/src/tui/app/mod.rs b/src/tui/app/mod.rs
index f03c673..5039774 100644
--- a/src/tui/app/mod.rs
+++ b/src/tui/app/mod.rs
@@ -1,3 +1,5 @@
+use std::future::Future;
+
use tokio::sync::mpsc;
use crate::providers::models::RequestContext;
@@ -77,6 +79,70 @@ impl RequestTaskHandles {
}
}
+ pub fn spawn_search<F>(&mut self, future: F)
+ where
+ F: Future<Output = ()> + Send + 'static,
+ {
+ self.cancel_search();
+ self.search = Some(tokio::spawn(future));
+ }
+
+ pub fn spawn_details<F>(&mut self, future: F)
+ where
+ F: Future<Output = ()> + Send + 'static,
+ {
+ self.cancel_details();
+ self.details = Some(tokio::spawn(future));
+ }
+
+ pub fn spawn_streams<F>(&mut self, future: F)
+ where
+ F: Future<Output = ()> + Send + 'static,
+ {
+ self.cancel_streams();
+ self.streams = Some(tokio::spawn(future));
+ }
+
+ pub fn spawn_suggest<F>(&mut self, future: F)
+ where
+ F: Future<Output = ()> + Send + 'static,
+ {
+ self.cancel_suggest();
+ self.suggest = Some(tokio::spawn(future));
+ }
+
+ pub fn spawn_homepage<F>(&mut self, future: F)
+ where
+ F: Future<Output = ()> + Send + 'static,
+ {
+ self.cancel_homepage();
+ self.homepage = Some(tokio::spawn(future));
+ }
+
+ pub fn spawn_download<F>(&mut self, future: F)
+ where
+ F: Future<Output = ()> + Send + 'static,
+ {
+ self.cancel_download();
+ self.download = Some(tokio::spawn(future));
+ }
+
+ pub fn spawn_stream_pool_init<F>(&mut self, future: F)
+ where
+ F: Future<Output = ()> + Send + 'static,
+ {
+ self.cancel_stream_pool_init();
+ self.stream_pool_init = Some(tokio::spawn(future));
+ }
+
+ pub fn spawn_episode_prefetch<F>(&mut self, future: F)
+ where
+ F: Future<Output = ()> + Send + 'static,
+ {
+ self.cancel_episode_prefetch();
+ self.episode_prefetch = Some(tokio::spawn(future));
+ }
+
pub fn cancel_all(&mut self) {
self.cancel_search();
self.cancel_details();
diff --git a/src/tui/app/requests.rs b/src/tui/app/requests.rs
index 2a764a5..f4b3e36 100644
--- a/src/tui/app/requests.rs
+++ b/src/tui/app/requests.rs
@@ -37,17 +37,16 @@ impl App {
return None;
}
- self.request_tasks.cancel_suggest();
let service = self.service.clone();
let sender = self.action_sender.clone();
let query_clone = query.clone();
- self.request_tasks.suggest = Some(tokio::spawn(async move {
+ self.request_tasks.spawn_suggest(async move {
if let Ok(res) = service.suggest(&query_clone).await {
sender
.send(Action::SuggestSuccess(request_id, query_clone, res))
.ok();
}
- }));
+ });
}
Action::SuggestSuccess(request_id, query, suggestions) => {
@@ -258,8 +257,7 @@ impl App {
let manifest_url = target.manifest_url.clone();
let r_type = target.r#type.clone();
let cat_id = target.catalog_id.clone();
- self.request_tasks.cancel_search();
- self.request_tasks.search = Some(tokio::spawn(async move {
+ self.request_tasks.spawn_search(async move {
let result = service
.fetch_addon_catalog(&manifest_url, &r_type, &cat_id)
.await;
@@ -281,7 +279,7 @@ impl App {
.ok();
}
}
- }));
+ });
}
Action::SearchSuccess {
@@ -1245,8 +1243,7 @@ impl App {
}
let service = self.service.clone();
let sender = self.action_sender.clone();
- self.request_tasks.cancel_stream_pool_init();
- self.request_tasks.stream_pool_init = Some(tokio::spawn(async move {
+ self.request_tasks.spawn_stream_pool_init(async move {
let resolutions = service
.fetch_collection_resolutions(&subject_id)
.await
@@ -1254,7 +1251,7 @@ impl App {
sender
.send(Action::StreamPoolInitialized(subject_id, resolutions))
.ok();
- }));
+ });
}
Action::StreamPoolInitialized(subject_id, resolutions) => {
@@ -1382,8 +1379,7 @@ impl App {
.unwrap_or_else(|| id.starts_with("series:") || season > 0 || episode > 0);
let has_stream_addons = addons.iter().any(|a| a.enabled && a.provides_stream);
- self.request_tasks.cancel_streams();
- self.request_tasks.streams = Some(tokio::spawn(async move {
+ self.request_tasks.spawn_streams(async move {
if !has_stream_addons {
sender
.send(Action::EpisodeStreamsFailed(
@@ -1444,7 +1440,7 @@ impl App {
))
.ok();
}
- }));
+ });
return None;
}
@@ -1458,8 +1454,7 @@ impl App {
let circleftp_client = self.service.circleftp_client.clone();
let dhakaflix_client = self.service.dhakaflix_client.clone();
let id = subject_id.clone();
- self.request_tasks.cancel_streams();
- self.request_tasks.streams = Some(tokio::spawn(async move {
+ self.request_tasks.spawn_streams(async move {
let result = match context.provider {
ProviderKind::FourKHdHub => {
if let Some(client) = fourk_client.as_ref() {
@@ -1547,7 +1542,7 @@ impl App {
.ok();
}
}
- }));
+ });
return None;
}
@@ -1561,7 +1556,7 @@ impl App {
let sender = self.action_sender.clone();
let cached = cached.clone();
let cached_subject_id = subject_id.clone();
- tokio::spawn(async move {
+ self.request_tasks.spawn_streams(async move {
tokio::time::sleep(std::time::Duration::from_millis(120)).await;
sender
.send(Action::EpisodeStreamsReady(
@@ -1582,8 +1577,7 @@ impl App {
let sender = self.action_sender.clone();
let id_clone = subject_id.clone();
- self.request_tasks.cancel_streams();
- self.request_tasks.streams = Some(tokio::spawn(async move {
+ self.request_tasks.spawn_streams(async move {
sender
.send(Action::SetStatus("Fetching streams...".to_string()))
.ok();
@@ -1613,7 +1607,7 @@ impl App {
.ok();
}
}
- }));
+ });
}
Action::EpisodeStreamsReady(
diff --git a/src/tui/app/run.rs b/src/tui/app/run.rs
index d28f877..eb8ee14 100644
--- a/src/tui/app/run.rs
+++ b/src/tui/app/run.rs
@@ -155,13 +155,8 @@ impl App {
return;
}
Some(ForcedProtocol::Type(protocol)) => {
- let font_size = Self::cell_size_override().unwrap_or(ratatui_image::FontSize {
- width: 10,
- height: 20,
- });
- #[allow(deprecated)]
- let mut picker = ratatui_image::picker::Picker::from_fontsize(font_size);
- picker.set_protocol_type(protocol);
+ let picker =
+ Self::create_picker_with_font_size(Self::cell_size_override(), protocol);
self.accept_picker(picker);
return;
}
@@ -190,19 +185,14 @@ impl App {
let mut picker = match (picker, Self::cell_size_override()) {
(Some(picker), None) => picker,
(probed, cell_size) => {
- let font_size = cell_size.unwrap_or(ratatui_image::FontSize {
- width: 10,
- height: 20,
- });
- #[allow(deprecated)]
- let mut rebuilt = ratatui_image::picker::Picker::from_fontsize(font_size);
- if let Some(probed) = probed {
- rebuilt.set_protocol_type(probed.protocol_type());
- }
- if let Some(ForcedProtocol::Type(protocol)) = Self::forced_protocol() {
- rebuilt.set_protocol_type(protocol);
- }
- rebuilt
+ let target_protocol = Self::forced_protocol()
+ .and_then(|p| match p {
+ ForcedProtocol::Type(proto) => Some(proto),
+ _ => None,
+ })
+ .or_else(|| probed.as_ref().map(|p| p.protocol_type()))
+ .unwrap_or(ProtocolType::Halfblocks);
+ Self::create_picker_with_font_size(cell_size, target_protocol)
}
};
@@ -291,6 +281,22 @@ impl App {
}
Some(ratatui_image::FontSize { width, height })
}
+
+ fn create_picker_with_font_size(
+ font_size: Option<ratatui_image::FontSize>,
+ protocol: ratatui_image::picker::ProtocolType,
+ ) -> ratatui_image::picker::Picker {
+ let mut picker = match font_size {
+ Some(size) =>
+ {
+ #[allow(deprecated)]
+ ratatui_image::picker::Picker::from_fontsize(size)
+ }
+ None => ratatui_image::picker::Picker::halfblocks(),
+ };
+ picker.set_protocol_type(protocol);
+ picker
+ }
pub fn contextual_title(&self) -> String {
match self.state.active_screen {
Screen::Home => match self.state.mode() {
@@ -732,9 +738,8 @@ impl App {
})
.border_type(crate::tui::overlay::border_type(basic));
- let inner_area = block.inner(dl_area);
- crate::tui::clear_area(frame, dl_area, &self.theme);
- frame.render_widget(block, dl_area);
+ let inner_area =
+ crate::tui::overlay::render_modal_frame(frame, dl_area, block, &self.theme);
if inner_area.height == 0 || inner_area.width < 10 {
return;
diff --git a/src/tui/overlay.rs b/src/tui/overlay.rs
index b0048ed..bd9d9e0 100644
--- a/src/tui/overlay.rs
+++ b/src/tui/overlay.rs
@@ -582,6 +582,18 @@ pub fn clear_modal_area(frame: &mut Frame, _bounds: Rect, popup: Rect, theme: &T
crate::tui::clear_area(frame, popup, theme);
}
+pub fn render_modal_frame(
+ frame: &mut Frame,
+ popup_area: Rect,
+ block: Block,
+ theme: &Theme,
+) -> Rect {
+ clear_modal_area(frame, popup_area, popup_area, theme);
+ let inner_area = block.inner(popup_area);
+ frame.render_widget(block, popup_area);
+ inner_area
+}
+
pub fn border_type(basic_terminal: bool) -> BorderType {
if basic_terminal {
BorderType::Plain