Avancement du projet
This commit is contained in:
20
src/app.rs
20
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<Account> {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,6 +101,9 @@ pub fn get_accounts(con: &Connection) -> Vec<Account> {
|
||||
let mut statement = con.prepare(query).unwrap();
|
||||
let mut vec = Vec::<Account>::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::<i64, _>("id").unwrap(),
|
||||
@@ -237,10 +240,11 @@ pub fn get_account_transactions(con: &Connection, ac_id: i64) -> Vec<Transaction
|
||||
if ac_id == 0 {
|
||||
return get_transactions(con);
|
||||
}
|
||||
// println!("allo{}", ac_id);
|
||||
|
||||
let query = "SELECT * FROM Transactions WHERE account_id = ?";
|
||||
let query = "SELECT * FROM Transactions WHERE account_id = :id";
|
||||
let mut statement = con.prepare(query).unwrap();
|
||||
statement.bind((1, ac_id)).unwrap();
|
||||
statement.bind((":id", ac_id)).unwrap();
|
||||
let mut vec = Vec::<Transaction>::new();
|
||||
|
||||
while let Ok(State::Row) = statement.next() {
|
||||
@@ -259,7 +263,7 @@ pub fn get_account_transactions(con: &Connection, ac_id: i64) -> Vec<Transaction
|
||||
}
|
||||
|
||||
pub fn get_transactions(con: &Connection) -> Vec<Transaction> {
|
||||
let query = "SELECT * FROM TransactionTypes";
|
||||
let query = "SELECT * FROM Transactions";
|
||||
let mut statement = con.prepare(query).unwrap();
|
||||
let mut vec = Vec::<Transaction>::new();
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
pub enum CurrentWidget {
|
||||
AccountList,
|
||||
TrxInfo,
|
||||
TrxList,
|
||||
}
|
||||
pub enum CurrentScreen {
|
||||
|
||||
@@ -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<B: Backend>(terminal: &mut Terminal<B>, 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();
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
|
||||
31
src/ui.rs
31
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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user