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 ratatui::widgets::ListState;
|
||||||
use sqlite::Connection;
|
use sqlite::Connection;
|
||||||
|
|
||||||
use crate::{
|
use crate::{data_layer, entities::*, enums::*};
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct App {
|
pub struct App {
|
||||||
pub current_screen: CurrentScreen,
|
pub current_screen: CurrentScreen,
|
||||||
pub current_widget: CurrentWidget,
|
pub current_widget: CurrentWidget,
|
||||||
pub acc_list: AccountList,
|
pub acc_list: AccountList,
|
||||||
pub trx_list: TrxList,
|
pub trx_list: TrxList,
|
||||||
|
pub new_account: Account,
|
||||||
|
pub new_transaction: Transaction,
|
||||||
pub connection: Connection,
|
pub connection: Connection,
|
||||||
exit: bool,
|
exit: bool,
|
||||||
}
|
}
|
||||||
@@ -89,12 +28,14 @@ impl App {
|
|||||||
current_widget: CurrentWidget::AccountList,
|
current_widget: CurrentWidget::AccountList,
|
||||||
acc_list: AccountList::new(),
|
acc_list: AccountList::new(),
|
||||||
trx_list: TrxList::new(),
|
trx_list: TrxList::new(),
|
||||||
|
new_account: Account::new(0, String::new(), AccountType::Cash),
|
||||||
|
new_transaction: Transaction::new_empty(),
|
||||||
connection: con,
|
connection: con,
|
||||||
exit: false,
|
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 mut accounts = self.acc_list.get_accounts(&self.connection);
|
||||||
let all = Account::new(0, "All".to_string(), AccountType::Cash);
|
let all = Account::new(0, "All".to_string(), AccountType::Cash);
|
||||||
accounts.insert(0, all);
|
accounts.insert(0, all);
|
||||||
@@ -111,8 +52,10 @@ impl App {
|
|||||||
self.acc_list.state.select_previous();
|
self.acc_list.state.select_previous();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_list_trx(&self) -> Vec<Transaction> {
|
pub fn get_list_trx(&mut self) -> Vec<Transaction> {
|
||||||
let accounts = &self.trx_list.get_trx(&self.connection, self);
|
let accounts = self
|
||||||
|
.trx_list
|
||||||
|
.get_trx(self.acc_list.get_selected_id(), &self.connection);
|
||||||
return accounts.to_vec();
|
return accounts.to_vec();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,4 +68,10 @@ impl App {
|
|||||||
pub fn previous_tr(&mut self) {
|
pub fn previous_tr(&mut self) {
|
||||||
self.trx_list.state.select_previous();
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
pub mod account;
|
pub mod account;
|
||||||
|
pub mod accountlist;
|
||||||
pub mod transaction;
|
pub mod transaction;
|
||||||
|
pub mod trxlist;
|
||||||
|
|
||||||
pub use account::{Account, AccountType};
|
pub use account::{Account, AccountType};
|
||||||
|
pub use accountlist::*;
|
||||||
pub use transaction::{Transaction, TransactionType};
|
pub use transaction::{Transaction, TransactionType};
|
||||||
|
pub use trxlist::*;
|
||||||
|
|||||||
38
src/entities/accountlist.rs
Normal file
38
src/entities/accountlist.rs
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
use ratatui::widgets::ListState;
|
||||||
|
use sqlite::Connection;
|
||||||
|
|
||||||
|
use crate::{data_layer, entities::*};
|
||||||
|
|
||||||
|
pub struct AccountList {
|
||||||
|
accounts: Vec<Account>,
|
||||||
|
pub state: ListState,
|
||||||
|
}
|
||||||
|
impl AccountList {
|
||||||
|
pub fn new() -> AccountList {
|
||||||
|
let mut list_state = ListState::default();
|
||||||
|
list_state.select_first();
|
||||||
|
return AccountList {
|
||||||
|
accounts: Vec::new(),
|
||||||
|
state: list_state,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_accounts(&mut self, con: &Connection) -> Vec<Account> {
|
||||||
|
if self.accounts.iter().count() == 0 {
|
||||||
|
self.accounts = data_layer::get_accounts(con);
|
||||||
|
}
|
||||||
|
return self.accounts.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_selected_id(&self) -> i64 {
|
||||||
|
if let Some(i) = self.state.selected() {
|
||||||
|
self.accounts.get(i).map(|a| a.get_id()).unwrap_or(-1)
|
||||||
|
} else {
|
||||||
|
-1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_account(&mut self, ac: Account) {
|
||||||
|
self.accounts.push(ac);
|
||||||
|
}
|
||||||
|
}
|
||||||
31
src/entities/trxlist.rs
Normal file
31
src/entities/trxlist.rs
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
use ratatui::widgets::ListState;
|
||||||
|
use sqlite::Connection;
|
||||||
|
|
||||||
|
use crate::{data_layer, entities::*};
|
||||||
|
|
||||||
|
pub struct TrxList {
|
||||||
|
trx: Vec<Transaction>,
|
||||||
|
ac_id: i64,
|
||||||
|
pub state: ListState,
|
||||||
|
}
|
||||||
|
impl TrxList {
|
||||||
|
pub fn new() -> TrxList {
|
||||||
|
let mut list_state = ListState::default();
|
||||||
|
list_state.select_first();
|
||||||
|
return TrxList {
|
||||||
|
trx: Vec::new(),
|
||||||
|
state: list_state,
|
||||||
|
ac_id: 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_trx(&mut self, ac_id: i64, con: &Connection) -> Vec<Transaction> {
|
||||||
|
if ac_id == -1 {
|
||||||
|
return Vec::new();
|
||||||
|
}
|
||||||
|
if self.trx.iter().count() == 0 || self.ac_id != ac_id {
|
||||||
|
self.trx = data_layer::get_account_transactions(con, ac_id);
|
||||||
|
}
|
||||||
|
return self.trx.clone();
|
||||||
|
}
|
||||||
|
}
|
||||||
11
src/enums.rs
Normal file
11
src/enums.rs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
pub enum CurrentWidget {
|
||||||
|
AccountList,
|
||||||
|
TrxInfo,
|
||||||
|
TrxList,
|
||||||
|
}
|
||||||
|
pub enum CurrentScreen {
|
||||||
|
Main,
|
||||||
|
Exiting,
|
||||||
|
NewAccount,
|
||||||
|
NewTransaction,
|
||||||
|
}
|
||||||
70
src/main.rs
70
src/main.rs
@@ -1,12 +1,10 @@
|
|||||||
mod app;
|
mod app;
|
||||||
mod data_layer;
|
mod data_layer;
|
||||||
mod entities;
|
mod entities;
|
||||||
|
mod enums;
|
||||||
mod ui;
|
mod ui;
|
||||||
use crate::{
|
use crate::{app::App, enums::*, ui::ui};
|
||||||
app::{App, CurrentScreen},
|
use entities::{Account, AccountType};
|
||||||
ui::ui,
|
|
||||||
};
|
|
||||||
use app::CurrentWidget;
|
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
Terminal,
|
Terminal,
|
||||||
crossterm::{
|
crossterm::{
|
||||||
@@ -62,27 +60,50 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, app: &mut App) -> io::Result<
|
|||||||
KeyCode::Left => {
|
KeyCode::Left => {
|
||||||
app.current_widget = CurrentWidget::AccountList;
|
app.current_widget = CurrentWidget::AccountList;
|
||||||
}
|
}
|
||||||
KeyCode::Char('j') => {
|
KeyCode::Char('j') => match app.current_widget {
|
||||||
match app.current_widget {
|
CurrentWidget::AccountList => {
|
||||||
CurrentWidget::AccountList => {
|
app.next_ac();
|
||||||
app.next_ac();
|
|
||||||
}
|
|
||||||
CurrentWidget::TrxList => {
|
|
||||||
//app.next_trx();
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
|
CurrentWidget::TrxList => {
|
||||||
|
app.next_tr();
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
},
|
||||||
|
KeyCode::Char('k') => match app.current_widget {
|
||||||
|
CurrentWidget::AccountList => {
|
||||||
|
app.previous_ac();
|
||||||
|
}
|
||||||
|
CurrentWidget::TrxList => {
|
||||||
|
app.previous_tr();
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
},
|
||||||
|
KeyCode::Char('n') => match app.current_widget {
|
||||||
|
CurrentWidget::AccountList => {
|
||||||
|
app.current_screen = CurrentScreen::NewAccount;
|
||||||
|
}
|
||||||
|
CurrentWidget::TrxList => {
|
||||||
|
app.current_screen = CurrentScreen::NewTransaction;
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
},
|
||||||
|
_ => {}
|
||||||
|
},
|
||||||
|
CurrentScreen::NewAccount => match key.code {
|
||||||
|
KeyCode::Enter => {
|
||||||
|
app.save_new_account();
|
||||||
|
app.current_screen = CurrentScreen::Main;
|
||||||
}
|
}
|
||||||
KeyCode::Char('k') => {
|
KeyCode::Esc => {
|
||||||
match app.current_widget {
|
app.current_screen = CurrentScreen::Main;
|
||||||
CurrentWidget::AccountList => {
|
app.new_account = Account::new(0, String::new(), AccountType::Cash);
|
||||||
app.previous_ac();
|
}
|
||||||
}
|
_ => {}
|
||||||
CurrentWidget::TrxList => {
|
},
|
||||||
//app.previous_trx();
|
CurrentScreen::NewTransaction => match key.code {
|
||||||
}
|
KeyCode::Enter => {}
|
||||||
_ => {}
|
KeyCode::Esc => {
|
||||||
}
|
app.current_screen = CurrentScreen::Main;
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
},
|
},
|
||||||
@@ -94,6 +115,7 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, app: &mut App) -> io::Result<
|
|||||||
app.current_screen = CurrentScreen::Main;
|
app.current_screen = CurrentScreen::Main;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
10
src/ui.rs
10
src/ui.rs
@@ -1,6 +1,7 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
app::{App, CurrentScreen, CurrentWidget},
|
app::App,
|
||||||
entities::{Account, Transaction},
|
entities::{Account, Transaction},
|
||||||
|
enums::*,
|
||||||
};
|
};
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
Frame,
|
Frame,
|
||||||
@@ -103,6 +104,13 @@ pub fn ui(frame: &mut Frame, app: &mut App) {
|
|||||||
&mut app.trx_list.state,
|
&mut app.trx_list.state,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if let CurrentScreen::NewAccount = app.current_screen {
|
||||||
|
let popup = Block::default()
|
||||||
|
.title("New account")
|
||||||
|
.borders(Borders::all())
|
||||||
|
.style(Style::default());
|
||||||
|
}
|
||||||
|
|
||||||
if let CurrentScreen::Exiting = app.current_screen {
|
if let CurrentScreen::Exiting = app.current_screen {
|
||||||
let popup = Block::default()
|
let popup = Block::default()
|
||||||
.title("Exiting program")
|
.title("Exiting program")
|
||||||
|
|||||||
Reference in New Issue
Block a user