Files
Owlerlay/src-tauri/src/countdown/service.rs
s0wlz (Matthias Puchstein) d0857d7028 [bugfix] fix countdown snapshot time calculations and unify tokio instants
- switch ClockAnchor to tokio::time::Instant for consistent async timing
- remove incorrect epoch-ms math in Countdown model and expose instant getters
- update CountdownService snapshot to use remaining_at(now) plus start/target instants
- add missing core extern declaration in lib.rs
2026-02-26 16:24:35 +01:00

63 lines
1.8 KiB
Rust

use crate::countdown::model::{Countdown, CountdownError, CountdownState};
use tokio::sync::Mutex;
use tokio::time::{Duration, Instant};
#[derive(Debug)]
pub struct CountdownService {
countdown: Mutex<Countdown>,
}
#[derive(Debug)]
pub struct CountdownSnapshot {
pub id: u64,
pub label: String,
pub state: CountdownState,
pub duration: Duration,
pub start_instant: Option<Instant>,
pub target_instant: Option<Instant>,
}
impl CountdownService {
pub fn default() -> Self {
Self::new(0, "Countdown0", Duration::new(600, 0))
}
pub fn new(id: u64, label: &str, duration: Duration) -> Self {
Self {
countdown: Mutex::new(Countdown::new(id, label, duration)),
}
}
pub async fn snapshot(&self, now: Instant) -> CountdownSnapshot {
let countdown = self.countdown.lock().await.clone();
CountdownSnapshot {
id: countdown.id(),
label: countdown.label().parse().unwrap(),
state: countdown.state(),
duration: countdown.remaining_at(now),
start_instant: countdown.start_timestamp(),
target_instant: countdown.target_timestamp(),
}
}
pub async fn start(&self, now: Instant) -> Result<(), CountdownError> {
let mut countdown = self.countdown.lock().await;
countdown.start(now)
}
pub async fn reset(&self) {
let mut countdown = self.countdown.lock().await;
countdown.reset()
}
pub async fn resume(&self, now: Instant) -> Result<(), CountdownError> {
let mut countdown = self.countdown.lock().await;
countdown.resume(now)
}
pub async fn pause(&self, now: Instant) -> Result<(), CountdownError> {
let mut countdown = self.countdown.lock().await;
countdown.pause(now)
}
}