Feed date_ago

This commit is contained in:
Mike Dilger 2022-12-21 19:37:14 +13:00
parent cb19731353
commit b7e2e576e4
3 changed files with 41 additions and 1 deletions

36
src/date_ago.rs Normal file
View File

@ -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)
}
}

View File

@ -5,6 +5,7 @@ extern crate lazy_static;
mod about;
mod comms;
mod date_ago;
mod db;
mod error;
mod event_related;

View File

@ -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));