use std::collections::HashMap;
use std::time::{Duration, Instant};
#[derive(Default)]
pub struct TypingState {
pub indicators: HashMap<String, HashMap<String, Instant>>,
pub sent: bool,
pub last_keypress: Option<Instant>,
}
impl TypingState {
pub fn cleanup(&mut self) -> bool {
let now = Instant::now();
let mut changed = false;
for senders in self.indicators.values_mut() {
let before = senders.len();
senders.retain(|_, ts| now.duration_since(*ts).as_secs() < 5);
if senders.len() != before {
changed = true;
}
}
self.indicators.retain(|_, senders| !senders.is_empty());
changed
}
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
}
}