summaryrefslogtreecommitdiff
path: root/schist_desktop_gui/src/gui/screens/files_screen.rs
blob: 373b54f8dafab6776e67c1638af0673538f4724a (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
mod update_files_screen;
mod view_files_screen;

use std::{fmt::Debug, path::PathBuf};

use crate::{
    gui::components::{navigation, Navigation, Panel},
    DatabaseConnection,
};

pub struct FilesScreen {
    options: Navigation<Panel<OptionId>>,
}

#[derive(Clone, Debug, PartialEq)]
pub enum OptionId {
    NewFile,
    PickFile,
    ImportFromActualbudget,
    OpenFile(std::path::PathBuf),
}

#[derive(Clone, Debug, PartialEq)]
pub enum Message {
    NavigationMessage(navigation::Message<Panel<OptionId>>),
    NextOption,
    PrevOption,
}

#[derive(Debug)]
pub enum Action {
    None,
    OpenedFile(DatabaseConnection),
}

impl<'a> FilesScreen {
    pub fn new(file_paths: &'a [PathBuf]) -> Self {
        let mut options = vec![
            Panel::new(OptionId::NewFile, "New file", None, None),
            Panel::new(OptionId::PickFile, "Open file...", None, None),
            Panel::new(
                OptionId::ImportFromActualbudget,
                "Import from Actualbudget",
                None,
                None,
            ),
        ];
        options.extend(
            file_paths
                .iter()
                .map(|file_path| file_panel(file_path, None)),
        );

        Self {
            options: Navigation::new(None, options),
        }
    }
}

fn file_panel<'a>(file_path: &'a PathBuf, err: Option<String>) -> Panel<OptionId> {
    let file_name = file_path
        .file_name()
        .map(|os_str| os_str.to_str().unwrap_or("[invalid Unicode]"))
        .unwrap_or("[invalid filename]");

    Panel::new(
        OptionId::OpenFile(file_path.clone()),
        file_name,
        file_path.to_str().map(String::from),
        err.map(|err| format!("Failed to open new file {:#?}: {:#?}", file_path, err)),
    )
}