Skip to content

Commit

Permalink
Implement setting the due date.
Browse files Browse the repository at this point in the history
  • Loading branch information
matta committed Sep 3, 2024
1 parent 68c0f3a commit d7e2129
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 3 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion sift-iced/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
39 changes: 38 additions & 1 deletion sift-iced/src/app.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -27,6 +29,9 @@ pub enum LoadedMessage {
CreateTaskInputChanged(String),
CreateTask,
Delete(TaskId),
EditDueDate(TaskId),
EditDueSubmit(date_picker::Date),
EditDueCancel,
}

impl App {
Expand All @@ -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),
})
}
Expand All @@ -72,6 +78,7 @@ impl App {

pub struct LoadedApp {
create_task_name: String,
editing_due_date: Option<TaskId>,
state: State,
}

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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,
}
}

Expand Down
4 changes: 3 additions & 1 deletion sift-iced/src/main.rs
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 4 additions & 0 deletions sift-persist/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,8 @@ impl Task {
pub fn set_completed(&mut self, completed: Option<DateTime<Utc>>) {
self.completed = completed;
}

pub fn set_due(&mut self, date: Option<NaiveDate>) {
self.due = date;
}
}

0 comments on commit d7e2129

Please sign in to comment.