summaryrefslogtreecommitdiff
path: root/schist_desktop_gui/src/gui/components/transactions_view.rs
blob: 8e076f11d5c38fbc727b31a88b95e6d706858ee3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
mod view_transactions_view;

use std::collections::HashMap;

use itertools::Itertools;
use schist_models::{Account, Transaction};

use crate::{
    gui::components::{navigation, table, AccountNavigationEntry, Navigation, Table},
    traits::Component,
};

#[derive(Clone, Debug)]
pub struct TransactionsView {
    navigation: Navigation<AccountNavigationEntry>,
    table: Table<i32>,
    transactions_by_account_id: HashMap<i32, Vec<Transaction>>,
}

#[derive(Clone, Debug)]
pub enum Message {
    NavigationMessage(navigation::Message<AccountNavigationEntry>),
    SelectNextAccount,
    SelectPrevAccount,
    SetAccounts(Vec<Account>),
    SetTransactions(Vec<Transaction>),
    TableMessage(table::Message),
}

pub enum Action {
    None,
}

impl TransactionsView {
    pub fn new(accounts: &[Account], transactions: &[Transaction]) -> Self {
        let account_navigation_entries: Vec<AccountNavigationEntry> = accounts
            .iter()
            .cloned()
            .map(AccountNavigationEntry::new)
            .collect();
        let navigation = Navigation::new(
            account_navigation_entries.first().cloned(),
            account_navigation_entries,
        );
        let mut transactions_view = Self {
            navigation: navigation,
            table: Table::default(),
            transactions_by_account_id: transactions
                .into_iter()
                .cloned()
                .into_group_map_by(|t| t.account_id),
        };
        transactions_view.refresh_table();
        transactions_view
    }

    fn refresh_table(&mut self) {
        self.table =
            Table {
                cols: vec![
                    table::Column::new(0, "Date"),
                    table::Column::new(1, "Bucket"),
                    table::Column::new(2, "Payee"),
                    table::Column::new(3, "Quantity"),
                    table::Column::new(4, "Balance"),
                ],
                rows: self.navigation.active_option.clone().map_or_else(
                    Vec::new,
                    |AccountNavigationEntry { account, .. }| {
                        self.transactions_by_account_id
                            .get(&account.id)
                            .cloned()
                            .unwrap_or_else(Vec::new)
                            .iter()
                            .map(|t| {
                                HashMap::from([
                                    (0, table::Value::String(t.date.to_string())),
                                    (
                                        1,
                                        table::Value::String(t.bucket_id.map_or_else(
                                            || String::from("None"),
                                            |id| id.to_string(),
                                        )),
                                    ),
                                    (2, table::Value::String(t.counterparty.clone())),
                                    (3, table::Value::Currency(t.amount)),
                                    (4, table::Value::String(String::from("TODO"))),
                                ])
                            })
                            .collect()
                    },
                ),
            };
    }
}

impl<'a> Component<'a, Message, Action> for TransactionsView {
    fn update(&mut self, message: Message) -> Action {
        match message {
            Message::NavigationMessage(message) => self.update_navigation(message),

            Message::SetAccounts(accounts) => {
                let account_navigation_entries: Vec<AccountNavigationEntry> = accounts
                    .into_iter()
                    .map(AccountNavigationEntry::new)
                    .collect();
                self.update_navigation(navigation::Message::SetOptions(account_navigation_entries))
            }

            Message::SetTransactions(transactions) => {
                self.transactions_by_account_id =
                    transactions.into_iter().into_group_map_by(|t| t.account_id);
                self.refresh_table();
                Action::None
            }

            Message::SelectNextAccount => self.update_navigation(navigation::Message::SelectNext),

            Message::SelectPrevAccount => self.update_navigation(navigation::Message::SelectPrev),
        }
    }
}

impl TransactionsView {
    fn update_navigation(
        &mut self,
        message: navigation::Message<AccountNavigationEntry>,
    ) -> Action {
        match self.navigation.update(message) {
            navigation::Action::ActivateOption(_) | navigation::Action::SelectOption(_) => {
                self.refresh_table();
                Action::None
            }
            navigation::Action::None => Action::None,
        }
    }
}