début new account

This commit is contained in:
thatscringebro
2026-02-09 14:30:51 -05:00
parent df4fd0202d
commit b267360b2e
7 changed files with 155 additions and 92 deletions

View File

@@ -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);
}
}

View File

@@ -1,5 +1,9 @@
pub mod account;
pub mod accountlist;
pub mod transaction;
pub mod trxlist;
pub use account::{Account, AccountType};
pub use accountlist::*;
pub use transaction::{Transaction, TransactionType};
pub use trxlist::*;

View 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
View 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
View File

@@ -0,0 +1,11 @@
pub enum CurrentWidget {
AccountList,
TrxInfo,
TrxList,
}
pub enum CurrentScreen {
Main,
Exiting,
NewAccount,
NewTransaction,
}

View File

@@ -1,12 +1,10 @@
mod app;
mod data_layer;
mod entities;
mod enums;
mod ui;
use crate::{
app::{App, CurrentScreen},
ui::ui,
};
use app::CurrentWidget;
use crate::{app::App, enums::*, ui::ui};
use entities::{Account, AccountType};
use ratatui::{
Terminal,
crossterm::{
@@ -62,27 +60,50 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, app: &mut App) -> io::Result<
KeyCode::Left => {
app.current_widget = CurrentWidget::AccountList;
}
KeyCode::Char('j') => {
match app.current_widget {
KeyCode::Char('j') => match app.current_widget {
CurrentWidget::AccountList => {
app.next_ac();
}
CurrentWidget::TrxList => {
//app.next_trx();
app.next_tr();
}
_ => {}
}
}
KeyCode::Char('k') => {
match app.current_widget {
},
KeyCode::Char('k') => match app.current_widget {
CurrentWidget::AccountList => {
app.previous_ac();
}
CurrentWidget::TrxList => {
//app.previous_trx();
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::Esc => {
app.current_screen = CurrentScreen::Main;
app.new_account = Account::new(0, String::new(), AccountType::Cash);
}
_ => {}
},
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;
}
},
_ => {}
}
}
}

View File

@@ -1,6 +1,7 @@
use crate::{
app::{App, CurrentScreen, CurrentWidget},
app::App,
entities::{Account, Transaction},
enums::*,
};
use ratatui::{
Frame,
@@ -103,6 +104,13 @@ pub fn ui(frame: &mut Frame, app: &mut App) {
&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 {
let popup = Block::default()
.title("Exiting program")