Add a total field & pretty recurring a bit

This commit is contained in:
Quentin Boyer 2023-11-11 23:24:37 +01:00
parent 066bcb5555
commit 0dca88b905

View file

@ -9,8 +9,8 @@ use anyhow::anyhow;
use directories::ProjectDirs; use directories::ProjectDirs;
use iced::{ use iced::{
font, subscription, font, subscription,
widget::{button, column, component, horizontal_rule, row, text, text_input}, widget::{button, column, component, horizontal_rule, row, text, text_input, horizontal_space},
window, Application, Command, Event, Renderer, Settings, Theme, window, Application, Command, Event, Renderer, Settings, Theme, Length,
}; };
use iced_aw::{card, modal}; use iced_aw::{card, modal};
use itertools::Itertools; use itertools::Itertools;
@ -182,7 +182,7 @@ where
fn view(&self, state: &Self::State) -> iced_aw::Element<'_, Self::Event, Renderer> { fn view(&self, state: &Self::State) -> iced_aw::Element<'_, Self::Event, Renderer> {
let underlay = column![ let underlay = column![
text("Recurring").size(20), text("Recurring").size(25),
button(text("Add")).on_press(RecurringMessage::Add), button(text("Add")).on_press(RecurringMessage::Add),
horizontal_rule(5), horizontal_rule(5),
column( column(
@ -191,6 +191,7 @@ where
.map(|(name, &value)| row![ .map(|(name, &value)| row![
text(name).size(17), text(name).size(17),
text(&format!("{value}")), text(&format!("{value}")),
horizontal_space(Length::Fill),
component(EditRecurring { component(EditRecurring {
value, value,
name, name,
@ -203,7 +204,10 @@ where
.intersperse_with(|| horizontal_rule(5).into()) .intersperse_with(|| horizontal_rule(5).into())
.collect() .collect()
), ),
]; horizontal_rule(5),
text(&format!("Total: {}", self.items.values().sum::<f64>())).size(20)
]
.max_width(500.0);
let overlay = match state.add_recurring { let overlay = match state.add_recurring {
true => Some( true => Some(
@ -233,7 +237,7 @@ enum Message {
struct Glaurung { struct Glaurung {
recurring: BTreeMap<String, f64>, recurring: BTreeMap<String, f64>,
save_file: File, save_file: PathBuf,
} }
#[derive(Serialize, Deserialize, Default)] #[derive(Serialize, Deserialize, Default)]
@ -257,12 +261,7 @@ impl Application for Glaurung {
( (
Self { Self {
recurring: config.save.recurring, recurring: config.save.recurring,
save_file: OpenOptions::new() save_file: config.save_file,
.create(true)
.write(true)
.truncate(true)
.open(config.save_file)
.expect("Can't open data file"),
}, },
Command::batch(vec![ Command::batch(vec![
font::load(iced_aw::graphics::icons::ICON_FONT_BYTES).map(Message::FontLoaded) font::load(iced_aw::graphics::icons::ICON_FONT_BYTES).map(Message::FontLoaded)
@ -286,8 +285,14 @@ impl Application for Glaurung {
Message::FontLoaded(r) => r.expect("could not load font"), Message::FontLoaded(r) => r.expect("could not load font"),
Message::Event(ev) => { Message::Event(ev) => {
if let Event::Window(window::Event::CloseRequested) = ev { if let Event::Window(window::Event::CloseRequested) = ev {
let save_file = OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(&self.save_file)
.expect("Can't open data file");
serde_json::to_writer( serde_json::to_writer(
&mut self.save_file, save_file,
&SaveFile { &SaveFile {
recurring: std::mem::take(&mut self.recurring), recurring: std::mem::take(&mut self.recurring),
}, },