168 lines
6.1 KiB
Rust
168 lines
6.1 KiB
Rust
use chrono::{Local, NaiveDate, NaiveDateTime, NaiveTime, TimeZone};
|
|
use sqlite::Connection;
|
|
|
|
use crate::{data_layer, entities::*, enums::*};
|
|
|
|
pub struct App {
|
|
pub current_screen: CurrentScreen,
|
|
pub current_widget: CurrentWidget,
|
|
pub acc_list: AccountList,
|
|
pub trx_table: TrxTable,
|
|
pub new_account: Account,
|
|
pub new_transaction: Transaction,
|
|
pub selected_transaction_input: TransactionInput,
|
|
pub current_input: String,
|
|
pub connection: Connection,
|
|
}
|
|
|
|
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");
|
|
}
|
|
};
|
|
|
|
data_layer::setup(&con);
|
|
|
|
return App {
|
|
current_screen: CurrentScreen::Main,
|
|
current_widget: CurrentWidget::AccountList,
|
|
acc_list: AccountList::new(),
|
|
trx_table: TrxTable::new(),
|
|
new_account: Account::new(0, String::new(), AccountType::Cash),
|
|
new_transaction: Transaction::new_empty(),
|
|
selected_transaction_input: TransactionInput::Type,
|
|
current_input: String::new(),
|
|
connection: con,
|
|
};
|
|
}
|
|
|
|
pub fn get_list_accounts(&mut self) -> Vec<Account> {
|
|
return self.acc_list.get_accounts(&self.connection);
|
|
}
|
|
|
|
pub fn next_ac(&mut self) {
|
|
self.acc_list.state.select_next();
|
|
}
|
|
pub fn previous_ac(&mut self) {
|
|
self.acc_list.state.select_previous();
|
|
}
|
|
|
|
pub fn get_list_trx(&mut self) -> Vec<Transaction> {
|
|
let accounts = self
|
|
.trx_table
|
|
.get_trx(self.acc_list.get_selected_id(), &self.connection);
|
|
return accounts.to_vec();
|
|
}
|
|
|
|
pub fn next_tr(&mut self) {
|
|
self.trx_table.state.select_next();
|
|
}
|
|
pub fn previous_tr(&mut self) {
|
|
self.trx_table.state.select_previous();
|
|
}
|
|
|
|
pub fn save_new_account(&mut self) {
|
|
let ac = data_layer::upsert_account(&self.connection, self.new_account.clone());
|
|
self.acc_list.add_account(ac);
|
|
self.new_account = Account::new(0, String::new(), AccountType::Cash);
|
|
}
|
|
|
|
pub fn save_new_tr(&mut self) {
|
|
self.next_tr_input();
|
|
let tr = data_layer::upsert_transaction(&self.connection, self.new_transaction.clone());
|
|
self.trx_table.add_tr(tr);
|
|
self.new_transaction = Transaction::new_empty();
|
|
self.current_input = String::new();
|
|
self.selected_transaction_input = TransactionInput::Type;
|
|
}
|
|
|
|
pub fn next_tr_input(&mut self) {
|
|
match self.selected_transaction_input {
|
|
TransactionInput::Type => {
|
|
self.selected_transaction_input = TransactionInput::Amount;
|
|
self.new_transaction.set_type(
|
|
self.current_input
|
|
.parse::<i64>()
|
|
.expect("Failed to parse data"),
|
|
&self.connection,
|
|
);
|
|
}
|
|
TransactionInput::Amount => {
|
|
self.selected_transaction_input = TransactionInput::Date;
|
|
self.new_transaction.set_amount(
|
|
self.current_input
|
|
.parse::<f64>()
|
|
.expect("Failed to parse data"),
|
|
);
|
|
}
|
|
TransactionInput::Date => {
|
|
self.selected_transaction_input = TransactionInput::Description;
|
|
let date = NaiveDate::parse_from_str(&self.current_input, "%Y-%m-%d")
|
|
.expect("Faile to parse data");
|
|
let time = NaiveTime::from_hms_opt(5, 0, 0).unwrap(); // to account for my local datetime
|
|
self.new_transaction
|
|
.set_date(Local.from_utc_datetime(&NaiveDateTime::new(date, time)));
|
|
}
|
|
TransactionInput::Description => {
|
|
self.new_transaction.set_desc(self.current_input.clone())
|
|
}
|
|
}
|
|
self.set_current_input();
|
|
}
|
|
pub fn previous_tr_input(&mut self) {
|
|
match self.selected_transaction_input {
|
|
TransactionInput::Type => {
|
|
self.new_transaction.set_type(
|
|
self.current_input
|
|
.parse::<i64>()
|
|
.expect("Failed to parse data"),
|
|
&self.connection,
|
|
);
|
|
}
|
|
TransactionInput::Amount => {
|
|
self.selected_transaction_input = TransactionInput::Type;
|
|
self.new_transaction.set_amount(
|
|
self.current_input
|
|
.parse::<f64>()
|
|
.expect("Failed to parse data"),
|
|
);
|
|
}
|
|
TransactionInput::Date => {
|
|
self.selected_transaction_input = TransactionInput::Amount;
|
|
let date = NaiveDate::parse_from_str(&self.current_input, "%Y-%m-%d")
|
|
.expect("Faile to parse data");
|
|
let time = NaiveTime::from_hms_opt(5, 0, 0).unwrap(); // to account for my local datetime
|
|
self.new_transaction
|
|
.set_date(Local.from_utc_datetime(&NaiveDateTime::new(date, time)));
|
|
}
|
|
TransactionInput::Description => {
|
|
self.selected_transaction_input = TransactionInput::Date;
|
|
self.new_transaction.set_desc(self.current_input.clone());
|
|
}
|
|
}
|
|
self.set_current_input();
|
|
}
|
|
fn set_current_input(&mut self) {
|
|
match self.selected_transaction_input {
|
|
TransactionInput::Type => {
|
|
self.current_input = self.new_transaction.get_type().get_id().to_string()
|
|
}
|
|
TransactionInput::Amount => {
|
|
self.current_input = self.new_transaction.get_amount().to_string()
|
|
}
|
|
TransactionInput::Date => {
|
|
self.current_input = self
|
|
.new_transaction
|
|
.get_date()
|
|
.format("%Y-%m-%d")
|
|
.to_string()
|
|
}
|
|
TransactionInput::Description => self.current_input = self.new_transaction.get_desc(),
|
|
}
|
|
}
|
|
}
|