BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / conversation / receipt_fade.rs

receipt_fade.rs

receipt_fade.rs 3.8 KiB Download Raw
1use std::collections::HashMap;
2use std::time::{Duration, Instant};
3
4use crate::rows::Receipt;
5
6pub(super) const RECEIPT_FADE: Duration = Duration::from_millis(600);
7
8/// Marks a receipt that turned from sent to read while the chat was open, never one seen on first paint.
9#[derive(Default)]
10pub(super) struct ReceiptFade {
11 conversation_id: String,
12 previous: HashMap<String, Receipt>,
13 fading: HashMap<String, Instant>,
14}
15
16impl ReceiptFade {
17 /// Returns true when a new fade started, so the caller can rebuild once it is over.
18 pub(super) fn apply(
19 &mut self,
20 conversation_id: &str,
21 settled: bool,
22 receipts: &mut HashMap<String, Receipt>,
23 now: Instant,
24 ) -> bool {
25 if self.conversation_id != conversation_id || !settled {
26 self.conversation_id = conversation_id.to_owned();
27 self.previous.clear();
28 self.fading.clear();
29 if !settled {
30 return false;
31 }
32 }
33 let mut started = false;
34 let baseline_known = !self.previous.is_empty();
35 for (message_id, receipt) in receipts.iter() {
36 if baseline_known
37 && *receipt == Receipt::Read
38 && self.previous.get(message_id) == Some(&Receipt::Sent)
39 {
40 self.fading.insert(message_id.clone(), now);
41 started = true;
42 }
43 }
44 self.fading
45 .retain(|_, since| now.duration_since(*since) < RECEIPT_FADE);
46 self.previous.clone_from(receipts);
47 for (message_id, receipt) in receipts.iter_mut() {
48 if *receipt == Receipt::Read && self.fading.contains_key(message_id) {
49 *receipt = Receipt::ReadLive;
50 }
51 }
52 started
53 }
54}
55
56#[cfg(test)]
57mod tests {
58 use super::*;
59
60 fn receipts(entries: &[(&str, Receipt)]) -> HashMap<String, Receipt> {
61 entries
62 .iter()
63 .map(|(id, receipt)| ((*id).to_owned(), *receipt))
64 .collect()
65 }
66
67 #[test]
68 fn the_first_paint_never_fades() {
69 let mut fade = ReceiptFade::default();
70 let now = Instant::now();
71 let mut first = receipts(&[("a", Receipt::Read)]);
72 assert!(!fade.apply("chat", true, &mut first, now));
73 assert_eq!(first["a"], Receipt::Read);
74 }
75
76 #[test]
77 fn a_change_while_receipts_load_does_not_fade() {
78 let mut fade = ReceiptFade::default();
79 let now = Instant::now();
80 let mut cached = receipts(&[("a", Receipt::Sent)]);
81 fade.apply("chat", false, &mut cached, now);
82 let mut fetched = receipts(&[("a", Receipt::Read)]);
83 assert!(!fade.apply("chat", true, &mut fetched, now));
84 assert_eq!(fetched["a"], Receipt::Read);
85 }
86
87 #[test]
88 fn a_live_sent_to_read_change_fades_once() {
89 let mut fade = ReceiptFade::default();
90 let now = Instant::now();
91 let mut baseline = receipts(&[("a", Receipt::Sent), ("b", Receipt::Sent)]);
92 fade.apply("chat", true, &mut baseline, now);
93 let mut live = receipts(&[("a", Receipt::Read), ("b", Receipt::Sent)]);
94 assert!(fade.apply("chat", true, &mut live, now));
95 assert_eq!(live["a"], Receipt::ReadLive);
96 assert_eq!(live["b"], Receipt::Sent);
97 let mut later = receipts(&[("a", Receipt::Read), ("b", Receipt::Sent)]);
98 assert!(!fade.apply("chat", true, &mut later, now + RECEIPT_FADE));
99 assert_eq!(later["a"], Receipt::Read);
100 }
101
102 #[test]
103 fn switching_chats_drops_the_baseline() {
104 let mut fade = ReceiptFade::default();
105 let now = Instant::now();
106 let mut first_chat = receipts(&[("a", Receipt::Sent)]);
107 fade.apply("one", true, &mut first_chat, now);
108 let mut other_chat = receipts(&[("a", Receipt::Read)]);
109 assert!(!fade.apply("two", true, &mut other_chat, now));
110 }
111}