2021-02-25 00:50:49 +01:00
use chrono ::{ Utc , DateTime } ;
2021-02-24 17:38:08 +01:00
use serde ::{ Deserialize , Serialize } ;
2021-02-28 11:54:31 +01:00
use crate ::item ::ItemId ;
2021-02-19 07:58:53 +01:00
2021-02-18 12:02:04 +01:00
/// A to-do task
2021-02-24 17:38:08 +01:00
#[ derive(Clone, Debug, PartialEq, Serialize, Deserialize) ]
2021-02-18 12:02:04 +01:00
pub struct Task {
2021-02-28 00:16:55 +01:00
/// The task unique ID, that will never change
2021-02-28 11:54:31 +01:00
id : ItemId ,
2021-02-28 00:16:55 +01:00
/// The last modification date of this task
2021-02-25 00:50:49 +01:00
last_modified : DateTime < Utc > ,
2021-02-28 00:16:55 +01:00
/// The display name of the task
name : String ,
/// The completion of the task
2021-02-20 00:10:05 +01:00
completed : bool ,
2021-02-18 12:02:04 +01:00
}
impl Task {
2021-02-25 00:50:49 +01:00
/// Create a new Task
2021-03-21 23:54:33 +01:00
pub fn new ( name : String , id : ItemId , last_modified : DateTime < Utc > ) -> Self {
2021-02-25 00:50:49 +01:00
Self {
2021-03-21 23:54:33 +01:00
id ,
2021-02-28 00:16:55 +01:00
name ,
last_modified ,
completed : false ,
2021-02-25 00:50:49 +01:00
}
}
2021-02-28 11:54:31 +01:00
pub fn id ( & self ) -> & ItemId { & self . id }
2021-02-20 00:10:05 +01:00
pub fn name ( & self ) -> & str { & self . name }
2021-02-18 12:02:04 +01:00
pub fn completed ( & self ) -> bool { self . completed }
2021-02-25 00:50:49 +01:00
pub fn last_modified ( & self ) -> DateTime < Utc > { self . last_modified }
fn update_last_modified ( & mut self ) {
self . last_modified = Utc ::now ( ) ;
}
2021-02-18 12:02:04 +01:00
2021-02-25 00:50:49 +01:00
/// Rename a task.
/// This updates its "last modified" field
pub fn set_name ( & mut self , new_name : String ) {
self . update_last_modified ( ) ;
self . name = new_name ;
}
pub fn set_completed ( & mut self , new_value : bool ) {
2021-02-18 12:02:04 +01:00
// TODO: either require a reference to the DataSource, so that it is aware
// or change a flag here, and the DataSource will be able to check the flags of all its content (but then the Calendar should only give a reference/Arc, not a clone)
2021-02-25 00:50:49 +01:00
self . update_last_modified ( ) ;
self . completed = new_value ;
2021-02-18 12:02:04 +01:00
}
2021-02-25 00:50:49 +01:00
}