mod update_files_screen; mod view_files_screen; use std::{fmt::Debug, path::PathBuf}; use crate::gui::components::{navigation, Navigation, Panel}; pub struct FilesScreen { pub options: Navigation>, } #[derive(Clone, Debug, PartialEq)] pub enum OptionId { NewFile, PickFile, ImportFromActualbudget, OpenFile(std::path::PathBuf), } #[derive(Clone, Debug, PartialEq)] pub enum Message { NavigationMessage(navigation::Message>), ReportFailedToCreateFile(String), ReportFailedToImportFromActualbudget(String), ReportFailedToPickFile(String), ReportFailedToOpenFile(PathBuf, String), ActivateSelectedOption, NextOption, PrevOption, } #[derive(Debug)] pub enum Action { ImportFromActualbudget, NewFile, None, OpenFile(PathBuf), PickFile, } 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) -> Panel { 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)), ) }