-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathwidget.controller.dart
More file actions
194 lines (182 loc) · 6.45 KB
/
widget.controller.dart
File metadata and controls
194 lines (182 loc) · 6.45 KB
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
// ignore_for_file: file_names
import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:home_widget/home_widget.dart';
import 'package:syncfusion_flutter_charts/charts.dart';
import 'package:taskwarrior/app/models/filters.dart';
import 'package:taskwarrior/app/models/json/task.dart';
import 'package:taskwarrior/app/models/storage.dart';
import 'package:taskwarrior/app/modules/home/controllers/home_controller.dart';
import 'package:taskwarrior/app/modules/splash/controllers/splash_controller.dart';
import 'package:taskwarrior/app/utils/taskfunctions/urgency.dart';
// import 'package:taskwarrior/widgets/taskfunctions/datetime_differences.dart';
class WidgetController extends GetxController {
final HomeController storageWidget = Get.find<HomeController>();
late Storage storage;
late final Filters filters; // Use RxList for observable list
List<CartesianSeries> dailyBurnDown = [];
Directory? baseDirectory;
RxList<Task> allData = <Task>[].obs; // Use RxList for observable list
bool stopTraver = false;
void fetchAllData() async {
if (Platform.isAndroid || Platform.isIOS) {
// storageWidget = StorageWidget.of(context!); // Use Get.context from GetX
// var currentProfile = ProfilesWidget.of(context!).currentProfile;
var currentProfile = Get.find<SplashController>().currentProfile.value;
baseDirectory = Get.find<SplashController>().baseDirectory();
storage =
Storage(Directory('${baseDirectory!.path}/profiles/$currentProfile'));
allData.assignAll(storage.data.allData());
sendAndUpdate();
}
}
Future<void> sendAndUpdate() async {
await sendData();
await updateWidget();
}
List<Map<String, dynamic>> getTW2Tasks(HomeController taskController) {
int lengthBeforeFilters = allData.length;
List<Task> tasks = allData;
debugPrint(
'Tasks: ${tasks.length}, ${taskController.projectFilter}, ${taskController.pendingFilter.value}, ${taskController.selectedSort.value}');
if (taskController.projectFilter.value != 'All Projects' &&
taskController.projectFilter.toString().isNotEmpty) {
tasks = tasks
.where((task) => task.project == taskController.projectFilter.value)
.toList();
} else {
tasks = List<Task>.from(tasks);
}
// Apply other filters and sorting
tasks.sort((a, b) => a.id!.compareTo(b.id!));
tasks = tasks.where((task) {
if (taskController.pendingFilter.value) {
return task.status == 'pending';
} else {
return task.status == 'completed';
}
}).toList();
tasks = tasks.where((task) {
var tags = task.tags?.toSet() ?? {};
if (taskController.tagUnion.value) {
if (taskController.selectedTags.isEmpty) {
return true;
}
return taskController.selectedTags.any((tag) => (tag.startsWith('+'))
? tags.contains(tag.substring(1))
: !tags.contains(tag.substring(1)));
} else {
return taskController.selectedTags.every((tag) => (tag.startsWith('+'))
? tags.contains(tag.substring(1))
: !tags.contains(tag.substring(1)));
}
}).toList();
// Apply sorting based on selectedSort
tasks.sort((a, b) {
switch (taskController.selectedSort.value) {
case 'Created+':
return a.entry.compareTo(b.entry);
case 'Created-':
return b.entry.compareTo(a.entry);
case 'Modified+':
return a.modified!.compareTo(b.modified!);
case 'Modified-':
return b.modified!.compareTo(a.modified!);
case 'Due till+':
return a.due!.compareTo(b.due!);
case 'Due till-':
return b.due!.compareTo(a.due!);
case 'Priority-':
return a.priority!.compareTo(b.priority!);
case 'Priority+':
return b.priority!.compareTo(a.priority!);
case 'Project+':
return a.project!.compareTo(b.project!);
case 'Project-':
return b.project!.compareTo(a.project!);
case 'Urgency-':
return b.urgency!.compareTo(a.urgency!);
case 'Urgency+':
return a.urgency!.compareTo(b.urgency!);
default:
return 0;
}
});
List<Map<String, String>> l = [];
for (var task in tasks) {
l.add({
"description": task.description,
"urgency": 'urgencyLevel : ${urgency(task)}',
"uuid": task.uuid,
"priority": task.priority ?? "N"
});
}
if (l.isEmpty && lengthBeforeFilters > 0) {
l.add({
"description": "No tasks found because of filter",
"urgency": "urgencyLevel : 0",
"priority": "2",
"uuid": "NO_TASK"
});
} else if (l.isEmpty && lengthBeforeFilters == 0) {
l.add({
"description": "No tasks found",
"urgency": "urgencyLevel : 0",
"priority": "1",
"uuid": "NO_TASK"
});
}
return l;
}
Future<void> sendData() async {
final HomeController taskController = Get.find<HomeController>();
List<Map<String, dynamic>> l;
if (!taskController.taskchampion.value &&
!taskController.taskReplica.value) {
l = getTW2Tasks(taskController);
} else if (taskController.taskchampion.value) {
var tasks = taskController.tasks;
l = [];
if (tasks.isNotEmpty) {
for (var task in tasks) {
if (task.status == 'deleted') continue;
l.add({
"description": task.description,
"urgency": 'urgencyLevel : 0',
"uuid": task.uuid,
"priority": task.priority ?? "N"
});
}
}
} else {
l = [];
var tasks = taskController.tasksFromReplica;
if (tasks.isNotEmpty) {
for (var task in tasks) {
l.add({
"description": task.description,
"urgency": 'urgencyLevel : 0',
"uuid": task.uuid,
"priority": task.priority ?? "N"
});
}
}
}
if (Platform.isAndroid || Platform.isIOS) {
await HomeWidget.saveWidgetData("tasks", jsonEncode(l));
}
}
Future updateWidget() async {
if (Platform.isAndroid || Platform.isIOS) {
try {
return HomeWidget.updateWidget(
name: 'TaskWarriorWidgetProvider', iOSName: 'TaskWarriorWidgets');
} on PlatformException catch (exception) {
debugPrint('Error Updating Widget. $exception');
}
}
}
}