From d7e212987f02927571236178586bea02a20cfcd4 Mon Sep 17 00:00:00 2001 From: Matt Armstrong Date: Mon, 2 Sep 2024 20:45:31 -0700 Subject: [PATCH] Implement setting the due date. --- Cargo.lock | 13 +++++++++++++ sift-iced/Cargo.toml | 8 +++++++- sift-iced/src/app.rs | 39 ++++++++++++++++++++++++++++++++++++++- sift-iced/src/main.rs | 4 +++- sift-persist/src/task.rs | 4 ++++ 5 files changed, 65 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a536267..27849ad 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2630,6 +2630,17 @@ dependencies = [ "thiserror", ] +[[package]] +name = "iced_aw" +version = "0.9.4" +source = "git+https://github.com/iced-rs/iced_aw#5482588a8eb4dfdbf1fb8af81d5ab278e8f34915" +dependencies = [ + "cfg-if", + "chrono", + "iced", + "once_cell", +] + [[package]] name = "iced_core" version = "0.13.0-dev" @@ -4792,7 +4803,9 @@ name = "sift-iced" version = "0.1.0" dependencies = [ "anyhow", + "chrono", "iced", + "iced_aw", "sift-core", "sift-persist", "sift-state", diff --git a/sift-iced/Cargo.toml b/sift-iced/Cargo.toml index f505494..90b2bc4 100644 --- a/sift-iced/Cargo.toml +++ b/sift-iced/Cargo.toml @@ -6,11 +6,17 @@ edition = "2021" [dependencies] sift-core = { path = "../sift-core" } sift-persist = { path = "../sift-persist" } -sift-state= { path = "../sift-state" } +sift-state = { path = "../sift-state" } anyhow = "1.0.86" +chrono = "0.4.38" [dependencies.iced] git = "https://github.com/iced-rs/iced.git" +[dependencies.iced_aw] +git = "https://github.com/iced-rs/iced_aw" +default-features = false +features = ["date_picker", "icons"] + [lints] workspace = true diff --git a/sift-iced/src/app.rs b/sift-iced/src/app.rs index f63da00..6df6225 100644 --- a/sift-iced/src/app.rs +++ b/sift-iced/src/app.rs @@ -1,11 +1,13 @@ use std::sync::LazyLock; +use chrono::NaiveDate; use iced::widget::{ button, center, checkbox, column, container, keyed_column, row, scrollable, text, text_input, }; use iced::Alignment::Center; use iced::Element; use iced::Length::Fill; +use iced_aw::date_picker; use sift_core::save_name; use sift_persist::{MemoryStore, Store as _, Task, TaskId}; use sift_state::State; @@ -27,6 +29,9 @@ pub enum LoadedMessage { CreateTaskInputChanged(String), CreateTask, Delete(TaskId), + EditDueDate(TaskId), + EditDueSubmit(date_picker::Date), + EditDueCancel, } impl App { @@ -47,6 +52,7 @@ impl App { Ok(store) => { self.loaded = Some(LoadedApp { create_task_name: String::new(), + editing_due_date: None, state: State::new(store), }) } @@ -72,6 +78,7 @@ impl App { pub struct LoadedApp { create_task_name: String, + editing_due_date: Option, state: State, } @@ -101,9 +108,23 @@ impl LoadedApp { let id = task.id(); let checkbox = checkbox(task.title().to_string(), task.completed().is_some()) .on_toggle(move |complete| LoadedMessage::CompleteToggled(id, complete)); + + let picker = { + let editing = self.editing_due_date == Some(id); + let button = + button("Due").on_press_with(move || LoadedMessage::EditDueDate(id)); + date_picker( + editing, + date_picker::Date::default(), + button, + LoadedMessage::EditDueCancel, + LoadedMessage::EditDueSubmit, + ) + }; + let delete = button("Delete").on_press_with(move || LoadedMessage::Delete(id)); - let row = row![checkbox, delete]; + let row = row![checkbox, picker, delete]; (task.id(), row.into()) })) .into() @@ -146,6 +167,22 @@ impl LoadedApp { LoadedMessage::Delete(id) => { self.state.delete_task(&id); } + LoadedMessage::EditDueDate(id) => { + self.editing_due_date = Some(id); + } + LoadedMessage::EditDueSubmit(date) => { + if let Some(id) = self.editing_due_date { + let date: NaiveDate = date.into(); + let mut task = self.state.store.get_task(&id).expect("FIXME: handle error"); + task.set_due(Some(date)); + self.state + .store + .with_transaction(|txn| txn.put_task(&task)) + .expect("FIXME: handle error"); + } + self.editing_due_date = None; + } + LoadedMessage::EditDueCancel => self.editing_due_date = None, } } diff --git a/sift-iced/src/main.rs b/sift-iced/src/main.rs index 87c2be3..575c403 100644 --- a/sift-iced/src/main.rs +++ b/sift-iced/src/main.rs @@ -1,5 +1,7 @@ use sift_iced::App; pub fn main() -> iced::Result { - iced::application("Sift", App::update, App::view).run_with(App::new) + iced::application("Sift", App::update, App::view) + .font(iced_aw::BOOTSTRAP_FONT_BYTES) + .run_with(App::new) } diff --git a/sift-persist/src/task.rs b/sift-persist/src/task.rs index 449b941..1f294b1 100644 --- a/sift-persist/src/task.rs +++ b/sift-persist/src/task.rs @@ -104,4 +104,8 @@ impl Task { pub fn set_completed(&mut self, completed: Option>) { self.completed = completed; } + + pub fn set_due(&mut self, date: Option) { + self.due = date; + } }