Implement editing of recurring costs
This commit is contained in:
parent
0dca88b905
commit
0d29a198b8
1 changed files with 33 additions and 16 deletions
49
src/main.rs
49
src/main.rs
|
|
@ -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,12 +113,14 @@ 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.modal = false;
|
state.edit = None;
|
||||||
return Some((self.on_submit)(v));
|
state.modal = false;
|
||||||
|
return Some((self.on_submit)(v));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
EditRecurringEvent::Open => {
|
EditRecurringEvent::Open => {
|
||||||
|
|
@ -116,7 +128,7 @@ where
|
||||||
}
|
}
|
||||||
EditRecurringEvent::Close => {
|
EditRecurringEvent::Close => {
|
||||||
state.modal = false;
|
state.modal = false;
|
||||||
state.edit.clear();
|
state.edit = None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -127,9 +139,14 @@ 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(
|
||||||
.max_width(300.0)
|
text(&format!("Edit {}", self.name)),
|
||||||
.on_close(EditRecurringEvent::Close),
|
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,
|
false => None,
|
||||||
};
|
};
|
||||||
|
|
@ -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)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue