diff --git a/src/app.rs b/src/app.rs index 5138a67..a806d78 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,8 +1,5 @@ use std::sync::Arc; -use crossterm::event::{self, Event, KeyCode}; -use tokio; - use log::warn; use crate::db::data_cache::DataCache; @@ -40,49 +37,6 @@ impl<'a> App<'a> { } } - pub fn poll_events(&mut self) -> AppResult<()> { - if let Event::Key(key) = event::read()? { - // 'q' needs to be handled at the top level so it can't be - // accidentally handed to a dead end - if let KeyCode::Char('q') = key.code { - if let Some(_) = self.states.nav_state.message.clone() { - self.states.nav_state.message = None; - } else { - if let ActiveFrame::Navigation = self.states.active_frame { - self.running = false; - } else { - self.states.active_frame = ActiveFrame::Navigation; - } - } - } else if let KeyCode::Esc = key.code { - if let Some(_) = self.states.nav_state.message.clone() { - self.states.nav_state.message = None; - } else { - if let ActiveFrame::Navigation = self.states.active_frame { - self.running = false; - } else { - self.states.active_frame = ActiveFrame::Navigation; - } - } - } - if let KeyCode::Char('r') = key.code { - self.refresh(); - } - match self.states.active_frame { - ActiveFrame::Navigation => { - NavigationState::handle_event(key, self); - } - ActiveFrame::History => { - HistoryState::handle_event(key, self); - } - ActiveFrame::NewTransaction => { - NewTransactionState::handle_event(key, self); - } - } - } - return Ok(()); - } - pub fn refresh(&mut self) { let db_clone1 = Arc::clone(&self.db); let db_clone2 = Arc::clone(&self.db); diff --git a/src/main.rs b/src/main.rs index e193538..6950208 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,19 @@ use std::fs::File; use std::io; use std::time::Duration; +use crossterm::event::{self, Event, KeyCode}; use simplelog::*; use ratatui::{backend::CrosstermBackend, Terminal}; -use recount::app::{App, AppResult}; +use recount::app::{App, AppResult, ActiveFrame}; use recount::db::data_cache::DataCache; use recount::db::DB; use recount::tui::Tui; +use recount::uis::history::HistoryState; +use recount::uis::navigation_frame::NavigationState; +use recount::uis::new_transaction::NewTransactionState; #[tokio::main] async fn main() -> AppResult<()> { @@ -36,7 +40,7 @@ async fn main() -> AppResult<()> { tui.draw(&mut app)?; // Handle events. - app.poll_events()?; + poll_events(&mut app)?; std::thread::sleep(Duration::from_millis(20)); } @@ -45,6 +49,50 @@ async fn main() -> AppResult<()> { tui.exit() } + +pub fn poll_events(app: &mut App) -> AppResult<()> { + if let Event::Key(key) = event::read()? { + // 'q' needs to be handled at the top level so it can't be + // accidentally handed to a dead end + if let KeyCode::Char('q') = key.code { + if let Some(_) = app.states.nav_state.message.clone() { + app.states.nav_state.message = None; + } else { + if let ActiveFrame::Navigation = app.states.active_frame { + app.running = false; + } else { + app.states.active_frame = ActiveFrame::Navigation; + } + } + } else if let KeyCode::Esc = key.code { + if let Some(_) = app.states.nav_state.message.clone() { + app.states.nav_state.message = None; + } else { + if let ActiveFrame::Navigation = app.states.active_frame { + app.running = false; + } else { + app.states.active_frame = ActiveFrame::Navigation; + } + } + } + if let KeyCode::Char('r') = key.code { + app.refresh(); + } + match app.states.active_frame { + ActiveFrame::Navigation => { + NavigationState::handle_event(key, app); + } + ActiveFrame::History => { + HistoryState::handle_event(key, app); + } + ActiveFrame::NewTransaction => { + NewTransactionState::handle_event(key, app); + } + } + } + return Ok(()); +} + pub fn init_logger(output_file: String) { // TODO: configure the log levels to something appropriate CombinedLogger::init(vec![