Add outputs sections

This commit is contained in:
Quentin Boyer 2023-11-12 19:49:12 +01:00
parent fd57d4644f
commit 017137dc33

View file

@ -524,6 +524,7 @@ where
enum Message {
Event(Event),
AddRecurring(String, f64),
AddSaving(String, f64),
FontLoaded(Result<(), font::Error>),
AddVariable(String),
EditVariable(String, String),
@ -535,6 +536,7 @@ struct Glaurung {
config: Config,
recurring: BTreeMap<String, f64>,
variable: BTreeMap<String, (String, Option<f64>)>,
savings: BTreeMap<String, f64>,
save_file: PathBuf,
earnings_1: f64,
earnings_2: f64,
@ -545,6 +547,8 @@ struct SaveFile {
#[serde(default)]
recurring: BTreeMap<String, f64>,
#[serde(default)]
savings: BTreeMap<String, f64>,
#[serde(default)]
variable: HashMap<String, String>,
#[serde(default)]
earnings_1: f64,
@ -570,6 +574,7 @@ impl Application for Glaurung {
Self {
config: config.config,
recurring: config.save.recurring,
savings: config.save.savings,
variable: config
.save
.variable
@ -602,6 +607,9 @@ impl Application for Glaurung {
Message::AddRecurring(name, value) => {
self.recurring.insert(name, value);
}
Message::AddSaving(name, value) => {
self.savings.insert(name, value);
}
Message::FontLoaded(r) => r.expect("could not load font"),
Message::Event(ev) => {
if let Event::Window(window::Event::CloseRequested) = ev {
@ -615,6 +623,7 @@ impl Application for Glaurung {
serde_json::to_writer(
save_file,
&SaveFile {
savings: std::mem::take(&mut self.savings),
recurring: std::mem::take(&mut self.recurring),
variable: std::mem::take(&mut self.variable)
.into_iter()
@ -652,6 +661,12 @@ impl Application for Glaurung {
}
fn view(&self) -> Element {
let total_spendings = self
.recurring
.values()
.chain(self.variable.values().flat_map(|(_, f)| f))
.sum::<f64>();
column![
text("Spendings").size(TEXT_H1),
component(FixedAmounts {
@ -673,14 +688,7 @@ impl Application for Glaurung {
let def = rule::StyleSheet::appearance(theme, &theme::Rule::Default);
rule::Appearance { width: 3, ..def }
}),
text(&format!(
"Total spendings: {} €",
self.recurring
.values()
.chain(self.variable.values().flat_map(|(_, f)| f))
.sum::<f64>()
))
.size(TEXT_EMPH2),
text(&format!("Total spendings: {total_spendings}",)).size(TEXT_EMPH2),
text("Earnings").size(TEXT_H1),
row![
text(&self.config.person_1).size(TEXT_EMPH1),
@ -705,6 +713,14 @@ impl Application for Glaurung {
self.earnings_1 + self.earnings_2,
))
.size(TEXT_EMPH2),
text("Outputs").size(TEXT_H1),
text(&format!("Main account: {total_spendings}")).size(TEXT_EMPH2),
component(FixedAmounts {
items: &self.savings,
title: "Savings",
add_label: "Add a saving output",
on_add: Message::AddSaving,
}),
]
.max_width(500)
.padding(5)