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,73 @@ 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 " );
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 () {
5490 return {
5591 'id' : id,
5692 'description' : description,
@@ -63,9 +99,11 @@ class Tasks {
6399 'end' : end,
64100 'entry' : entry,
65101 'modified' : modified,
102+ 'tags' : tags != null ? tags? .join (" " ) : ""
66103 };
67104 }
68105}
106+
69107String origin = 'http://localhost:8080' ;
70108
71109Future <List <Tasks >> fetchTasks (String uuid, String encryptionSecret) async {
@@ -99,8 +137,8 @@ Future<void> updateTasksInDatabase(List<Tasks> tasks) async {
99137 //add tasks without UUID to the server and delete them from database
100138 for (var task in tasksWithoutUUID) {
101139 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 ! : [] );
104142 } catch (e) {
105143 debugPrint ('Failed to add task without UUID to server: $e ' );
106144 }
@@ -222,15 +260,16 @@ Future<void> completeTask(String email, String taskUuid) async {
222260 }
223261}
224262
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 {
227265 var baseUrl = await CredentialsStorage .getApiUrl ();
228266 String apiUrl = '$baseUrl /add-task' ;
229267 var c = await CredentialsStorage .getClientId ();
230268 var e = await CredentialsStorage .getEncryptionSecret ();
269+ debugPrint ("Database Adding Tags $tags $description " );
231270 debugPrint (c);
232271 debugPrint (e);
233- await http.post (
272+ var res = await http.post (
234273 Uri .parse (apiUrl),
235274 headers: {
236275 'Content-Type' : 'text/plain' ,
@@ -243,9 +282,10 @@ Future<void> addTaskAndDeleteFromDatabase(
243282 'project' : project,
244283 'due' : due,
245284 'priority' : priority,
285+ 'tags' : tags
246286 }),
247287 );
248-
288+ debugPrint ( 'Database res ${ res . body }' );
249289 var taskDatabase = TaskDatabase ();
250290 await taskDatabase.open ();
251291 await taskDatabase._database! .delete (
@@ -305,9 +345,11 @@ class TaskDatabase {
305345 var databasesPath = await getDatabasesPath ();
306346 String path = join (databasesPath, 'tasks.db' );
307347
308- _database = await openDatabase (path, version: 1 ,
348+ _database = await openDatabase (path,
349+ version: 1 ,
350+ onOpen: (db) async => await addTagsColumnIfNeeded (db),
309351 onCreate: (Database db, version) async {
310- await db.execute ('''
352+ await db.execute ('''
311353 CREATE TABLE Tasks (
312354 uuid TEXT PRIMARY KEY,
313355 id INTEGER,
@@ -322,7 +364,16 @@ class TaskDatabase {
322364 modified TEXT
323365 )
324366 ''' );
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+ }
326377 }
327378
328379 Future <void > ensureDatabaseIsOpen () async {
@@ -335,20 +386,21 @@ class TaskDatabase {
335386 await ensureDatabaseIsOpen ();
336387
337388 final List <Map <String , dynamic >> maps = await _database! .query ('Tasks' );
389+ debugPrint ("Database fetch ${maps .last }" );
338390 var a = List .generate (maps.length, (i) {
339391 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 ( ' ' ) : [] );
352404 });
353405 // debugPrint('Tasks from db');
354406 // debugPrint(a.toString());
@@ -377,20 +429,21 @@ class TaskDatabase {
377429
378430 Future <void > insertTask (Tasks task) async {
379431 await ensureDatabaseIsOpen ();
380-
381- await _database! .insert (
432+ debugPrint ( "Database Insert" );
433+ var dbi = await _database! .insert (
382434 'Tasks' ,
383- task.toJson (),
435+ task.toDbJson (),
384436 conflictAlgorithm: ConflictAlgorithm .replace,
385437 );
438+ debugPrint ("Database Insert ${task .toDbJson ()} $dbi " );
386439 }
387440
388441 Future <void > updateTask (Tasks task) async {
389442 await ensureDatabaseIsOpen ();
390443
391444 await _database! .update (
392445 'Tasks' ,
393- task.toJson (),
446+ task.toDbJson (),
394447 where: 'uuid = ?' ,
395448 whereArgs: [task.uuid],
396449 );
@@ -406,7 +459,7 @@ class TaskDatabase {
406459 );
407460
408461 if (maps.isNotEmpty) {
409- return Tasks .fromJson (maps.first);
462+ return Tasks .fromDbJson (maps.first);
410463 } else {
411464 return null ;
412465 }
@@ -473,7 +526,7 @@ class TaskDatabase {
473526 );
474527
475528 return List .generate (maps.length, (i) {
476- return Tasks .fromJson (maps[i]);
529+ return Tasks .fromDbJson (maps[i]);
477530 });
478531 }
479532
@@ -483,21 +536,21 @@ class TaskDatabase {
483536 where: 'project = ?' ,
484537 whereArgs: [project],
485538 );
486-
539+ debugPrint ( "DB Stored for $ maps " );
487540 return List .generate (maps.length, (i) {
488541 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 ( ' ' ) );
501554 });
502555 }
503556
@@ -520,7 +573,7 @@ class TaskDatabase {
520573 whereArgs: ['%$query %' , '%$query %' ],
521574 );
522575 return List .generate (maps.length, (i) {
523- return Tasks .fromJson (maps[i]);
576+ return Tasks .fromDbJson (maps[i]);
524577 });
525578 }
526579
0 commit comments