Implement editing of recurring costs

This commit is contained in:
Quentin Boyer 2023-11-12 12:21:07 +01:00
parent 0dca88b905
commit 0d29a198b8

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, horizontal_space}, widget::{button, column, component, horizontal_rule, horizontal_space, row, text, text_input},
window, Application, Command, Event, Renderer, Settings, Theme, Length, window, Application, Command, Event, Length, Renderer, Settings, Theme,
}; };
use iced_aw::{card, modal}; use iced_aw::{card, modal};
use itertools::Itertools; use itertools::Itertools;
@ -75,14 +75,14 @@ where
} }
struct EditRecurring<'a, F> { struct EditRecurring<'a, F> {
value: f64, value: String,
name: &'a str, name: &'a str,
on_submit: F, on_submit: F,
} }
#[derive(Default)] #[derive(Default)]
struct EditRecurringState { struct EditRecurringState {
edit: String, edit: Option<String>,
modal: bool, modal: bool,
} }
@ -94,6 +94,16 @@ enum EditRecurringEvent {
Submit, Submit,
} }
impl<'a, F> EditRecurring<'a, F> {
fn new(value: f64, name: &'a str, on_submit: F) -> Self {
Self {
value: value.to_string(),
name,
on_submit,
}
}
}
impl<M, F> iced::widget::Component<M, Renderer> for EditRecurring<'_, F> impl<M, F> iced::widget::Component<M, Renderer> for EditRecurring<'_, F>
where where
F: FnMut(f64) -> M, F: FnMut(f64) -> M,
@ -103,20 +113,22 @@ where
fn update(&mut self, state: &mut Self::State, event: Self::Event) -> Option<M> { fn update(&mut self, state: &mut Self::State, event: Self::Event) -> Option<M> {
match event { match event {
EditRecurringEvent::Edit(_) => todo!(), EditRecurringEvent::Edit(v) => state.edit = Some(v),
EditRecurringEvent::Submit => { EditRecurringEvent::Submit => {
if let Ok(v) = state.edit.parse() { if let Some(e) = &state.edit {
state.edit.clear(); if let Ok(v) = e.parse() {
state.edit = None;
state.modal = false; state.modal = false;
return Some((self.on_submit)(v)); return Some((self.on_submit)(v));
} }
} }
}
EditRecurringEvent::Open => { EditRecurringEvent::Open => {
state.modal = true; state.modal = true;
} }
EditRecurringEvent::Close => { EditRecurringEvent::Close => {
state.modal = false; state.modal = false;
state.edit.clear(); state.edit = None;
} }
} }
@ -127,7 +139,12 @@ where
let underlay = button(text("Edit")).on_press(EditRecurringEvent::Open); let underlay = button(text("Edit")).on_press(EditRecurringEvent::Open);
let overlay = match state.modal { let overlay = match state.modal {
true => Some( true => Some(
card(text(&format!("Edit {}", self.name)), text("todo")) card(
text(&format!("Edit {}", self.name)),
text_input("new value", state.edit.as_ref().unwrap_or(&self.value))
.on_input(EditRecurringEvent::Edit)
.on_submit(EditRecurringEvent::Submit),
)
.max_width(300.0) .max_width(300.0)
.on_close(EditRecurringEvent::Close), .on_close(EditRecurringEvent::Close),
), ),
@ -192,11 +209,11 @@ where
text(name).size(17), text(name).size(17),
text(&format!("{value}")), text(&format!("{value}")),
horizontal_space(Length::Fill), horizontal_space(Length::Fill),
component(EditRecurring { component(EditRecurring::new(
value, value,
name, name,
on_submit: |v| RecurringMessage::DoAdd(name.to_string(), v) |v| RecurringMessage::DoAdd(name.to_string(), v)
}) ))
] ]
.spacing(5) .spacing(5)
.align_items(iced::Alignment::Center) .align_items(iced::Alignment::Center)