11use crate :: app:: { App , InputField , InputMode } ;
22use crate :: date:: { TaskDate , DATE_FORMAT } ;
33use crate :: task:: Task ;
4- use anyhow:: { Context , Result } ;
4+ use anyhow:: { anyhow , Context , Result } ;
55
66pub trait Command {
7- fn execute ( & mut self , app : & mut App ) -> Result < ( ) > ;
7+ fn execute ( & self , app : & mut App ) -> Result < ( ) > ;
88}
99
10- pub struct EnterEditModeCommand ;
10+ /// Enter in add command input mode
11+ pub struct EnterAddModeCommand ;
1112
12- impl Command for EnterEditModeCommand {
13- fn execute ( & mut self , app : & mut App ) -> Result < ( ) > {
13+ impl Command for EnterAddModeCommand {
14+ fn execute ( & self , app : & mut App ) -> Result < ( ) > {
1415 app. input_mode = InputMode :: Adding ;
1516 app. input_field = InputField :: Title ;
1617 Ok ( ( ) )
@@ -21,8 +22,11 @@ impl Command for EnterEditModeCommand {
2122pub struct AddTaskCommand ;
2223
2324impl Command for AddTaskCommand {
24- fn execute ( & mut self , app : & mut App ) -> Result < ( ) > {
25+ fn execute ( & self , app : & mut App ) -> Result < ( ) > {
2526 let mut t = Task :: new ( ) ;
27+ if app. input_title . is_empty ( ) {
28+ return Err ( anyhow ! ( "You must insert at least a title for the task" ) ) ;
29+ }
2630 if !app. input_date . is_empty ( ) {
2731 t. date = TaskDate :: try_from ( app. input_date . drain ( ..) . collect :: < String > ( ) )
2832 . context ( "Invalid date format, use dd-mm-yyyy" ) ?;
@@ -35,45 +39,12 @@ impl Command for AddTaskCommand {
3539 }
3640}
3741
38- /// Toggle completed for selected task status
39- pub struct ToggleTaskStatusCommand ;
40-
41- impl Command for ToggleTaskStatusCommand {
42- fn execute ( & mut self , app : & mut App ) -> Result < ( ) > {
43- if let Some ( index) = app. task_list . state . selected ( ) {
44- let item = & mut app. task_list . items [ index] ;
45- item. completed = match item. completed {
46- true => false ,
47- false => true ,
48- } ;
49- let _ = app
50- . tasks_service
51- . toggle_task_status ( item. id , item. completed ) ;
52- } ;
53- Ok ( ( ) )
54- }
55- }
56-
57- /// Switch between priorities
58- pub struct ToggleItemPriorityCommand ;
59-
60- impl Command for ToggleItemPriorityCommand {
61- fn execute ( & mut self , app : & mut App ) -> Result < ( ) > {
62- if let Some ( index) = app. task_list . state . selected ( ) {
63- let item = & mut app. task_list . items [ index] ;
64- item. priority = item. priority . next ( ) ;
65- app. tasks_service . change_priority ( item. id , & item. priority ) ;
66- }
67- Ok ( ( ) )
68- }
69- }
70-
7142/// Start editing a task, move cursor to Title input field
7243/// and set InputMode equal to InputMode::EditingExisting
7344pub struct StartEditingExistingTaskCommand ;
7445
7546impl Command for StartEditingExistingTaskCommand {
76- fn execute ( & mut self , app : & mut App ) -> Result < ( ) > {
47+ fn execute ( & self , app : & mut App ) -> Result < ( ) > {
7748 if let Some ( index) = app. task_list . state . selected ( ) {
7849 app. input_title = app. task_list . items [ index] . title . clone ( ) ;
7950 app. input_description = app. task_list . items [ index] . description . clone ( ) ;
@@ -94,8 +65,11 @@ impl Command for StartEditingExistingTaskCommand {
9465pub struct FinishEditingExistingTaskCommand ;
9566
9667impl Command for FinishEditingExistingTaskCommand {
97- fn execute ( & mut self , app : & mut App ) -> Result < ( ) > {
68+ fn execute ( & self , app : & mut App ) -> Result < ( ) > {
9869 if let Some ( index) = app. task_list . state . selected ( ) {
70+ if app. input_title . is_empty ( ) {
71+ return Err ( anyhow ! ( "You must insert at least a title for the task" ) ) ;
72+ }
9973 if !app. input_date . is_empty ( ) {
10074 app. task_list . items [ index] . date =
10175 TaskDate :: try_from ( app. input_date . drain ( ..) . collect :: < String > ( ) )
@@ -111,10 +85,44 @@ impl Command for FinishEditingExistingTaskCommand {
11185 }
11286}
11387
88+
89+ /// Toggle completed for selected task status
90+ pub struct ToggleTaskStatusCommand ;
91+
92+ impl Command for ToggleTaskStatusCommand {
93+ fn execute ( & self , app : & mut App ) -> Result < ( ) > {
94+ if let Some ( index) = app. task_list . state . selected ( ) {
95+ let item = & mut app. task_list . items [ index] ;
96+ item. completed = match item. completed {
97+ true => false ,
98+ false => true ,
99+ } ;
100+ let _ = app
101+ . tasks_service
102+ . toggle_task_status ( item. id , item. completed ) ;
103+ } ;
104+ Ok ( ( ) )
105+ }
106+ }
107+
108+ /// Switch between priorities
109+ pub struct ToggleItemPriorityCommand ;
110+
111+ impl Command for ToggleItemPriorityCommand {
112+ fn execute ( & self , app : & mut App ) -> Result < ( ) > {
113+ if let Some ( index) = app. task_list . state . selected ( ) {
114+ let item = & mut app. task_list . items [ index] ;
115+ item. priority = item. priority . next ( ) ;
116+ app. tasks_service . change_priority ( item. id , & item. priority ) ;
117+ }
118+ Ok ( ( ) )
119+ }
120+ }
121+
114122pub struct DeleteTaskCommand ;
115123
116124impl Command for DeleteTaskCommand {
117- fn execute ( & mut self , app : & mut App ) -> Result < ( ) > {
125+ fn execute ( & self , app : & mut App ) -> Result < ( ) > {
118126 if let Some ( index) = app. task_list . state . selected ( ) {
119127 app. tasks_service . delete_task ( app. task_list . items [ index] . id ) ;
120128 app. task_list . items . remove ( index) ;
@@ -128,8 +136,8 @@ impl Command for DeleteTaskCommand {
128136pub struct StopEditingCommand ;
129137
130138impl Command for StopEditingCommand {
131- fn execute ( & mut self , app : & mut App ) -> Result < ( ) > {
132- app. input_mode = InputMode :: Normal ;
139+ fn execute ( & self , app : & mut App ) -> Result < ( ) > {
140+ app. input_mode = InputMode :: View ;
133141 app. input_title . clear ( ) ;
134142 app. input_description . clear ( ) ;
135143 app. input_date . clear ( ) ;
0 commit comments