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