From 69567a1cff76c744d5506c90294d0c874eb2f4e1 Mon Sep 17 00:00:00 2001 From: flo Date: Mon, 12 Dec 2022 10:35:19 -0700 Subject: [PATCH 1/2] Text color in chart should match other elements. --- crates/core/src/theme/text.rs | 7 +++---- src/gui/element/wallet/operation/chart.rs | 21 +++++++++++++-------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/crates/core/src/theme/text.rs b/crates/core/src/theme/text.rs index dacdf93..cc011d0 100644 --- a/crates/core/src/theme/text.rs +++ b/crates/core/src/theme/text.rs @@ -4,11 +4,10 @@ use iced::widget::text; impl text::StyleSheet for Theme { type Style = iced_style::theme::Text; - // TODO extend the color palette to support text colors fn appearance(&self, style: Self::Style) -> text::Appearance { - //text::Appearance { - // color: Some(self.palette.text.primary), - //} + // text::Appearance { + // color: Some(self.palette.bright.surface), + // } Default::default() } } diff --git a/src/gui/element/wallet/operation/chart.rs b/src/gui/element/wallet/operation/chart.rs index ebd3bcd..9444e67 100644 --- a/src/gui/element/wallet/operation/chart.rs +++ b/src/gui/element/wallet/operation/chart.rs @@ -176,7 +176,8 @@ impl Chart for BalanceChart { (chart_color.b * 255.0) as u8, ); - let date_color = self.theme.palette.bright.secondary; + // let date_color = self.theme.palette.bright.secondary; + let date_color = self.theme.palette.normal.surface; let date_color = RGBColor( (date_color.r * 255.0) as u8, (date_color.g * 255.0) as u8, @@ -190,6 +191,13 @@ impl Chart for BalanceChart { (background_color.b * 255.0) as u8, ); + let text_color = self.theme.palette.bright.surface; + let text_color = RGBColor( + (text_color.r * 255.0) as u8, + (text_color.g * 255.0) as u8, + (text_color.b * 255.0) as u8, + ); + chart .configure_mesh() .bold_line_style(background_color) @@ -230,28 +238,25 @@ impl Chart for BalanceChart { ))) .expect("Failed to draw hover point"); - // TODO these colors should be black when the theme is light. Solution A: we can either expand the - // color_palette struct to include a text color. Solution B: we can extend the ice-plotter lib to send the style down draw so we can - // tap into the system default text color like we have everywhere else. Solution C: meh, just hardcode it for now, white suffices on most - // backgrounds. + // draw balance above the point chart .draw_series(std::iter::once(Text::new( format!("{}", amount), (time, max_value), ("sans-serif", CHART_CAPTION_HEAD) .into_font() - .color(&plotters::style::colors::WHITE.mix(1.0)), + .color(&text_color.mix(1.0)) ))) .expect("Failed to draw text"); - // date + // date below balance with a slight faded color chart .draw_series(std::iter::once(Text::new( format!("{}", time.format("%b %d, %Y")), (time, max_value * 0.84), ("sans-serif", CHART_CAPTION_SUB) .into_font() - .color(&plotters::style::colors::WHITE.mix(0.5)), + .color(&text_color.mix(0.7)) ))) .expect("Failed to draw text"); } From f35071b37fc30b57a3919c5684ce3d8b7be65165 Mon Sep 17 00:00:00 2001 From: flo Date: Mon, 12 Dec 2022 18:20:37 -0700 Subject: [PATCH 2/2] Balance sum logic from maintained sorted collection of confirmed transactions. --- src/gui/element/wallet/operation/chart.rs | 1 - .../wallet/operation/tx_list_display.rs | 85 +++++++++++++------ 2 files changed, 61 insertions(+), 25 deletions(-) diff --git a/src/gui/element/wallet/operation/chart.rs b/src/gui/element/wallet/operation/chart.rs index 9444e67..2e7584f 100644 --- a/src/gui/element/wallet/operation/chart.rs +++ b/src/gui/element/wallet/operation/chart.rs @@ -176,7 +176,6 @@ impl Chart for BalanceChart { (chart_color.b * 255.0) as u8, ); - // let date_color = self.theme.palette.bright.secondary; let date_color = self.theme.palette.normal.surface; let date_color = RGBColor( (date_color.r * 255.0) as u8, diff --git a/src/gui/element/wallet/operation/tx_list_display.rs b/src/gui/element/wallet/operation/tx_list_display.rs index e1c1e25..b4d60d8 100644 --- a/src/gui/element/wallet/operation/tx_list_display.rs +++ b/src/gui/element/wallet/operation/tx_list_display.rs @@ -9,7 +9,7 @@ use grin_gui_core::{ use grin_gui_widgets::widget::header; use iced_aw::Card; use iced_native::Widget; -use std::{path::PathBuf, str::FromStr}; +use std::{borrow::Borrow, path::PathBuf, str::FromStr}; use super::tx_list::{HeaderState, TxList, TxLogEntryWrap}; @@ -40,11 +40,15 @@ use { }; pub struct StateContainer { + // maintains a list of all confirmed transactions sorted by date + confirmed_txns: Vec, wallet_txs: TxList, tx_header_state: HeaderState, mode: Mode, pub expanded_type: ExpandType, + + // balance history for wallet as (date, grin_balance) pub balance_data: Vec<(chrono::DateTime, f64)>, } @@ -56,6 +60,7 @@ impl Default for StateContainer { expanded_type: ExpandType::None, mode: Mode::NotInit, balance_data: vec![], + confirmed_txns: vec![], } } } @@ -137,37 +142,69 @@ pub fn handle_message<'a>( .collect(); state.wallet_txs = TxList { txs: tx_wrap_list }; - let mut datetime_sums = vec![]; - for (idx, tx) in txs.iter().enumerate() { - if tx.confirmed { - // trunc transaction date to day - //let datetime = tx.confirmation_ts.unwrap().duration_trunc(chrono::Duration::days(1)).unwrap(); - let datetime = chrono::DateTime::from_str("2019-01-20T00:00:00Z").unwrap(); - let credits = tx.amount_credited; - let debits = tx.amount_debited; + let confirmed_txns: Vec<&TxLogEntry> = txs.iter().filter(|tx| tx.confirmed).collect(); + + if !confirmed_txns.is_empty() { + // added new confirmed transactions to state confirmed set? + let mut added = false; - datetime_sums.push((datetime, credits - debits)); + for tx in confirmed_txns.iter() { + // if tx is not in state confirmed transactions, add it + if state + .confirmed_txns + .iter() + .find(|t| t.id == tx.id) + .is_none() + { + // push to state confirmed transactions + state.confirmed_txns.push(tx.clone().to_owned()); + added = true; + debug!("Confirmed Tx: {:?}", tx); + } } - } - // fill in sum data for days without transactions - if !datetime_sums.is_empty() { - let mut sum = 0; - let mut dt = datetime_sums.first().unwrap().0; - let today = chrono::Utc::now().duration_trunc(chrono::Duration::days(1)).unwrap(); + if added { + // sort state transactions by date + state.confirmed_txns.sort_by(|a, b| { + a.confirmation_ts.unwrap().cmp(&b.confirmation_ts.unwrap()) + }); + + let mut datetime_sums = vec![]; + for tx in state.confirmed_txns.iter() { + // trunc transaction date to day + //let datetime = tx.confirmation_ts.unwrap().duration_trunc(chrono::Duration::days(1)).unwrap(); + // this should be the date time above but for dev purposes lets backdate it + let datetime = chrono::DateTime::from_str("2019-01-20T00:00:00Z").unwrap(); + let credits = tx.amount_credited; + let debits = tx.amount_debited; + + datetime_sums.push((datetime, credits - debits)); + } - while dt <= today { - let txns = datetime_sums - .iter() - .filter(|(date, _)| *date == dt); + let mut sum = 0; + let mut dt = datetime_sums.first().unwrap().0; + let today = chrono::Utc::now() + .duration_trunc(chrono::Duration::days(1)) + .unwrap(); - sum = sum + txns.map(|x| x.1).collect::>().iter().sum::(); + // fill in sum data for days without transactions + let mut balance_history = vec![]; + while dt <= today { + // get all transactions for this date + let txns = datetime_sums.iter().filter(|(date, _)| *date == dt); - let dec_sum = (sum as f64 / grin_gui_core::GRIN_BASE as f64) as f64; + // sum up balance amount + sum = sum + txns.map(|x| x.1).collect::>().iter().sum::(); - state.balance_data.push((dt.to_owned(), dec_sum)); + // convert to grin units + let grin_sum = (sum as f64 / grin_gui_core::GRIN_BASE as f64) as f64; + balance_history.push((dt.to_owned(), grin_sum)); + + dt = dt + chrono::Duration::days(1); + } - dt = dt + chrono::Duration::days(1); + // finally we update state with the newly constructed balance history + state.balance_data = balance_history; } } }