ft_rs/src/app.rs
thatscringebro 2395d72c7e fixed list
2025-12-17 19:13:54 -05:00

84 lines
2.0 KiB
Rust

use ratatui::widgets::ListState;
use sqlite::Connection;
use crate::{
data_layer,
entities::{Account, AccountType},
};
pub enum CurrentScreen {
Main,
Exiting,
}
pub enum CurrentWidget {
AccountList,
TrxInfo,
TrxList,
}
pub struct AccountList {
accounts: Vec<Account>,
pub state: ListState,
}
impl AccountList {
fn new() -> AccountList {
let mut list_state = ListState::default();
list_state.select_first();
return AccountList {
accounts: Vec::new(),
state: list_state,
};
}
fn get_accounts(&self, con: &Connection) -> Vec<Account> {
if self.accounts.iter().count() == 0 {
return data_layer::get_accounts(con);
}
return self.accounts.clone();
}
}
pub struct App {
pub current_screen: CurrentScreen,
pub current_widget: CurrentWidget,
pub acc_list: AccountList,
pub connection: Connection,
exit: bool,
}
impl App {
pub fn new() -> App {
let con = match Connection::open("ft_rs.db") {
Ok(con) => con,
Err(e) => {
eprintln!("Error opening database: {}", e);
panic!("stopping");
}
};
return App {
current_screen: CurrentScreen::Main,
current_widget: CurrentWidget::AccountList,
acc_list: AccountList::new(),
connection: con,
exit: false,
};
}
pub fn get_list_accounts(&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;
}
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();
}
}