Skip to content

Commit

Permalink
feat: added a due date to task (DarthBenro008#11)
Browse files Browse the repository at this point in the history
- created a simple Datetime selector in the task add method
- Set the initial date field to today date
- Handled the Timezone offset based on local date
- Prepared time implementation (google api doesn't support it yet)
  • Loading branch information
Thibault committed Aug 28, 2022
1 parent c74e5e6 commit 88c47a2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
34 changes: 32 additions & 2 deletions src/handlers/task_handler.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use std::process::exit;

use crate::models::tasks::Tasks;
use crate::printer::{print_error, print_success, print_task_table, print_warning};
use crate::service::google_api::GoogleApiClient;
use crate::service::google_tasks::ApiTasks;
use anyhow;
use chrono::{DateTime, Local};
use console::Term;
use dialoguer::{theme::ColorfulTheme, Input, Select};

Expand Down Expand Up @@ -47,7 +50,7 @@ impl TaskManager {
} else {
String::from("needsAction")
};
Tasks::new(None, title, notes.unwrap_or_else(||String::from("")), status)
Tasks::new(None, title, notes.unwrap_or_else(||String::from("")), status, "".to_string())
}

fn create_task_with_prompts (&self, notes: Option<String>, done: bool) -> anyhow::Result<Tasks> {
Expand All @@ -65,6 +68,33 @@ impl TaskManager {
)?;

let items = vec!["No", "Yes"];
let add_due = Select::with_theme(&ColorfulTheme::default())
.with_prompt("Add a due date?")
.items(&items)
.default(0)
.interact_on_opt(&Term::stderr())?
.unwrap();

let due : String = if add_due!=1 {"".to_string()} else {
let today = Local::today();
let user_input = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Due date")
// We initialize the field with today's date
.with_initial_text(today.format("%Y-%m-%d").to_string())
.allow_empty(true)
.interact_text()
.unwrap();
// We complete the user's input with the time (not used by google API) and the local timezone offset
match DateTime::parse_from_str(&[user_input,"00:00:00".to_string(),today.offset().to_string()].join(" "), "%Y-%m-%d %H:%M:%S %z") {
Ok(date) => {
date.to_rfc3339_opts(chrono::SecondsFormat::Millis, false)},
Err(_) => {println!("Provided date is not valid, abording..."); exit(1);},

}
};



let completed = if done { 1_usize } else {
Select::with_theme(&ColorfulTheme::default())
.with_prompt("Is the task completed?")
Expand All @@ -80,7 +110,7 @@ impl TaskManager {
String::from("needsAction")
};

Ok(Tasks::new(None, title, notes, status))
Ok(Tasks::new(None, title, notes, status, due))
}

pub fn show_task(&self, pos: usize) -> anyhow::Result<()> {
Expand Down
4 changes: 2 additions & 2 deletions src/models/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub struct Tasks {
}

impl Tasks {
pub fn new(id: Option<String>, title: String, notes: String, status: String) -> Tasks {
pub fn new(id: Option<String>, title: String, notes: String, status: String, due: String) -> Tasks {
Tasks {
kind: "".to_string(),
id,
Expand All @@ -45,7 +45,7 @@ impl Tasks {
position: None,
notes,
status,
due: String::from(""),
due,
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,13 @@ pub fn force_write(action: String) -> anyhow::Result<()> {
}

pub fn print_task_table(tasks: &[Tasks]) {
let mut table = Table::new();
table.set_format(*format::consts::FORMAT_NO_BORDER);
if tasks.is_empty() {
print_success("You have no tasks!".to_string());
return;
}

let mut table = Table::new();
table.set_format(*format::consts::FORMAT_NO_BORDER);
table.add_row(row![cb => "Index", "Title", "Status", "Notes", "Due"]);
let mut order = 1;
for task in tasks {
Expand Down
2 changes: 2 additions & 0 deletions src/service/google_tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ impl ApiTasks for TasksDatabase {
"".to_string(),
"".to_string(),
"".to_string(),
"".to_string()
))
}
fn fetch_all_tasks(
Expand Down Expand Up @@ -161,6 +162,7 @@ impl ApiTasks for TasksDatabase {
"".to_string(),
"".to_string(),
"".to_string(),
"".to_string()
))
}

Expand Down

0 comments on commit 88c47a2

Please sign in to comment.