Add a total section

This commit is contained in:
traxys 2023-12-08 21:31:18 +01:00
parent 4cfe91039c
commit 8417c4a8db
2 changed files with 30 additions and 12 deletions

View file

@ -11,12 +11,14 @@ use iced::{
use iced_aw::{card, modal, selection_list}; use iced_aw::{card, modal, selection_list};
use itertools::Itertools; use itertools::Itertools;
use crate::{CompareLoad, CompareSide, ReportDate, Spendings, TEXT_EMPH2, TEXT_H1, TEXT_H2}; use crate::{
CompareLoad, CompareSide, Report, ReportDate, Spendings, TEXT_EMPH2, TEXT_H1, TEXT_H2,
};
pub(crate) struct Compare<F> { pub(crate) struct Compare<'a, F> {
pub left: Option<(CompareLoad, Box<dyn Spendings>)>, pub left: Option<(CompareLoad, Box<dyn Spendings>)>,
pub right: Option<(CompareLoad, Box<dyn Spendings>)>, pub right: Option<(CompareLoad, Box<dyn Spendings>)>,
pub reports: Vec<ReportDate>, pub archive: &'a BTreeMap<ReportDate, Report>,
pub on_load: F, pub on_load: F,
} }
@ -49,7 +51,7 @@ impl Section {
} }
} }
impl<M, F> iced::widget::Component<M, Renderer> for Compare<F> impl<'a, M, F> iced::widget::Component<M, Renderer> for Compare<'a, F>
where where
F: FnMut(CompareSide, CompareLoad) -> M, F: FnMut(CompareSide, CompareLoad) -> M,
{ {
@ -78,7 +80,7 @@ where
let heading_load = |on_submit: fn(_) -> _| { let heading_load = |on_submit: fn(_) -> _| {
component(CompareLoadChoice { component(CompareLoadChoice {
on_submit, on_submit,
choices: &self.reports, choices: self.archive.keys().copied().collect(),
}) })
}; };
@ -154,8 +156,8 @@ where
] ]
}; };
let left = self.left.as_ref().map(|(_, r)| r); let left = self.left.as_ref().map(|(_, r)| &**r);
let right = self.right.as_ref().map(|(_, r)| r); let right = self.right.as_ref().map(|(_, r)| &**r);
fn item_compare<'a, I, M>( fn item_compare<'a, I, M>(
collapse: bool, collapse: bool,
@ -284,6 +286,16 @@ where
compare compare
} }
fn mk_total<'a>(
archive: &BTreeMap<ReportDate, Report>,
side: Option<&'a dyn Spendings>,
) -> Option<impl Iterator<Item = (&'a str, f64)>> {
let side = side?;
let (main_account, _) = side.main_account(archive);
Some(std::iter::once(("Main", main_account)).chain(side.savings()))
}
scrollable(table::<_, _, iced::Element<_>, _>( scrollable(table::<_, _, iced::Element<_>, _>(
properties, properties,
itertools::chain![ itertools::chain![
@ -300,6 +312,12 @@ where
left.map(|r| r.variable()), left.map(|r| r.variable()),
right.map(|r| r.variable()) right.map(|r| r.variable())
), ),
iter::once(mk_section(Section::Total)),
item_compare(
state.collapsed[Section::Variable],
mk_total(self.archive, left),
mk_total(self.archive, right),
),
], ],
)) ))
.into() .into()
@ -361,8 +379,8 @@ where
) )
} }
struct CompareLoadChoice<'a, F> { struct CompareLoadChoice<F> {
choices: &'a [ReportDate], choices: Vec<ReportDate>,
on_submit: F, on_submit: F,
} }
@ -380,7 +398,7 @@ enum CompareLoadChoiceMsg {
Select(CompareLoad), Select(CompareLoad),
} }
impl<'a, M, F> iced::widget::Component<M, Renderer> for CompareLoadChoice<'a, F> impl<M, F> iced::widget::Component<M, Renderer> for CompareLoadChoice<F>
where where
F: FnMut(CompareLoad) -> M, F: FnMut(CompareLoad) -> M,
{ {
@ -414,7 +432,7 @@ where
.on_press(CompareLoadChoiceMsg::Select(CompareLoad::Current)), .on_press(CompareLoadChoiceMsg::Select(CompareLoad::Current)),
horizontal_rule(5.), horizontal_rule(5.),
text("Historic Reports"), text("Historic Reports"),
selection_list(self.choices, |_, v| CompareLoadChoiceMsg::Select( selection_list(&self.choices, |_, v| CompareLoadChoiceMsg::Select(
CompareLoad::Report(v) CompareLoad::Report(v)
)) ))
.height(Length::Shrink), .height(Length::Shrink),

View file

@ -518,7 +518,7 @@ impl Application for Glaurung {
CurrentView::Compare => component(Compare { CurrentView::Compare => component(Compare {
left: self.compare_load(self.compare_left), left: self.compare_load(self.compare_left),
right: self.compare_load(self.compare_right), right: self.compare_load(self.compare_right),
reports: self.archive.keys().copied().collect(), archive: &self.archive,
on_load: Message::CompareLoad, on_load: Message::CompareLoad,
}), }),
}; };