-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstep2.dart
More file actions
278 lines (239 loc) · 8.64 KB
/
step2.dart
File metadata and controls
278 lines (239 loc) · 8.64 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// see Serializing JSON inside model classes in
// https://flutter.dev/docs/development/data-and-backend/json
import 'package:intl/intl.dart';
import 'dart:convert' as convert;
import 'package:flutter/material.dart';
final DateFormat _dateFormatter = DateFormat("yyyy-MM-dd HH:mm:ss");
abstract class Activity {
late int id;
late String name;
DateTime? initialDate;
DateTime? finalDate;
late int duration;
List<dynamic> children = List<dynamic>.empty(growable: true);
Activity.fromJson(Map<String, dynamic> json)
: id = json['id'],
name = json['name'],
initialDate = json['initialDate']==null ? null : _dateFormatter.parse(json['initialDate']),
finalDate = json['finalDate']==null ? null : _dateFormatter.parse(json['finalDate']),
duration = json['duration'];
}
class Project extends Activity {
Project.fromJson(Map<String, dynamic> json) : super.fromJson(json) {
if (json.containsKey('activities')) {
// json has only 1 level because depth=1 or 0 in time_tracker
for (Map<String, dynamic> jsonChild in json['activities']) {
if (jsonChild['class'] == "project") {
children.add(Project.fromJson(jsonChild));
// condition on key avoids infinite recursion
} else if (jsonChild['class'] == "task") {
children.add(Task.fromJson(jsonChild));
} else {
assert(false);
}
}
}
}
}
class Task extends Activity {
late bool active;
Task.fromJson(Map<String, dynamic> json) : super.fromJson(json) {
active = json['active'];
for (Map<String, dynamic> jsonChild in json['intervals']) {
children.add(Interval.fromJson(jsonChild));
}
}
}
class Interval {
late int id;
DateTime? initialDate;
DateTime? finalDate;
late int duration;
late bool active;
Interval.fromJson(Map<String, dynamic> json)
: id = json['id'],
initialDate = json['initialDate']==null ? null : _dateFormatter.parse(json['initialDate']),
finalDate = json['finalDate']==null ? null : _dateFormatter.parse(json['finalDate']),
duration = json['duration'],
active = json['active'];
}
class Tree {
late Activity root;
Tree(Map<String, dynamic> dec) {
// 1 level tree, root and children only, root is either Project or Task. If Project
// children are Project or Task, that is, Activity. If root is Task, children are Interval.
assert (dec['class'] == "project" || dec['class']=='task');
if (dec['class'] == "project") {
root = Project.fromJson(dec);
} else {
root = Task.fromJson(dec);
}
}
}
Tree getTree() {
String strJson = "{"
"\"name\":\"root\", \"class\":\"project\", \"id\":0, \"initialDate\":\"2020-09-22 16:04:56\", \"finalDate\":\"2020-09-22 16:05:22\", \"duration\":26,"
"\"activities\": [ "
"{ \"name\":\"software design\", \"class\":\"project\", \"id\":1, \"initialDate\":\"2020-09-22 16:05:04\", \"finalDate\":\"2020-09-22 16:05:16\", \"duration\":16 },"
"{ \"name\":\"software testing\", \"class\":\"project\", \"id\":2, \"initialDate\": null, \"finalDate\":null, \"duration\":0 },"
"{ \"name\":\"databases\", \"class\":\"project\", \"id\":3, \"finalDate\":null, \"initialDate\":null, \"duration\":0 },"
"{ \"name\":\"transportation\", \"class\":\"task\", \"id\":6, \"active\":false, \"initialDate\":\"2020-09-22 16:04:56\", \"finalDate\":\"2020-09-22 16:05:22\", \"duration\":10, \"intervals\":[] }"
"] "
"}";
Map<String, dynamic> decoded = convert.jsonDecode(strJson);
Tree tree = Tree(decoded);
return tree;
}
testLoadTree() {
Tree tree = getTree();
print("root name ${tree.root.name}, duration ${tree.root.duration}");
for (Activity act in tree.root.children) {
print("child name ${act.name}, duration ${act.duration}");
}
}
//void main() {
// testLoadTree();
//}
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'TimeTracker',
theme: ThemeData(
primarySwatch: Colors.blue,
textTheme: const TextTheme(
subtitle1: TextStyle(fontSize:20.0),
bodyText2:TextStyle(fontSize:20.0)),
),
home: PageActivities(),
);
}
}
class PageActivities extends StatefulWidget {
@override
_PageActivitiesState createState() => _PageActivitiesState();
}
class _PageActivitiesState extends State<PageActivities> {
late Tree tree;
@override
void initState() {
super.initState();
tree = getTree();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(tree.root.name),
actions: <Widget>[
IconButton(icon: Icon(Icons.home),
onPressed: () {}
// TODO go home page = root
),
//TODO other actions
],
),
body: ListView.separated(
// it's like ListView.builder() but better
// because it includes a separator between items
padding: const EdgeInsets.all(16.0),
itemCount: tree.root.children.length,
itemBuilder: (BuildContext context, int index) =>
_buildRow(tree.root.children[index], index),
separatorBuilder: (BuildContext context, int index) =>
const Divider(),
),
);
}
Widget _buildRow(Activity activity, int index) {
String strDuration = Duration(seconds: activity.duration).toString().split('.').first;
// split by '.' and taking first element of resulting list
// removes the microseconds part
assert (activity is Project || activity is Task);
if (activity is Project) {
return ListTile(
title: Text('${activity.name}'),
trailing: Text('$strDuration'),
onTap: () => {},
// TODO, navigate down to show children tasks and projects
);
} else {
Task task = activity as Task;
Widget trailing;
trailing = Text('$strDuration');
return ListTile(
title: Text('${activity.name}'),
trailing: trailing,
onTap: () => _navigateDownIntervals(index),
// TODO, navigate down to show intervals
onLongPress: () {},
// TODO start/stop counting the time for this task
);
}
}
void _navigateDownIntervals(int childId) {
Navigator.of(context)
.push(MaterialPageRoute<void>(builder: (context) => PageIntervals())
);
}
}
Tree getTreeTask() {
String strJson = "{"
"\"name\":\"transportation\",\"class\":\"task\", \"id\":6, \"active\":false, \"initialDate\":\"2020-09-22 13:36:08\", \"finalDate\":\"2020-09-22 13:36:34\", \"duration\":10,"
"\"intervals\":["
"{\"class\":\"interval\", \"id\":7, \"active\":false, \"initialDate\":\"2020-09-22 13:36:08\", \"finalDate\":\"2020-09-22 13:36:14\", \"duration\":6},"
"{\"class\":\"interval\", \"id\":8, \"active\":false, \"initialDate\":\"2020-09-22 13:36:30\", \"finalDate\":\"2020-09-22 13:36:34\", \"duration\":4}"
"]}";
Map<String, dynamic> decoded = convert.jsonDecode(strJson);
Tree tree = Tree(decoded);
return tree;
}
class PageIntervals extends StatefulWidget {
@override
_PageIntervalsState createState() => _PageIntervalsState();
}
class _PageIntervalsState extends State<PageIntervals> {
late Tree tree;
@override
void initState() {
super.initState();
tree = getTreeTask();
// the root is a task and the children its intervals
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(tree.root.name),
actions: <Widget>[
IconButton(icon: Icon(Icons.home),
onPressed: () {} // TODO go home page = root
),
//TODO other actions
],
),
body: ListView.separated(
// it's like ListView.builder() but better because it includes a
// separator between items
padding: const EdgeInsets.all(16.0),
itemCount: tree.root.children.length, // number of intervals
itemBuilder: (BuildContext context, int index) =>
_buildRow(tree.root.children[index], index),
separatorBuilder: (BuildContext context, int index) =>
const Divider(),
),
);
}
Widget _buildRow(Interval interval, int index) {
String strDuration = Duration(seconds: interval.duration).toString().split('.').first;
String strInitialDate = interval.initialDate.toString().split('.')[0];
// this removes the microseconds part
String strFinalDate = interval.finalDate.toString().split('.')[0];
return ListTile(
title: Text('from ${strInitialDate} to ${strFinalDate}'),
trailing: Text('$strDuration'),
);
}
}