summaryrefslogtreecommitdiff
path: root/schist_desktop_gui/src/gui/components/transactions_view.rs
blob: 11249a1693b23e0232008095a284f1545d914f4f (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
mod view_transactions_view;

use std::collections::HashMap;

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

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

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

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

pub enum Action {
    None,
}

impl TransactionsView {
    pub fn new(accounts: &[Account], buckets: &[Bucket], 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,
        );
        Self {
            buckets: buckets.to_vec(),
            navigation: navigation,
            transactions_by_account_id: transactions
                .into_iter()
                .cloned()
                .into_group_map_by(|t| t.account_id),
        }
    }
}

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::SetBuckets(buckets) => {
                self.buckets = buckets;
                Action::None
            }

            Message::SetTransactions(transactions) => {
                self.transactions_by_account_id =
                    transactions.into_iter().into_group_map_by(|t| t.account_id);
                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(_) => {
                Action::None
            }
            navigation::Action::None => Action::None,
        }
    }
}