use std::collections::HashMap;
use std::time::{Duration, Instant};
#[derive(Default)]
pub struct TypingState {
pub indicators: HashMap<String, (String, Instant)>,
pub sent: bool,
pub last_keypress: Option<Instant>,
}
impl TypingState {
pub fn cleanup(&mut self) -> bool {
let before = self.indicators.len();
let now = Instant::now();
self.indicators
.retain(|_, (_, ts)| now.duration_since(*ts).as_secs() < 5);
self.indicators.len() != before
}
pub fn check_timeout(&mut self) -> bool {
if !self.sent {
return false;
}
let elapsed = self
.last_keypress
.map(|t| t.elapsed() > Duration::from_secs(5))
.unwrap_or(false);
if elapsed {
self.sent = false;
self.last_keypress = None;
return true;
}
false
}
pub fn reset(&mut self) -> bool {
let was_typing = self.sent;
self.sent = false;
self.last_keypress = None;
was_typing
}
}