From bd592aacc7665481d9d6add3adce21e0b360b8ad Mon Sep 17 00:00:00 2001 From: Mike Dilger Date: Thu, 22 Dec 2022 06:22:54 +1300 Subject: [PATCH] Change back to linear feed for now --- src/globals.rs | 31 ++++++++++++++++++++++++++----- src/ui/feed.rs | 2 +- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/globals.rs b/src/globals.rs index 4c32973c..9ad5bbdf 100644 --- a/src/globals.rs +++ b/src/globals.rs @@ -61,7 +61,7 @@ lazy_static! { #[allow(dead_code)] pub async fn get_feed() -> Vec { - let mut feed: Vec = GLOBALS + let feed: Vec = GLOBALS .feed_events .lock() .await @@ -71,13 +71,13 @@ pub async fn get_feed() -> Vec { .filter(|e| e.in_reply_to.is_none()) // only root events .cloned() .collect(); - feed.sort_unstable_by(|a, b| a.last_reply_at.cmp(&b.last_reply_at)); - feed.iter().map(|e| e.id).collect() + + sort_feed(feed) } #[allow(dead_code)] pub fn blocking_get_feed() -> Vec { - let mut feed: Vec = GLOBALS + let feed: Vec = GLOBALS .feed_events .blocking_lock() .iter() @@ -86,7 +86,28 @@ pub fn blocking_get_feed() -> Vec { //.filter(|e| e.in_reply_to.is_none()) // only root events .cloned() .collect(); - feed.sort_unstable_by(|a, b| a.last_reply_at.cmp(&b.last_reply_at)); + + sort_feed(feed) +} + +fn sort_feed(mut feed: Vec) -> Vec { + // Threaded, TBD: + // feed.sort_unstable_by(|a, b| a.last_reply_at.cmp(&b.last_reply_at)); + + // Linear sort neweset first: + feed.sort_unstable_by(|a, b| { + if a.event.is_some() && b.event.is_some() { + b.event.as_ref().unwrap().created_at.cmp( + &a.event.as_ref().unwrap().created_at) + } else if a.event.is_some() { + std::cmp::Ordering::Greater + } else if b.event.is_some() { + std::cmp::Ordering::Less + } else { + std::cmp::Ordering::Equal + } + }); + feed.iter().map(|e| e.id).collect() } diff --git a/src/ui/feed.rs b/src/ui/feed.rs index 4732a6b9..3e370426 100644 --- a/src/ui/feed.rs +++ b/src/ui/feed.rs @@ -8,7 +8,7 @@ pub(super) fn update(_app: &mut GossipUi, ctx: &Context, _frame: &mut eframe::Fr let screen_rect = ctx.input().screen_rect; // Rect ScrollArea::vertical().show(ui, |ui| { - for id in feed.iter().rev() { + for id in feed.iter() { // Stop rendering at the bottom of the window: let pos2 = ui.next_widget_position(); if pos2.y > screen_rect.max.y {