1
//! The Accounts tab's Balance column, filled from one `balance-report` call.
2

            
3
use std::collections::HashMap;
4

            
5
use cli_core::render::{WireValue, parse_wire};
6
use cli_core::reports::value_to_balance_rows;
7

            
8
use super::App;
9
use crate::route::RouteCtx;
10
use crate::tabs::fetch::Fetch;
11
use crate::tabs::list::{Balances, ListTab};
12
use crate::tabs::reports::table::amount_text;
13
use crate::view::ViewId;
14
use crate::window::WindowId;
15

            
16
impl App {
17
    /// Refetches the balances of every loaded Accounts window after a transaction changed them.
18
21
    pub(super) fn refetch_account_balances(&mut self) {
19
21
        for window in self.window_ids(ViewId::Accounts) {
20
23
            let loaded = self
21
23
                .win::<ListTab>(window)
22
23
                .is_some_and(|list| matches!(list.state, Fetch::Loaded(_)));
23
23
            if loaded {
24
3
                self.fetch_account_balances(window);
25
20
            }
26
        }
27
21
    }
28

            
29
44
    pub(super) fn fetch_account_balances(&mut self, window: WindowId) {
30
44
        self.set_account_balances(window, Balances::Loading);
31
44
        let Some(seq) = self
32
44
            .win_mut::<ListTab>(window)
33
44
            .map(|list| list.balances_seq.advance())
34
        else {
35
            return;
36
        };
37
44
        let ctx = RouteCtx::AccountBalances { seq };
38
44
        if self
39
44
            .dispatch_eval(window, ctx, "(balance-report)".to_string())
40
44
            .is_none()
41
        {
42
8
            self.fail_account_balances(window, crate::tabs::nms_eval::UNAVAILABLE);
43
36
        }
44
44
    }
45

            
46
78
    fn set_account_balances(&mut self, window: WindowId, state: Balances) {
47
78
        if let Some(list) = self.win_mut::<ListTab>(window) {
48
78
            list.set_balances(state);
49
        }
50
78
    }
51

            
52
10
    pub(super) fn fail_account_balances(&mut self, window: WindowId, reason: &str) {
53
10
        self.set_account_balances(window, Balances::Failed);
54
10
        self.status = format!("balances failed: {reason}");
55
10
    }
56

            
57
28
    pub(super) fn deliver_account_balances(&mut self, window: WindowId, seq: u64, wire: &str) {
58
28
        let awaited = self
59
28
            .win::<ListTab>(window)
60
28
            .is_some_and(|list| list.balances_seq.awaits(seq));
61
28
        if !awaited {
62
2
            return;
63
26
        }
64
26
        match balance_cells(wire) {
65
24
            Ok(cells) => self.set_account_balances(window, Balances::Ready(cells)),
66
2
            Err(reason) => self.fail_account_balances(window, &reason),
67
        }
68
28
    }
69
}
70

            
71
/// Own-balance amount texts, zero amounts dropped, by account id; accounts without any are absent.
72
26
fn balance_cells(wire: &str) -> Result<HashMap<String, Vec<String>>, String> {
73
26
    let WireValue::Value(value) = parse_wire(wire).map_err(|err| err.to_string())?;
74
25
    let rows = value_to_balance_rows(&value).map_err(|err| err.to_string())?;
75
24
    Ok(rows
76
24
        .iter()
77
45
        .map(|row| {
78
45
            let texts = row
79
45
                .own
80
45
                .iter()
81
41
                .filter(|a| *a.amount.numer() != 0)
82
45
                .map(amount_text)
83
                .collect();
84
45
            (row.account_id.to_string(), texts)
85
45
        })
86
        .collect())
87
26
}