diff options
Diffstat (limited to 'schist_desktop_gui/src/gui')
| -rw-r--r-- | schist_desktop_gui/src/gui/screens.rs | 2 | ||||
| -rw-r--r-- | schist_desktop_gui/src/gui/screens/files_screen.rs | 16 | ||||
| -rw-r--r-- | schist_desktop_gui/src/gui/screens/files_screen/update_files_screen.rs | 123 |
3 files changed, 33 insertions, 108 deletions
diff --git a/schist_desktop_gui/src/gui/screens.rs b/schist_desktop_gui/src/gui/screens.rs index abca2b6..5acf73b 100644 --- a/schist_desktop_gui/src/gui/screens.rs +++ b/schist_desktop_gui/src/gui/screens.rs @@ -4,7 +4,7 @@ pub mod main_screen; pub use files_screen::FilesScreen; pub use main_screen::MainScreen; -#[derive(Default)] +#[derive(Clone, Default)] pub enum Screen { #[default] FilesScreen, diff --git a/schist_desktop_gui/src/gui/screens/files_screen.rs b/schist_desktop_gui/src/gui/screens/files_screen.rs index 9757355..9700e7a 100644 --- a/schist_desktop_gui/src/gui/screens/files_screen.rs +++ b/schist_desktop_gui/src/gui/screens/files_screen.rs @@ -3,13 +3,10 @@ mod view_files_screen; use std::{fmt::Debug, path::PathBuf}; -use crate::{ - gui::components::{navigation, Navigation, Panel}, - DatabaseConnection, -}; +use crate::gui::components::{navigation, Navigation, Panel}; pub struct FilesScreen { - options: Navigation<Panel<OptionId>>, + pub options: Navigation<Panel<OptionId>>, } #[derive(Clone, Debug, PartialEq)] @@ -23,6 +20,10 @@ pub enum OptionId { #[derive(Clone, Debug, PartialEq)] pub enum Message { NavigationMessage(navigation::Message<Panel<OptionId>>), + ReportFailedToCreateFile(String), + ReportFailedToImportFromActualbudget(String), + ReportFailedToPickFile(String), + ReportFailedToOpenFile(PathBuf, String), ActivateSelectedOption, NextOption, PrevOption, @@ -30,8 +31,11 @@ pub enum Message { #[derive(Debug)] pub enum Action { + ImportFromActualbudget, + NewFile, None, - OpenedFile(DatabaseConnection), + OpenFile(PathBuf), + PickFile, } impl<'a> FilesScreen { diff --git a/schist_desktop_gui/src/gui/screens/files_screen/update_files_screen.rs b/schist_desktop_gui/src/gui/screens/files_screen/update_files_screen.rs index aa47f56..1339138 100644 --- a/schist_desktop_gui/src/gui/screens/files_screen/update_files_screen.rs +++ b/schist_desktop_gui/src/gui/screens/files_screen/update_files_screen.rs @@ -1,13 +1,6 @@ -use actualbudget_to_schist_transformer::schist_state::{export_schist_state, SchistState}; -use itertools::Itertools; - use crate::{ - database_connection::DatabaseConnectionResult, - dialog::{import_from_actualbudget_dialog, new_file_dialog, pick_file_dialog}, gui::components::{navigation, Panel}, - paths::get_schist_database_directory, traits::Component, - DatabaseConnection, }; use super::{Action, FilesScreen, Message, OptionId}; @@ -24,92 +17,31 @@ impl<'a> Component<'a, Message, Action> for FilesScreen { } None => Action::None, }, - } - } -} - -impl FilesScreen { - fn pick_file( - &mut self, - ) -> Option<Result<DatabaseConnection, (std::path::PathBuf, anyhow::Error)>> { - pick_file_dialog(get_schist_database_directory().ok()).map(|db_connection_result| { - db_connection_result.ok().inspect_err(|(_path, err)| { - self.report_err( - super::OptionId::PickFile, - format!("{}", err.chain().join("\n | ")).as_str(), - "Failed to open file", - ); - }) - }) - } - - fn open_file( - &mut self, - file_path: std::path::PathBuf, - ) -> Result<DatabaseConnection, (std::path::PathBuf, anyhow::Error)> { - DatabaseConnection::establish(file_path.clone()) - .ok() - .inspect_err(|(_path, err)| { - self.report_err( - OptionId::OpenFile(file_path), - format!("{}", err.chain().join("\n | ")).as_str(), - "Failed to open file", - ); - }) - } - - fn new_file( - &mut self, - ) -> Option<Result<DatabaseConnection, (std::path::PathBuf, anyhow::Error)>> { - new_file_dialog(get_schist_database_directory().ok()).map(|result| { - result.ok().inspect_err(|(_path, err)| { - self.report_err( - OptionId::NewFile, - "Failed to create new file", - format!("{}", err.chain().join("\n | ")).as_str(), - ); - }) - }) - } - - fn import_from_actualbudget(&mut self) -> Option<anyhow::Result<DatabaseConnection>> { - match import_from_actualbudget_dialog() { - Some(Ok(state)) => self.new_file_with_state(&state), - Some(Err((_path, err))) => Some({ + 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", - format!("{}", err.chain().join("\n | ")).as_str(), - ); - Err(err) - }), - None => None, - } - } - - fn new_file_with_state( - &mut self, - state: &SchistState, - ) -> Option<anyhow::Result<DatabaseConnection>> { - match new_file_dialog(get_schist_database_directory().ok()) - .map(DatabaseConnectionResult::ok) - { - Some(Ok(mut file)) => Some({ - export_schist_state(state, &mut file.connection).ok()?; - Ok(file) - }), - Some(Err((_path, err))) => Some({ - self.report_err( - OptionId::NewFile, - format!("{}", err.chain().join("\n | ")).as_str(), - "Failed to create new file", ); - Err(err) - }), - None => None, + 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( @@ -134,33 +66,22 @@ impl FilesScreen { Panel { id: OptionId::NewFile, .. - } => match self.new_file() { - Some(Ok(file)) => Action::OpenedFile(file), - Some(Err(_)) | None => Action::None, - }, + } => Action::NewFile, Panel { id: OptionId::OpenFile(file_path), .. - } => self - .open_file(file_path.clone()) - .map_or(Action::None, Action::OpenedFile), + } => Action::OpenFile(file_path), Panel { id: OptionId::PickFile, .. - } => match self.pick_file() { - Some(Ok(file)) => Action::OpenedFile(file), - Some(Err(_)) | None => Action::None, - }, + } => Action::PickFile, Panel { id: OptionId::ImportFromActualbudget, .. - } => match self.import_from_actualbudget() { - Some(Ok(file)) => Action::OpenedFile(file), - Some(Err(_)) | None => Action::None, - }, + } => Action::ImportFromActualbudget, }, navigation::Action::SelectOption(_) | navigation::Action::None => Action::None, } |
