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

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

#[derive(Clone, Debug)]
pub struct BucketsView {
    greeting: String,
    navigation: Navigation<BucketNameAndBalance>,
}

#[derive(Clone, Debug)]
pub enum Message {
    NavigationMessage(navigation::Message<BucketNameAndBalance>),
    SelectNextBucket,
    SelectPrevBucket,
    SetBuckets(Vec<Bucket>),
}

pub enum Action {
    None,
}

impl BucketsView {
    pub fn new(buckets: Vec<Bucket>, greeting: &str) -> Self {
        Self {
            greeting: greeting.to_owned(),
            navigation: Navigation::new(
                buckets.first().cloned().map(BucketNameAndBalance::new),
                buckets.into_iter().map(BucketNameAndBalance::new).collect(),
            ),
        }
    }

    fn update_greeting(&mut self, active_bucket: &Bucket) {
        self.greeting = format!("Hello, {} bucket!", active_bucket.name);
    }
}

impl<'a> Viewable<'a, Message> for BucketsView {
    fn view(&'a self) -> iced::Element<'a, Message>
    where
        Message: 'a,
    {
        let navigation = self.navigation.view().map(Message::NavigationMessage);
        let main_content = column![Text::default(&self.greeting).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 BucketsView {
    fn update(&mut self, message: Message) -> Action {
        match message {
            Message::SetBuckets(buckets) => {
                self.navigation.update(navigation::Message::SetOptions(
                    buckets.into_iter().map(BucketNameAndBalance::new).collect(),
                ));
                Action::None
            }
            Message::NavigationMessage(message) => match self.navigation.update(message) {
                navigation::Action::SelectOption(bucket) => {
                    self.update_greeting(&bucket.bucket);
                    Action::None
                }
                navigation::Action::None => Action::None,
            },
            Message::SelectNextBucket => {
                match self.navigation.update(navigation::Message::SelectNext) {
                    navigation::Action::SelectOption(bucket) => {
                        self.update_greeting(&bucket.bucket);
                        Action::None
                    }
                    navigation::Action::None => Action::None,
                }
            }
            Message::SelectPrevBucket => {
                match self.navigation.update(navigation::Message::SelectPrev) {
                    navigation::Action::SelectOption(bucket) => {
                        self.update_greeting(&bucket.bucket);
                        Action::None
                    }
                    navigation::Action::None => Action::None,
                }
            }
        }
    }
}