2023-08-07 22:27:27 -07:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use log::warn;
|
|
|
|
|
2023-08-11 22:46:43 -07:00
|
|
|
use crate::db::data_cache::DataCache;
|
2023-08-12 11:54:09 -07:00
|
|
|
use crate::db::DB;
|
2023-06-01 20:36:56 -07:00
|
|
|
use crate::uis::history::HistoryState;
|
2023-09-22 21:03:30 -07:00
|
|
|
use crate::uis::manual_transaction::ManualTransactionState;
|
2023-06-01 20:36:56 -07:00
|
|
|
use crate::uis::navigation_frame::NavigationState;
|
2023-09-22 21:03:30 -07:00
|
|
|
use crate::uis::quick_transaction::QuickTransactionState;
|
2023-05-31 22:27:26 -07:00
|
|
|
|
2023-05-31 18:00:17 -07:00
|
|
|
pub type AppResult<T> = std::result::Result<T, Box<dyn std::error::Error>>;
|
2023-05-28 22:18:30 -07:00
|
|
|
|
2023-06-01 20:36:56 -07:00
|
|
|
pub enum InputMode {
|
|
|
|
Insert,
|
|
|
|
Normal,
|
2023-05-31 22:27:26 -07:00
|
|
|
}
|
|
|
|
|
2023-05-28 22:18:30 -07:00
|
|
|
pub struct App<'a> {
|
|
|
|
pub running: bool,
|
2023-05-31 22:27:26 -07:00
|
|
|
|
2023-06-01 20:36:56 -07:00
|
|
|
pub states: States<'a>,
|
2023-05-31 22:27:26 -07:00
|
|
|
|
2023-06-01 20:36:56 -07:00
|
|
|
pub input_mode: InputMode,
|
2023-08-07 22:27:27 -07:00
|
|
|
|
|
|
|
pub db: Arc<tokio::sync::Mutex<DB>>,
|
2023-08-11 22:46:43 -07:00
|
|
|
pub data_cache: DataCache,
|
2023-05-28 22:18:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> App<'a> {
|
2023-08-11 22:46:43 -07:00
|
|
|
pub fn new(db: DB, data_cache: DataCache) -> App<'a> {
|
2023-05-31 18:00:17 -07:00
|
|
|
App {
|
|
|
|
running: true,
|
2023-08-11 22:46:43 -07:00
|
|
|
states: States::new(),
|
2023-06-01 20:36:56 -07:00
|
|
|
input_mode: InputMode::Normal,
|
2023-08-07 22:27:27 -07:00
|
|
|
db: Arc::new(tokio::sync::Mutex::new(db)),
|
2023-08-11 22:46:43 -07:00
|
|
|
data_cache,
|
2023-05-31 18:00:17 -07:00
|
|
|
}
|
2023-05-28 22:18:30 -07:00
|
|
|
}
|
|
|
|
|
2023-08-07 22:27:27 -07:00
|
|
|
pub fn refresh(&mut self) {
|
2023-08-17 22:24:12 -07:00
|
|
|
let db_clone1 = Arc::clone(&self.db);
|
|
|
|
let db_clone2 = Arc::clone(&self.db);
|
|
|
|
let db_clone3 = Arc::clone(&self.db);
|
2023-08-12 11:54:09 -07:00
|
|
|
|
|
|
|
tokio::spawn(async move {
|
2023-08-17 22:24:12 -07:00
|
|
|
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;
|
2023-08-07 22:27:27 -07:00
|
|
|
match res {
|
2023-08-12 11:54:09 -07:00
|
|
|
Ok(_) => {}
|
|
|
|
Err(e) => warn!("{}", e),
|
2023-08-07 22:27:27 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2023-06-01 20:36:56 -07:00
|
|
|
}
|
2023-05-31 22:27:26 -07:00
|
|
|
|
2023-06-01 20:36:56 -07:00
|
|
|
pub enum ActiveFrame {
|
|
|
|
Navigation,
|
|
|
|
NewTransaction,
|
2023-09-22 21:03:30 -07:00
|
|
|
ManualTransaction,
|
2023-06-01 20:36:56 -07:00
|
|
|
History,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct States<'a> {
|
|
|
|
pub nav_state: NavigationState<'a>,
|
2023-09-22 21:03:30 -07:00
|
|
|
pub transactions: QuickTransactionState<'a>,
|
|
|
|
pub manual_transactions: ManualTransactionState,
|
2023-06-01 20:36:56 -07:00
|
|
|
pub history: HistoryState,
|
|
|
|
|
|
|
|
pub active_frame: ActiveFrame,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> States<'a> {
|
2023-08-11 22:46:43 -07:00
|
|
|
pub fn new() -> States<'a> {
|
2023-06-01 20:36:56 -07:00
|
|
|
States {
|
|
|
|
nav_state: NavigationState::new(),
|
2023-09-22 21:03:30 -07:00
|
|
|
transactions: QuickTransactionState::new(),
|
|
|
|
manual_transactions: ManualTransactionState::new(),
|
2023-06-01 20:36:56 -07:00
|
|
|
history: HistoryState::new(),
|
2023-05-31 22:27:26 -07:00
|
|
|
|
2023-06-01 20:36:56 -07:00
|
|
|
active_frame: ActiveFrame::Navigation,
|
|
|
|
}
|
2023-05-31 22:27:26 -07:00
|
|
|
}
|
|
|
|
}
|
2023-09-22 21:32:17 -07:00
|
|
|
|
|
|
|
impl<'a> Default for States<'a> {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|