Recount-TUI/src/app.rs

105 lines
2.5 KiB
Rust

use std::sync::Arc;
use log::warn;
use crate::db::data_cache::DataCache;
use crate::db::DB;
use crate::uis::history::HistoryState;
use crate::uis::manual_transaction::ManualTransactionState;
use crate::uis::navigation_frame::NavigationState;
use crate::uis::quick_transaction::QuickTransactionState;
pub type AppResult<T> = std::result::Result<T, Box<dyn std::error::Error>>;
pub enum InputMode {
Insert,
Normal,
}
pub struct App<'a> {
pub running: bool,
pub states: States<'a>,
pub input_mode: InputMode,
pub db: Arc<tokio::sync::Mutex<DB>>,
pub data_cache: DataCache,
}
impl<'a> App<'a> {
pub fn new(db: DB, data_cache: DataCache) -> App<'a> {
App {
running: true,
states: States::new(),
input_mode: InputMode::Normal,
db: Arc::new(tokio::sync::Mutex::new(db)),
data_cache,
}
}
pub fn refresh(&mut self) {
let db_clone1 = Arc::clone(&self.db);
let db_clone2 = Arc::clone(&self.db);
let db_clone3 = Arc::clone(&self.db);
tokio::spawn(async move {
let res = db_clone1.lock().await.get_all_transactions().await;
match res {
Ok(_) => {}
Err(e) => warn!("{}", e),
}
});
tokio::spawn(async move {
let res = db_clone2.lock().await.get_all_accounts().await;
match res {
Ok(_) => {}
Err(e) => warn!("{}", e),
}
});
tokio::spawn(async move {
let res = db_clone3.lock().await.get_all_buckets().await;
match res {
Ok(_) => {}
Err(e) => warn!("{}", e),
}
});
}
}
pub enum ActiveFrame {
Navigation,
NewTransaction,
ManualTransaction,
History,
}
pub struct States<'a> {
pub nav_state: NavigationState<'a>,
pub transactions: QuickTransactionState,
pub manual_transactions: ManualTransactionState,
pub history: HistoryState,
pub active_frame: ActiveFrame,
}
impl<'a> States<'a> {
pub fn new() -> States<'a> {
States {
nav_state: NavigationState::new(),
transactions: QuickTransactionState::new(),
manual_transactions: ManualTransactionState::new(),
history: HistoryState::new(),
active_frame: ActiveFrame::Navigation,
}
}
}
impl<'a> Default for States<'a> {
fn default() -> Self {
Self::new()
}
}