diff --git a/src/app.rs b/src/app.rs index 7082ef2..330a32b 100644 --- a/src/app.rs +++ b/src/app.rs @@ -3,6 +3,7 @@ use crate::error::Error; use crate::fonts::setup_gossip_fonts; use crate::frame_history::FrameHistory; use crate::images::fetch_img; +use crate::notecache::NoteCache; use crate::timeline; use crate::ui::padding; use crate::Result; @@ -97,6 +98,7 @@ pub struct Damus { compose: String, initial_filter: Vec, + note_cache: HashMap, pool: RelayPool, timelines: Vec, @@ -449,6 +451,7 @@ impl Damus { state: DamusState::Initializing, pool: RelayPool::new(), img_cache: HashMap::new(), + note_cache: HashMap::new(), initial_filter, n_panels: 1, timelines: vec![Timeline::new()], @@ -457,6 +460,12 @@ impl Damus { frame_history: FrameHistory::default(), } } + + pub fn get_note_cache_mut(&mut self, note_ref: &NoteRef) -> &mut NoteCache { + self.note_cache + .entry(note_ref.key) + .or_insert_with(|| NoteCache::new(note_ref.created_at)) + } } fn render_pfp(ui: &mut egui::Ui, img_cache: &mut ImageCache, url: &str) { diff --git a/src/lib.rs b/src/lib.rs index 71d965c..41f1914 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,6 +11,7 @@ mod filter; mod ui; mod timecache; mod time; +mod notecache; mod frame_history; mod timeline; diff --git a/src/notecache.rs b/src/notecache.rs new file mode 100644 index 0000000..8c0f9df --- /dev/null +++ b/src/notecache.rs @@ -0,0 +1,17 @@ +use crate::time::time_ago_since; +use crate::timecache::TimeCached; +use std::time::Duration; + +pub struct NoteCache { + reltime: TimeCached, +} + +impl NoteCache { + pub fn new(created_at: u64) -> Self { + let reltime = TimeCached::new( + Duration::from_secs(1), + Box::new(move || time_ago_since(created_at)), + ); + NoteCache { reltime } + } +}