From c620c8278ffb8a051a5613be7620b2589c6a19f7 Mon Sep 17 00:00:00 2001 From: edouardparis Date: Mon, 4 Nov 2024 16:44:27 +0100 Subject: [PATCH] Add specific comparaison order for HistoryTransaction --- gui/src/app/state/mod.rs | 4 ++-- gui/src/app/state/transactions.rs | 4 ++-- gui/src/daemon/model.rs | 13 +++++++++++++ 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/gui/src/app/state/mod.rs b/gui/src/app/state/mod.rs index a71a968f..070d9143 100644 --- a/gui/src/app/state/mod.rs +++ b/gui/src/app/state/mod.rs @@ -296,7 +296,7 @@ impl State for Home { limit += HISTORY_EVENT_PAGE_SIZE; events = daemon.list_history_txs(0, last_event_date, limit).await?; } - events.sort_by(|a, b| b.time.cmp(&a.time)); + events.sort_by(|a, b| a.compare(b)); Ok(events) }, Message::HistoryTransactionsExtension, @@ -332,7 +332,7 @@ impl State for Home { let mut txs = daemon .list_history_txs(0, now, HISTORY_EVENT_PAGE_SIZE) .await?; - txs.sort_by(|a, b| b.time.cmp(&a.time)); + txs.sort_by(|a, b| a.compare(b)); let mut pending_txs = daemon.list_pending_txs().await?; pending_txs.extend(txs); diff --git a/gui/src/app/state/transactions.rs b/gui/src/app/state/transactions.rs index fbe9edbe..c7285109 100644 --- a/gui/src/app/state/transactions.rs +++ b/gui/src/app/state/transactions.rs @@ -239,7 +239,7 @@ impl State for TransactionsPanel { limit += HISTORY_EVENT_PAGE_SIZE; txs = daemon.list_history_txs(0, last_tx_date, limit).await?; } - txs.sort_by(|a, b| b.time.cmp(&a.time)); + txs.sort_by(|a, b| a.compare(b)); Ok(txs) }, Message::HistoryTransactionsExtension, @@ -272,7 +272,7 @@ impl State for TransactionsPanel { let mut txs = daemon .list_history_txs(0, now, HISTORY_EVENT_PAGE_SIZE) .await?; - txs.sort_by(|a, b| b.time.cmp(&a.time)); + txs.sort_by(|a, b| a.compare(b)); let mut pending_txs = daemon.list_pending_txs().await?; pending_txs.extend(txs); diff --git a/gui/src/daemon/model.rs b/gui/src/daemon/model.rs index 2a7e05df..072f4775 100644 --- a/gui/src/daemon/model.rs +++ b/gui/src/daemon/model.rs @@ -1,3 +1,4 @@ +use std::cmp::Ordering; use std::collections::{HashMap, HashSet}; use liana::descriptors::LianaDescriptor; @@ -369,6 +370,18 @@ impl HistoryTransaction { } } + pub fn compare(&self, other: &Self) -> Ordering { + match (&self.time, &other.time) { + // `None` values come first + (None, Some(_)) => Ordering::Less, + (Some(_), None) => Ordering::Greater, + // Both are `None`, so we consider them equal + (None, None) => self.txid.cmp(&other.txid), + // Both are `Some`, compare by descending time, then by txid + (Some(time1), Some(time2)) => time2.cmp(time1).then_with(|| self.txid.cmp(&other.txid)), + } + } + pub fn is_external(&self) -> bool { matches!( self.kind,