summaryrefslogtreecommitdiff
path: root/schist_desktop_gui/src/gui/screens/files_screen/update_files_screen.rs
blob: 1339138752630c5b413207e70caa142c5343f246 (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
use crate::{
    gui::components::{navigation, Panel},
    traits::Component,
};

use super::{Action, FilesScreen, Message, OptionId};

impl<'a> Component<'a, Message, Action> for FilesScreen {
    fn update(&mut self, message: Message) -> Action {
        match message {
            Message::NavigationMessage(message) => self.update_options(message),
            Message::NextOption => self.update_options(navigation::Message::SelectNext),
            Message::PrevOption => self.update_options(navigation::Message::SelectPrev),
            Message::ActivateSelectedOption => match &self.options.active_option {
                Some(option) => {
                    self.update_options(navigation::Message::ActivateOption(option.clone()))
                }
                None => Action::None,
            },
            Message::ReportFailedToOpenFile(path, err) => {
                self.report_err(OptionId::OpenFile(path), &err, "Failed to open file");
                Action::None
            }
            Message::ReportFailedToCreateFile(err) => {
                self.report_err(OptionId::NewFile, &err, "Failed to create new file");
                Action::None
            }
            Message::ReportFailedToImportFromActualbudget(err) => {
                self.report_err(
                    OptionId::ImportFromActualbudget,
                    &err,
                    "Failed to import from Actualbudget",
                );
                Action::None
            }
            Message::ReportFailedToPickFile(err) => {
                self.report_err(OptionId::PickFile, &err, "Failed to choose file");
                Action::None
            }
        }
    }
}

impl FilesScreen {
    fn report_err(&mut self, id: OptionId, err: &str, dialog_title: &str) {
        let old_panel = self.options.find(|o| o.id == id);
        let file_panel_with_err = Panel::new(
            id.clone(),
            old_panel.as_ref().map(|p| p.name.as_str()).unwrap_or(""),
            old_panel.as_ref().map(|p| p.small.clone()).flatten(),
            Some(err.to_string()),
        );
        self.options
            .replace_first(|o| o.id == id, &vec![file_panel_with_err]);

        rfd::MessageDialog::new()
            .set_title(dialog_title)
            .set_level(rfd::MessageLevel::Error)
            .set_description(err)
            .show();
    }

    fn update_options(&mut self, message: navigation::Message<Panel<OptionId>>) -> Action {
        match self.options.update(message) {
            navigation::Action::ActivateOption(option) => match option {
                Panel {
                    id: OptionId::NewFile,
                    ..
                } => Action::NewFile,

                Panel {
                    id: OptionId::OpenFile(file_path),
                    ..
                } => Action::OpenFile(file_path),

                Panel {
                    id: OptionId::PickFile,
                    ..
                } => Action::PickFile,

                Panel {
                    id: OptionId::ImportFromActualbudget,
                    ..
                } => Action::ImportFromActualbudget,
            },
            navigation::Action::SelectOption(_) | navigation::Action::None => Action::None,
        }
    }
}