summaryrefslogtreecommitdiff
path: root/schist_desktop_gui/src/gui/components/transactions_view.rs
blob: 88532a92b89bbf3dbbf2fa7794e8e45880e39640 (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
use iced::{
    alignment,
    widget::{column, row, Container},
    Element, Length,
};
use schist_models::Account;

use crate::{
    gui::components::{navigation, Navigation, Text},
    style::SPACING_LG,
    traits::{Component, Viewable},
};

#[derive(Clone, Debug)]
pub struct TransactionsView {
    accounts: Vec<Account>,
    greeting: String,
    navigation: Navigation<Text>,
}

#[derive(Clone, Debug)]
pub enum Message {
    NavigationMessage(navigation::Message<Text>),
    SelectNextTransaction,
    SelectPrevTransaction,
    SetAccounts(Vec<Account>),
}

pub enum Action {
    None,
}

impl TransactionsView {
    pub fn new(accounts: Vec<Account>, greeting: &str) -> Self {
        let account_names = view_account_names(accounts.clone());
        let navigation = Navigation::new(account_names.clone().first().cloned(), account_names);
        Self {
            accounts: accounts,
            greeting: greeting.to_owned(),
            navigation: navigation,
        }
    }
}

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![iced::widget::text(self.greeting.clone())];
        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) => {
                match self.navigation.update(message) {
                    navigation::Action::SelectOption(group_name) => {
                        self.greeting = make_greeting(group_name);
                        Action::None
                    }
                    navigation::Action::None => Action::None,
                };
                Action::None
            }
            Message::SetAccounts(accounts) => {
                self.accounts = accounts.clone();
                match self
                    .navigation
                    .update(navigation::Message::SetOptions(view_account_names(
                        accounts,
                    ))) {
                    navigation::Action::SelectOption(option) => {
                        self.greeting = make_greeting(option);
                        Action::None
                    }
                    navigation::Action::None => Action::None,
                }
            }
            Message::SelectNextTransaction => {
                match self.navigation.update(navigation::Message::SelectNext) {
                    navigation::Action::SelectOption(option) => {
                        self.greeting = make_greeting(option);
                        Action::None
                    }
                    navigation::Action::None => Action::None,
                }
            }
            Message::SelectPrevTransaction => {
                match self.navigation.update(navigation::Message::SelectPrev) {
                    navigation::Action::SelectOption(option) => {
                        self.greeting = make_greeting(option);
                        Action::None
                    }
                    navigation::Action::None => Action::None,
                }
            }
        }
    }
}

fn make_greeting(account_name: Text) -> String {
    format!("Hello, {} account!", account_name.content)
}

fn view_account_names(accounts: Vec<Account>) -> Vec<Text> {
    accounts
        .iter()
        .map(|acc| Text::new(acc.name.clone()))
        .collect()
}