trx as list => to change to table

This commit is contained in:
thatscringebro 2025-12-18 15:57:49 -05:00
parent 2395d72c7e
commit df4fd0202d
3 changed files with 79 additions and 5 deletions

View File

@ -3,7 +3,7 @@ use sqlite::Connection;
use crate::{
data_layer,
entities::{Account, AccountType},
entities::{Account, AccountType, Transaction},
};
pub enum CurrentScreen {
@ -38,10 +38,39 @@ impl AccountList {
}
}
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 current_screen: CurrentScreen,
pub current_widget: CurrentWidget,
pub acc_list: AccountList,
pub trx_list: TrxList,
pub connection: Connection,
exit: bool,
}
@ -59,6 +88,7 @@ impl App {
current_screen: CurrentScreen::Main,
current_widget: CurrentWidget::AccountList,
acc_list: AccountList::new(),
trx_list: TrxList::new(),
connection: con,
exit: false,
};
@ -80,4 +110,19 @@ impl App {
pub fn previous_ac(&mut self) {
self.acc_list.state.select_previous();
}
pub fn get_list_trx(&self) -> Vec<Transaction> {
let accounts = &self.trx_list.get_trx(&self.connection, self);
return accounts.to_vec();
}
pub fn first_tr(&mut self) {
self.trx_list.state.select_first();
}
pub fn next_tr(&mut self) {
self.trx_list.state.select_next();
}
pub fn previous_tr(&mut self) {
self.trx_list.state.select_previous();
}
}

View File

@ -2,6 +2,7 @@ use crate::{data_layer, entities::Account};
use chrono::{DateTime, Utc};
use sqlite::Connection;
#[derive(Clone)]
pub struct Transaction {
id: i64,
account_id: i64,

View File

@ -1,6 +1,6 @@
use crate::{
app::{App, CurrentScreen, CurrentWidget},
entities::Account,
entities::{Account, Transaction},
};
use ratatui::{
Frame,
@ -50,7 +50,7 @@ pub fn ui(frame: &mut Frame, app: &mut App) {
CurrentWidget::TrxList => trx_block = trx_block.border_style(active_style),
};
let items: Vec<ListItem> = app
let ac_items: Vec<ListItem> = app
.get_list_accounts()
.iter()
.enumerate()
@ -60,7 +60,7 @@ pub fn ui(frame: &mut Frame, app: &mut App) {
})
.collect();
let list = List::new(items)
let list = List::new(ac_items)
.block(ac_block)
.highlight_style(SELECTED_STYLE)
.highlight_symbol(">")
@ -79,7 +79,29 @@ pub fn ui(frame: &mut Frame, app: &mut App) {
"No account selected...".to_string()
};
frame.render_widget(Paragraph::new(info).block(info_block), right_layout[0]);
frame.render_widget(Paragraph::new("inner 1").block(trx_block), right_layout[1]);
let trx_items: Vec<ListItem> = app
.get_list_trx()
.iter()
.enumerate()
.map(|(i, tr)| {
let color = alternate_colors(i);
ListItem::from(tr).bg(color)
})
.collect();
let trx_list = List::new(trx_items)
.block(trx_block)
.highlight_style(SELECTED_STYLE)
.highlight_symbol(">")
.highlight_spacing(HighlightSpacing::Always);
StatefulWidget::render(
trx_list,
right_layout[1],
frame.buffer_mut(),
&mut app.trx_list.state,
);
if let CurrentScreen::Exiting = app.current_screen {
let popup = Block::default()
@ -134,3 +156,9 @@ impl From<&Account> for ListItem<'_> {
ListItem::new(line)
}
}
impl From<&Transaction> for ListItem<'_> {
fn from(value: &Transaction) -> Self {
let line = Line::styled(value.get_desc(), TEXT_FG_COLOR);
ListItem::new(line)
}
}