better ac list

This commit is contained in:
thatscringebro 2025-12-17 13:49:31 -05:00
parent 33fb39e732
commit dd6b6f7b2c
4 changed files with 55 additions and 5 deletions

View File

@ -1,7 +1,10 @@
use ratatui::widgets::ListState; use ratatui::widgets::ListState;
use sqlite::Connection; use sqlite::Connection;
use crate::{data_layer, entities::Account}; use crate::{
data_layer,
entities::{Account, AccountType},
};
pub enum CurrentScreen { pub enum CurrentScreen {
Main, Main,
@ -60,6 +63,19 @@ impl App {
} }
pub fn get_list_accounts(&self) -> Vec<Account> { pub fn get_list_accounts(&self) -> Vec<Account> {
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();
} }
} }

View File

@ -78,9 +78,16 @@ pub fn get_account(con: &Connection, id: i64) -> Account {
} }
pub fn get_account_total(id: i64, con: &Connection) -> f64 { 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(); let mut statement = con.prepare(query).unwrap();
if id != 0 {
statement.bind((1, id)).unwrap(); statement.bind((1, id)).unwrap();
}
if let Ok(State::Row) = statement.next() { if let Ok(State::Row) = statement.next() {
return statement.read::<f64, _>("total").unwrap(); return statement.read::<f64, _>("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<Transaction> { pub fn get_account_transactions(con: &Connection, ac_id: i64) -> Vec<Transaction> {
if ac_id == 0 {
return get_transactions(con);
}
let query = "SELECT * FROM Transactions WHERE account_id = ?"; let query = "SELECT * FROM Transactions WHERE account_id = ?";
let mut statement = con.prepare(query).unwrap(); let mut statement = con.prepare(query).unwrap();
statement.bind((1, ac_id)).unwrap(); statement.bind((1, ac_id)).unwrap();

View File

@ -62,6 +62,28 @@ 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') => {
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 { CurrentScreen::Exiting => match key.code {

View File

@ -18,7 +18,7 @@ const ALT_ROW_BG_COLOR: Color = SLATE.c900;
const TEXT_FG_COLOR: Color = SLATE.c200; const TEXT_FG_COLOR: Color = SLATE.c200;
const SELECTED_STYLE: Style = Style::new().bg(SLATE.c800).add_modifier(Modifier::BOLD); 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() let layout = Layout::default()
.direction(Direction::Horizontal) .direction(Direction::Horizontal)
.constraints(vec![Constraint::Percentage(20), Constraint::Percentage(80)]) .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]); frame.render_widget(list, layout[0]);
app.first_ac();
let info = if let Some(i) = app.acc_list.state.selected() { let info = if let Some(i) = app.acc_list.state.selected() {
format!( format!(
"Total: {}", "Total: {}",