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
119
120
121
122
123
124
125
126
127
|
use iced::{
alignment,
widget::{column, row, Container},
Length,
};
use schist_models::Bucket;
use crate::{
gui::components::{navigation, BucketNavigationEntry, Navigation, Text},
style::SPACING_LG,
traits::{Component, Viewable},
};
#[derive(Clone, Debug)]
pub struct BucketsView {
greeting: String,
navigation: Navigation<BucketNavigationEntry>,
}
#[derive(Clone, Debug)]
pub enum Message {
NavigationMessage(navigation::Message<BucketNavigationEntry>),
SelectNext,
SelectPrev,
SetBuckets(Vec<Bucket>),
}
pub enum Action {
None,
}
impl BucketsView {
pub fn new(groups: Vec<String>, greeting: &str) -> Self {
Self {
greeting: greeting.to_owned(),
navigation: Navigation::new(
groups
.first()
.map(String::as_str)
.map(BucketNavigationEntry::from_group_closed),
groups
.iter()
.map(String::as_str)
.map(BucketNavigationEntry::from_group_closed)
.collect(),
),
}
}
pub fn active_group(&self) -> Option<String> {
self.navigation
.active_option
.as_ref()
.map(|active_option| active_option.group())
}
fn update_greeting(&mut self, active_entry: &str) {
self.greeting = format!("Hello, {}!", active_entry);
}
}
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) => {
let groups: Vec<BucketNavigationEntry> = buckets
.iter()
.map(|bucket| bucket.group.as_str())
.unique()
.map(BucketNavigationEntry::from_group_closed)
.collect();
match self
.navigation
.update(navigation::Message::SetOptions(groups))
{
navigation::Action::SelectOption(bucket_navigation_entry) => {
self.update_greeting(&bucket_navigation_entry.to_string());
Action::None
}
navigation::Action::None => Action::None,
}
}
Message::NavigationMessage(message) => match self.navigation.update(message) {
navigation::Action::SelectOption(bucket_navigation_entry) => {
self.update_greeting(&bucket_navigation_entry.to_string());
Action::None
}
navigation::Action::None => Action::None,
},
Message::SelectNext => match self.navigation.update(navigation::Message::SelectNext) {
navigation::Action::SelectOption(bucket_navigation_entry) => {
self.update_greeting(&bucket_navigation_entry.to_string());
Action::None
}
navigation::Action::None => Action::None,
},
Message::SelectPrev => match self.navigation.update(navigation::Message::SelectPrev) {
navigation::Action::SelectOption(bucket_navigation_entry) => {
self.update_greeting(&bucket_navigation_entry.to_string());
Action::None
}
navigation::Action::None => Action::None,
},
}
}
}
|