Skip to content

Commit

Permalink
improv: use Task instead of Command
Browse files Browse the repository at this point in the history
  • Loading branch information
edfloreshz committed Nov 4, 2024
1 parent 9017ad8 commit 46ea397
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 138 deletions.
153 changes: 78 additions & 75 deletions src/app.rs

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/app/markdown.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use tasks_core::models::list::List;
use tasks_core::models::status::Status;
use tasks_core::models::task::Task;
use tasks_core::models::List;
use tasks_core::models::Status;
use tasks_core::models::Task;

pub trait Markdown {
fn markdown(&self) -> String;
Expand Down
54 changes: 26 additions & 28 deletions src/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ use cosmic::iced::{Alignment, Length, Subscription};
use cosmic::iced_widget::row;
use cosmic::{theme, widget, Apply, Element};
use slotmap::{DefaultKey, SecondaryMap, SlotMap};
use tasks_core::models::list::List;
use tasks_core::models::status::Status;
use tasks_core::models::task::Task;
use tasks_core::models::{self, List, Status};

use crate::fl;

pub struct Content {
list: Option<List>,
tasks: SlotMap<DefaultKey, Task>,
tasks: SlotMap<DefaultKey, models::Task>,
editing: SecondaryMap<DefaultKey, bool>,
task_input_ids: SecondaryMap<DefaultKey, widget::Id>,
input: String,
Expand All @@ -24,24 +22,24 @@ pub enum Message {
Complete(DefaultKey, bool),
Delete(DefaultKey),
EditMode(DefaultKey, bool),
Export(Vec<Task>),
Export(Vec<models::Task>),
Input(String),
List(Option<List>),
Select(Task),
SetItems(Vec<Task>),
Select(models::Task),
SetItems(Vec<models::Task>),
TitleSubmit(DefaultKey),
TitleUpdate(DefaultKey, String),
UpdateTask(Task),
UpdateTask(models::Task),
}

pub enum Command {
pub enum Task {
Iced(cosmic::app::Task<super::app::Message>),
GetTasks(String),
DisplayTask(Task),
UpdateTask(Task),
DisplayTask(models::Task),
UpdateTask(models::Task),
Delete(String),
CreateTask(Task),
Export(Vec<Task>),
CreateTask(models::Task),
Export(Vec<models::Task>),
}

impl Content {
Expand Down Expand Up @@ -182,13 +180,13 @@ impl Content {
.into()
}

pub fn update(&mut self, message: Message) -> Vec<Command> {
let mut commands = Vec::new();
pub fn update(&mut self, message: Message) -> Vec<Task> {
let mut tasks = Vec::new();
match message {
Message::List(list) => {
self.list.clone_from(&list);
if let Some(list) = list {
commands.push(Command::GetTasks(list.id().clone()));
tasks.push(Task::GetTasks(list.id().clone()));
}
}
Message::TitleUpdate(id, title) => {
Expand All @@ -198,23 +196,23 @@ impl Content {
}
Message::TitleSubmit(id) => {
if let Some(task) = self.tasks.get(id) {
commands.push(Command::UpdateTask(task.clone()));
tasks.push(Task::UpdateTask(task.clone()));
self.editing.insert(id, false);
}
}
Message::Delete(id) => {
if let Some(task) = self.tasks.remove(id) {
commands.push(Command::Delete(task.id().clone()));
tasks.push(Task::Delete(task.id().clone()));
}
}
Message::EditMode(id, editing) => {
self.editing.insert(id, editing);
if editing {
commands.push(Command::Iced(widget::text_input::focus(
tasks.push(Task::Iced(widget::text_input::focus(
self.task_input_ids[id].clone(),
)));
} else if let Some(task) = self.tasks.get(id) {
commands.push(Command::UpdateTask(task.clone()));
tasks.push(Task::UpdateTask(task.clone()));
}
}
Message::SetItems(tasks) => {
Expand All @@ -225,7 +223,7 @@ impl Content {
}
}
Message::Select(task) => {
commands.push(Command::DisplayTask(task));
tasks.push(Task::DisplayTask(task));
}
Message::Complete(id, complete) => {
let task = self.tasks.get_mut(id);
Expand All @@ -235,15 +233,15 @@ impl Content {
} else {
Status::NotStarted
};
commands.push(Command::UpdateTask(task.clone()));
tasks.push(Task::UpdateTask(task.clone()));
}
}
Message::Input(input) => self.input = input,
Message::AddTask => {
if let Some(list) = &self.list {
if !self.input.is_empty() {
let task = Task::new(self.input.clone(), list.id().clone());
commands.push(Command::CreateTask(task.clone()));
let task = models::Task::new(self.input.clone(), list.id().clone());
tasks.push(Task::CreateTask(task.clone()));
let id = self.tasks.insert(task);
self.task_input_ids.insert(id, widget::Id::unique());
self.input.clear();
Expand All @@ -257,14 +255,14 @@ impl Content {
.find(|t| t.id() == updated_task.id());
if let Some(task) = task {
*task = updated_task.clone();
commands.push(Command::UpdateTask(task.clone()));
tasks.push(Task::UpdateTask(task.clone()));
}
}
Message::Export(tasks) => {
commands.push(Command::Export(tasks));
Message::Export(exported_tasks) => {
tasks.push(Task::Export(exported_tasks));
}
}
commands
tasks
}

pub fn view(&self) -> Element<Message> {
Expand Down
19 changes: 10 additions & 9 deletions src/core/src/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
pub mod task;

pub mod list;

pub mod priority;

pub mod status;

pub mod recurrence;
mod task;
pub use task::Task;
mod list;
pub use list::List;
mod priority;
pub use priority::Priority;
mod status;
pub use status::Status;
mod recurrence;
pub use recurrence::Recurrence;
2 changes: 1 addition & 1 deletion src/core/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize};
use sqlx::{sqlite::SqliteRow, Connection};

use crate::{
models::{list::List, priority::Priority, recurrence::Recurrence, status::Status, task::Task},
models::{List, Priority, Recurrence, Status, Task},
services::computer::ComputerStorage,
task_service::TasksProvider,
};
Expand Down
2 changes: 1 addition & 1 deletion src/core/src/services/computer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use async_trait::async_trait;

use crate::{
models::{list::List, task::Task},
models::{List, Task},
task_service::TasksProvider,
Error,
};
Expand Down
2 changes: 1 addition & 1 deletion src/core/src/services/computer/engine.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::path::PathBuf;

use crate::{
models::{list::List, task::Task},
models::{List, Task},
Error, TasksError,
};

Expand Down
2 changes: 1 addition & 1 deletion src/core/src/task_service.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use async_trait::async_trait;

use crate::{
models::{list::List, task::Task},
models::{List, Task},
Error,
};

Expand Down
33 changes: 16 additions & 17 deletions src/details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@ use cosmic::widget::segmented_button;
use cosmic::widget::segmented_button::Entity;
use cosmic::{theme, widget, Element};
use slotmap::{DefaultKey, SecondaryMap, SlotMap};
use tasks_core::models::priority::Priority;
use tasks_core::models::status::Status;
use tasks_core::models::task::Task;
use tasks_core::models::{self, Priority, Status};

use crate::fl;

pub struct Details {
pub task: Option<Task>,
pub task: Option<models::Task>,
pub priority_model: segmented_button::Model<segmented_button::SingleSelect>,
pub subtask_input: String,
pub subtasks: SlotMap<DefaultKey, Task>,
pub subtasks: SlotMap<DefaultKey, models::Task>,
pub editing: SecondaryMap<DefaultKey, bool>,
pub sub_task_input_ids: SecondaryMap<DefaultKey, widget::Id>,
}
Expand All @@ -38,9 +36,9 @@ pub enum Message {
SetDueDate(NaiveDate),
}

pub enum Command {
pub enum Task {
Focus(widget::Id),
UpdateTask(Task),
UpdateTask(models::Task),
OpenCalendarDialog,
Iced(cosmic::app::Task<super::app::Message>),
}
Expand Down Expand Up @@ -75,8 +73,8 @@ impl Details {
}
}

pub fn update(&mut self, message: Message) -> Vec<Command> {
let mut commands = vec![];
pub fn update(&mut self, message: Message) -> Vec<Task> {
let mut tasks = vec![];
match message {
Message::SetTitle(title) => {
if let Some(ref mut task) = &mut self.task {
Expand All @@ -96,11 +94,11 @@ impl Details {
Message::EditMode(id, editing) => {
self.editing.insert(id, editing);
if editing {
commands.push(Command::Iced(widget::text_input::focus(
tasks.push(Task::Iced(widget::text_input::focus(
self.sub_task_input_ids[id].clone(),
)));
} else if let Some(task) = self.subtasks.get(id) {
commands.push(Command::UpdateTask(task.clone()));
tasks.push(Task::UpdateTask(task.clone()));
}
}
Message::PriorityActivate(entity) => {
Expand Down Expand Up @@ -132,25 +130,26 @@ impl Details {
self.subtasks.remove(id);
}
Message::SubTaskEditDone => {
commands.push(Command::Focus(widget::Id::new("new_sub_task_input")));
tasks.push(Task::Focus(widget::Id::new("new_sub_task_input")));
}
Message::SubTaskInput(text) => {
self.subtask_input = text;
}
Message::AddTask => {
if let Some(ref mut task) = &mut self.task {
if !self.subtask_input.is_empty() {
let sub_task = Task::new(self.subtask_input.clone(), task.id().clone());
let sub_task =
models::Task::new(self.subtask_input.clone(), task.id().clone());
task.sub_tasks.push(sub_task.clone());
let id = self.subtasks.insert(sub_task);
self.sub_task_input_ids.insert(id, widget::Id::unique());
self.subtask_input.clear();
commands.push(Command::Focus(widget::Id::new("new_sub_task_input")));
tasks.push(Task::Focus(widget::Id::new("new_sub_task_input")));
}
}
}
Message::OpenCalendarDialog => {
commands.push(Command::OpenCalendarDialog);
tasks.push(Task::OpenCalendarDialog);
}
Message::SetDueDate(date) => {
let tz = Utc::now().timezone();
Expand All @@ -162,10 +161,10 @@ impl Details {

if let Some(task) = &mut self.task {
task.sub_tasks = self.subtasks.values().cloned().collect();
commands.push(Command::UpdateTask(task.clone()));
tasks.push(Task::UpdateTask(task.clone()));
}

commands
tasks
}

pub fn view(&self) -> Element<Message> {
Expand Down
4 changes: 2 additions & 2 deletions src/todo.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::app::markdown::Markdown;
use crate::Error;
use tasks_core::models::list::List;
use tasks_core::models::task::Task;
use tasks_core::models::List;
use tasks_core::models::Task;
use tasks_core::service::TaskService;
use tasks_core::TasksError;

Expand Down

0 comments on commit 46ea397

Please sign in to comment.