diff --git a/src/app.rs b/src/app.rs index 9c37fd2..28b8e59 100644 --- a/src/app.rs +++ b/src/app.rs @@ -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, + 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 { + 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 { + 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(); + } } diff --git a/src/entities/transaction.rs b/src/entities/transaction.rs index 0b79998..16522da 100644 --- a/src/entities/transaction.rs +++ b/src/entities/transaction.rs @@ -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, diff --git a/src/ui.rs b/src/ui.rs index 5a108e0..c089600 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -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 = app + let ac_items: Vec = 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 = 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) + } +}