From dd6b6f7b2c3ec21b72efceffb013705a18c828fb Mon Sep 17 00:00:00 2001 From: thatscringebro Date: Wed, 17 Dec 2025 13:49:31 -0500 Subject: [PATCH] better ac list --- src/app.rs | 20 ++++++++++++++++++-- src/data_layer.rs | 15 +++++++++++++-- src/main.rs | 22 ++++++++++++++++++++++ src/ui.rs | 3 ++- 4 files changed, 55 insertions(+), 5 deletions(-) diff --git a/src/app.rs b/src/app.rs index 71506cb..dbf9674 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,7 +1,10 @@ use ratatui::widgets::ListState; use sqlite::Connection; -use crate::{data_layer, entities::Account}; +use crate::{ + data_layer, + entities::{Account, AccountType}, +}; pub enum CurrentScreen { Main, @@ -60,6 +63,19 @@ impl App { } pub fn get_list_accounts(&self) -> Vec { - return 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); + accounts.insert(0, all); + return accounts; + } + + pub fn first_ac(&mut self) { + self.acc_list.state.select_first(); + } + pub fn next_ac(&mut self) { + self.acc_list.state.select_next(); + } + pub fn previous_ac(&mut self) { + self.acc_list.state.select_previous(); } } diff --git a/src/data_layer.rs b/src/data_layer.rs index 1d9b59d..ea9142f 100644 --- a/src/data_layer.rs +++ b/src/data_layer.rs @@ -78,9 +78,16 @@ pub fn get_account(con: &Connection, id: i64) -> Account { } pub fn get_account_total(id: i64, con: &Connection) -> f64 { - let query = "SELECT SUM(amount) as total FROM Transactions WHERE account_id = ?"; + let mut query = "SELECT SUM(amount) as total FROM Transactions".to_owned(); + if id != 0 { + query.push_str("WHERE account_id = ?") + } + let mut statement = con.prepare(query).unwrap(); - statement.bind((1, id)).unwrap(); + + if id != 0 { + statement.bind((1, id)).unwrap(); + } if let Ok(State::Row) = statement.next() { return statement.read::("total").unwrap(); @@ -227,6 +234,10 @@ pub fn get_transaction(con: &Connection, id: i64) -> Transaction { } pub fn get_account_transactions(con: &Connection, ac_id: i64) -> Vec { + if ac_id == 0 { + return get_transactions(con); + } + let query = "SELECT * FROM Transactions WHERE account_id = ?"; let mut statement = con.prepare(query).unwrap(); statement.bind((1, ac_id)).unwrap(); diff --git a/src/main.rs b/src/main.rs index ea5ab7e..04ff29c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -62,6 +62,28 @@ fn run_app(terminal: &mut Terminal, app: &mut App) -> io::Result< KeyCode::Left => { app.current_widget = CurrentWidget::AccountList; } + KeyCode::Char('j') => { + match app.current_widget { + CurrentWidget::AccountList => { + app.next_ac(); + } + CurrentWidget::TrxList => { + //app.next_trx(); + } + _ => {} + } + } + KeyCode::Char('k') => { + match app.current_widget { + CurrentWidget::AccountList => { + app.previous_ac(); + } + CurrentWidget::TrxList => { + //app.previous_trx(); + } + _ => {} + } + } _ => {} }, CurrentScreen::Exiting => match key.code { diff --git a/src/ui.rs b/src/ui.rs index 39bfce8..f5e451a 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -18,7 +18,7 @@ const ALT_ROW_BG_COLOR: Color = SLATE.c900; const TEXT_FG_COLOR: Color = SLATE.c200; const SELECTED_STYLE: Style = Style::new().bg(SLATE.c800).add_modifier(Modifier::BOLD); -pub fn ui(frame: &mut Frame, app: &App) { +pub fn ui(frame: &mut Frame, app: &mut App) { let layout = Layout::default() .direction(Direction::Horizontal) .constraints(vec![Constraint::Percentage(20), Constraint::Percentage(80)]) @@ -65,6 +65,7 @@ pub fn ui(frame: &mut Frame, app: &App) { frame.render_widget(list, layout[0]); + app.first_ac(); let info = if let Some(i) = app.acc_list.state.selected() { format!( "Total: {}",