|
| 1 | +import 'dart:developer'; |
| 2 | + |
| 3 | +// import 'package:connectivity_plus/connectivity_plus.dart'; |
1 | 4 | import 'package:get/get.dart';
|
2 | 5 | import 'package:task_management/db/db_helper.dart';
|
3 | 6 | import 'package:task_management/models/task.dart';
|
| 7 | +import 'package:task_management/services/notification_services.dart'; |
| 8 | +import 'package:task_management/services/task_services.dart'; |
4 | 9 |
|
5 | 10 | class TaskController extends GetxController {
|
6 | 11 | //this will hold the data and update the ui
|
7 |
| - |
| 12 | + final TaskServices _taskServices = TaskServices(); |
8 | 13 | @override
|
9 | 14 | void onReady() {
|
10 | 15 | getTasks();
|
| 16 | + getRemoteTasks(); |
| 17 | + syncData(); |
11 | 18 | super.onReady();
|
12 | 19 | }
|
13 | 20 |
|
14 | 21 | final RxList<Task> taskList = new RxList<Task>();
|
15 | 22 |
|
| 23 | + final RxList<Task> remoteTaskList = new RxList<Task>(); |
| 24 | + |
| 25 | + //create resource |
| 26 | + void createResource() async { |
| 27 | + await DBHelper.initDb(); |
| 28 | + } |
| 29 | + |
16 | 30 | // add data to table
|
17 | 31 | Future<int> addTask(Task task) async {
|
| 32 | + await _taskServices.addTask(task); |
18 | 33 | return await DBHelper.insert(task);
|
19 | 34 | }
|
20 | 35 |
|
| 36 | + // bulk insert data to table |
| 37 | + void bulkInsert(List<Task> tasks) async { |
| 38 | + await DBHelper.bulkInsert(tasks); |
| 39 | + getTasks(); |
| 40 | + } |
| 41 | + |
21 | 42 | // get all the data from table
|
22 | 43 | void getTasks() async {
|
23 | 44 | List<Map<String, dynamic>> tasks = await DBHelper.query();
|
24 | 45 | taskList.assignAll(tasks.map((data) => new Task.fromJson(data)).toList());
|
25 | 46 | }
|
26 | 47 |
|
| 48 | + //get all remote tasks |
| 49 | + void getRemoteTasks() async { |
| 50 | + remoteTaskList.assignAll(await _taskServices.getTasks()); |
| 51 | + log(remoteTaskList.length.toString()); |
| 52 | + } |
| 53 | + |
| 54 | + // sync data |
| 55 | + void syncData() async { |
| 56 | + // Fetch remote and local task IDs |
| 57 | + List<String> remoteTaskIdList = await _taskServices.getTaskIds(); |
| 58 | + List<String> localTaskIdList = |
| 59 | + taskList.map((e) => e.id.toString()).toList(); |
| 60 | + log(remoteTaskIdList.length.toString()); |
| 61 | + log(localTaskIdList.length.toString()); |
| 62 | + // If remote data is empty, push all local data to the remote |
| 63 | + if (remoteTaskIdList.isEmpty) { |
| 64 | + taskList.forEach((task) { |
| 65 | + _taskServices.addTask(task); |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + // If local data is empty, pull all remote data into local |
| 70 | + if (localTaskIdList.isEmpty) { |
| 71 | + List<Task> tasks = await _taskServices.getTasks(); |
| 72 | + taskList.assignAll(tasks); |
| 73 | + bulkInsert(tasks); |
| 74 | + } |
| 75 | + |
| 76 | + // Sync data between local and remote |
| 77 | + else { |
| 78 | + // Find tasks that exist in local but not in remote |
| 79 | + List<String> notExistInRemote = localTaskIdList |
| 80 | + .where((localId) => !remoteTaskIdList.contains(localId)) |
| 81 | + .toList(); |
| 82 | + // Push those tasks to the remote |
| 83 | + notExistInRemote.forEach((id) { |
| 84 | + Task task = |
| 85 | + taskList.firstWhere((element) => element.id.toString() == id); |
| 86 | + _taskServices.addTask(task); |
| 87 | + log("Added local task to remote"); |
| 88 | + }); |
| 89 | + |
| 90 | + // Find tasks that exist in remote but not in local |
| 91 | + List<String> notExistInLocal = remoteTaskIdList |
| 92 | + .where((remoteId) => !localTaskIdList.contains(remoteId)) |
| 93 | + .toList(); |
| 94 | + if (notExistInLocal.isNotEmpty) { |
| 95 | + List<Task> remoteTasks = await _taskServices.getTasks(); |
| 96 | + List<Task> newLocalTasks = remoteTasks |
| 97 | + .where((task) => notExistInLocal.contains(task.id.toString())) |
| 98 | + .toList(); |
| 99 | + taskList.addAll(newLocalTasks); |
| 100 | + //schedule notification for new tasks which have todays date |
| 101 | + for (Task task in newLocalTasks) { |
| 102 | + var selectedDate = DateTime.parse(task.startTime); |
| 103 | + selectedDate = DateTime(selectedDate.year, selectedDate.month, |
| 104 | + selectedDate.day, selectedDate.hour, selectedDate.minute, 00); |
| 105 | + if (selectedDate.isAfter(DateTime.now())) { |
| 106 | + await NotifyHelper() |
| 107 | + .scheduleNotification(selectedDate, task.title, task.note); |
| 108 | + } |
| 109 | + bulkInsert(newLocalTasks); |
| 110 | + log("Added remote tasks to local"); |
| 111 | + } |
| 112 | + } |
| 113 | + // Reload tasks to ensure UI updates |
| 114 | + getTasks(); |
| 115 | + } |
| 116 | + } |
| 117 | + |
27 | 118 | // delete data from table
|
28 | 119 | void deleteTask(Task task) async {
|
| 120 | + await _taskServices.deleteTask(task.id.toString()); |
29 | 121 | await DBHelper.delete(task);
|
30 | 122 | getTasks();
|
31 | 123 | }
|
32 | 124 |
|
33 | 125 | // update data int table
|
34 | 126 | void markTaskCompleted(int id) async {
|
| 127 | + Task task = taskList.firstWhere((element) => element.id == id); |
| 128 | + task.isCompleted = 1; |
| 129 | + await _taskServices.updateTaskStatus(task); |
35 | 130 | await DBHelper.update(id);
|
36 | 131 | getTasks();
|
37 | 132 | }
|
| 133 | + |
| 134 | + //delete all data |
| 135 | + void clearStorage() async { |
| 136 | + await DBHelper.deleteTableData(); |
| 137 | + // getTasks(); |
| 138 | + } |
38 | 139 | }
|
0 commit comments