début new account
This commit is contained in:
83
src/app.rs
83
src/app.rs
@@ -1,76 +1,15 @@
|
||||
use ratatui::widgets::ListState;
|
||||
use sqlite::Connection;
|
||||
|
||||
use crate::{
|
||||
data_layer,
|
||||
entities::{Account, AccountType, Transaction},
|
||||
};
|
||||
|
||||
pub enum CurrentScreen {
|
||||
Main,
|
||||
Exiting,
|
||||
}
|
||||
pub enum CurrentWidget {
|
||||
AccountList,
|
||||
TrxInfo,
|
||||
TrxList,
|
||||
}
|
||||
|
||||
pub struct AccountList {
|
||||
accounts: Vec<Account>,
|
||||
pub state: ListState,
|
||||
}
|
||||
impl AccountList {
|
||||
fn new() -> AccountList {
|
||||
let mut list_state = ListState::default();
|
||||
list_state.select_first();
|
||||
return AccountList {
|
||||
accounts: Vec::new(),
|
||||
state: list_state,
|
||||
};
|
||||
}
|
||||
|
||||
fn get_accounts(&self, con: &Connection) -> Vec<Account> {
|
||||
if self.accounts.iter().count() == 0 {
|
||||
return data_layer::get_accounts(con);
|
||||
}
|
||||
return self.accounts.clone();
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TrxList {
|
||||
trx: Vec<Transaction>,
|
||||
pub state: ListState,
|
||||
}
|
||||
impl TrxList {
|
||||
fn new() -> TrxList {
|
||||
let mut list_state = ListState::default();
|
||||
list_state.select_first();
|
||||
return TrxList {
|
||||
trx: Vec::new(),
|
||||
state: list_state,
|
||||
};
|
||||
}
|
||||
|
||||
fn get_trx(&self, con: &Connection, app: &App) -> Vec<Transaction> {
|
||||
if self.trx.iter().count() == 0 {
|
||||
if let Some(i) = app.acc_list.state.selected() {
|
||||
return data_layer::get_account_transactions(
|
||||
con,
|
||||
app.get_list_accounts()[i].get_id(),
|
||||
);
|
||||
}
|
||||
return Vec::new();
|
||||
}
|
||||
return self.trx.clone();
|
||||
}
|
||||
}
|
||||
use crate::{data_layer, entities::*, enums::*};
|
||||
|
||||
pub struct App {
|
||||
pub current_screen: CurrentScreen,
|
||||
pub current_widget: CurrentWidget,
|
||||
pub acc_list: AccountList,
|
||||
pub trx_list: TrxList,
|
||||
pub new_account: Account,
|
||||
pub new_transaction: Transaction,
|
||||
pub connection: Connection,
|
||||
exit: bool,
|
||||
}
|
||||
@@ -89,12 +28,14 @@ impl App {
|
||||
current_widget: CurrentWidget::AccountList,
|
||||
acc_list: AccountList::new(),
|
||||
trx_list: TrxList::new(),
|
||||
new_account: Account::new(0, String::new(), AccountType::Cash),
|
||||
new_transaction: Transaction::new_empty(),
|
||||
connection: con,
|
||||
exit: false,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn get_list_accounts(&self) -> Vec<Account> {
|
||||
pub fn get_list_accounts(&mut self) -> Vec<Account> {
|
||||
let mut accounts = self.acc_list.get_accounts(&self.connection);
|
||||
let all = Account::new(0, "All".to_string(), AccountType::Cash);
|
||||
accounts.insert(0, all);
|
||||
@@ -111,8 +52,10 @@ impl App {
|
||||
self.acc_list.state.select_previous();
|
||||
}
|
||||
|
||||
pub fn get_list_trx(&self) -> Vec<Transaction> {
|
||||
let accounts = &self.trx_list.get_trx(&self.connection, self);
|
||||
pub fn get_list_trx(&mut self) -> Vec<Transaction> {
|
||||
let accounts = self
|
||||
.trx_list
|
||||
.get_trx(self.acc_list.get_selected_id(), &self.connection);
|
||||
return accounts.to_vec();
|
||||
}
|
||||
|
||||
@@ -125,4 +68,10 @@ impl App {
|
||||
pub fn previous_tr(&mut self) {
|
||||
self.trx_list.state.select_previous();
|
||||
}
|
||||
|
||||
pub fn save_new_account(&mut self) {
|
||||
let ac = data_layer::upsert_account(&self.connection, self.new_account.clone());
|
||||
self.acc_list.add_account(ac);
|
||||
self.new_account = Account::new(0, String::new(), AccountType::Cash);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user