From 56e1aa04aa2889e5941e624df3d989de7debf7a5 Mon Sep 17 00:00:00 2001 From: edouard Date: Fri, 21 Apr 2023 17:07:35 +0200 Subject: [PATCH] gui: change home warning close #439 --- gui/src/app/state/mod.rs | 38 ++++++++++---------- gui/src/app/view/coins.rs | 46 ++++++++++++------------ gui/src/app/view/home.rs | 75 ++++++++++++++++++++++----------------- 3 files changed, 84 insertions(+), 75 deletions(-) diff --git a/gui/src/app/state/mod.rs b/gui/src/app/state/mod.rs index 49d9e1dc..5e1867f1 100644 --- a/gui/src/app/state/mod.rs +++ b/gui/src/app/state/mod.rs @@ -48,8 +48,8 @@ pub struct Home { wallet: Arc, balance: Amount, unconfirmed_balance: Amount, - recovery_warning: Option<(Amount, usize)>, - recovery_alert: Option<(Amount, usize)>, + remaining_sequence: Option, + number_of_expiring_coins: usize, pending_events: Vec, events: Vec, selected_event: Option, @@ -74,8 +74,8 @@ impl Home { wallet, balance, unconfirmed_balance, - recovery_alert: None, - recovery_warning: None, + remaining_sequence: None, + number_of_expiring_coins: 0, selected_event: None, events: Vec::new(), pending_events: Vec::new(), @@ -106,8 +106,8 @@ impl State for Home { view::home::home_view( &self.balance, &self.unconfirmed_balance, - self.recovery_warning.as_ref(), - self.recovery_alert.as_ref(), + &self.remaining_sequence, + self.number_of_expiring_coins, &self.pending_events, &self.events, ), @@ -127,8 +127,8 @@ impl State for Home { self.warning = None; self.balance = Amount::from_sat(0); self.unconfirmed_balance = Amount::from_sat(0); - let mut recovery_warning = (Amount::from_sat(0), 0); - let mut recovery_alert = (Amount::from_sat(0), 0); + self.remaining_sequence = None; + self.number_of_expiring_coins = 0; for coin in coins { if coin.spend_info.is_none() { if coin.block_height.is_some() { @@ -136,24 +136,22 @@ impl State for Home { let timelock = self.wallet.main_descriptor.first_timelock_value(); let seq = remaining_sequence(&coin, cache.blockheight as u32, timelock); - if seq == 0 { - recovery_alert.0 += coin.amount; - recovery_alert.1 += 1; - } else if seq < timelock as u32 * 10 / 100 { - recovery_warning.0 += coin.amount; - recovery_warning.1 += 1; + // number of block in a day + if seq <= 144 { + self.number_of_expiring_coins += 1; + } + if let Some(last) = &mut self.remaining_sequence { + if seq < *last { + *last = seq + } + } else { + self.remaining_sequence = Some(seq); } } else { self.unconfirmed_balance += coin.amount; } } } - if recovery_warning.1 > 0 { - self.recovery_warning = Some(recovery_warning); - } - if recovery_alert.1 > 0 { - self.recovery_alert = Some(recovery_alert); - } } }, Message::HistoryTransactions(res) => match res { diff --git a/gui/src/app/view/coins.rs b/gui/src/app/view/coins.rs index 8ac14b16..6dfe6968 100644 --- a/gui/src/app/view/coins.rs +++ b/gui/src/app/view/coins.rs @@ -202,31 +202,33 @@ pub fn coin_sequence_label<'a, T: 'a>(seq: u32, timelock: u32) -> Container<'a, } } -/// returns y,m,d,h,m pub fn expire_message(sequence: u32) -> String { - let mut n_minutes = sequence * 10; - if n_minutes <= 1440 { + if sequence <= 144 { "Expires today".to_string() - } else if n_minutes <= 2 * 1440 { + } else if sequence <= 2 * 144 { "Expires in ≈ 2 days".to_string() } else { - let n_years = n_minutes / 525960; - n_minutes -= n_years * 525960; - let n_months = n_minutes / 43830; - n_minutes -= n_months * 43830; - let n_days = n_minutes / 1440; - - let units: Vec = [(n_years, "year"), (n_months, "month"), (n_days, "day")] - .iter() - .filter_map(|(n, u)| { - if *n != 0 { - Some(format!("{} {}{}", n, u, if *n > 1 { "s" } else { "" })) - } else { - None - } - }) - .collect(); - - format!("Expires in {}", units.join(",")) + format!("Expires in {}", expire_message_units(sequence).join(",")) } } + +/// returns y,m,d +pub fn expire_message_units(sequence: u32) -> Vec { + let mut n_minutes = sequence * 10; + let n_years = n_minutes / 525960; + n_minutes -= n_years * 525960; + let n_months = n_minutes / 43830; + n_minutes -= n_months * 43830; + let n_days = n_minutes / 1440; + + [(n_years, "year"), (n_months, "month"), (n_days, "day")] + .iter() + .filter_map(|(n, u)| { + if *n != 0 { + Some(format!("{} {}{}", n, u, if *n > 1 { "s" } else { "" })) + } else { + None + } + }) + .collect() +} diff --git a/gui/src/app/view/home.rs b/gui/src/app/view/home.rs index a85b704e..15e8590a 100644 --- a/gui/src/app/view/home.rs +++ b/gui/src/app/view/home.rs @@ -11,15 +11,18 @@ use liana_ui::{ widget::*, }; -use crate::{app::view::message::Message, daemon::model::HistoryTransaction}; +use crate::{ + app::view::{coins, message::Message}, + daemon::model::HistoryTransaction, +}; pub const HISTORY_EVENT_PAGE_SIZE: u64 = 20; pub fn home_view<'a>( balance: &'a bitcoin::Amount, unconfirmed_balance: &'a bitcoin::Amount, - recovery_warning: Option<&(bitcoin::Amount, usize)>, - recovery_alert: Option<&(bitcoin::Amount, usize)>, + remaining_sequence: &Option, + number_of_expiring_coins: usize, pending_events: &[HistoryTransaction], events: &Vec, ) -> Element<'a, Message> { @@ -40,39 +43,45 @@ pub fn home_view<'a>( None }), ) - .push_maybe(recovery_warning.map(|(a, c)| { - Row::new() - .spacing(15) - .align_items(Alignment::Center) - .push(icon::hourglass_icon().size(30).style(color::ORANGE)) - .push( + .push_maybe(if number_of_expiring_coins == 0 { + remaining_sequence.map(|sequence| { + Container::new( Row::new() - .spacing(5) - .push(text(format!( - "Recovery path will be soon available for {} coins", - c - ))) - .push(text("(")) - .push(amount(a)) - .push(text(")")), + .spacing(15) + .align_items(Alignment::Center) + .push( + h4_regular(format!( + "Your next coin to expire will in ≈ {}", + coins::expire_message_units(sequence).join(",") + )) + .width(Length::Fill), + ) + .push( + icon::tooltip_icon() + .size(20) + .style(color::GREY_3) + .width(Length::Units(20)), + ) + .width(Length::Fill), ) - .padding(10) - })) - .push_maybe(recovery_alert.map(|(a, c)| { - Row::new() - .spacing(15) - .align_items(Alignment::Center) - .push(icon::hourglass_done_icon().style(color::RED)) - .push( - Row::new() - .spacing(5) - .push(text(format!("Recovery path is available for {} coins", c))) - .push(text("(")) - .push(amount(a)) - .push(text(")")), + .padding(25) + .style(theme::Card::Border) + }) + } else { + Some( + Container::new( + Row::new().spacing(15).align_items(Alignment::Center).push( + h4_regular(format!( + "You have {} coins that are already or about to be expired", + number_of_expiring_coins + )) + .width(Length::Fill), + ), ) - .padding(10) - })) + .padding(25) + .style(theme::Card::Invalid), + ) + }) .push( Column::new() .spacing(10)