better ac list
This commit is contained in:
parent
33fb39e732
commit
dd6b6f7b2c
20
src/app.rs
20
src/app.rs
@ -1,7 +1,10 @@
|
||||
use ratatui::widgets::ListState;
|
||||
use sqlite::Connection;
|
||||
|
||||
use crate::{data_layer, entities::Account};
|
||||
use crate::{
|
||||
data_layer,
|
||||
entities::{Account, AccountType},
|
||||
};
|
||||
|
||||
pub enum CurrentScreen {
|
||||
Main,
|
||||
@ -60,6 +63,19 @@ impl App {
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@ -78,9 +78,16 @@ pub fn get_account(con: &Connection, id: i64) -> Account {
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
if id != 0 {
|
||||
statement.bind((1, id)).unwrap();
|
||||
}
|
||||
|
||||
if let Ok(State::Row) = statement.next() {
|
||||
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> {
|
||||
if ac_id == 0 {
|
||||
return get_transactions(con);
|
||||
}
|
||||
|
||||
let query = "SELECT * FROM Transactions WHERE account_id = ?";
|
||||
let mut statement = con.prepare(query).unwrap();
|
||||
statement.bind((1, ac_id)).unwrap();
|
||||
|
||||
22
src/main.rs
22
src/main.rs
@ -62,6 +62,28 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, app: &mut App) -> io::Result<
|
||||
KeyCode::Left => {
|
||||
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 {
|
||||
|
||||
@ -18,7 +18,7 @@ const ALT_ROW_BG_COLOR: Color = SLATE.c900;
|
||||
const TEXT_FG_COLOR: Color = SLATE.c200;
|
||||
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()
|
||||
.direction(Direction::Horizontal)
|
||||
.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]);
|
||||
|
||||
app.first_ac();
|
||||
let info = if let Some(i) = app.acc_list.state.selected() {
|
||||
format!(
|
||||
"Total: {}",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user