1use std::ops::Range;
4
5use cli_core::render::ListRow;
6use cli_core::transaction::{Page, submit_call};
7use forms::{FormValues, PAGING_KEYS, TRANSACTION_QUERY};
8use tvui::range_text;
9
10pub const PAGE_SIZE: u64 = 20;
11
12#[derive(Debug, Clone, Default, PartialEq, Eq)]
13pub struct Pager {
14 offset: u64,
15 total: Option<u64>,
16 has_more: bool,
17 filter: FormValues,
18 summary: String,
19}
20
21#[must_use]
23pub fn filter_of(values: &FormValues) -> FormValues {
24 let mut filter = FormValues::default();
25 TRANSACTION_QUERY
26 .filter_fields()
27 .filter_map(|field| Some((field.key, values.text(field.key)?)))
28 .filter(|(_, text)| !text.is_empty())
29 .for_each(|(key, text)| filter.set(key, text));
30 filter
31}
32
33#[must_use]
35pub fn filter_summary(account_label: &str, filter: &FormValues) -> String {
36 let (from, to) = (filter.text("from"), filter.text("to"));
37 let range = (from.is_some() || to.is_some())
38 .then(|| format!("{}\u{2013}{}", from.unwrap_or(""), to.unwrap_or("")));
39 let account = filter.text("account").map(|_| account_label.to_string());
40 [account, range]
41 .into_iter()
42 .flatten()
43 .collect::<Vec<_>>()
44 .join(" ")
45}
46
47fn query_call(filter: &FormValues, offset: u64) -> String {
48 let mut values = filter.clone();
49 let [limit_key, offset_key] = PAGING_KEYS;
50 values.set(limit_key, &PAGE_SIZE.to_string());
51 values.set(offset_key, &offset.to_string());
52 submit_call(&TRANSACTION_QUERY, &values)
53}
54
55impl Pager {
56 #[must_use]
58 pub fn request(&self) -> String {
59 query_call(&self.filter, self.offset)
60 }
61
62 #[must_use]
64 pub fn request_filtered(filter: &FormValues) -> String {
65 query_call(&filter_of(filter), 0)
66 }
67
68 pub fn set_filter(&mut self, filter: &FormValues, summary: String) {
70 *self = Self {
71 filter: filter_of(filter),
72 summary,
73 ..Self::default()
74 };
75 }
76
77 pub fn clear_filter(&mut self) {
79 *self = Self::default();
80 }
81
82 #[must_use]
83 pub fn filter(&self) -> &FormValues {
84 &self.filter
85 }
86
87 #[must_use]
88 pub fn summary(&self) -> &str {
89 &self.summary
90 }
91
92 pub fn advance(&mut self) -> bool {
94 let moved = self.has_more;
95 if moved {
96 self.offset = self.offset.saturating_add(PAGE_SIZE);
97 }
98 moved
99 }
100
101 pub fn retreat(&mut self) -> bool {
103 let moved = self.offset > 0;
104 self.offset = self.offset.saturating_sub(PAGE_SIZE);
105 moved
106 }
107
108 pub fn accept(&mut self, page: &Page) -> bool {
111 self.total = page.total;
112 self.has_more = page.has_more;
113 let overshot = page.rows.is_empty() && self.offset > 0;
114 if overshot {
115 let rows = page.total.unwrap_or(0);
116 self.offset = rows.saturating_sub(1) / PAGE_SIZE * PAGE_SIZE;
117 }
118 overshot
119 }
120
121 #[must_use]
123 pub fn extent(&self, loaded: usize) -> (usize, usize) {
124 let first = usize::try_from(self.offset).unwrap_or(usize::MAX);
125 let total = self.total.and_then(|total| usize::try_from(total).ok());
126 (total.unwrap_or(first.saturating_add(loaded)), first)
127 }
128
129 #[must_use]
131 pub fn range(&self, visible: Range<usize>) -> Option<String> {
132 let total = usize::try_from(self.total?).ok()?;
133 let first = usize::try_from(self.offset).ok()?;
134 range_text(
135 first.saturating_add(visible.start)..first.saturating_add(visible.end),
136 total,
137 )
138 }
139}
140
141#[must_use]
143pub fn rows(page: &Page) -> Vec<ListRow> {
144 page.rows
145 .iter()
146 .map(|row| ListRow {
147 id: Some(row.id.clone()),
148 cells: vec![row.date.clone(), row.note.clone(), row.amount.clone()],
149 })
150 .collect()
151}
152
153#[cfg(test)]
154mod filter_tests;
155#[cfg(test)]
156mod tests;