From 46ea3978c8c8c33c272413fb00c61f09775f5479 Mon Sep 17 00:00:00 2001 From: Eduardo Flores Date: Mon, 4 Nov 2024 11:55:26 +0000 Subject: [PATCH] improv: use Task instead of Command --- src/app.rs | 153 ++++++++++++----------- src/app/markdown.rs | 6 +- src/content.rs | 54 ++++---- src/core/src/models/mod.rs | 19 +-- src/core/src/service.rs | 2 +- src/core/src/services/computer.rs | 2 +- src/core/src/services/computer/engine.rs | 2 +- src/core/src/task_service.rs | 2 +- src/details.rs | 33 +++-- src/todo.rs | 4 +- 10 files changed, 139 insertions(+), 138 deletions(-) diff --git a/src/app.rs b/src/app.rs index 8421ed9..c4a5cdf 100644 --- a/src/app.rs +++ b/src/app.rs @@ -17,11 +17,10 @@ use cosmic::widget::menu::key_bind::KeyBind; use cosmic::widget::segmented_button::{Entity, EntityMut, SingleSelect}; use cosmic::widget::{horizontal_space, scrollable, segmented_button}; use cosmic::{ - app, cosmic_config, cosmic_theme, executor, theme, widget, Application, ApplicationExt, - Element, Task as Command, + app, cosmic_config, cosmic_theme, executor, theme, widget, Application, ApplicationExt, Element, }; -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::{Provider, TaskService}; use crate::app::config::{AppTheme, CONFIG_VERSION}; @@ -175,7 +174,7 @@ impl MenuAction for NavMenuAction { } impl Tasks { - fn update_config(&mut self) -> Command> { + fn update_config(&mut self) -> app::Task { app::command::set_theme(self.config.app_theme.theme()) } @@ -261,8 +260,7 @@ impl Application for Tasks { &mut self.core } - fn init(mut core: Core, flags: Self::Flags) -> (Self, Command>) { - core.nav_bar_toggle_condensed(); + fn init(core: Core, flags: Self::Flags) -> (Self, app::Task) { let nav_model = segmented_button::ModelBuilder::default().build(); let service = TaskService::new(Self::APP_ID, Provider::Computer); let mut app = Tasks { @@ -281,15 +279,18 @@ impl Application for Tasks { dialog_text_input: widget::Id::unique(), }; - let mut commands = vec![Command::perform(TaskService::migrate(Self::APP_ID), |_| { - message::app(Message::Tasks(TasksAction::FetchLists)) - })]; + app.core.nav_bar_toggle_condensed(); + + let mut tasks = vec![app::Task::perform( + TaskService::migrate(Self::APP_ID), + |_| message::app(Message::Tasks(TasksAction::FetchLists)), + )]; if let Some(id) = app.core.main_window_id() { - commands.push(app.set_window_title(fl!("tasks"), id)); + tasks.push(app.set_window_title(fl!("tasks"), id)); } - (app, Command::batch(commands)) + (app, app::Task::batch(tasks)) } fn context_drawer(&self) -> Option> { @@ -326,18 +327,18 @@ impl Application for Tasks { Some(&self.nav_model) } - fn on_escape(&mut self) -> Command> { + fn on_escape(&mut self) -> app::Task { if self.dialog_pages.pop_front().is_some() { - return Command::none(); + return app::Task::none(); } self.core.window.show_context = false; - Command::none() + app::Task::none() } - fn on_nav_select(&mut self, entity: Entity) -> Command> { - let mut commands = vec![]; + fn on_nav_select(&mut self, entity: Entity) -> app::Task { + let mut tasks = vec![]; self.nav_model.activate(entity); let location_opt = self.nav_model.data::(entity); @@ -345,15 +346,15 @@ impl Application for Tasks { let message = Message::Content(content::Message::List(Some(list.clone()))); let window_title = format!("{} - {}", list.name, fl!("tasks")); if let Some(window_id) = self.core.main_window_id() { - commands.push(self.set_window_title(window_title, window_id)); + tasks.push(self.set_window_title(window_title, window_id)); } return self.update(message); } - Command::batch(commands) + app::Task::batch(tasks) } - fn update(&mut self, message: Self::Message) -> Command> { + fn update(&mut self, message: Self::Message) -> app::Task { // Helper for updating config values efficiently macro_rules! config_set { ($name: ident, $value: expr) => { @@ -381,16 +382,16 @@ impl Application for Tasks { }; } - let mut commands = vec![]; + let mut tasks = vec![]; match message { Message::Content(message) => { - let content_commands = self.content.update(message); - for content_command in content_commands { - match content_command { - content::Command::Iced(command) => return command, - content::Command::GetTasks(list_id) => { - commands.push(Command::perform( + let content_tasks = self.content.update(message); + for content_task in content_tasks { + match content_task { + content::Task::Iced(task) => return task, + content::Task::GetTasks(list_id) => { + tasks.push(app::Task::perform( todo::fetch_tasks(list_id, self.service.clone()), |result| match result { Ok(data) => message::app(Message::Content( @@ -400,7 +401,7 @@ impl Application for Tasks { }, )); } - content::Command::DisplayTask(task) => { + content::Task::DisplayTask(task) => { let entity = self.details.priority_model.entity_at(task.priority as u16); if let Some(entity) = entity { @@ -415,23 +416,23 @@ impl Application for Tasks { .sub_task_input_ids .insert(id, widget::Id::unique()); }); - commands.push( + tasks.push( self.update(Message::ToggleContextPage(ContextPage::TaskDetails)), ); } - content::Command::UpdateTask(task) => { + content::Task::UpdateTask(task) => { self.details.task = Some(task.clone()); - let command = Command::perform( + let task = app::Task::perform( todo::update_task(task, self.service.clone().clone()), |result| match result { Ok(()) | Err(_) => message::none(), }, ); - commands.push(command); + tasks.push(task); } - content::Command::Delete(id) => { + content::Task::Delete(id) => { if let Some(list) = self.nav_model.active_data::() { - let command = Command::perform( + let task = app::Task::perform( todo::delete_task( list.id().clone(), id.clone(), @@ -441,44 +442,46 @@ impl Application for Tasks { Ok(()) | Err(_) => message::none(), }, ); - commands.push(command); + tasks.push(task); } } - content::Command::CreateTask(task) => { - let command = Command::perform( + content::Task::CreateTask(task) => { + let task = app::Task::perform( todo::create_task(task, self.service.clone()), |result| match result { Ok(()) | Err(_) => message::none(), }, ); - commands.push(command); + tasks.push(task); } - content::Command::Export(tasks) => { - commands.push(self.update(Message::Tasks(TasksAction::Export(tasks)))); + content::Task::Export(exported_tasks) => { + tasks.push( + self.update(Message::Tasks(TasksAction::Export(exported_tasks))), + ); } } } } Message::Details(message) => { - let details_commands = self.details.update(message); - for details_command in details_commands { - match details_command { - details::Command::UpdateTask(task) => { - commands.push(self.update(Message::Content( + let details_tasks = self.details.update(message); + for details_task in details_tasks { + match details_task { + details::Task::UpdateTask(task) => { + tasks.push(self.update(Message::Content( content::Message::UpdateTask(task.clone()), ))); } - details::Command::OpenCalendarDialog => { - commands.push(self.update(Message::Dialog(DialogAction::Open( + details::Task::OpenCalendarDialog => { + tasks.push(self.update(Message::Dialog(DialogAction::Open( DialogPage::Calendar(Local::now().date_naive()), )))); } - details::Command::Focus(id) => { - commands.push( + details::Task::Focus(id) => { + tasks.push( self.update(Message::Application(ApplicationAction::Focus(id))), ); } - details::Command::Iced(command) => return command, + details::Task::Iced(task) => return task, } } } @@ -512,7 +515,7 @@ impl Application for Tasks { } page => self.dialog_pages.push_back(page), } - commands.push(self.update(Message::Application(ApplicationAction::Focus( + tasks.push(self.update(Message::Application(ApplicationAction::Focus( self.dialog_text_input.clone(), )))); } @@ -527,7 +530,7 @@ impl Application for Tasks { match dialog_page { DialogPage::New(name) => { let list = List::new(&name); - commands.push(Command::perform( + tasks.push(app::Task::perform( todo::create_list(list, self.service.clone()), |result| match result { Ok(list) => { @@ -542,15 +545,15 @@ impl Application for Tasks { self.nav_model.text_set(entity, name.clone()); if let Some(list) = self.nav_model.active_data_mut::() { list.name.clone_from(&name); - let command = Command::perform( + let task = app::Task::perform( todo::update_list(list.clone(), self.service.clone()), |_| message::none(), ); - commands.push(command); + tasks.push(task); } } DialogPage::Delete => { - commands.push(self.update(Message::Tasks(TasksAction::DeleteList))); + tasks.push(self.update(Message::Tasks(TasksAction::DeleteList))); } DialogPage::Icon(icon) => { if let Some(list) = self.nav_model.active_data::() { @@ -560,11 +563,11 @@ impl Application for Tasks { } if let Some(list) = self.nav_model.active_data_mut::() { list.icon = Some(icon); - let command = Command::perform( + let task = app::Task::perform( todo::update_list(list.clone(), self.service.clone()), |_| message::none(), ); - commands.push(command); + tasks.push(task); } } DialogPage::Calendar(date) => { @@ -580,7 +583,7 @@ impl Application for Tasks { }, Message::Tasks(tasks_action) => match tasks_action { TasksAction::FetchLists => { - commands.push(Command::perform( + tasks.push(app::Task::perform( todo::fetch_lists(self.service.clone()), |result| match result { Ok(data) => { @@ -595,39 +598,39 @@ impl Application for Tasks { self.create_nav_item(&list); } let Some(entity) = self.nav_model.iter().next() else { - return Command::none(); + return app::Task::none(); }; self.nav_model.activate(entity); - let command = self.on_nav_select(entity); - commands.push(command); + let task = self.on_nav_select(entity); + tasks.push(task); } TasksAction::AddList(list) => { self.create_nav_item(&list); let Some(entity) = self.nav_model.iter().last() else { - return Command::none(); + return app::Task::none(); }; - let command = self.on_nav_select(entity); - commands.push(command); + let task = self.on_nav_select(entity); + tasks.push(task); } TasksAction::DeleteList => { if let Some(list) = self.nav_model.active_data::() { - let command = Command::perform( + let task = app::Task::perform( todo::delete_list(list.id().clone(), self.service.clone()), |result| match result { Ok(()) | Err(_) => message::none(), }, ); - commands.push(self.update(Message::Content(content::Message::List(None)))); + tasks.push(self.update(Message::Content(content::Message::List(None)))); - commands.push(command); + tasks.push(task); } self.nav_model.remove(self.nav_model.active()); } - TasksAction::Export(tasks) => { + TasksAction::Export(exported_tasks) => { if let Some(list) = self.nav_model.active_data() { - let exported_markdown = todo::export_list(list, &tasks); - commands.push(self.update(Message::Dialog(DialogAction::Open( + let exported_markdown = todo::export_list(list, &exported_tasks); + tasks.push(self.update(Message::Dialog(DialogAction::Open( DialogPage::Export(exported_markdown), )))); } @@ -679,25 +682,25 @@ impl Application for Tasks { ApplicationAction::Modifiers(modifiers) => { self.modifiers = modifiers; } - ApplicationAction::Focus(id) => commands.push(widget::text_input::focus(id)), + ApplicationAction::Focus(id) => tasks.push(widget::text_input::focus(id)), ApplicationAction::NavMenuAction(nav_menu_action) => match nav_menu_action { NavMenuAction::Rename(entity) => { if self.nav_model.data::(entity).is_some() { - commands.push(self.update(Message::Dialog(DialogAction::Open( + tasks.push(self.update(Message::Dialog(DialogAction::Open( DialogPage::Rename(String::new()), )))); } } NavMenuAction::SetIcon(entity) => { if self.nav_model.data::(entity).is_some() { - commands.push(self.update(Message::Dialog(DialogAction::Open( + tasks.push(self.update(Message::Dialog(DialogAction::Open( DialogPage::Icon(String::new()), )))); } } NavMenuAction::Delete(entity) => { if self.nav_model.data::(entity).is_some() { - commands.push(self.update(Message::Dialog(DialogAction::Open( + tasks.push(self.update(Message::Dialog(DialogAction::Open( DialogPage::Delete, )))); } @@ -707,7 +710,7 @@ impl Application for Tasks { } } - Command::batch(commands) + app::Task::batch(tasks) } fn view(&self) -> Element { diff --git a/src/app/markdown.rs b/src/app/markdown.rs index ed2b63e..c19b690 100644 --- a/src/app/markdown.rs +++ b/src/app/markdown.rs @@ -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; diff --git a/src/content.rs b/src/content.rs index 2ff244f..9f2eee4 100644 --- a/src/content.rs +++ b/src/content.rs @@ -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, - tasks: SlotMap, + tasks: SlotMap, editing: SecondaryMap, task_input_ids: SecondaryMap, input: String, @@ -24,24 +22,24 @@ pub enum Message { Complete(DefaultKey, bool), Delete(DefaultKey), EditMode(DefaultKey, bool), - Export(Vec), + Export(Vec), Input(String), List(Option), - Select(Task), - SetItems(Vec), + Select(models::Task), + SetItems(Vec), TitleSubmit(DefaultKey), TitleUpdate(DefaultKey, String), - UpdateTask(Task), + UpdateTask(models::Task), } -pub enum Command { +pub enum Task { Iced(cosmic::app::Task), GetTasks(String), - DisplayTask(Task), - UpdateTask(Task), + DisplayTask(models::Task), + UpdateTask(models::Task), Delete(String), - CreateTask(Task), - Export(Vec), + CreateTask(models::Task), + Export(Vec), } impl Content { @@ -182,13 +180,13 @@ impl Content { .into() } - pub fn update(&mut self, message: Message) -> Vec { - let mut commands = Vec::new(); + pub fn update(&mut self, message: Message) -> Vec { + 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) => { @@ -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) => { @@ -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); @@ -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(); @@ -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 { diff --git a/src/core/src/models/mod.rs b/src/core/src/models/mod.rs index 34c1076..709246a 100644 --- a/src/core/src/models/mod.rs +++ b/src/core/src/models/mod.rs @@ -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; diff --git a/src/core/src/service.rs b/src/core/src/service.rs index 93bfbce..8b57f85 100644 --- a/src/core/src/service.rs +++ b/src/core/src/service.rs @@ -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, }; diff --git a/src/core/src/services/computer.rs b/src/core/src/services/computer.rs index b131197..79fcccd 100644 --- a/src/core/src/services/computer.rs +++ b/src/core/src/services/computer.rs @@ -1,7 +1,7 @@ use async_trait::async_trait; use crate::{ - models::{list::List, task::Task}, + models::{List, Task}, task_service::TasksProvider, Error, }; diff --git a/src/core/src/services/computer/engine.rs b/src/core/src/services/computer/engine.rs index 07f0eae..1483124 100644 --- a/src/core/src/services/computer/engine.rs +++ b/src/core/src/services/computer/engine.rs @@ -1,7 +1,7 @@ use std::path::PathBuf; use crate::{ - models::{list::List, task::Task}, + models::{List, Task}, Error, TasksError, }; diff --git a/src/core/src/task_service.rs b/src/core/src/task_service.rs index a65b6ec..5de381d 100644 --- a/src/core/src/task_service.rs +++ b/src/core/src/task_service.rs @@ -1,7 +1,7 @@ use async_trait::async_trait; use crate::{ - models::{list::List, task::Task}, + models::{List, Task}, Error, }; diff --git a/src/details.rs b/src/details.rs index 3a04404..e6c25ab 100644 --- a/src/details.rs +++ b/src/details.rs @@ -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, + pub task: Option, pub priority_model: segmented_button::Model, pub subtask_input: String, - pub subtasks: SlotMap, + pub subtasks: SlotMap, pub editing: SecondaryMap, pub sub_task_input_ids: SecondaryMap, } @@ -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), } @@ -75,8 +73,8 @@ impl Details { } } - pub fn update(&mut self, message: Message) -> Vec { - let mut commands = vec![]; + pub fn update(&mut self, message: Message) -> Vec { + let mut tasks = vec![]; match message { Message::SetTitle(title) => { if let Some(ref mut task) = &mut self.task { @@ -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) => { @@ -132,7 +130,7 @@ 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; @@ -140,17 +138,18 @@ impl Details { 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(); @@ -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 { diff --git a/src/todo.rs b/src/todo.rs index ddbdc98..a13b699 100644 --- a/src/todo.rs +++ b/src/todo.rs @@ -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;