2
2
3
3
import 'dart:convert' ;
4
4
import 'package:flutter/material.dart' ;
5
+ import 'package:flutter_test/flutter_test.dart' ;
5
6
import 'package:http/http.dart' as http;
6
7
import 'package:sqflite/sqflite.dart' ;
7
8
import 'package:path/path.dart' ;
@@ -19,38 +20,73 @@ class Tasks {
19
20
final String ? end;
20
21
final String entry;
21
22
final String ? modified;
22
-
23
- Tasks ({
24
- required this .id,
25
- required this .description,
26
- required this .project,
27
- required this .status,
28
- required this .uuid,
29
- required this .urgency,
30
- required this .priority,
31
- required this .due,
32
- required this .end,
33
- required this .entry,
34
- required this .modified,
35
- });
23
+ final List <dynamic >? tags;
24
+
25
+ Tasks (
26
+ {required this .id,
27
+ required this .description,
28
+ required this .project,
29
+ required this .status,
30
+ required this .uuid,
31
+ required this .urgency,
32
+ required this .priority,
33
+ required this .due,
34
+ required this .end,
35
+ required this .entry,
36
+ required this .modified,
37
+ required this .tags});
36
38
37
39
factory Tasks .fromJson (Map <String , dynamic > json) {
38
40
return Tasks (
39
- id: json['id' ],
40
- description: json['description' ],
41
- project: json['project' ],
42
- status: json['status' ],
43
- uuid: json['uuid' ],
44
- urgency: json['urgency' ].toDouble (),
45
- priority: json['priority' ],
46
- due: json['due' ],
47
- end: json['end' ],
48
- entry: json['entry' ],
49
- modified: json['modified' ],
50
- );
41
+ id: json['id' ],
42
+ description: json['description' ],
43
+ project: json['project' ],
44
+ status: json['status' ],
45
+ uuid: json['uuid' ],
46
+ urgency: json['urgency' ].toDouble (),
47
+ priority: json['priority' ],
48
+ due: json['due' ],
49
+ end: json['end' ],
50
+ entry: json['entry' ],
51
+ modified: json['modified' ],
52
+ tags: json['tags' ]);
53
+ }
54
+ factory Tasks .fromDbJson (Map <String , dynamic > json) {
55
+ debugPrint ("FROM: $json " );
56
+ return Tasks (
57
+ id: json['id' ],
58
+ description: json['description' ],
59
+ project: json['project' ],
60
+ status: json['status' ],
61
+ uuid: json['uuid' ],
62
+ urgency: json['urgency' ].toDouble (),
63
+ priority: json['priority' ],
64
+ due: json['due' ],
65
+ end: json['end' ],
66
+ entry: json['entry' ],
67
+ modified: json['modified' ],
68
+ tags: json['tags' ].toString ().split (' ' ));
51
69
}
52
70
53
71
Map <String , dynamic > toJson () {
72
+ debugPrint ("TAGS: $tags " );
73
+ return {
74
+ 'id' : id,
75
+ 'description' : description,
76
+ 'project' : project,
77
+ 'status' : status,
78
+ 'uuid' : uuid,
79
+ 'urgency' : urgency,
80
+ 'priority' : priority,
81
+ 'due' : due,
82
+ 'end' : end,
83
+ 'entry' : entry,
84
+ 'modified' : modified,
85
+ 'tags' : tags
86
+ };
87
+ }
88
+
89
+ Map <String , dynamic > toDbJson () {
54
90
return {
55
91
'id' : id,
56
92
'description' : description,
@@ -63,9 +99,11 @@ class Tasks {
63
99
'end' : end,
64
100
'entry' : entry,
65
101
'modified' : modified,
102
+ 'tags' : tags != null ? tags? .join (" " ) : ""
66
103
};
67
104
}
68
105
}
106
+
69
107
String origin = 'http://localhost:8080' ;
70
108
71
109
Future <List <Tasks >> fetchTasks (String uuid, String encryptionSecret) async {
@@ -99,8 +137,8 @@ Future<void> updateTasksInDatabase(List<Tasks> tasks) async {
99
137
//add tasks without UUID to the server and delete them from database
100
138
for (var task in tasksWithoutUUID) {
101
139
try {
102
- await addTaskAndDeleteFromDatabase (
103
- task.description , task.project ! , task.due ! , task.priority ! );
140
+ await addTaskAndDeleteFromDatabase (task.description, task.project ! ,
141
+ task.due ! , task.priority ! , task.tags != null ? task.tags ! : [] );
104
142
} catch (e) {
105
143
debugPrint ('Failed to add task without UUID to server: $e ' );
106
144
}
@@ -222,15 +260,16 @@ Future<void> completeTask(String email, String taskUuid) async {
222
260
}
223
261
}
224
262
225
- Future <void > addTaskAndDeleteFromDatabase (
226
- String description , String project, String due, String priority ) async {
263
+ Future <void > addTaskAndDeleteFromDatabase (String description, String project,
264
+ String due , String priority, List < dynamic > tags ) async {
227
265
var baseUrl = await CredentialsStorage .getApiUrl ();
228
266
String apiUrl = '$baseUrl /add-task' ;
229
267
var c = await CredentialsStorage .getClientId ();
230
268
var e = await CredentialsStorage .getEncryptionSecret ();
269
+ debugPrint ("Database Adding Tags $tags $description " );
231
270
debugPrint (c);
232
271
debugPrint (e);
233
- await http.post (
272
+ var res = await http.post (
234
273
Uri .parse (apiUrl),
235
274
headers: {
236
275
'Content-Type' : 'text/plain' ,
@@ -243,9 +282,10 @@ Future<void> addTaskAndDeleteFromDatabase(
243
282
'project' : project,
244
283
'due' : due,
245
284
'priority' : priority,
285
+ 'tags' : tags
246
286
}),
247
287
);
248
-
288
+ debugPrint ( 'Database res ${ res . body }' );
249
289
var taskDatabase = TaskDatabase ();
250
290
await taskDatabase.open ();
251
291
await taskDatabase._database! .delete (
@@ -305,9 +345,11 @@ class TaskDatabase {
305
345
var databasesPath = await getDatabasesPath ();
306
346
String path = join (databasesPath, 'tasks.db' );
307
347
308
- _database = await openDatabase (path, version: 1 ,
348
+ _database = await openDatabase (path,
349
+ version: 1 ,
350
+ onOpen: (db) async => await addTagsColumnIfNeeded (db),
309
351
onCreate: (Database db, version) async {
310
- await db.execute ('''
352
+ await db.execute ('''
311
353
CREATE TABLE Tasks (
312
354
uuid TEXT PRIMARY KEY,
313
355
id INTEGER,
@@ -322,7 +364,16 @@ class TaskDatabase {
322
364
modified TEXT
323
365
)
324
366
''' );
325
- });
367
+ });
368
+ }
369
+
370
+ Future <void > addTagsColumnIfNeeded (Database db) async {
371
+ try {
372
+ await db.rawQuery ("SELECT tags FROM Tasks LIMIT 0" );
373
+ } catch (e) {
374
+ await db.execute ("ALTER TABLE Tasks ADD COLUMN tags TEXT" );
375
+ debugPrint ("Added Column tags" );
376
+ }
326
377
}
327
378
328
379
Future <void > ensureDatabaseIsOpen () async {
@@ -335,20 +386,21 @@ class TaskDatabase {
335
386
await ensureDatabaseIsOpen ();
336
387
337
388
final List <Map <String , dynamic >> maps = await _database! .query ('Tasks' );
389
+ debugPrint ("Database fetch ${maps .last }" );
338
390
var a = List .generate (maps.length, (i) {
339
391
return Tasks (
340
- id: maps[i]['id' ],
341
- description: maps[i]['description' ],
342
- project: maps[i]['project' ],
343
- status: maps[i]['status' ],
344
- uuid: maps[i]['uuid' ],
345
- urgency: maps[i]['urgency' ],
346
- priority: maps[i]['priority' ],
347
- due: maps[i]['due' ],
348
- end: maps[i]['end' ],
349
- entry: maps[i]['entry' ],
350
- modified: maps[i]['modified' ],
351
- );
392
+ id: maps[i]['id' ],
393
+ description: maps[i]['description' ],
394
+ project: maps[i]['project' ],
395
+ status: maps[i]['status' ],
396
+ uuid: maps[i]['uuid' ],
397
+ urgency: maps[i]['urgency' ],
398
+ priority: maps[i]['priority' ],
399
+ due: maps[i]['due' ],
400
+ end: maps[i]['end' ],
401
+ entry: maps[i]['entry' ],
402
+ modified: maps[i]['modified' ],
403
+ tags : maps[i][ 'tags' ] != null ? maps[i][ 'tags' ]. split ( ' ' ) : [] );
352
404
});
353
405
// debugPrint('Tasks from db');
354
406
// debugPrint(a.toString());
@@ -377,20 +429,21 @@ class TaskDatabase {
377
429
378
430
Future <void > insertTask (Tasks task) async {
379
431
await ensureDatabaseIsOpen ();
380
-
381
- await _database! .insert (
432
+ debugPrint ( "Database Insert" );
433
+ var dbi = await _database! .insert (
382
434
'Tasks' ,
383
- task.toJson (),
435
+ task.toDbJson (),
384
436
conflictAlgorithm: ConflictAlgorithm .replace,
385
437
);
438
+ debugPrint ("Database Insert ${task .toDbJson ()} $dbi " );
386
439
}
387
440
388
441
Future <void > updateTask (Tasks task) async {
389
442
await ensureDatabaseIsOpen ();
390
443
391
444
await _database! .update (
392
445
'Tasks' ,
393
- task.toJson (),
446
+ task.toDbJson (),
394
447
where: 'uuid = ?' ,
395
448
whereArgs: [task.uuid],
396
449
);
@@ -406,7 +459,7 @@ class TaskDatabase {
406
459
);
407
460
408
461
if (maps.isNotEmpty) {
409
- return Tasks .fromJson (maps.first);
462
+ return Tasks .fromDbJson (maps.first);
410
463
} else {
411
464
return null ;
412
465
}
@@ -473,7 +526,7 @@ class TaskDatabase {
473
526
);
474
527
475
528
return List .generate (maps.length, (i) {
476
- return Tasks .fromJson (maps[i]);
529
+ return Tasks .fromDbJson (maps[i]);
477
530
});
478
531
}
479
532
@@ -483,21 +536,21 @@ class TaskDatabase {
483
536
where: 'project = ?' ,
484
537
whereArgs: [project],
485
538
);
486
-
539
+ debugPrint ( "DB Stored for $ maps " );
487
540
return List .generate (maps.length, (i) {
488
541
return Tasks (
489
- uuid: maps[i]['uuid' ],
490
- id: maps[i]['id' ],
491
- description: maps[i]['description' ],
492
- project: maps[i]['project' ],
493
- status: maps[i]['status' ],
494
- urgency: maps[i]['urgency' ],
495
- priority: maps[i]['priority' ],
496
- due: maps[i]['due' ],
497
- end: maps[i]['end' ],
498
- entry: maps[i]['entry' ],
499
- modified: maps[i]['modified' ],
500
- );
542
+ uuid: maps[i]['uuid' ],
543
+ id: maps[i]['id' ],
544
+ description: maps[i]['description' ],
545
+ project: maps[i]['project' ],
546
+ status: maps[i]['status' ],
547
+ urgency: maps[i]['urgency' ],
548
+ priority: maps[i]['priority' ],
549
+ due: maps[i]['due' ],
550
+ end: maps[i]['end' ],
551
+ entry: maps[i]['entry' ],
552
+ modified: maps[i]['modified' ],
553
+ tags : maps[i][ 'tags' ]. toString (). split ( ' ' ) );
501
554
});
502
555
}
503
556
@@ -520,7 +573,7 @@ class TaskDatabase {
520
573
whereArgs: ['%$query %' , '%$query %' ],
521
574
);
522
575
return List .generate (maps.length, (i) {
523
- return Tasks .fromJson (maps[i]);
576
+ return Tasks .fromDbJson (maps[i]);
524
577
});
525
578
}
526
579
0 commit comments