Skip to content

Commit

Permalink
Async updates
Browse files Browse the repository at this point in the history
  • Loading branch information
alanvardy committed Dec 19, 2024
1 parent da060d0 commit 6a1c365
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 63 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased (on main branch only)

- Added due to multi-select in `task edit`
- Make all updates selected in multi-select run asynchronously

## 2024-12-19 v0.6.17

Expand Down
11 changes: 6 additions & 5 deletions PUBLISH_CHECKLIST.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

Create `tod-bin` directory for pushing to AUR

```bash
```fish
./setup_aur.sh
```

Expand All @@ -13,20 +13,21 @@ Create `tod-bin` directory for pushing to AUR
1. Update `CHANGELOG.md` with version number
2. Create PR with

```bash
VERSION=0.6.17 ./create_pr.sh
```fish
set VERSION 0.6.17
./create_pr.sh
```

3. Wait for it to pass, then merge and pull in latest changes

```bash
```fish
gh pr merge -r --admin
gs
```

4. Release it to all the places

```bash
```fish
./release.sh
```

Expand Down
2 changes: 2 additions & 0 deletions release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

echo "=== RUNNING cargo aur ===" &&
cargo aur &&
echo "=== CREATING GITHUB RELEASE ===" &&
gh release create "$VERSION" ./target/cargo-aur/*.tar.gz --title "$VERSION" --generate-notes &&
echo "=== RUNNING cargo publish ===" &&
cargo publish &&
echo "=== RUNNING push_aur.sh ===" &&
Expand Down
14 changes: 7 additions & 7 deletions src/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,17 @@ pub async fn edit_task(config: &Config, filter: String) -> Result<String, Error>
});
}

let mut result = String::new();
let mut handles = Vec::new();
for attribute in selections {
// Stops the inputs from rolling over each other in terminal
println!();
result = tasks::update_task(config, &task, &attribute).await?
if let Some(handle) = tasks::update_task(config, &task, &attribute).await? {
handles.push(handle);
}
}

Ok(result)
future::join_all(handles).await;
Ok(String::from("Finished editing task"))
}

pub async fn label(config: &Config, filter: &str, labels: &Vec<String>) -> Result<String, Error> {
Expand Down Expand Up @@ -226,10 +229,7 @@ mod tests {
.mock_select(0);

let result = edit_task(&config, String::from("today"));
assert_eq!(
result.await,
Ok("The content is the same, no need to change it".to_string())
);
assert_eq!(result.await, Ok("Finished editing task".to_string()));
mock.assert();
}
#[tokio::test]
Expand Down
14 changes: 7 additions & 7 deletions src/projects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,14 +352,17 @@ pub async fn edit_task(config: &Config, project: &Project) -> Result<String, Err
});
}

let mut result = String::new();
let mut handles = Vec::new();
for attribute in selections {
// Stops the inputs from rolling over each other in terminal
println!();
result = tasks::update_task(config, &task, &attribute).await?
if let Some(handle) = tasks::update_task(config, &task, &attribute).await? {
handles.push(handle);
}
}

Ok(result)
future::join_all(handles).await;
Ok(String::from("Finished editing task"))
}

/// All tasks for a project
Expand Down Expand Up @@ -860,10 +863,7 @@ mod tests {
let project = binding.first().unwrap();

let result = edit_task(&config, project);
assert_eq!(
result.await,
Ok("The content is the same, no need to change it".to_string())
);
assert_eq!(result.await, Ok("Finished editing task".to_string()));
mock.assert();
}
#[tokio::test]
Expand Down
94 changes: 58 additions & 36 deletions src/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use tokio::task::JoinHandle;

pub mod format;
pub mod priority;
use crate::color;
use crate::config::Config;
use crate::config::SortValue;
use crate::error::Error;
Expand Down Expand Up @@ -350,57 +349,45 @@ pub async fn update_task(
config: &Config,
task: &Task,
attribute: &TaskAttribute,
) -> Result<String, Error> {
) -> Result<Option<JoinHandle<()>>, Error> {
match attribute {
TaskAttribute::Content => {
let task_content = task.content.as_str();
let value = task.content.as_str();

let new_task_content =
input::string_with_default("Edit the task you selected:", task_content)?;
let new_value = input::string_with_default("Enter new content:", value)?;

if task_content == new_task_content {
return Ok(color::green_string(
"The content is the same, no need to change it",
));
if *value == new_value {
Ok(None)
} else {
let handle = spawn_update_task_content(config.clone(), task.clone(), new_value);
Ok(Some(handle))
}

todoist::update_task_content(config, task, new_task_content).await
}
TaskAttribute::Description => {
let value = task.description.as_str();

let new_value = input::string_with_default("Edit the task you selected:", value)?;
let new_value = input::string_with_default("Enter a new description:", value)?;

if value == new_value {
return Ok(color::green_string(
"The description is the same, no need to change it",
));
if *value == new_value {
Ok(None)
} else {
let handle = spawn_update_task_description(config.clone(), task.clone(), new_value);
Ok(Some(handle))
}

todoist::update_task_description(config, task, new_value).await
}
TaskAttribute::Priority => {
let value = &task.priority;
let priorities = priority::all_priorities();

let new_value = input::select("select your priority:", priorities, config.mock_select)?;

let new_value = input::select("Select your priority:", priorities, config.mock_select)?;
if *value == new_value {
return Ok(color::green_string(
"The priority is the same, no need to change it",
));
}

todoist::update_task_priority(config, task, &new_value).await
}
TaskAttribute::Due => {
if let Some(handle) = tasks::spawn_schedule_task(config.clone(), task.clone())? {
handle.await?;
Ok(String::from("Updated due"))
Ok(None)
} else {
Ok(String::from("No change"))
let handle = spawn_update_task_priority(config.clone(), task.clone(), new_value);
Ok(Some(handle))
}
}
TaskAttribute::Due => tasks::spawn_schedule_task(config.clone(), task.clone()),
}
}

Expand Down Expand Up @@ -583,7 +570,7 @@ pub fn spawn_schedule_task(config: Config, task: Task) -> Result<Option<JoinHand
}
}

// Completes task inside another thread
/// Completes task inside another thread
pub fn spawn_complete_task(config: Config, task: Task) -> JoinHandle<()> {
tokio::spawn(async move {
if let Err(e) = todoist::complete_task(&config, &task.id, false).await {
Expand All @@ -592,7 +579,7 @@ pub fn spawn_complete_task(config: Config, task: Task) -> JoinHandle<()> {
})
}

// Deletes task inside another thread
/// Deletes task inside another thread
pub fn spawn_delete_task(config: Config, task: Task) -> JoinHandle<()> {
tokio::spawn(async move {
if let Err(e) = todoist::delete_task(&config, &task, false).await {
Expand All @@ -601,7 +588,7 @@ pub fn spawn_delete_task(config: Config, task: Task) -> JoinHandle<()> {
})
}

// Updates task inside another thread
/// Updates task inside another thread
pub fn spawn_update_task_due(
config: Config,
task: Task,
Expand All @@ -618,6 +605,41 @@ pub fn spawn_update_task_due(
})
}

/// Updates task inside another thread
pub fn spawn_update_task_content(config: Config, task: Task, content: String) -> JoinHandle<()> {
tokio::spawn(async move {
if let Err(e) = todoist::update_task_content(&config, &task, content, false).await {
config.tx().send(e).unwrap();
}
})
}

/// Updates task inside another thread
pub fn spawn_update_task_description(
config: Config,
task: Task,
description: String,
) -> JoinHandle<()> {
tokio::spawn(async move {
if let Err(e) = todoist::update_task_description(&config, &task, description, false).await {
config.tx().send(e).unwrap();
}
})
}

/// Updates task inside another thread
pub fn spawn_update_task_priority(
config: Config,
task: Task,
priority: Priority,
) -> JoinHandle<()> {
tokio::spawn(async move {
if let Err(e) = todoist::update_task_priority(&config, &task, &priority, false).await {
config.tx().send(e).unwrap();
}
})
}

pub fn sync_json_to_tasks(json: String) -> Result<Vec<Task>, Error> {
let body: Body = serde_json::from_str(&json)?;
Ok(body.items)
Expand Down Expand Up @@ -750,7 +772,7 @@ pub async fn set_priority(

let config = config.clone();
Ok(tokio::spawn(async move {
if let Err(e) = todoist::update_task_priority(&config, &task, &priority).await {
if let Err(e) = todoist::update_task_priority(&config, &task, &priority, false).await {
config.tx().send(e).unwrap();
}
}))
Expand Down
19 changes: 11 additions & 8 deletions src/todoist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,12 @@ pub async fn update_task_priority(
config: &Config,
task: &Task,
priority: &Priority,
spinner: bool,
) -> Result<String, Error> {
let body = json!({ "priority": priority });
let url = format!("{}{}", REST_V2_TASKS_URL, task.id);

request::post_todoist_rest(config, url, body, true).await?;
request::post_todoist_rest(config, url, body, spinner).await?;
// Does not pass back an task
Ok(String::from("✓"))
}
Expand Down Expand Up @@ -209,12 +210,13 @@ pub async fn update_task_due_natural_language(
pub async fn update_task_content(
config: &Config,
task: &Task,
new_name: String,
content: String,
spinner: bool,
) -> Result<String, Error> {
let body = json!({ "content": new_name });
let body = json!({ "content": content});
let url = format!("{}{}", REST_V2_TASKS_URL, task.id);

request::post_todoist_rest(config, url, body, true).await?;
request::post_todoist_rest(config, url, body, spinner).await?;
// Does not pass back a task
Ok(String::from("✓"))
}
Expand All @@ -223,12 +225,13 @@ pub async fn update_task_content(
pub async fn update_task_description(
config: &Config,
task: &Task,
new_name: String,
description: String,
spinner: bool,
) -> Result<String, Error> {
let body = json!({ "description": new_name });
let body = json!({ "description": description});
let url = format!("{}{}", REST_V2_TASKS_URL, task.id);

request::post_todoist_rest(config, url, body, true).await?;
request::post_todoist_rest(config, url, body, spinner).await?;
// Does not pass back a task
Ok(String::from("✓"))
}
Expand Down Expand Up @@ -493,7 +496,7 @@ mod tests {

let config = test::fixtures::config().await.mock_url(server.url());

let response = update_task_priority(&config, &task, &Priority::High).await;
let response = update_task_priority(&config, &task, &Priority::High, true).await;
mock.assert();
assert_eq!(response, Ok(String::from("✓")));
}
Expand Down

0 comments on commit 6a1c365

Please sign in to comment.