From 3d8778962b4a380efd098616ac49b6dedd2b8fbe Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Mon, 12 Aug 2019 03:21:22 -0400 Subject: add a stub submit handler --- src/main.rs | 17 +---------- src/views.rs | 2 +- src/views/txn_table.rs | 77 +++++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 66 insertions(+), 30 deletions(-) diff --git a/src/main.rs b/src/main.rs index 358a842..7d19af4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,22 +21,7 @@ fn main() { budget.id() ))); - let inflows_table = views::inflows_table(&budget); - layout.add_child(views::vi_view( - cursive::views::CircularFocus::wrap_arrows( - cursive::views::BoxView::with_min_height( - std::cmp::min(std::cmp::max(inflows_table.len(), 1), 5) + 2, - cursive::views::BoxView::with_full_width(inflows_table), - ), - ), - )); - - let outflows_table = views::outflows_table(&budget); - layout.add_child(views::vi_view( - cursive::views::CircularFocus::wrap_arrows( - cursive::views::BoxView::with_full_screen(outflows_table), - ), - )); + layout.add_child(views::txn_tables(&budget)); app.add_fullscreen_layer(layout); app.run(); diff --git a/src/views.rs b/src/views.rs index b6846b8..7c00834 100644 --- a/src/views.rs +++ b/src/views.rs @@ -1,5 +1,5 @@ mod txn_table; -pub use txn_table::{inflows_table, outflows_table}; +pub use txn_table::txn_tables; pub fn vi_view(v: V) -> impl cursive::view::View { cursive::views::OnEventView::new(v) diff --git a/src/views/txn_table.rs b/src/views/txn_table.rs index 0ad73da..ef72ac7 100644 --- a/src/views/txn_table.rs +++ b/src/views/txn_table.rs @@ -1,7 +1,7 @@ use cursive::view::{Identifiable, ViewWrapper}; #[derive(Clone, Copy, Eq, Hash, PartialEq)] -pub enum TxnColumn { +enum TxnColumn { Selected, Date, Payee, @@ -10,7 +10,7 @@ pub enum TxnColumn { type TxnTableView = cursive_table_view::TableView; -pub struct TableView { +struct TableView { view: cursive::views::OnEventView>, } @@ -58,7 +58,7 @@ impl cursive_table_view::TableViewItem } } -pub fn inflows_table(budget: &crate::ynab::Budget) -> TableView { +fn inflows_table(budget: &crate::ynab::Budget) -> TableView { let inflows = budget .reimbursables() .iter() @@ -68,7 +68,7 @@ pub fn inflows_table(budget: &crate::ynab::Budget) -> TableView { txn_table(inflows, "inflows_table") } -pub fn outflows_table(budget: &crate::ynab::Budget) -> TableView { +fn outflows_table(budget: &crate::ynab::Budget) -> TableView { let outflows = budget .reimbursables() .iter() @@ -90,19 +90,70 @@ fn txn_table( c.align(cursive::align::HAlign::Right).width(10) }) .default_column(TxnColumn::Date) + .on_submit(move |s, _, _| { + let total_outflow = s + .call_on_id("outflows_table", |v: &mut TxnTableView| -> i64 { + v.borrow_items() + .iter() + .filter(|t| t.selected) + .map(|t| t.amount) + .sum() + }) + .unwrap(); + let total_inflow = s + .call_on_id("inflows_table", |v: &mut TxnTableView| -> i64 { + v.borrow_items() + .iter() + .filter(|t| t.selected) + .map(|t| t.amount) + .sum() + }) + .unwrap(); + let total_amount = total_outflow + total_inflow; + if total_amount == 0 { + s.add_layer(cursive::views::Dialog::info( + "success, sum is zero!", + )) + } else { + s.add_layer(cursive::views::Dialog::info(format!( + "failed, sum is {}", + total_amount + ))) + } + }) .with_id(id); table.get_mut().set_items(txns); let view = cursive::views::OnEventView::new(table).on_event(' ', move |s| { - s.call_on( - &cursive::view::Selector::Id(id), - |v: &mut TxnTableView| { - if let Some(idx) = v.item() { - let txn = v.borrow_item_mut(idx).unwrap(); - txn.selected = !txn.selected; - } - }, - ); + s.call_on_id(&id, |v: &mut TxnTableView| { + if let Some(idx) = v.item() { + let txn = v.borrow_item_mut(idx).unwrap(); + txn.selected = !txn.selected; + } + }); }); TableView { view } } + +pub fn txn_tables(budget: &crate::ynab::Budget) -> impl cursive::view::View { + let mut layout = cursive::views::LinearLayout::vertical(); + + let inflows_table = inflows_table(&budget); + layout.add_child(crate::views::vi_view( + cursive::views::CircularFocus::wrap_arrows( + cursive::views::BoxView::with_min_height( + std::cmp::min(std::cmp::max(inflows_table.len(), 1), 5) + 2, + cursive::views::BoxView::with_full_width(inflows_table), + ), + ), + )); + + let outflows_table = outflows_table(&budget); + layout.add_child(crate::views::vi_view( + cursive::views::CircularFocus::wrap_arrows( + cursive::views::BoxView::with_full_screen(outflows_table), + ), + )); + + layout +} -- cgit v1.2.3