22
33import 'dart:convert' ;
44import 'package:flutter/material.dart' ;
5+ import 'package:flutter_test/flutter_test.dart' ;
56import 'package:http/http.dart' as http;
67import 'package:sqflite/sqflite.dart' ;
78import 'package:path/path.dart' ;
@@ -19,38 +20,56 @@ class Tasks {
1920 final String ? end;
2021 final String entry;
2122 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});
3638
3739 factory Tasks .fromJson (Map <String , dynamic > json) {
3840 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 (' ' ));
5169 }
5270
5371 Map <String , dynamic > toJson () {
72+ debugPrint ("TAGS: $tags " );
5473 return {
5574 'id' : id,
5675 'description' : description,
@@ -63,6 +82,24 @@ class Tasks {
6382 'end' : end,
6483 'entry' : entry,
6584 'modified' : modified,
85+ 'tags' : tags
86+ };
87+ }
88+
89+ Map <String , dynamic > toDbJson () {
90+ return {
91+ 'id' : id,
92+ 'description' : description,
93+ 'project' : project,
94+ 'status' : status,
95+ 'uuid' : uuid,
96+ 'urgency' : urgency,
97+ 'priority' : priority,
98+ 'due' : due,
99+ 'end' : end,
100+ 'entry' : entry,
101+ 'modified' : modified,
102+ 'tags' : tags != null ? tags? .join (" " ) : ""
66103 };
67104 }
68105}
@@ -100,8 +137,8 @@ Future<void> updateTasksInDatabase(List<Tasks> tasks) async {
100137 //add tasks without UUID to the server and delete them from database
101138 for (var task in tasksWithoutUUID) {
102139 try {
103- await addTaskAndDeleteFromDatabase (
104- 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 ! : [] );
105142 } catch (e) {
106143 debugPrint ('Failed to add task without UUID to server: $e ' );
107144 }
@@ -221,14 +258,15 @@ Future<void> completeTask(String email, String taskUuid) async {
221258 }
222259}
223260
224- Future <void > addTaskAndDeleteFromDatabase (
225- String description , String project, String due, String priority ) async {
261+ Future <void > addTaskAndDeleteFromDatabase (String description, String project,
262+ String due , String priority, List < dynamic > tags ) async {
226263 String apiUrl = '$baseUrl /add-task' ;
227264 var c = await CredentialsStorage .getClientId ();
228265 var e = await CredentialsStorage .getEncryptionSecret ();
266+ debugPrint ("Database Adding Tags $tags $description " );
229267 debugPrint (c);
230268 debugPrint (e);
231- await http.post (
269+ var res = await http.post (
232270 Uri .parse (apiUrl),
233271 headers: {
234272 'Content-Type' : 'text/plain' ,
@@ -241,9 +279,10 @@ Future<void> addTaskAndDeleteFromDatabase(
241279 'project' : project,
242280 'due' : due,
243281 'priority' : priority,
282+ 'tags' : tags
244283 }),
245284 );
246-
285+ debugPrint ( 'Database res ${ res . body }' );
247286 var taskDatabase = TaskDatabase ();
248287 await taskDatabase.open ();
249288 await taskDatabase._database! .delete (
@@ -302,9 +341,11 @@ class TaskDatabase {
302341 var databasesPath = await getDatabasesPath ();
303342 String path = join (databasesPath, 'tasks.db' );
304343
305- _database = await openDatabase (path, version: 1 ,
344+ _database = await openDatabase (path,
345+ version: 1 ,
346+ onOpen: (db) async => await addTagsColumnIfNeeded (db),
306347 onCreate: (Database db, version) async {
307- await db.execute ('''
348+ await db.execute ('''
308349 CREATE TABLE Tasks (
309350 uuid TEXT PRIMARY KEY,
310351 id INTEGER,
@@ -319,7 +360,16 @@ class TaskDatabase {
319360 modified TEXT
320361 )
321362 ''' );
322- });
363+ });
364+ }
365+
366+ Future <void > addTagsColumnIfNeeded (Database db) async {
367+ try {
368+ await db.rawQuery ("SELECT tags FROM Tasks LIMIT 0" );
369+ } catch (e) {
370+ await db.execute ("ALTER TABLE Tasks ADD COLUMN tags TEXT" );
371+ debugPrint ("Added Column tags" );
372+ }
323373 }
324374
325375 Future <void > ensureDatabaseIsOpen () async {
@@ -332,20 +382,21 @@ class TaskDatabase {
332382 await ensureDatabaseIsOpen ();
333383
334384 final List <Map <String , dynamic >> maps = await _database! .query ('Tasks' );
385+ debugPrint ("Database fetch ${maps .last }" );
335386 var a = List .generate (maps.length, (i) {
336387 return Tasks (
337- id: maps[i]['id' ],
338- description: maps[i]['description' ],
339- project: maps[i]['project' ],
340- status: maps[i]['status' ],
341- uuid: maps[i]['uuid' ],
342- urgency: maps[i]['urgency' ],
343- priority: maps[i]['priority' ],
344- due: maps[i]['due' ],
345- end: maps[i]['end' ],
346- entry: maps[i]['entry' ],
347- modified: maps[i]['modified' ],
348- );
388+ id: maps[i]['id' ],
389+ description: maps[i]['description' ],
390+ project: maps[i]['project' ],
391+ status: maps[i]['status' ],
392+ uuid: maps[i]['uuid' ],
393+ urgency: maps[i]['urgency' ],
394+ priority: maps[i]['priority' ],
395+ due: maps[i]['due' ],
396+ end: maps[i]['end' ],
397+ entry: maps[i]['entry' ],
398+ modified: maps[i]['modified' ],
399+ tags : maps[i][ 'tags' ] != null ? maps[i][ 'tags' ]. split ( ' ' ) : [] );
349400 });
350401 // debugPrint('Tasks from db');
351402 // debugPrint(a.toString());
@@ -374,20 +425,21 @@ class TaskDatabase {
374425
375426 Future <void > insertTask (Tasks task) async {
376427 await ensureDatabaseIsOpen ();
377-
378- await _database! .insert (
428+ debugPrint ( "Database Insert" );
429+ var dbi = await _database! .insert (
379430 'Tasks' ,
380- task.toJson (),
431+ task.toDbJson (),
381432 conflictAlgorithm: ConflictAlgorithm .replace,
382433 );
434+ debugPrint ("Database Insert ${task .toDbJson ()} $dbi " );
383435 }
384436
385437 Future <void > updateTask (Tasks task) async {
386438 await ensureDatabaseIsOpen ();
387439
388440 await _database! .update (
389441 'Tasks' ,
390- task.toJson (),
442+ task.toDbJson (),
391443 where: 'uuid = ?' ,
392444 whereArgs: [task.uuid],
393445 );
@@ -403,7 +455,7 @@ class TaskDatabase {
403455 );
404456
405457 if (maps.isNotEmpty) {
406- return Tasks .fromJson (maps.first);
458+ return Tasks .fromDbJson (maps.first);
407459 } else {
408460 return null ;
409461 }
@@ -470,7 +522,7 @@ class TaskDatabase {
470522 );
471523
472524 return List .generate (maps.length, (i) {
473- return Tasks .fromJson (maps[i]);
525+ return Tasks .fromDbJson (maps[i]);
474526 });
475527 }
476528
@@ -480,21 +532,21 @@ class TaskDatabase {
480532 where: 'project = ?' ,
481533 whereArgs: [project],
482534 );
483-
535+ debugPrint ( "DB Stored for $ maps " );
484536 return List .generate (maps.length, (i) {
485537 return Tasks (
486- uuid: maps[i]['uuid' ],
487- id: maps[i]['id' ],
488- description: maps[i]['description' ],
489- project: maps[i]['project' ],
490- status: maps[i]['status' ],
491- urgency: maps[i]['urgency' ],
492- priority: maps[i]['priority' ],
493- due: maps[i]['due' ],
494- end: maps[i]['end' ],
495- entry: maps[i]['entry' ],
496- modified: maps[i]['modified' ],
497- );
538+ uuid: maps[i]['uuid' ],
539+ id: maps[i]['id' ],
540+ description: maps[i]['description' ],
541+ project: maps[i]['project' ],
542+ status: maps[i]['status' ],
543+ urgency: maps[i]['urgency' ],
544+ priority: maps[i]['priority' ],
545+ due: maps[i]['due' ],
546+ end: maps[i]['end' ],
547+ entry: maps[i]['entry' ],
548+ modified: maps[i]['modified' ],
549+ tags : maps[i][ 'tags' ]. toString (). split ( ' ' ) );
498550 });
499551 }
500552
@@ -517,7 +569,7 @@ class TaskDatabase {
517569 whereArgs: ['%$query %' , '%$query %' ],
518570 );
519571 return List .generate (maps.length, (i) {
520- return Tasks .fromJson (maps[i]);
572+ return Tasks .fromDbJson (maps[i]);
521573 });
522574 }
523575
0 commit comments