Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: custom task engine #13

Merged
merged 7 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
665 changes: 526 additions & 139 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ open = "5.0.2"
dirs = "5.0.1"
chrono = "0.4.35"
tokio = "1"
done_core = { path = "src/core" }
cosmic-tasks-core = { path = "src/core" }
tracing = "0.1.40"
cli-clipboard = "0.4.0"
slotmap = "1.0.7"
Expand Down
127 changes: 73 additions & 54 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ use cosmic::{
app, cosmic_config, cosmic_theme, executor, theme, widget, Application, ApplicationExt,
Command, Element,
};
use done_core::models::list::List;
use done_core::models::task::Task;
use done_core::service::Service;
use cosmic_tasks_core::models::list::List;
use cosmic_tasks_core::models::task::Task;
use cosmic_tasks_core::service::{Provider, TaskService};

use crate::app::config::{AppTheme, CONFIG_VERSION};
use crate::app::key_bind::key_binds;
Expand All @@ -38,6 +38,7 @@ pub mod settings;

pub struct App {
core: Core,
service: TaskService,
nav_model: segmented_button::SingleSelectModel,
content: Content,
details: Details,
Expand All @@ -57,6 +58,7 @@ pub enum Message {
Details(details::Message),
ToggleContextPage(ContextPage),
LaunchUrl(String),
FetchLists,
PopulateLists(Vec<List>),
WindowClose,
WindowNew,
Expand All @@ -65,7 +67,7 @@ pub enum Message {
DialogUpdate(DialogPage),
Key(Modifiers, Key),
Modifiers(Modifiers),
AppTheme(AppTheme),
AppTheme(usize),
SystemThemeModeChange(cosmic_theme::ThemeMode),
OpenNewListDialog,
OpenRenameListDialog,
Expand Down Expand Up @@ -166,7 +168,7 @@ impl App {
}

fn about(&self) -> Element<Message> {
let cosmic_theme::Spacing { space_xxs, .. } = theme::active().cosmic().spacing;
let spacing = theme::active().cosmic().spacing;
let repository = "https://github.com/edfloreshz/cosmic-tasks";
let hash = env!("VERGEN_GIT_SHA");
let short_hash: String = hash.chars().take(7).collect();
Expand All @@ -181,15 +183,15 @@ impl App {
widget::text::title3(fl!("cosmic-tasks")).into(),
widget::button::link(repository)
.on_press(Message::LaunchUrl(repository.to_string()))
.padding(0)
.padding(spacing.space_none)
.into(),
widget::button::link(fl!("git-description", hash = short_hash.as_str(), date = date))
.on_press(Message::LaunchUrl(format!("{}/commits/{}", repository, hash)))
.padding(0)
.padding(spacing.space_none)
.into(),
])
.align_items(Alignment::Center)
.spacing(space_xxs)
.spacing(spacing.space_xxs)
.width(Length::Fill)
.into()
}
Expand All @@ -205,13 +207,7 @@ impl App {
widget::settings::item::builder(fl!("theme")).control(widget::dropdown(
&self.app_themes,
Some(app_theme_selected),
move |index| {
Message::AppTheme(match index {
1 => AppTheme::Dark,
2 => AppTheme::Light,
_ => AppTheme::System,
})
},
Message::AppTheme,
)),
)
.into()])
Expand Down Expand Up @@ -248,11 +244,12 @@ impl Application for App {

fn init(mut core: Core, flags: Self::Flags) -> (Self, Command<CosmicMessage<Self::Message>>) {
core.nav_bar_toggle_condensed();
let nav_model = segmented_button::ModelBuilder::default();

let nav_model = segmented_button::ModelBuilder::default().build();
let service = TaskService::new(Self::APP_ID, Provider::Computer);
let app = App {
core,
nav_model: nav_model.build(),
service: service.clone(),
nav_model,
content: Content::new(),
details: Details::new(),
config_handler: flags.config_handler,
Expand All @@ -266,9 +263,9 @@ impl Application for App {
};

let commands = vec![Command::perform(
todo::fetch_lists(),
TaskService::migrate(Self::APP_ID),
|result| match result {
Ok(data) => message::app(Message::PopulateLists(data)),
Ok(_) => message::app(Message::FetchLists),
Err(_) => message::none(),
},
)];
Expand Down Expand Up @@ -346,7 +343,6 @@ impl Application for App {
),
DialogPage::Icon(icon) => {
let icon_buttons: Vec<Element<_>> = emojis::iter()
.into_iter()
.map(|emoji| {
widget::button(
widget::container(widget::text(emoji.to_string()))
Expand Down Expand Up @@ -569,14 +565,15 @@ impl Application for App {
match content_command {
content::Command::Iced(command) => return command,
content::Command::GetTasks(list_id) => {
commands.push(Command::perform(todo::fetch_tasks(list_id), |result| {
match result {
commands.push(Command::perform(
todo::fetch_tasks(list_id, self.service.clone()),
|result| match result {
Ok(data) => message::app(Message::Content(
content::Message::SetItems(data),
)),
Err(_) => message::none(),
}
}));
},
));
}
content::Command::DisplayTask(task) => {
let entity =
Expand All @@ -599,18 +596,24 @@ impl Application for App {
}
content::Command::UpdateTask(task) => {
self.details.task = Some(task.clone());
let command =
Command::perform(todo::update_task(task), |result| match result {
let command = Command::perform(
todo::update_task(task, self.service.clone().clone()),
|result| match result {
Ok(_) => message::none(),
Err(_) => message::none(),
});
},
);
commands.push(command);
}
content::Command::Delete(id) => {
if let Some(list) = self.nav_model.data::<List>(self.nav_model.active())
{
let command = Command::perform(
todo::delete_task(list.id.clone(), id.clone()),
todo::delete_task(
list.id().clone(),
id.clone(),
self.service.clone().clone(),
),
|result| match result {
Ok(_) => message::none(),
Err(_) => message::none(),
Expand All @@ -620,11 +623,13 @@ impl Application for App {
}
}
content::Command::CreateTask(task) => {
let command =
Command::perform(todo::create_task(task), |result| match result {
let command = Command::perform(
todo::create_task(task, self.service.clone()),
|result| match result {
Ok(_) => message::none(),
Err(_) => message::none(),
});
},
);
commands.push(command);
}
content::Command::Export(tasks) => {
Expand Down Expand Up @@ -698,13 +703,27 @@ impl Application for App {
log::warn!("failed to open {:?}: {}", url, err);
}
},
Message::AppTheme(app_theme) => {
Message::AppTheme(index) => {
let app_theme = match index {
1 => AppTheme::Dark,
2 => AppTheme::Light,
_ => AppTheme::System,
};
config_set!(app_theme, app_theme);
return self.update_config();
}
Message::SystemThemeModeChange(_) => {
return self.update_config();
}
Message::FetchLists => {
commands.push(Command::perform(
todo::fetch_lists(self.service.clone()),
|result| match result {
Ok(data) => message::app(Message::PopulateLists(data)),
Err(_) => message::none(),
},
));
}
Message::PopulateLists(lists) => {
for list in lists {
self.create_nav_item(list);
Expand Down Expand Up @@ -736,14 +755,13 @@ impl Application for App {
}
Message::DeleteList => {
if let Some(list) = self.nav_model.data::<List>(self.nav_model.active()) {
let command =
Command::perform(
todo::delete_list(list.id.clone()),
|result| match result {
Ok(_) => message::none(),
Err(_) => message::none(),
},
);
let command = Command::perform(
todo::delete_list(list.id().clone(), self.service.clone()),
|result| match result {
Ok(_) => message::none(),
Err(_) => message::none(),
},
);

commands.push(self.update(Message::Content(content::Message::List(None))));

Expand Down Expand Up @@ -801,23 +819,24 @@ impl Application for App {
if let Some(dialog_page) = self.dialog_pages.pop_front() {
match dialog_page {
DialogPage::New(name) => {
let list = List::new(&name, Service::Computer);
commands.push(Command::perform(todo::create_list(list), |result| {
match result {
let list = List::new(&name);
commands.push(Command::perform(
todo::create_list(list, self.service.clone()),
|result| match result {
Ok(list) => message::app(Message::AddList(list)),
Err(_) => message::none(),
}
}));
},
));
}
DialogPage::Rename { to: name } => {
let entity = self.nav_model.active();
self.nav_model.text_set(entity, name.clone());
if let Some(list) = self.nav_model.active_data_mut::<List>() {
list.name = name.clone();
let command =
Command::perform(todo::update_list(list.clone()), |_| {
message::none()
});
let command = Command::perform(
todo::update_list(list.clone(), self.service.clone()),
|_| message::none(),
);
commands.push(command);
}
}
Expand All @@ -832,10 +851,10 @@ impl Application for App {
}
if let Some(list) = self.nav_model.active_data_mut::<List>() {
list.icon = Some(icon);
let command =
Command::perform(todo::update_list(list.clone()), |_| {
message::none()
});
let command = Command::perform(
todo::update_list(list.clone(), self.service.clone()),
|_| message::none(),
);
commands.push(command);
}
}
Expand Down
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 done_core::models::list::List;
use done_core::models::status::Status;
use done_core::models::task::Task;
use cosmic_tasks_core::models::list::List;
use cosmic_tasks_core::models::status::Status;
use cosmic_tasks_core::models::task::Task;

pub trait Markdown {
fn markdown(&self) -> String;
Expand Down
9 changes: 1 addition & 8 deletions src/app/settings.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use crate::app::icon_cache::{IconCache, ICON_CACHE};
use crate::app::{App, Flags};
use crate::app::Flags;
use cosmic::app::Settings;
use cosmic::iced::{Limits, Size};
use cosmic::Application;
use done_core::service::Services;
use std::sync::Mutex;

use super::config::CosmicTasksConfig;
Expand All @@ -13,7 +11,6 @@ pub fn init() -> (Settings, Flags) {
set_localization();
set_icon_cache();
set_logger();
start_services();
let settings = get_app_settings();
let flags = get_flags();
(settings, flags)
Expand All @@ -38,10 +35,6 @@ pub fn set_icon_cache() {
ICON_CACHE.get_or_init(|| Mutex::new(IconCache::new()));
}

pub fn start_services() {
Services::init(App::APP_ID);
}

pub fn get_flags() -> Flags {
let (config_handler, config) = (
CosmicTasksConfig::config_handler(),
Expand Down
Loading