rex_app/views/
chart_view.rs

1use chrono::NaiveDate;
2use rex_db::models::FullTx;
3use rex_shared::models::Cent;
4use std::collections::{HashMap, HashSet};
5
6use crate::views::TxViewGroup;
7
8pub struct ChartView {
9    txs: TxViewGroup,
10    dates: HashSet<NaiveDate>,
11    first_date: NaiveDate,
12    last_date: NaiveDate,
13}
14
15pub(crate) fn get_chart_view(txs: TxViewGroup) -> ChartView {
16    let mut first_date = NaiveDate::default();
17    let mut last_date = NaiveDate::default();
18
19    let default_date = NaiveDate::default();
20
21    let unique_dates: HashSet<NaiveDate> = txs
22        .0
23        .iter()
24        .map(|tx| {
25            let tx_date = tx.tx.date.date();
26
27            if first_date == default_date {
28                first_date = tx_date;
29            }
30
31            if last_date == default_date {
32                last_date = tx_date;
33            }
34
35            if tx_date < first_date {
36                first_date = tx_date;
37            }
38
39            if tx_date > last_date {
40                last_date = tx_date;
41            }
42
43            tx_date
44        })
45        .collect();
46
47    ChartView {
48        txs,
49        dates: unique_dates,
50        first_date,
51        last_date,
52    }
53}
54
55impl ChartView {
56    #[must_use]
57    pub fn is_empty(&self) -> bool {
58        self.txs.0.is_empty()
59    }
60
61    #[must_use]
62    pub fn contains_date(&self, date: &NaiveDate) -> bool {
63        self.dates.contains(date)
64    }
65
66    #[must_use]
67    pub fn start_date(&self) -> NaiveDate {
68        self.first_date
69    }
70
71    #[must_use]
72    pub fn end_date(&self) -> NaiveDate {
73        self.last_date
74    }
75
76    #[must_use]
77    pub fn get_balance(&self, index: usize) -> &HashMap<i32, Cent> {
78        self.txs.get_tx_balance(index)
79    }
80
81    #[must_use]
82    pub fn len(&self) -> usize {
83        self.txs.len()
84    }
85
86    #[must_use]
87    pub fn get_tx(&self, index: usize) -> &FullTx {
88        &self.txs.0[index].tx
89    }
90}