Wire log view to database for transaction persistence
This commit is contained in:
@@ -1,20 +1,18 @@
|
||||
use adw::prelude::*;
|
||||
use gtk::glib;
|
||||
use outlay_core::db::Database;
|
||||
use outlay_core::models::{NewTransaction, TransactionType};
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
pub struct LogView {
|
||||
pub container: gtk::Box,
|
||||
pub type_toggle: gtk::ToggleButton,
|
||||
pub amount_row: adw::EntryRow,
|
||||
pub currency_row: adw::ComboRow,
|
||||
pub category_row: adw::ComboRow,
|
||||
pub date_label: gtk::Label,
|
||||
pub note_row: adw::EntryRow,
|
||||
pub save_button: gtk::Button,
|
||||
pub recent_group: adw::PreferencesGroup,
|
||||
pub toast_overlay: adw::ToastOverlay,
|
||||
}
|
||||
|
||||
impl LogView {
|
||||
pub fn new() -> Self {
|
||||
pub fn new(db: Rc<Database>) -> Self {
|
||||
let toast_overlay = adw::ToastOverlay::new();
|
||||
let container = gtk::Box::new(gtk::Orientation::Vertical, 0);
|
||||
|
||||
let clamp = adw::Clamp::new();
|
||||
@@ -26,6 +24,9 @@ impl LogView {
|
||||
|
||||
let inner = gtk::Box::new(gtk::Orientation::Vertical, 24);
|
||||
|
||||
// Category IDs tracked alongside the model
|
||||
let category_ids: Rc<RefCell<Vec<i64>>> = Rc::new(RefCell::new(Vec::new()));
|
||||
|
||||
// -- Transaction type toggle --
|
||||
let type_box = gtk::Box::new(gtk::Orientation::Horizontal, 0);
|
||||
type_box.add_css_class("linked");
|
||||
@@ -68,8 +69,8 @@ impl LogView {
|
||||
.model(&category_model)
|
||||
.build();
|
||||
|
||||
// Populate with default expense categories
|
||||
Self::populate_categories(&category_model, true);
|
||||
// Populate from database
|
||||
Self::populate_categories_from_db(&db, &category_model, &category_ids, TransactionType::Expense);
|
||||
|
||||
form_group.add(&category_row);
|
||||
|
||||
@@ -99,7 +100,6 @@ impl LogView {
|
||||
date_row.add_suffix(&date_box);
|
||||
date_row.set_activatable_widget(Some(&date_menu_btn));
|
||||
|
||||
// Connect calendar day-selected to update the label
|
||||
let date_label_ref = date_label.clone();
|
||||
let popover_ref = popover.clone();
|
||||
calendar.connect_day_selected(move |cal| {
|
||||
@@ -123,30 +123,132 @@ impl LogView {
|
||||
save_button.set_halign(gtk::Align::Center);
|
||||
save_button.set_margin_top(8);
|
||||
|
||||
// -- Wire type toggle to filter categories --
|
||||
let category_model_ref = category_model.clone();
|
||||
expense_btn.connect_toggled(move |btn| {
|
||||
if btn.is_active() {
|
||||
Self::populate_categories(&category_model_ref, true);
|
||||
}
|
||||
});
|
||||
let category_model_ref2 = category_model.clone();
|
||||
income_btn.connect_toggled(move |btn| {
|
||||
if btn.is_active() {
|
||||
Self::populate_categories(&category_model_ref2, false);
|
||||
}
|
||||
});
|
||||
// -- Wire type toggle to filter categories from DB --
|
||||
{
|
||||
let db_ref = db.clone();
|
||||
let model_ref = category_model.clone();
|
||||
let ids_ref = category_ids.clone();
|
||||
expense_btn.connect_toggled(move |btn| {
|
||||
if btn.is_active() {
|
||||
Self::populate_categories_from_db(
|
||||
&db_ref, &model_ref, &ids_ref, TransactionType::Expense,
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
{
|
||||
let db_ref = db.clone();
|
||||
let model_ref = category_model.clone();
|
||||
let ids_ref = category_ids.clone();
|
||||
income_btn.connect_toggled(move |btn| {
|
||||
if btn.is_active() {
|
||||
Self::populate_categories_from_db(
|
||||
&db_ref, &model_ref, &ids_ref, TransactionType::Income,
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// -- Recent transactions (placeholder) --
|
||||
// -- Recent transactions --
|
||||
let recent_group = adw::PreferencesGroup::builder()
|
||||
.title("Recent")
|
||||
.build();
|
||||
|
||||
let placeholder = adw::ActionRow::builder()
|
||||
.title("No transactions yet")
|
||||
.build();
|
||||
placeholder.add_css_class("dim-label");
|
||||
recent_group.add(&placeholder);
|
||||
Self::refresh_recent(&db, &recent_group);
|
||||
|
||||
// -- Wire save button --
|
||||
{
|
||||
let db_ref = db.clone();
|
||||
let expense_btn_ref = expense_btn.clone();
|
||||
let amount_row_ref = amount_row.clone();
|
||||
let currency_row_ref = currency_row.clone();
|
||||
let currency_model_ref = currency_model.clone();
|
||||
let category_row_ref = category_row.clone();
|
||||
let ids_ref = category_ids.clone();
|
||||
let date_label_ref = date_label.clone();
|
||||
let note_row_ref = note_row.clone();
|
||||
let recent_group_ref = recent_group.clone();
|
||||
let toast_overlay_ref = toast_overlay.clone();
|
||||
|
||||
save_button.connect_clicked(move |_| {
|
||||
let amount_text = amount_row_ref.text();
|
||||
let amount: f64 = match amount_text.trim().parse() {
|
||||
Ok(v) if v > 0.0 => v,
|
||||
_ => {
|
||||
let toast = adw::Toast::new("Please enter a valid amount");
|
||||
toast_overlay_ref.add_toast(toast);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let txn_type = if expense_btn_ref.is_active() {
|
||||
TransactionType::Expense
|
||||
} else {
|
||||
TransactionType::Income
|
||||
};
|
||||
|
||||
let cat_idx = category_row_ref.selected() as usize;
|
||||
let ids = ids_ref.borrow();
|
||||
let category_id = match ids.get(cat_idx) {
|
||||
Some(&id) => id,
|
||||
None => {
|
||||
let toast = adw::Toast::new("Please select a category");
|
||||
toast_overlay_ref.add_toast(toast);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let currency_idx = currency_row_ref.selected();
|
||||
let currency_item = currency_model_ref
|
||||
.string(currency_idx)
|
||||
.map(|s| s.to_string())
|
||||
.unwrap_or_else(|| "USD".to_string());
|
||||
|
||||
let date_text = date_label_ref.label();
|
||||
let date = chrono::NaiveDate::parse_from_str(&date_text, "%Y-%m-%d")
|
||||
.unwrap_or_else(|_| chrono::Local::now().date_naive());
|
||||
|
||||
let note_text = note_row_ref.text();
|
||||
let note = if note_text.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(note_text.to_string())
|
||||
};
|
||||
|
||||
let new_txn = NewTransaction {
|
||||
amount,
|
||||
transaction_type: txn_type,
|
||||
category_id,
|
||||
currency: currency_item,
|
||||
exchange_rate: 1.0,
|
||||
note,
|
||||
date,
|
||||
recurring_id: None,
|
||||
};
|
||||
|
||||
match db_ref.insert_transaction(&new_txn) {
|
||||
Ok(_) => {
|
||||
let msg = match txn_type {
|
||||
TransactionType::Expense => "Expense saved",
|
||||
TransactionType::Income => "Income saved",
|
||||
};
|
||||
let toast = adw::Toast::new(msg);
|
||||
toast_overlay_ref.add_toast(toast);
|
||||
|
||||
// Clear form
|
||||
amount_row_ref.set_text("");
|
||||
note_row_ref.set_text("");
|
||||
|
||||
// Refresh recent
|
||||
Self::refresh_recent(&db_ref, &recent_group_ref);
|
||||
}
|
||||
Err(e) => {
|
||||
let toast = adw::Toast::new(&format!("Error saving: {}", e));
|
||||
toast_overlay_ref.add_toast(toast);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// -- Assemble --
|
||||
inner.append(&type_box);
|
||||
@@ -155,44 +257,104 @@ impl LogView {
|
||||
inner.append(&recent_group);
|
||||
|
||||
clamp.set_child(Some(&inner));
|
||||
container.append(&clamp);
|
||||
toast_overlay.set_child(Some(&clamp));
|
||||
container.append(&toast_overlay);
|
||||
|
||||
LogView {
|
||||
container,
|
||||
type_toggle: expense_btn,
|
||||
amount_row,
|
||||
currency_row,
|
||||
category_row,
|
||||
date_label,
|
||||
note_row,
|
||||
save_button,
|
||||
recent_group,
|
||||
toast_overlay,
|
||||
}
|
||||
}
|
||||
|
||||
fn populate_categories(model: >k::StringList, is_expense: bool) {
|
||||
// Remove existing items
|
||||
fn populate_categories_from_db(
|
||||
db: &Database,
|
||||
model: >k::StringList,
|
||||
ids: &Rc<RefCell<Vec<i64>>>,
|
||||
txn_type: TransactionType,
|
||||
) {
|
||||
while model.n_items() > 0 {
|
||||
model.remove(0);
|
||||
}
|
||||
let mut id_list = ids.borrow_mut();
|
||||
id_list.clear();
|
||||
|
||||
if is_expense {
|
||||
let cats = [
|
||||
"Food & Dining", "Groceries", "Transport", "Housing",
|
||||
"Utilities", "Healthcare", "Entertainment", "Shopping",
|
||||
"Education", "Personal Care", "Travel", "Insurance",
|
||||
"Gifts & Donations", "Other Expense",
|
||||
];
|
||||
for c in cats {
|
||||
model.append(c);
|
||||
if let Ok(cats) = db.list_categories(Some(txn_type)) {
|
||||
for cat in cats {
|
||||
let display = match &cat.icon {
|
||||
Some(icon) => format!("{} {}", icon, cat.name),
|
||||
None => cat.name.clone(),
|
||||
};
|
||||
model.append(&display);
|
||||
id_list.push(cat.id);
|
||||
}
|
||||
} else {
|
||||
let cats = [
|
||||
"Salary", "Freelance", "Investments", "Gifts Received",
|
||||
"Refunds", "Other Income",
|
||||
];
|
||||
for c in cats {
|
||||
model.append(c);
|
||||
}
|
||||
}
|
||||
|
||||
fn refresh_recent(db: &Database, group: &adw::PreferencesGroup) {
|
||||
// Remove all existing children by rebuilding
|
||||
// PreferencesGroup doesn't have a clear method, so we remove then re-add
|
||||
// We track children via a helper: iterate and remove
|
||||
while let Some(child) = group.first_child() {
|
||||
// The group has an internal listbox; we need to find ActionRows
|
||||
// Actually, PreferencesGroup.remove() takes a widget ref
|
||||
if let Some(row) = child.downcast_ref::<adw::ActionRow>() {
|
||||
group.remove(row);
|
||||
} else if let Some(row) = child.downcast_ref::<gtk::ListBoxRow>() {
|
||||
// Try generic removal via parent
|
||||
if let Some(parent) = row.parent() {
|
||||
if let Some(listbox) = parent.downcast_ref::<gtk::ListBox>() {
|
||||
listbox.remove(row);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
match db.list_recent_transactions(5) {
|
||||
Ok(txns) if !txns.is_empty() => {
|
||||
for txn in &txns {
|
||||
let cat_name = db
|
||||
.get_category(txn.category_id)
|
||||
.map(|c| {
|
||||
match &c.icon {
|
||||
Some(icon) => format!("{} {}", icon, c.name),
|
||||
None => c.name,
|
||||
}
|
||||
})
|
||||
.unwrap_or_else(|_| "Unknown".to_string());
|
||||
|
||||
let amount_str = match txn.transaction_type {
|
||||
TransactionType::Expense => format!("-{:.2} {}", txn.amount, txn.currency),
|
||||
TransactionType::Income => format!("+{:.2} {}", txn.amount, txn.currency),
|
||||
};
|
||||
|
||||
let subtitle = match &txn.note {
|
||||
Some(n) if !n.is_empty() => format!("{} - {}", txn.date, n),
|
||||
_ => txn.date.to_string(),
|
||||
};
|
||||
|
||||
let row = adw::ActionRow::builder()
|
||||
.title(&cat_name)
|
||||
.subtitle(&subtitle)
|
||||
.build();
|
||||
|
||||
let amount_label = gtk::Label::new(Some(&amount_str));
|
||||
match txn.transaction_type {
|
||||
TransactionType::Expense => amount_label.add_css_class("error"),
|
||||
TransactionType::Income => amount_label.add_css_class("success"),
|
||||
}
|
||||
row.add_suffix(&amount_label);
|
||||
|
||||
group.add(&row);
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
let placeholder = adw::ActionRow::builder()
|
||||
.title("No transactions yet")
|
||||
.build();
|
||||
placeholder.add_css_class("dim-label");
|
||||
group.add(&placeholder);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user