//! Kezdőlap: the admin's setup checklist, the spotlight (popular movies and //! series taking turns), "Folytasd, ahol abbahagytad", the server's rows. use std::any::Any; use std::collections::HashMap; use std::time::Instant; use crate::api::{HomeData, MediaType, NcoreStatus, TitleState, TitleSummary, VpnStatus}; use crate::app::{Cx, Look, Screen}; use crate::page::{Ch, Frame, Kind, L, Spec}; use crate::screens::{self, Load, continue_card, row_copy, title_cards}; use crate::{format, player}; const DWELL_SECS: u64 = 9; pub struct Home { data: Load, states: HashMap, setup: Option<(VpnStatus, NcoreStatus)>, /// The server answered 409: nothing can play until VPN / nCore are set up. needs_setup: bool, spotlight: Vec, index: usize, since: Instant, hero_focused: bool, loaded: bool, } impl Home { pub fn new() -> Home { Home { data: Load::Loading, states: HashMap::new(), setup: None, needs_setup: false, spotlight: Vec::new(), index: 0, since: Instant::now(), hero_focused: true, loaded: false, } } fn load(&mut self, cx: &Cx) { let api = cx.api(); let admin = cx.user().is_admin; cx.spawn::( move || { let home = api.home(); let states = api.list_status().unwrap_or_default(); let setup = if admin { api.vpn().ok().zip(api.ncore().ok()) } else { None }; (home, states, setup) }, |s, cx, (home, states, setup)| { s.needs_setup = matches!(&home, Err(e) if crate::api::status_of(e) == Some(409)); s.data = if s.needs_setup { Load::Ready(HomeData { rows: Vec::new(), continue_watching: Vec::new() }) } else { Load::from(home, cx) }; s.states = states; s.setup = setup; if let Some(d) = s.data.ready() { s.spotlight = spotlight(&d.rows); s.index = s.index.min(s.spotlight.len().saturating_sub(1)); } // The spotlight's picture also shows under the menu. if s.hero_focused { s.focus(cx, "hero", ""); } }, ); } fn current(&self) -> Option<&TitleSummary> { self.spotlight.get(self.index) } fn find(&self, key: &str) -> Option { let d = self.data.ready()?; if let Some((kind, id)) = screens::parse_title_key(key) { return d.rows.iter().flat_map(|r| r.items.iter()).chain(self.spotlight.iter()).find(|t| t.kind == kind && t.id == id).cloned(); } if let Some((kind, id, _)) = screens::parse_continue(key) { return d.continue_watching.iter().map(|c| &c.title).find(|t| t.kind == kind && t.id == id).cloned(); } None } } /// Popular movies and series, alternating, that have artwork worth showing. fn spotlight(rows: &[crate::api::HomeRow]) -> Vec { let pick = |id: &str| -> Vec { rows.iter().find(|r| r.id == id).map(|r| r.items.iter().filter(|t| t.backdrop.is_some()).cloned().collect()).unwrap_or_default() }; let (movies, series) = (pick("popular-movie"), pick("popular-tv")); let mut out = Vec::new(); let mut i = 0; while out.len() < 6 && (i < movies.len() || i < series.len()) { if let Some(m) = movies.get(i) { out.push(m.clone()); } if let Some(s) = series.get(i).filter(|_| out.len() < 6) { out.push(s.clone()); } i += 1; } if out.is_empty() { out.extend(rows.first().map(|r| r.items.iter().filter(|t| t.backdrop.is_some()).take(6).cloned().collect::>()).unwrap_or_default()); } out } pub fn hu_text(t: &TitleSummary) -> &'static str { match t.hu { Some(h) if h.dub => "Magyar szinkron", Some(h) if h.sub => "Magyar felirat", _ => "", } } /// "★ 7.8 · 2024 · Film · Dráma, Krimi · Magyar szinkron" pub fn meta_line(t: &TitleSummary, extra: &str) -> String { let mut p: Vec = Vec::new(); if let Some(r) = t.rating.filter(|r| *r > 0.0) { p.push(format!("★ {}", format::rating(r))); } if let Some(y) = t.year_label.clone().or_else(|| t.year.map(|y| y.to_string())) { p.push(y); } p.push(t.kind.label().into()); if !extra.is_empty() { p.push(extra.into()); } if !t.genres.is_empty() { p.push(t.genres.iter().take(2).map(|g| format::genre(g)).collect::>().join(", ")); } let hu = hu_text(t); if !hu.is_empty() { p.push(hu.into()); } p.join(" · ") } impl Screen for Home { fn spec(&mut self, cx: &Cx) -> Spec { if !self.loaded { self.loaded = true; self.load(cx); } let api = cx.api(); let mut lines = Vec::new(); // Setup checklist (admin): VPN and nCore. if let Some((vpn, ncore)) = &self.setup { let vpn_ok = vpn.traffic.path == "vpn"; let ncore_ok = ncore.configured && ncore.circuit.state == "ok"; let left = [vpn_ok, ncore_ok].iter().filter(|b| !**b).count(); if left > 0 || self.needs_setup { let title = match left { 0 => "Minden készen áll".to_string(), 1 => "Még egy lépés, és indulhat a filmezés".to_string(), _ => "Még két lépés, és indulhat a filmezés".to_string(), }; lines.push(L::notice("setup", "info", title, "Tunnel nélkül semmi nem megy ki a szerverről, a katalógus sem tölt be. Az nCore fiókból jönnek a filmek és a sorozatok.")); lines.push(L::checks( "setup-checks", vec![ (vpn_ok as i32, "WireGuard tunnel csatlakoztatva".into(), vpn.traffic.reason.clone().unwrap_or_default()), (ncore_ok as i32, "nCore fiók megadva".into(), ncore.circuit.message.clone().unwrap_or_default()), ], )); let mut chips = Vec::new(); if !vpn_ok { chips.push(Ch::new("vpn", "VPN beállítása").tone("primary")); } if !ncore_ok { chips.push(Ch::new("ncore", "nCore fiók megadása").tone("primary")); } if !chips.is_empty() { lines.push(L::chips("setup-go", "", chips)); } } } else if self.needs_setup { lines.push(L::notice("setup", "info", "A szerver még nincs kész", "A VPN-t és az nCore fiókot az admin állítja be. Amint kész, itt megjelennek a filmek és a sorozatok.")); } // Spotlight. if let Some(t) = self.current() { let listed = self.states.get(&format!("{}:{}", t.kind.key(), t.id)).is_some_and(|s| s.listed); let mut hero = L::new("hero", Kind::Hero); hero.style = "KIEMELT TARTALOM".into(); hero.title = t.title.clone(); hero.logo = api.image(t.logo.as_deref(), true); hero.meta = meta_line(t, ""); hero.text = t.overview.clone().unwrap_or_default(); hero.subtitle = format!("{} / {}", self.index + 1, self.spotlight.len()); hero.chips = vec![ Ch::new("play", if t.kind == MediaType::Movie { "Lejátszás" } else { "Epizódok" }).icon("play").tone("primary"), Ch::new("info", "További infók").icon("info"), Ch::new("list", "").icon(if listed { "check" } else { "plus" }).active(listed), ]; lines.push(hero); } if let Some(st) = self.data.status("status") { lines.push(st); } if let Some(d) = self.data.ready() { if !d.continue_watching.is_empty() { lines.push(L::cards("continue", "Folytasd, ahol abbahagytad", "landscape", d.continue_watching.iter().map(|c| continue_card(&api, c)).collect()).captions()); } for r in &d.rows { let (title, note) = row_copy(&r.id).map(|(t, n)| (t.to_string(), n.to_string())).unwrap_or((r.title.clone(), String::new())); lines.push(L::cards(format!("row:{}", r.id), title, "poster", title_cards(&api, &r.items, &self.states)).sub(note)); } } Spec { lines, frame: Frame { nav: Some(0), ..Frame::default() } } } fn activate(&mut self, cx: &Cx, line: &str, item: &str) { match (line, item) { ("setup-go", "vpn") => cx.c.open_settings("vpn"), ("setup-go", "ncore") => cx.c.open_settings("ncore"), ("hero", "play") => { let Some(t) = self.current().cloned() else { return }; if t.kind == MediaType::Movie { cx.play(player::Request::title(t.kind, &t.id, t.title.clone(), None, t.backdrop.clone())); } else { cx.push(Box::new(screens::detail::Detail::new(t.kind, &t.id.clone(), Some(t.clone())))); } } ("hero", "info") => { if let Some(t) = self.current().cloned() { cx.push(Box::new(screens::detail::Detail::new(t.kind, &t.id.clone(), Some(t.clone())))); } } ("hero", "list") => { let Some(t) = self.current().cloned() else { return }; let key = format!("{}:{}", t.kind.key(), t.id); let on = !self.states.get(&key).is_some_and(|s| s.listed); self.states.entry(key).or_default().listed = on; let api = cx.api(); cx.call::("Listám", move || api.set_listed(t.kind, &t.id, on), |_, _| {}); cx.toast(if on { "Hozzáadva a listádhoz" } else { "Eltávolítva a listádról" }); } (_, key) => { let found = self.find(key); screens::open_card(cx, key, |_| found.clone()); } } } fn focus(&mut self, cx: &Cx, line: &str, item: &str) { self.hero_focused = line == "hero"; self.since = Instant::now(); if self.hero_focused { if let Some(t) = self.current() { let url = cx.api().image(t.backdrop.as_deref(), false); cx.backdrop(&url, Look::Sharp, &format!("spot:{}", t.id)); } return; } match self.find(item) { Some(t) => { let url = cx.api().image(t.backdrop.as_deref(), false); cx.backdrop(&url, Look::Soft, item); } None => cx.backdrop("", Look::Soft, ""), } } fn tick(&mut self, cx: &Cx) { // The spotlight turns after a while without a key press (any focus // move restarts the clock): Play always plays what it shows. if self.spotlight.len() >= 2 && self.since.elapsed().as_secs() >= DWELL_SECS { self.since = Instant::now(); self.index = (self.index + 1) % self.spotlight.len(); cx.refresh(); if self.hero_focused { self.focus(cx, "hero", ""); } } } fn shown(&mut self, cx: &Cx) { self.load(cx); } fn as_any(&mut self) -> &mut dyn Any { self } }