aboutsummaryrefslogtreecommitdiffstats
path: root/src/views/txn_tables.rs
blob: f2940396e6a28950e910dc1305e3da0f131f8fad (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
use cursive::view::{Identifiable, View};

const SELECTED_TOTAL_ID: &str = "selected_total";
const INFLOWS_TABLE_ID: &str = "inflows_table";
const OUTFLOWS_TABLE_ID: &str = "outflows_table";

pub struct TxnTables {
    view: super::util::FullView<cursive::views::LinearLayout>,
}

impl cursive::view::ViewWrapper for TxnTables {
    cursive::wrap_impl!(
        self.view: super::util::FullView<cursive::views::LinearLayout>
    );
}

impl TxnTables {
    pub fn new(id: &'static str, budget: &crate::ynab::Budget) -> Self {
        let mut layout = cursive::views::LinearLayout::vertical();

        layout.add_child(
            cursive::views::TextView::new("Selected: $0.00 (0 transactions)")
                .h_align(cursive::align::HAlign::Right)
                .with_id(SELECTED_TOTAL_ID),
        );

        let inflows = budget
            .reimbursables()
            .iter()
            .filter(|t| !t.reimbursed && t.amount > 0)
            .cloned()
            .collect();
        let mut inflows_table =
            super::txn_table::TxnTable::new(inflows, INFLOWS_TABLE_ID);
        layout.add_child(cursive::views::TextView::new(format!(
            "Inflows: {} ({} transaction{}",
            crate::ynab::format_amount(inflows_table.amount()),
            inflows_table.len(),
            if inflows_table.len() == 1 { ") " } else { "s)" }
        )));
        layout.add_child(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),
            ),
        ));

        layout.add_child(cursive::views::TextView::new(" "));

        let outflows = budget
            .reimbursables()
            .iter()
            .filter(|t| !t.reimbursed && t.amount <= 0)
            .cloned()
            .collect();
        let mut outflows_table =
            super::txn_table::TxnTable::new(outflows, OUTFLOWS_TABLE_ID);
        layout.add_child(cursive::views::TextView::new(format!(
            "Outflows: {} ({} transaction{}",
            crate::ynab::format_amount(outflows_table.amount()),
            outflows_table.len(),
            if outflows_table.len() == 1 {
                ") "
            } else {
                "s)"
            }
        )));
        layout.add_child(cursive::views::CircularFocus::wrap_arrows(
            cursive::views::BoxView::with_full_screen(outflows_table),
        ));

        let event_view = cursive::views::OnEventView::new(layout)
            .on_event(cursive::event::Key::F0, move |s| {
                submit(s);
            })
            .on_event('r', move |s| {
                refresh(s);
            })
            .on_pre_event_inner(' ', |v, _| select(v))
            .with_id(id);

        TxnTables { view: event_view }
    }
}

fn submit(s: &mut cursive::Cursive) {
    let inflows: Vec<_> = s
        .call_on_id(
            INFLOWS_TABLE_ID,
            |v: &mut cursive::views::OnEventView<
                super::txn_table::TxnTableView,
            >| {
                v.get_inner_mut()
                    .borrow_items()
                    .iter()
                    .filter(|t| t.selected)
                    .cloned()
                    .collect()
            },
        )
        .unwrap();
    let outflows: Vec<_> = s
        .call_on_id(
            OUTFLOWS_TABLE_ID,
            |v: &mut cursive::views::OnEventView<
                super::txn_table::TxnTableView,
            >| {
                v.get_inner_mut()
                    .borrow_items()
                    .iter()
                    .filter(|t| t.selected)
                    .cloned()
                    .collect()
            },
        )
        .unwrap();
    let total_inflow: i64 = inflows.iter().map(|t| t.amount).sum();
    let total_outflow: i64 = outflows.iter().map(|t| t.amount).sum();
    let total_amount = total_inflow + total_outflow;
    if total_amount == 0 && (inflows.len() + outflows.len() > 0) {
        let budget: &mut crate::ynab::Budget = s.user_data().unwrap();
        let txns: Vec<_> = inflows.iter().chain(outflows.iter()).collect();
        let err = budget.reconcile_transactions(&txns);
        if let Some(err) = err {
            s.add_layer(super::util::dialog(&format!("Error: {}", err)))
        } else {
            s.add_layer(super::util::dialog(&format!(
                "Successfully updated {} transactions",
                txns.len()
            )));
            s.call_on_id(
                INFLOWS_TABLE_ID,
                |v: &mut cursive::views::OnEventView<
                    super::txn_table::TxnTableView,
                >| {
                    let v = v.get_inner_mut();
                    let all_txns = v.borrow_items_mut();
                    for id in txns.iter().map(|t| t.id.clone()) {
                        if let Some(idx) =
                            all_txns.iter().position(|t| t.id == id)
                        {
                            all_txns.remove(idx);
                        }
                    }
                    if let Some(row) = v.row() {
                        if row >= v.len() {
                            v.set_selected_row(v.len() - 1);
                        }
                    }
                },
            )
            .unwrap();
            s.call_on_id(
                OUTFLOWS_TABLE_ID,
                |v: &mut cursive::views::OnEventView<
                    super::txn_table::TxnTableView,
                >| {
                    let v = v.get_inner_mut();
                    let all_txns = v.borrow_items_mut();
                    for id in txns.iter().map(|t| t.id.clone()) {
                        if let Some(idx) =
                            all_txns.iter().position(|t| t.id == id)
                        {
                            all_txns.remove(idx);
                        }
                    }
                    if let Some(row) = v.row() {
                        if row >= v.len() {
                            v.set_selected_row(v.len() - 1);
                        }
                    }
                },
            )
            .unwrap();
        }
    } else if total_amount != 0 {
        s.add_layer(super::util::dialog(&format!(
            "Selected amount is {}, must be 0",
            crate::ynab::format_amount(total_amount)
        )))
    }
}

fn refresh(s: &mut cursive::Cursive) {
    let budget: &mut crate::ynab::Budget = s.user_data().unwrap();
    budget.refresh();

    let mut inflows: Vec<_> = budget
        .reimbursables()
        .iter()
        .filter(|t| !t.reimbursed && t.amount > 0)
        .cloned()
        .collect();
    s.call_on_id(
        INFLOWS_TABLE_ID,
        |v: &mut cursive::views::OnEventView<
            super::txn_table::TxnTableView,
        >| {
            let v = v.get_inner_mut();
            let selected: std::collections::HashSet<_> = v
                .borrow_items()
                .iter()
                .filter(|t| t.selected)
                .map(|t| t.id.clone())
                .collect();
            let row = v
                .item()
                .and_then(|idx| v.borrow_item(idx).map(|t| t.id.clone()));
            for mut t in inflows.iter_mut() {
                if selected.contains(&t.id) {
                    t.selected = true;
                }
            }
            let idx =
                row.and_then(|id| inflows.iter().position(|t| t.id == id));
            v.set_items(inflows);
            if let Some(idx) = idx {
                v.set_selected_item(idx);
            }
        },
    )
    .unwrap();

    let budget: &mut crate::ynab::Budget = s.user_data().unwrap();
    let mut outflows: Vec<_> = budget
        .reimbursables()
        .iter()
        .filter(|t| !t.reimbursed && t.amount <= 0)
        .cloned()
        .collect();
    s.call_on_id(
        OUTFLOWS_TABLE_ID,
        |v: &mut cursive::views::OnEventView<
            super::txn_table::TxnTableView,
        >| {
            let v = v.get_inner_mut();
            let selected: std::collections::HashSet<_> = v
                .borrow_items()
                .iter()
                .filter(|t| t.selected)
                .map(|t| t.id.clone())
                .collect();
            let row = v
                .item()
                .and_then(|idx| v.borrow_item(idx).map(|t| t.id.clone()));
            for mut t in outflows.iter_mut() {
                if selected.contains(&t.id) {
                    t.selected = true;
                }
            }
            let idx =
                row.and_then(|id| outflows.iter().position(|t| t.id == id));
            v.set_items(outflows);
            if let Some(idx) = idx {
                v.set_selected_item(idx);
            }
        },
    )
    .unwrap();

    render_selected_total(s);
}

fn select(
    v: &mut cursive::views::LinearLayout,
) -> Option<cursive::event::EventResult> {
    let idx = v.get_focus_index();
    let child = v.get_child_mut(idx).unwrap();
    child.call_on_any(
        &cursive::view::Selector::Id(INFLOWS_TABLE_ID),
        Box::new(|v| {
            v.downcast_mut::<cursive::views::IdView<
                cursive::views::OnEventView<super::txn_table::TxnTableView>,
            >>()
            .map(|v| v.on_event(cursive::event::Event::Char(' ')));
        }),
    );
    child.call_on_any(
        &cursive::view::Selector::Id(OUTFLOWS_TABLE_ID),
        Box::new(|v| {
            v.downcast_mut::<cursive::views::IdView<
                cursive::views::OnEventView<super::txn_table::TxnTableView>,
            >>()
            .map(|v| v.on_event(cursive::event::Event::Char(' ')));
        }),
    );
    Some(cursive::event::EventResult::with_cb(|s| {
        render_selected_total(s);
    }))
}

fn render_selected_total(s: &mut cursive::Cursive) {
    let inflows: Vec<_> = s
        .call_on_id(
            INFLOWS_TABLE_ID,
            |v: &mut cursive::views::OnEventView<
                super::txn_table::TxnTableView,
            >| {
                v.get_inner_mut()
                    .borrow_items()
                    .iter()
                    .filter(|t| t.selected)
                    .map(|t| t.amount)
                    .collect()
            },
        )
        .unwrap();
    let outflows: Vec<_> = s
        .call_on_id(
            OUTFLOWS_TABLE_ID,
            |v: &mut cursive::views::OnEventView<
                super::txn_table::TxnTableView,
            >| {
                v.get_inner_mut()
                    .borrow_items()
                    .iter()
                    .filter(|t| t.selected)
                    .map(|t| t.amount)
                    .collect()
            },
        )
        .unwrap();
    let outflow: i64 = outflows.iter().sum();
    let inflow: i64 = inflows.iter().sum();
    let amount = outflow + inflow;
    s.call_on_id(SELECTED_TOTAL_ID, |v: &mut cursive::views::TextView| {
        let mut sstr =
            cursive::utils::markup::StyledString::plain("Selected: ");
        let color = if amount == 0 && outflows.len() + inflows.len() != 0 {
            cursive::theme::Color::Dark(cursive::theme::BaseColor::Green)
        } else {
            cursive::theme::Color::TerminalDefault
        };
        sstr.append(cursive::utils::markup::StyledString::styled(
            crate::ynab::format_amount(amount),
            color,
        ));
        sstr.append(format!(
            " ({} transaction{}",
            outflows.len() + inflows.len(),
            if outflows.len() + inflows.len() == 1 {
                ") "
            } else {
                "s)"
            }
        ));
        v.set_content(sstr);
    });
}