Skip to content

Commit

Permalink
chore: update libcosmic and improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
edfloreshz committed Oct 26, 2024
1 parent 2fd615c commit d9a9686
Show file tree
Hide file tree
Showing 14 changed files with 787 additions and 645 deletions.
1,200 changes: 652 additions & 548 deletions Cargo.lock

Large diffs are not rendered by default.

23 changes: 13 additions & 10 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ 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,
Command, Element,
Element, Task as Command,
};
use tasks_core::models::list::List;
use tasks_core::models::task::Task;
Expand Down Expand Up @@ -190,7 +190,7 @@ impl Tasks {
.padding(spacing.space_none)
.into(),
])
.align_items(Alignment::Center)
.align_x(Alignment::Center)
.spacing(spacing.space_xxs)
.width(Length::Fill)
.into()
Expand Down Expand Up @@ -363,7 +363,7 @@ impl Application for Tasks {
.control(
widget::container(scrollable(widget::row::with_children(vec![
widget::flex_row(icon_buttons).into(),
horizontal_space(Length::Fixed(f32::from(spacing.space_s))).into(),
horizontal_space().into(),
])))
.height(Length::Fixed(300.0)),
);
Expand Down Expand Up @@ -465,7 +465,9 @@ impl Application for Tasks {
if let Some(list) = location_opt {
let message = Message::Content(content::Message::List(Some(list.clone())));
let window_title = format!("{} - {}", list.name, fl!("tasks"));
commands.push(self.set_window_title(window_title, self.main_window_id()));
if let Some(window_id) = self.core.main_window_id() {
commands.push(self.set_window_title(window_title, window_id));
}
return self.update(message);
}

Expand All @@ -477,11 +479,10 @@ impl Application for Tasks {
struct ThemeSubscription;

let mut subscriptions = vec![
event::listen_with(|event, status| match event {
Event::Keyboard(KeyEvent::KeyPressed { key, modifiers, .. }) => match status {
event::Status::Ignored => Some(Message::Key(modifiers, key)),
event::Status::Captured => None,
},
event::listen_with(|event, _status, _window_id| match event {
Event::Keyboard(KeyEvent::KeyPressed { key, modifiers, .. }) => {
Some(Message::Key(modifiers, key))
}
Event::Keyboard(KeyEvent::ModifiersChanged(modifiers)) => {
Some(Message::Modifiers(modifiers))
}
Expand Down Expand Up @@ -677,7 +678,9 @@ impl Application for Tasks {
self.set_context_title(context_page.clone().title());
}
Message::WindowClose => {
return window::close(window::Id::MAIN);
if let Some(window_id) = self.core.main_window_id() {
return window::close(window_id);
}
}
Message::WindowNew => match env::current_exe() {
Ok(exe) => match process::Command::new(&exe).spawn() {
Expand Down
28 changes: 13 additions & 15 deletions src/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub enum Message {
}

pub enum Command {
Iced(cosmic::app::Command<super::app::Message>),
Iced(cosmic::app::Task<super::app::Message>),
GetTasks(String),
DisplayTask(Task),
UpdateTask(Task),
Expand All @@ -58,14 +58,14 @@ impl Content {
fn list_header<'a>(&'a self, list: &'a List) -> Element<'a, Message> {
let spacing = theme::active().cosmic().spacing;
let export_button = widget::button::icon(IconCache::get_handle("share-symbolic", 18))
.style(theme::Button::Suggested)
.class(cosmic::style::Button::Suggested)
.padding(spacing.space_xxs)
.on_press(Message::Export(self.tasks.values().cloned().collect()));
let default_icon = emojis::get_by_shortcode("pencil").unwrap().to_string();
let icon = list.icon.clone().unwrap_or(default_icon);

widget::row::with_capacity(3)
.align_items(Alignment::Center)
.align_y(Alignment::Center)
.spacing(spacing.space_s)
.padding([spacing.space_none, spacing.space_xxs])
.push(widget::text(icon).size(spacing.space_m))
Expand All @@ -87,21 +87,19 @@ impl Content {
.padding([spacing.space_none, spacing.space_xxs]);

for (id, item) in &self.tasks {
let item_checkbox =
widget::checkbox("", item.status == Status::Completed, move |value| {
Message::Complete(id, value)
});
let item_checkbox = widget::checkbox("", item.status == Status::Completed)
.on_toggle(move |value| Message::Complete(id, value));

let delete_button =
widget::button::icon(IconCache::get_handle("user-trash-full-symbolic", 18))
.padding(spacing.space_xxs)
.style(theme::Button::Destructive)
.class(cosmic::style::Button::Destructive)
.on_press(Message::Delete(id));

let details_button =
widget::button::icon(IconCache::get_handle("info-outline-symbolic", 18))
.padding(spacing.space_xxs)
.style(theme::Button::Standard)
.class(cosmic::style::Button::Standard)
.on_press(Message::Select(item.clone()));

let task_item_text = widget::editable_input(
Expand All @@ -116,7 +114,7 @@ impl Content {
.width(Length::Fill);

let row = widget::row::with_capacity(4)
.align_items(Alignment::Center)
.align_y(Alignment::Center)
.spacing(spacing.space_xxs)
.padding([spacing.space_xxxs, spacing.space_xxs])
.push(item_checkbox)
Expand Down Expand Up @@ -148,7 +146,7 @@ impl Content {
widget::text(fl!("no-tasks-suggestion")).into(),
])
.spacing(10)
.align_items(Alignment::Center),
.align_x(Alignment::Center),
)
.align_y(Vertical::Center)
.align_x(Horizontal::Center)
Expand All @@ -172,15 +170,15 @@ impl Content {
.into(),
widget::button::icon(IconCache::get_handle("mail-send-symbolic", 18))
.padding(spacing.space_xxs)
.style(theme::Button::Suggested)
.class(cosmic::style::Button::Suggested)
.on_press(Message::AddTask)
.into(),
])
.padding(spacing.space_xxs)
.spacing(spacing.space_xxs)
.align_items(Alignment::Center)
.align_y(Alignment::Center)
.apply(widget::container)
.style(cosmic::style::Container::ContextDrawer)
.class(cosmic::style::Container::ContextDrawer)
.into()
}

Expand Down Expand Up @@ -280,7 +278,7 @@ impl Content {
widget::text(fl!("no-list-suggestion")).into(),
])
.spacing(10)
.align_items(Alignment::Center),
.align_x(Alignment::Center),
)
.align_y(Vertical::Center)
.align_x(Horizontal::Center)
Expand Down
2 changes: 1 addition & 1 deletion src/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ edition = "2021"
[dependencies]
serde_json = "1.0.87"
ron = "0.8.1"
anyhow = "1.0.66"
tracing = "0.1.37"
async-trait = "0.1.68"
libset = "0.1.6"
emojis = "0.6.1"
dirs = "5.0.1"
derive-getters = "0.3.0"
derive_setters = "0.1.6"
thiserror = "1.0.65"

[dependencies.sqlx]
version = "0.8.0"
Expand Down
33 changes: 33 additions & 0 deletions src/core/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use thiserror::Error;

#[derive(Debug, Error)]
pub enum Error {
#[error("The requested service was unavailable")]
ServiceUnavailable,
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("Ron spanned error: {0}")]
RonSpanned(#[from] ron::error::SpannedError),
#[error("Ron deserialization error: {0}")]
RonDeserialization(#[from] ron::de::Error),
#[error("Sqlx error: {0}")]
Sqlx(#[from] sqlx::Error),
#[error("Libset error: {0}")]
Libset(#[from] libset::Error),
#[error("TaskS error: {0}")]
Tasks(#[from] TasksError),
}

#[derive(Debug, Error)]
pub enum TasksError {
#[error("The requested service is unavailable")]
ServiceUnavailable,
#[error("Task not found")]
TaskNotFound,
#[error("Task already exists")]
ExistingTask,
#[error("List not found")]
ListNotFound,
#[error("List already exists")]
ExistingList,
}
2 changes: 2 additions & 0 deletions src/core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
pub mod error;
pub mod models;
pub mod service;
pub mod services;
pub(crate) mod task_service;
pub use error::*;
2 changes: 2 additions & 0 deletions src/core/src/models/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ pub struct List {
pub icon: Option<String>,
}

unsafe impl Send for List {}

impl FromIterator<List> for List {
fn from_iter<T: IntoIterator<Item = List>>(iter: T) -> Self {
let mut list = Self::default();
Expand Down
7 changes: 4 additions & 3 deletions src/core/src/service.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::path::PathBuf;

use crate::Error;
use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};
use sqlx::{sqlite::SqliteRow, Connection};
Expand Down Expand Up @@ -53,7 +54,7 @@ impl TaskService {
}
}

pub async fn migrate(app_id: &str) -> Result<(), Box<dyn std::error::Error>> {
pub async fn migrate(app_id: &str) -> Result<(), Error> {
if let Some(mut service) = TaskService::new(app_id, Provider::Computer).get_service() {
let database_path = dirs::config_dir()
.unwrap()
Expand All @@ -80,7 +81,7 @@ impl TaskService {

use sqlx::Row;

async fn get_tasks(database_path: &PathBuf) -> Result<Vec<Task>, Box<dyn std::error::Error>> {
async fn get_tasks(database_path: &PathBuf) -> Result<Vec<Task>, Error> {
let mut conn = sqlx::SqliteConnection::connect(database_path.to_str().unwrap()).await?;
let tasks = sqlx::query("SELECT * FROM tasks")
.map(|row: SqliteRow| Task {
Expand Down Expand Up @@ -122,7 +123,7 @@ async fn get_tasks(database_path: &PathBuf) -> Result<Vec<Task>, Box<dyn std::er
Ok(tasks)
}

async fn get_lists(database_path: &PathBuf) -> Result<Vec<List>, Box<dyn std::error::Error>> {
async fn get_lists(database_path: &PathBuf) -> Result<Vec<List>, Error> {
let mut conn = sqlx::SqliteConnection::connect(database_path.to_str().unwrap()).await?;
let tasks = sqlx::query("SELECT * FROM lists")
.map(|row: SqliteRow| List {
Expand Down
22 changes: 11 additions & 11 deletions src/core/src/services/computer.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use anyhow::Result;
use async_trait::async_trait;

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

use self::engine::ComputerStorageEngine;
Expand All @@ -23,43 +23,43 @@ impl ComputerStorage {

#[async_trait]
impl TasksProvider for ComputerStorage {
async fn get_task(&mut self, list_id: String, task_id: String) -> Result<Task> {
async fn get_task(&mut self, list_id: String, task_id: String) -> Result<Task, Error> {
self.engine.get_task(&list_id, &task_id)
}

async fn get_tasks_from_list(&mut self, parent_list: String) -> Result<Vec<Task>> {
async fn get_tasks_from_list(&mut self, parent_list: String) -> Result<Vec<Task>, Error> {
self.engine.tasks(&parent_list)
}

async fn create_task(&mut self, task: Task) -> Result<Task> {
async fn create_task(&mut self, task: Task) -> Result<Task, Error> {
self.engine.create_task(task)
}

async fn update_task(&mut self, task: Task) -> Result<()> {
async fn update_task(&mut self, task: Task) -> Result<(), Error> {
self.engine.update_task(task)
}

async fn delete_task(&mut self, list_id: String, task_id: String) -> Result<()> {
async fn delete_task(&mut self, list_id: String, task_id: String) -> Result<(), Error> {
self.engine.delete_task(&list_id, &task_id)
}

async fn get_lists(&mut self) -> Result<Vec<List>> {
async fn get_lists(&mut self) -> Result<Vec<List>, Error> {
self.engine.lists()
}

async fn get_list(&mut self, id: String) -> Result<List> {
async fn get_list(&mut self, id: String) -> Result<List, Error> {
self.engine.get_list(&id)
}

async fn create_list(&mut self, list: List) -> Result<List> {
async fn create_list(&mut self, list: List) -> Result<List, Error> {
self.engine.create_list(list)
}

async fn update_list(&mut self, list: List) -> Result<()> {
async fn update_list(&mut self, list: List) -> Result<(), Error> {
self.engine.update_list(list)
}

async fn delete_list(&mut self, id: String) -> Result<()> {
async fn delete_list(&mut self, id: String) -> Result<(), Error> {
self.engine.delete_list(&id)
}
}
Loading

0 comments on commit d9a9686

Please sign in to comment.