summaryrefslogtreecommitdiff
path: root/schist_desktop_gui/src/gui/components/transactions_table/view_transactions_table.rs
blob: b6e8599ccfe8370b413c55f21c327442268f36bb (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
use iced::{
    widget::{
        scrollable::{self, Scrollbar},
        table,
        table::column,
        Scrollable,
    },
    Element,
};

use crate::{gui::components::Text, traits::Viewable};

use super::{Message, TransactionRow, TransactionsTable};

impl<'a> Viewable<'a, Message> for TransactionsTable {
    fn view(&'a self) -> Element<'a, Message> {
        let columns = [
            column(
                Text::default("Date").width(iced::Length::Fixed(128.0)),
                view_date_cell,
            ),
            column(
                Text::default("Bucket").width(iced::Length::Fixed(128.0)),
                view_bucket_cell,
            ),
            column(
                Text::default("Payee").width(iced::Length::Fixed(256.0)),
                view_payee_cell,
            ),
            column(
                Text::default("Quantity")
                    .width(iced::Length::Fixed(96.0))
                    .align_right(),
                view_quantity_cell,
            ),
            column(
                Text::default("Balance")
                    .width(iced::Length::Fixed(96.0))
                    .align_right(),
                view_balance_cell,
            ),
        ];

        let rows = self
            .active_account
            .as_ref()
            .map_or_else(Vec::default, |active_account| {
                self.transaction_rows_by_account_id
                    .get(&active_account.id)
                    .cloned()
                    .unwrap_or_else(Vec::default)
            });

        let table = Scrollable::new(table(columns, rows)).direction(scrollable::Direction::Both {
            horizontal: Scrollbar::default(),
            vertical: Scrollbar::default(),
        });

        table.into()
    }
}

fn view_date_cell(row: TransactionRow) -> Text {
    Text::default(row.date.format("%e %b %Y").as_str())
        .width(iced::Length::Fixed(128.0))
        .small()
        .clip()
}

fn view_bucket_cell(row: TransactionRow) -> Text {
    Text::default(&row.bucket)
        .style(row.bucket_font_style)
        .width(iced::Length::Fixed(128.0))
        .small()
        .clip()
}

fn view_payee_cell(row: TransactionRow) -> Text {
    Text::default(&row.payee)
        .style(row.payee_font_style)
        .width(iced::Length::Fixed(256.0))
        .small()
        .clip()
}

fn view_quantity_cell(row: TransactionRow) -> Text {
    Text::currency(row.amount)
        .width(iced::Length::Fixed(96.0))
        .small()
        .clip()
        .align_right()
}

fn view_balance_cell(row: TransactionRow) -> Text {
    Text::currency(row.balance)
        .width(iced::Length::Fixed(96.0))
        .small()
        .clip()
        .align_right()
}