diff --git a/src/main.rs b/src/main.rs index 2e688c4..f4a2822 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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, 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 iced::widget::Component 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 { 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)