-
Notifications
You must be signed in to change notification settings - Fork 0
/
interfaces.vala
441 lines (378 loc) · 15.1 KB
/
interfaces.vala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
using Utils;
using Tasks;
using TaskCollections;
using TaskManagerNS;
namespace UserInterfaces {
enum Responses {
NONE,
SHOW_COLLECTIONS,
ADD_COLLECTION,
REMOVE_COLLECTION,
EDIT_COLLECTION,
ADD_TASK,
REMOVE_TASK,
EDIT_TASK,
ABOUT,
QUIT
}
interface UserInterface : GLib.Object {
public abstract void run ();
public abstract TaskManager tasks_manager {
get;
set;
}
}
class CommandLineInterface : GLib.Object, UserInterface {
private TaskManager _manager;
public TaskManager tasks_manager {
get { return _manager; }
set { _manager = value; }
}
public CommandLineInterface () {
_manager = TaskManager.get_instance ();
}
/* Main method, that interacts with the user */
public void run () {
show_welcome();
println ();
Responses response = main_menu ();
while (response != Responses.QUIT) {
println ();
switch (response) {
case Responses.SHOW_COLLECTIONS: response = show_collections ();
break;
case Responses.ADD_COLLECTION: response = add_collection ();
break;
case Responses.REMOVE_COLLECTION: response = remove_collection ();
break;
case Responses.EDIT_COLLECTION: response = edit_collection ();
break;
case Responses.ADD_TASK: response = add_task ();
break;
case Responses.REMOVE_TASK: response = remove_task ();
break;
case Responses.EDIT_TASK: response = edit_task ();
break;
case Responses.ABOUT: response = show_about ();
break;
case Responses.NONE: response = main_menu ();
break;
default: assert_not_reached ();
}
}
show_good_bye();
}
private void show_welcome () {
println ("Welcome to DoNotForget!");
}
private void show_good_bye () {
println ("Thanks for using DoNotForget!");
}
private Responses show_about () {
println ("This application was created with the intention to");
println ("demonstrate Vala programming language features and");
println ("techniques.");
println ();
println ("Author: Vratislav Podzimek");
println ("email: vratislav.podzimek@gmail.com");
println ("Please email me bug report, if you encounter bug.");
println ();
raw_input ("Hit ENTER to get back to main menu");
return Responses.NONE;
}
private Responses main_menu () {
println ("MAIN MENU:");
println ("S -- show task collections");
println ("A -- add task collection");
println ("R -- remove task collection");
println ("E -- edit task collection");
println ("a -- add task");
println ("r -- remove task");
println ("e -- edit task");
println ("---------------------------");
println ("? -- show info about this program");
println ("q -- quit DoNotForget");
println ("===========================");
var input = raw_input ("What would you like to do now?");
if ((input.length < 1) || (!(input[0].to_string() in "SAREare?q")))
return Responses.NONE;
switch (input[0]) {
case 'S': return Responses.SHOW_COLLECTIONS;
case 'A': return Responses.ADD_COLLECTION;
case 'R': return Responses.REMOVE_COLLECTION;
case 'E': return Responses.EDIT_COLLECTION;
case 'a': return Responses.ADD_TASK;
case 'r': return Responses.REMOVE_TASK;
case 'e': return Responses.EDIT_TASK;
case '?': return Responses.ABOUT;
case 'q': return Responses.QUIT;
}
return Responses.NONE;
}
private Responses show_collections () {
foreach (var collection in _manager.collections) {
println ("Collection '%s':".printf (collection.title));
foreach (var task in collection)
println (@"$task");
println ();
}
raw_input ("Hit ENTER to get back to main menu");
return Responses.NONE;
}
private Responses add_collection () {
var input = "";
while (input == "") {
input = raw_input ("Please enter the name of the new " +
"collection:");
}
var collection = new GrowableTaskCollection (input);
try {
_manager.add_collection (collection);
}
catch (TaskManagerError e) {
stderr.printf ("Failed to add collection '%s': %s\n",
collection.title, e.message);
}
input = raw_input("Do you want to add another collection? [y/N]");
if (positive_answer (input))
return Responses.ADD_COLLECTION;
else
return Responses.NONE;
}
private void show_available_collections () {
println ("Available collections:");
foreach (var collection in _manager.collections)
println (collection.title);
println ("===================================");
}
private string get_existing_collection_title (string prep = "") {
show_available_collections ();
string input = "";
bool existing_collection = false;
string question;
if (prep != "")
question = @"$prep which collection?";
else
question = "Which collection?";
while (!existing_collection) {
input = raw_input (question);
existing_collection = _manager.has_collection (input);
}
return input;
}
private Responses remove_collection () {
if (_manager.collections.size == 0) {
println ("You need to add some collection first");
return Responses.ADD_COLLECTION;
}
var title = get_existing_collection_title ();
_manager.remove_collection_by_title (title);
var input = raw_input ("Do you want to remove another collection? [y/N]");
if (positive_answer (input))
return Responses.REMOVE_COLLECTION;
else
return Responses.NONE;
}
private Responses edit_collection () {
if (_manager.collections.size == 0) {
println ("You need to add some collection first");
return Responses.ADD_COLLECTION;
}
var title = get_existing_collection_title ();
TaskCollection? collection = _manager[title];
string input = "";
do {
input = raw_input ("Please enter the collection title:");
}
while (input == "");
//FIXME: TaskManager needs a way to handle collections' titles
// changes
_manager.remove_collection_by_title (collection.title);
collection.title = input;
try {
_manager.add_collection (collection);
}
catch (TaskManagerError e) {
stderr.printf ("Failed to add collection '%s'",
collection.title);
}
input = raw_input ("Do you want to edit another collection? [y/N]");
if (positive_answer (input))
return Responses.EDIT_COLLECTION;
else
return Responses.NONE;
}
private Responses add_task (TaskCollection? collection_arg = null) {
if (_manager.collections.size == 0) {
println ("You need to add collection first");
return Responses.ADD_COLLECTION;
}
string title;
TaskCollection? collection;
if (collection_arg == null) {
title = get_existing_collection_title ("To");
collection = _manager[title];
if (collection == null) {
return Responses.NONE;
}
}
else {
title = collection_arg.title;
collection = collection_arg;
}
string task_desc = "";
while (task_desc == "")
task_desc = raw_input ("Enter the task description:");
var completed_string = raw_input ("Is the task completed? " +
"([y/N] or percentage of completion)");
int completion = 0;
Task new_task;
if (positive_answer (completed_string))
new_task = new Task.with_state (task_desc, true);
else {
try {
completion = checked_int_parse (completed_string);
new_task = new LongTimeTask.with_progress (task_desc,
completion > 0 ? completion : 0);
}
catch (ParsingError e) {
new_task = new Task (task_desc);
}
}
try {
collection.add_task (new_task);
}
catch (TaskCollectionError e) {
stderr.printf (@"Cannot add task '$new_task': %s", e.message);
}
var input = raw_input ("Do you want to add another task? [y/N]");
if (positive_answer (input)) {
input = raw_input ("To the same collection? [y/N]");
if (positive_answer (input)) {
println ();
return add_task (collection);
}
else return Responses.ADD_TASK;
}
else
return Responses.NONE;
}
private Task? get_task_choice (TaskCollection collection) {
int num = 1;
if (collection.number_of_tasks == 0) {
return null;
}
foreach (var task in collection)
println ("%2d) %s".printf (num++, task.description));
num = 0;
var task_number = "";
while (num <= 0) {
task_number = raw_input ("Enter the task number:");
try {
num = checked_int_parse (task_number);
}
catch (ParsingError e) {
continue;
}
}
Task? task = collection[num - 1];
return task;
}
private Responses remove_task (TaskCollection? collection_arg = null) {
if (_manager.collections.size == 0) {
println ("You need to add collection first");
return Responses.ADD_COLLECTION;
}
string title;
TaskCollection? collection;
if (collection_arg == null) {
title = get_existing_collection_title ("From");
collection = _manager[title];
if (collection == null) {
return Responses.NONE;
}
}
else {
title = collection_arg.title;
collection = collection_arg;
}
var task = get_task_choice (collection);
if (task != null)
collection.remove_task (task);
else {
println ("You need to add some task first");
return add_task (collection);
}
var input = raw_input ("Do you want to remove another task? [y/N]");
if (positive_answer (input)) {
input = raw_input ("From the same collection? [y/N]");
if (positive_answer (input)) {
println ();
return remove_task (collection);
}
else return Responses.REMOVE_TASK;
}
else
return Responses.NONE;
}
private Responses edit_task (TaskCollection? collection_arg = null) {
if (_manager.collections.size == 0) {
println ("You need to add some collection first");
return Responses.ADD_COLLECTION;
}
string title;
TaskCollection? collection;
if (collection_arg == null) {
title = get_existing_collection_title ("From");
collection = _manager[title];
if (collection == null) {
return Responses.NONE;
}
}
else {
title = collection_arg.title;
collection = collection_arg;
}
var task = get_task_choice (collection);
if (task == null) {
println ("You need to add some task first");
return add_task (collection);
}
println (@"$task");
var input = raw_input ("Enter new task description or just hit " +
"ENTER to use the old one:");
if (input != "")
task.description = input;
if (task is LongTimeTask) {
var valid_input = false;
while (!valid_input) {
input = raw_input ("Enter updated task progress:");
try {
var num = checked_int_parse (input);
LongTimeTask task2 = task as LongTimeTask;
task2.progress = num >= 0 ? num : 0;
valid_input = true;
}
catch (ParsingError e) {
continue;
}
}
}
else {
input = raw_input ("Is the collection now done? [y/N]");
task.done = positive_answer (input);
}
input = raw_input ("Do you want to edit another task? [y/N]");
if (positive_answer (input)) {
input = raw_input ("From the same collection? [y/N]");
if (positive_answer (input)) {
println ();
return edit_task (collection);
}
else return Responses.EDIT_TASK;
}
else
return Responses.NONE;
}
}
}