moved poll to main

This commit is contained in:
Nickiel12 2023-09-22 20:22:59 -07:00
parent 6108ff164a
commit f1dca49208
2 changed files with 50 additions and 48 deletions

View file

@ -1,8 +1,5 @@
use std::sync::Arc; use std::sync::Arc;
use crossterm::event::{self, Event, KeyCode};
use tokio;
use log::warn; use log::warn;
use crate::db::data_cache::DataCache; 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) { pub fn refresh(&mut self) {
let db_clone1 = Arc::clone(&self.db); let db_clone1 = Arc::clone(&self.db);
let db_clone2 = Arc::clone(&self.db); let db_clone2 = Arc::clone(&self.db);

View file

@ -1,15 +1,19 @@
use std::fs::File; use std::fs::File;
use std::io; use std::io;
use std::time::Duration; use std::time::Duration;
use crossterm::event::{self, Event, KeyCode};
use simplelog::*; use simplelog::*;
use ratatui::{backend::CrosstermBackend, Terminal}; 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::data_cache::DataCache;
use recount::db::DB; use recount::db::DB;
use recount::tui::Tui; use recount::tui::Tui;
use recount::uis::history::HistoryState;
use recount::uis::navigation_frame::NavigationState;
use recount::uis::new_transaction::NewTransactionState;
#[tokio::main] #[tokio::main]
async fn main() -> AppResult<()> { async fn main() -> AppResult<()> {
@ -36,7 +40,7 @@ async fn main() -> AppResult<()> {
tui.draw(&mut app)?; tui.draw(&mut app)?;
// Handle events. // Handle events.
app.poll_events()?; poll_events(&mut app)?;
std::thread::sleep(Duration::from_millis(20)); std::thread::sleep(Duration::from_millis(20));
} }
@ -45,6 +49,50 @@ async fn main() -> AppResult<()> {
tui.exit() 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) { pub fn init_logger(output_file: String) {
// TODO: configure the log levels to something appropriate // TODO: configure the log levels to something appropriate
CombinedLogger::init(vec![ CombinedLogger::init(vec![