From b7e2e576e4bb322d8c6b08fb04a76279f40e5c51 Mon Sep 17 00:00:00 2001 From: Mike Dilger Date: Wed, 21 Dec 2022 19:37:14 +1300 Subject: [PATCH] Feed date_ago --- src/date_ago.rs | 36 ++++++++++++++++++++++++++++++++++++ src/main.rs | 1 + src/ui/feed.rs | 5 ++++- 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 src/date_ago.rs diff --git a/src/date_ago.rs b/src/date_ago.rs new file mode 100644 index 00000000..7e0a3558 --- /dev/null +++ b/src/date_ago.rs @@ -0,0 +1,36 @@ + +use nostr_proto::Unixtime; + +pub fn date_ago(then: Unixtime) -> String { + let now = Unixtime::now().unwrap(); + let seconds = now.0 - then.0; + let minutes: f32 = seconds as f32 / 60.0; + let hours: f32 = minutes / 60.0; + let days: f32 = hours / 24.0; + let years: f32 = days / 365.0; + + if seconds < 45 { + format!("{}s", seconds) + } else if seconds < 90 { + "1m".to_string() + } + else if minutes < 45.0 { + format!("{}m", minutes as i64) + } else if minutes < 90.0 { + "1h".to_string() + } else if hours < 24.0 { + format!("{}h", hours as i64) + } else if hours < 42.0 { + "1d".to_string() + } else if days < 30.0 { + format!("{}d", days as i64) + } else if days < 45.0 { + "1m".to_string() + } else if days < 365.0 { + format!("{}m", (days/30.0) as i64) + } else if years < 1.5 { + "1y".to_string() + } else { + format!("{}y", years as i64) + } +} diff --git a/src/main.rs b/src/main.rs index 665798d6..9fe08ffb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ extern crate lazy_static; mod about; mod comms; +mod date_ago; mod db; mod error; mod event_related; diff --git a/src/ui/feed.rs b/src/ui/feed.rs index f1e69417..807550db 100644 --- a/src/ui/feed.rs +++ b/src/ui/feed.rs @@ -12,12 +12,15 @@ pub(super) fn update(_app: &mut GossipUi, ctx: &Context, _frame: &mut eframe::Fr ScrollArea::vertical().show(ui, |ui| { - for id in feed.iter() { + for id in feed.iter().rev() { // Stop rendering at the bottom of the window: let pos2 = ui.next_widget_position(); if pos2.y > screen_rect.max.y { break; } if let Some(event) = crate::globals::GLOBALS.events.blocking_lock().get(id) { + + ui.label(crate::date_ago::date_ago(event.created_at)); + if let Some(person) = crate::globals::GLOBALS.people.blocking_lock().get(&event.pubkey) { if let Some(name) = &person.name { ui.label(&format!("{}", name));