Add buttons to interract with last month

This commit is contained in:
Quentin Boyer 2023-11-13 10:51:44 +01:00
parent 78647a59e5
commit 140e70616a

View file

@ -11,7 +11,7 @@ use iced::{
font, subscription, theme,
widget::{
button, column, component, horizontal_rule, horizontal_space, row, rule, scrollable, text,
text_input,
text_input, Row,
},
window, Alignment, Application, Command, Event, Length, Renderer, Settings, Theme,
};
@ -635,6 +635,7 @@ enum Message {
EditEarings2(f64),
SetDate(ReportDate),
Load(ReportDate),
InitFrom(ReportDate),
}
struct Glaurung {
@ -670,6 +671,24 @@ struct ReportDate {
pub month: u8,
}
impl std::ops::Add<u64> for ReportDate {
type Output = Self;
fn add(self, rhs: u64) -> Self::Output {
let mut new_month = self.month + (rhs % 12) as u8;
let mut new_year = self.year + rhs / 12;
if new_month > 12 {
new_month -= 12;
new_year += 1;
}
Self {
year: new_year,
month: new_month,
}
}
}
impl std::fmt::Display for ReportDate {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}-{}", self.year, self.month)
@ -754,6 +773,18 @@ impl Glaurung {
self.earnings_2 = report.earnings_2;
}
fn init_from(&mut self, report: Report) {
self.recurring = report.recurring;
self.savings = report.savings;
self.earnings_1 = 0.;
self.earnings_2 = 0.;
self.variable = report
.variable
.into_keys()
.map(|k| (k, Default::default()))
.collect();
}
fn report(&self) -> Report {
Report {
savings: self.savings.clone(),
@ -872,6 +903,11 @@ impl Application for Glaurung {
self.date = Some(d);
self.load(self.archive.get(&d).cloned().unwrap_or_default());
}
Message::InitFrom(d) => {
if let Some(report) = self.archive.get(&d) {
self.init_from(report.clone())
}
}
}
Command::none()
@ -986,6 +1022,35 @@ impl Application for Glaurung {
None => "No month set".to_string(),
};
let mut save_buttons = Row::new();
if let Some(&last) = self.archive.keys().next_back() {
let next_month = last + 1;
save_buttons = save_buttons.push(
button(text(&format!("Archive as {next_month}")))
.on_press(Message::SetDate(next_month)),
);
}
save_buttons = save_buttons
.push(component(DatePicker {
label: "Archive",
confirm: "Archive",
title: "Set report date",
on_pick: Message::SetDate,
}))
.push(component(DatePicker {
label: "Load",
confirm: "Load",
title: "Load archived report",
on_pick: Message::Load,
}));
if let Some(&last) = self.archive.keys().next_back() {
save_buttons = save_buttons
.push(button(text("Init from last month")).on_press(Message::InitFrom(last)));
}
scrollable(
column![
text(date).size(TEXT_H1),
@ -1039,21 +1104,7 @@ impl Application for Glaurung {
on_add: Message::AddSaving,
}),
per_person,
row![
component(DatePicker {
label: "Archive",
confirm: "Archive",
title: "Set report date",
on_pick: Message::SetDate
}),
component(DatePicker {
label: "Load",
confirm: "Load",
title: "Load archived report",
on_pick: Message::Load
})
]
.spacing(5)
save_buttons.spacing(5),
]
.align_items(Alignment::Center)
.padding(15),