diff --git a/src/app.rs b/src/app.rs index 7937bfe..568789e 100644 --- a/src/app.rs +++ b/src/app.rs @@ -23,6 +23,9 @@ impl App { panic!("stopping"); } }; + + data_layer::setup(&con); + return App { current_screen: CurrentScreen::Main, current_widget: CurrentWidget::AccountList, @@ -36,15 +39,9 @@ impl App { } pub fn get_list_accounts(&mut self) -> Vec { - 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; + return self.acc_list.get_accounts(&self.connection); } - pub fn first_ac(&mut self) { - self.acc_list.state.select_first(); - } pub fn next_ac(&mut self) { self.acc_list.state.select_next(); } @@ -59,9 +56,6 @@ impl App { 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(); } @@ -74,4 +68,10 @@ impl App { self.acc_list.add_account(ac); self.new_account = Account::new(0, String::new(), AccountType::Cash); } + + pub fn save_new_tr(&mut self) { + let tr = data_layer::upsert_transaction(&self.connection, self.new_transaction.clone()); + self.trx_list.add_tr(tr); + self.new_transaction = Transaction::new_empty(); + } } diff --git a/src/data_layer.rs b/src/data_layer.rs index e1cf7a6..aa68511 100644 --- a/src/data_layer.rs +++ b/src/data_layer.rs @@ -101,6 +101,9 @@ pub fn get_accounts(con: &Connection) -> Vec { let mut statement = con.prepare(query).unwrap(); let mut vec = Vec::::new(); + let all = Account::new(0, "All".to_string(), AccountType::Cash); + vec.push(all); + while let Ok(State::Row) = statement.next() { vec.push(Account::new( statement.read::("id").unwrap(), @@ -237,10 +240,11 @@ pub fn get_account_transactions(con: &Connection, ac_id: i64) -> Vec::new(); while let Ok(State::Row) = statement.next() { @@ -259,7 +263,7 @@ pub fn get_account_transactions(con: &Connection, ac_id: i64) -> Vec Vec { - let query = "SELECT * FROM TransactionTypes"; + let query = "SELECT * FROM Transactions"; let mut statement = con.prepare(query).unwrap(); let mut vec = Vec::::new(); diff --git a/src/entities/trxlist.rs b/src/entities/trxlist.rs index a1c4dd2..7d34128 100644 --- a/src/entities/trxlist.rs +++ b/src/entities/trxlist.rs @@ -24,8 +24,13 @@ impl TrxList { return Vec::new(); } if self.trx.iter().count() == 0 || self.ac_id != ac_id { + self.ac_id = ac_id; self.trx = data_layer::get_account_transactions(con, ac_id); } return self.trx.clone(); } + + pub fn add_tr(&mut self, tr: Transaction) { + self.trx.push(tr); + } } diff --git a/src/enums.rs b/src/enums.rs index 4262df8..eb0b98f 100644 --- a/src/enums.rs +++ b/src/enums.rs @@ -1,6 +1,5 @@ pub enum CurrentWidget { AccountList, - TrxInfo, TrxList, } pub enum CurrentScreen { diff --git a/src/main.rs b/src/main.rs index fb60d19..df0a8c1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ mod entities; mod enums; mod ui; use crate::{app::App, enums::*, ui::ui}; -use entities::{Account, AccountType}; +use entities::{Account, AccountType, Transaction}; use ratatui::{ Terminal, crossterm::{ @@ -101,9 +101,13 @@ fn run_app(terminal: &mut Terminal, app: &mut App) -> io::Result< _ => {} }, CurrentScreen::NewTransaction => match key.code { - KeyCode::Enter => {} + KeyCode::Enter => { + app.save_new_tr(); + app.current_screen = CurrentScreen::Main; + } KeyCode::Esc => { app.current_screen = CurrentScreen::Main; + app.new_transaction = Transaction::new_empty(); } _ => {} }, diff --git a/src/ui.rs b/src/ui.rs index 4718965..8668769 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -6,15 +6,9 @@ use crate::{ use ratatui::{ Frame, layout::{Constraint, Direction, Layout, Rect}, - style::{ - Color, Modifier, Style, Stylize, - palette::tailwind::{BLUE, GREEN, SLATE}, - }, + style::{Color, Modifier, Style, Stylize, palette::tailwind::SLATE}, text::{Line, Text}, - widgets::{ - Block, Borders, Clear, HighlightSpacing, List, ListItem, Paragraph, StatefulWidget, Widget, - Wrap, - }, + widgets::{Block, Borders, HighlightSpacing, List, ListItem, Paragraph, StatefulWidget, Wrap}, }; const NORMAL_ROW_BG: Color = SLATE.c950; @@ -36,7 +30,7 @@ pub fn ui(frame: &mut Frame, app: &mut App) { .title("Accounts") .borders(Borders::ALL) .border_style(Style::new().fg(Color::DarkGray)); - let mut info_block = Block::default() + let info_block = Block::default() .title("Account info") .borders(Borders::ALL) .border_style(Style::new().fg(Color::DarkGray)); @@ -47,7 +41,6 @@ pub fn ui(frame: &mut Frame, app: &mut App) { let active_style = Style::default(); match app.current_widget { CurrentWidget::AccountList => ac_block = ac_block.border_style(active_style), - CurrentWidget::TrxInfo => info_block = info_block.border_style(active_style), CurrentWidget::TrxList => trx_block = trx_block.border_style(active_style), }; @@ -109,6 +102,19 @@ pub fn ui(frame: &mut Frame, app: &mut App) { .title("New account") .borders(Borders::all()) .style(Style::default()); + + let area = centered_rect(50, 40, frame.area()); + frame.render_widget(popup, area); + } + + if let CurrentScreen::NewTransaction = app.current_screen { + let popup = Block::default() + .title("New Transaction") + .borders(Borders::all()) + .style(Style::default()); + + let area = centered_rect(50, 40, frame.area()); + frame.render_widget(popup, area); } if let CurrentScreen::Exiting = app.current_screen { @@ -160,7 +166,10 @@ const fn alternate_colors(i: usize) -> Color { impl From<&Account> for ListItem<'_> { fn from(value: &Account) -> Self { - let line = Line::styled(value.get_name(), TEXT_FG_COLOR); + let line = Line::styled( + value.get_id().to_string() + " " + &value.get_name(), + TEXT_FG_COLOR, + ); ListItem::new(line) } }