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