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
|
use std::collections::HashMap;
use iced::{
alignment,
widget::{column, row, Container},
Element, Length,
};
use itertools::Itertools;
use schist_models::{Account, Transaction};
use crate::{
gui::components::{navigation, AccountNavigationEntry, Navigation, Text},
style::SPACING_LG,
traits::{Component, Viewable},
};
#[derive(Clone, Debug)]
pub struct TransactionsView {
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>),
}
pub enum Action {
None,
}
impl TransactionsView {
pub fn new(accounts: Vec<Account>, transactions: Vec<Transaction>) -> Self {
let account_navigation_entries: Vec<AccountNavigationEntry> = accounts
.into_iter()
.map(AccountNavigationEntry::new)
.collect();
let navigation = Navigation::new(
account_navigation_entries.first().cloned(),
account_navigation_entries,
);
Self {
navigation: navigation,
transactions_by_account_id: transactions
.into_iter()
.into_group_map_by(|t| t.account_id),
}
}
}
impl<'a> Viewable<'a, Message> for TransactionsView {
fn view(&'a self) -> Element<'a, Message> {
let navigation = self.navigation.view().map(Message::NavigationMessage);
let main_content = column![
Text::default("Hello, transactions!").as_element(),
Text::default(&format!(
"{} transactions in this account.",
self.navigation.active_option.clone().map_or(
0,
|AccountNavigationEntry { account, .. }| self
.transactions_by_account_id
.get(&account.id)
.map_or(0, |transactions| transactions.len())
)
))
.as_element(),
];
row![
Container::new(navigation).width(Length::FillPortion(1)),
Container::new(main_content).width(Length::FillPortion(3)),
]
.align_y(alignment::Vertical::Center)
.height(Length::Fill)
.spacing(SPACING_LG)
.into()
}
}
impl<'a> Component<'a, Message, Action> for TransactionsView {
fn update(&mut self, message: Message) -> Action {
match message {
Message::NavigationMessage(message) => {
self.navigation.update(message);
Action::None
}
Message::SetAccounts(accounts) => {
let account_navigation_entries: Vec<AccountNavigationEntry> = accounts
.into_iter()
.map(AccountNavigationEntry::new)
.collect();
self.navigation
.update(navigation::Message::SetOptions(account_navigation_entries));
Action::None
}
Message::SelectNextAccount => {
self.navigation.update(navigation::Message::SelectNext);
Action::None
}
Message::SelectPrevAccount => {
self.navigation.update(navigation::Message::SelectPrev);
Action::None
}
}
}
}
|