summaryrefslogtreecommitdiff
path: root/schist_desktop_gui/src/gui/components/bucket_navigation_entry.rs
blob: 69665a3203de46b768c1695f3cd54a79eb846d3e (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
use bucket_group_name::BucketGroupName;
use bucket_name_and_balance::BucketNameAndBalance;
use iced::Element;
use itertools::Either;
use schist_models::Bucket;

use crate::{gui::components::navigation, traits::Focusable};

mod bucket_group_name;
mod bucket_name_and_balance;

#[derive(Clone, Debug, PartialEq)]
pub struct BucketNavigationEntry(Either<BucketNameAndBalance, BucketGroupName>);

impl BucketNavigationEntry {
    pub fn from_bucket(bucket: &Bucket) -> Self {
        Self(Either::Left(BucketNameAndBalance::new(bucket)))
    }

    pub fn from_group_closed(group: &str) -> Self {
        Self(Either::Right(BucketGroupName::new(group, false)))
    }

    pub fn from_group_open(group: &str) -> Self {
        Self(Either::Right(BucketGroupName::new(group, true)))
    }

    pub fn is_bucket_and<P>(&self, pred: P) -> bool
    where
        P: Fn(&Bucket) -> bool,
    {
        self.0
            .as_ref()
            .map_left(|bucket_name_and_balance| pred(&bucket_name_and_balance.bucket))
            .left_or(false)
    }

    pub fn is_group_and<P>(&self, pred: P) -> bool
    where
        P: Fn(&BucketGroupName) -> bool,
    {
        self.0.as_ref().map_right(pred).right_or(false)
    }

    pub fn to_string(&self) -> String {
        self.0
            .as_ref()
            .map_left(|bucket_name_and_balance| bucket_name_and_balance.bucket.name.clone())
            .map_right(|bucket_group_name| bucket_group_name.group.clone())
            .into_inner()
    }

    pub fn group(&self) -> String {
        self.0
            .as_ref()
            .map_left(|bucket_name_and_balance| bucket_name_and_balance.bucket.group.clone())
            .map_right(|bucket_group_name| bucket_group_name.group.clone())
            .into_inner()
    }
}

impl<'a> From<&'a BucketNavigationEntry>
    for Element<'a, navigation::Message<BucketNavigationEntry>>
{
    fn from(bucket_navigation_entry: &'a BucketNavigationEntry) -> Self {
        bucket_navigation_entry.0.as_ref().either_into()
    }
}

impl Focusable for BucketNavigationEntry {
    fn focus(&mut self) {
        match self.0.as_mut() {
            Either::Left(group) => group.focus(),
            Either::Right(bucket) => bucket.focus(),
        };
    }

    fn unfocus(&mut self) {
        match self.0.as_mut() {
            Either::Left(group) => group.unfocus(),
            Either::Right(bucket) => bucket.unfocus(),
        };
    }
}