pinakes/crates/pinakes-tui/src/event.rs
NotAShelf 66861b8a20
pinakes-tui: add book management view and api key authentication
Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I20f205d9e06a93a89e8f4433ed6f80576a6a6964
2026-03-08 00:43:31 +03:00

76 lines
2 KiB
Rust

use std::time::Duration;
use crossterm::event::{self, Event as CrosstermEvent, KeyEvent};
use tokio::sync::mpsc;
#[derive(Debug)]
pub enum AppEvent {
Key(KeyEvent),
Tick,
ApiResult(ApiResult),
}
#[derive(Debug)]
pub enum ApiResult {
MediaList(Vec<crate::client::MediaResponse>),
SearchResults(crate::client::SearchResponse),
AllTags(Vec<crate::client::TagResponse>),
Collections(Vec<crate::client::CollectionResponse>),
ImportDone(crate::client::ImportResponse),
ScanDone(Vec<crate::client::ScanResponse>),
AuditLog(Vec<crate::client::AuditEntryResponse>),
Duplicates(Vec<crate::client::DuplicateGroupResponse>),
DatabaseStats(crate::client::DatabaseStatsResponse),
Statistics(crate::client::LibraryStatisticsResponse),
ScheduledTasks(Vec<crate::client::ScheduledTaskResponse>),
BooksList(Vec<crate::client::MediaResponse>),
BookSeries(Vec<crate::client::SeriesSummary>),
BookAuthors(Vec<crate::client::AuthorSummary>),
MediaUpdated,
ReadingProgressUpdated,
Error(String),
}
pub struct EventHandler {
tx: mpsc::UnboundedSender<AppEvent>,
rx: mpsc::UnboundedReceiver<AppEvent>,
}
impl EventHandler {
pub fn new(tick_rate: Duration) -> Self {
let (tx, rx) = mpsc::unbounded_channel();
let event_tx = tx.clone();
std::thread::spawn(move || {
loop {
match event::poll(tick_rate) {
Ok(true) => {
if let Ok(CrosstermEvent::Key(key)) = event::read()
&& event_tx.send(AppEvent::Key(key)).is_err()
{
break;
}
},
Ok(false) => {
if event_tx.send(AppEvent::Tick).is_err() {
break;
}
},
Err(e) => {
tracing::warn!(error = %e, "event poll failed");
},
}
}
});
Self { tx, rx }
}
pub fn sender(&self) -> mpsc::UnboundedSender<AppEvent> {
self.tx.clone()
}
pub async fn next(&mut self) -> Option<AppEvent> {
self.rx.recv().await
}
}