Terminal interface to find, download, and stream movies, TV shows, and live TV using local media players.
feat: track watch history for movies and series
Samir Hossains9f89e1dbfdab
diff --git a/src/history.rs b/src/history.rsnew file mode 100644index 0000000..1fed833--- /dev/null+++ b/src/history.rs@@ -0,0 +1,57 @@+use serde::{Deserialize, Serialize};+use std::collections::HashSet;+use std::fs;+use std::path::PathBuf;++#[derive(Default, Serialize, Deserialize)]+pub struct HistoryManager {+ watched: HashSet<String>,+}++impl HistoryManager {+ pub fn new() -> Self {+ if let Some(path) = Self::history_file_path() {+ if path.exists() {+ if let Ok(content) = fs::read_to_string(&path) {+ if let Ok(history) = serde_json::from_str(&content) {+ return history;+ }+ }+ }+ }+ Self::default()+ }++ fn history_file_path() -> Option<PathBuf> {+ let mut path = dirs::data_dir()?;+ path.push("moviebox-tui");+ if !path.exists() {+ let _ = fs::create_dir_all(&path);+ }+ path.push("history.json");+ Some(path)+ }++ pub fn save(&self) {+ if let Some(path) = Self::history_file_path() {+ if let Ok(content) = serde_json::to_string(self) {+ let _ = fs::write(path, content);+ }+ }+ }++ fn key(provider: &str, subject_id: &str, season: usize, episode: usize) -> String {+ format!("{provider}::{subject_id}::{season}::{episode}")+ }++ pub fn mark_watched(&mut self, provider: &str, subject_id: &str, season: usize, episode: usize) {+ let key = Self::key(provider, subject_id, season, episode);+ self.watched.insert(key);+ self.save();+ }++ pub fn is_watched(&self, provider: &str, subject_id: &str, season: usize, episode: usize) -> bool {+ let key = Self::key(provider, subject_id, season, episode);+ self.watched.contains(&key)+ }+}diff --git a/src/lib.rs b/src/lib.rsindex 8b36830..3bea6c7 100644--- a/src/lib.rs+++ b/src/lib.rs@@ -1,4 +1,5 @@pub mod cache;pub mod download;+pub mod history;pub mod providers;pub mod tui;diff --git a/src/tui/app.rs b/src/tui/app.rsindex f1a345a..8d9ac95 100644--- a/src/tui/app.rs+++ b/src/tui/app.rs@@ -147,12 +147,20 @@ impl App {}fn launch_player(- &self,+ &mut self,kind: crate::tui::state::PlayerKind,link: String,subtitle: Option<String>,headers: Vec<(String, String)>,) {+ if let Some(subject_id) = &self.state.active_subject_id {+ let season = self.state.selected_season;+ let episode = self.state.selected_episode;+ let provider = self.state.active_provider.cache_key();+ self.state.history.mark_watched(provider, subject_id, season, episode);+ self.state.dirty = true;+ }+let client = self.client.http_client().clone();let sender = self.action_sender.clone();tokio::spawn(async move {diff --git a/src/tui/screens/details.rs b/src/tui/screens/details.rsindex 97c66ba..e2e7463 100644--- a/src/tui/screens/details.rs+++ b/src/tui/screens/details.rs@@ -316,9 +316,40 @@ pub fn draw(frame: &mut Frame, area: Rect, state: &mut AppState, theme: &Theme)frame.render_widget(placeholder, poster_area);}+ let is_currently_watched = if let Some(subject_id) = state.active_subject_id.as_deref() {+ let provider = state.active_provider.cache_key();+ let (se, ep) = if type_val == 2 {+ let se_idx = state.season_list_state.selected().unwrap_or(0);+ let ep_idx = state.episode_list_state.selected().unwrap_or(0);+ let se_num = state+ .available_seasons+ .get(se_idx)+ .and_then(|s| s.get("se"))+ .and_then(|v| v.as_i64())+ .unwrap_or(1) as usize;+ let ep_num = if let Some(eps) = state.available_episode_numbers.get(se_idx) {+ eps.get(ep_idx).copied().unwrap_or(ep_idx + 1)+ } else {+ ep_idx + 1+ };+ (se_num, ep_num)+ } else {+ (0, 0)+ };+ state.history.is_watched(provider, subject_id, se, ep)+ } else {+ false+ };++ let display_title = if is_currently_watched {+ format!("✓ {title}")+ } else {+ title.to_string()+ };+let title_line = Line::from(vec![Span::styled(- title.to_string(),+ display_title,theme.text.add_modifier(ratatui::style::Modifier::BOLD),),Span::styled(" ", theme.text),@@ -623,9 +654,25 @@ pub fn draw(frame: &mut Frame, area: Rect, state: &mut AppState, theme: &Theme).available_episode_numbers.get(state.season_list_state.selected().unwrap_or(0)){+ let season_idx = state.season_list_state.selected().unwrap_or(0);+ let se_num = state+ .available_seasons+ .get(season_idx)+ .and_then(|s| s.get("se"))+ .and_then(|v| v.as_i64())+ .unwrap_or(1) as usize;+ let subject_id = state.active_subject_id.as_deref().unwrap_or("");+ let provider = state.active_provider.cache_key();+ep_numbers.iter()- .map(|&ep| ListItem::new(format!("Episode {}", ep)).style(theme.text))+ .map(|&ep| {+ if state.history.is_watched(provider, subject_id, se_num, ep) {+ ListItem::new(format!("✓ Episode {}", ep)).style(theme.text_dim)+ } else {+ ListItem::new(format!("Episode {}", ep)).style(theme.text)+ }+ }).collect()} else {vec![]@@ -1330,13 +1377,28 @@ fn render_workflow(format!("Season {}", state.selected_season)},));+ let is_watched = if let Some(subject_id) = state.active_subject_id.as_deref() {+ let provider = state.active_provider.cache_key();+ state.history.is_watched(provider, subject_id, state.selected_season, state.selected_episode)+ } else {+ false+ };++ let ep_label = if compact {+ format!("E{}", state.selected_episode)+ } else {+ format!("Episode {}", state.selected_episode)+ };++ let ep_label = if is_watched {+ format!("✓ {ep_label}")+ } else {+ ep_label+ };+steps.push((crate::tui::state::DetailsPane::Episodes,- if compact {- format!("E{}", state.selected_episode)- } else {- format!("Episode {}", state.selected_episode)- },+ ep_label,));}steps.push((diff --git a/src/tui/state.rs b/src/tui/state.rsindex bc648b8..7d4eaac 100644--- a/src/tui/state.rs+++ b/src/tui/state.rs@@ -155,6 +155,7 @@ pub struct AppState {pub tv_wizard_options: Vec<String>,pub tv_wizard_selected_idx: usize,pub tv_wizard_selections: std::collections::HashSet<String>,+ pub history: crate::history::HistoryManager,}impl Default for AppState {@@ -260,6 +261,7 @@ impl Default for AppState {],tv_wizard_selected_idx: 0,tv_wizard_selections: std::collections::HashSet::new(),+ history: crate::history::HistoryManager::new(),}}}