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 = std::result::Result>; pub enum InputMode { Insert, Normal, } pub struct App<'a> { pub running: bool, pub states: States<'a>, pub input_mode: InputMode, pub db: Arc>, 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<'a>, 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, } } }