This repository has been archived by the owner on Dec 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.dart
115 lines (106 loc) · 2.35 KB
/
task.dart
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
part of time_tracker;
/**
* Task.
*/
class Task {
String name = '';
int seconds = 0;
String get time {
return _formatTimeString(seconds);
}
void set time(time) {
var _seconds = _parseTimeString(time);
if (_seconds != -1) {
seconds = _seconds;
}
}
Timer _timer;
bool working = false;
String toggleStateLabel = 'Start';
/**
* Constructor.
*/
Task() {}
/**
* From map constructor.
*/
Task.fromMap(Map values) {
if (values.containsKey('name') && values['name'] is String) {
name = values['name'];
}
if (values.containsKey('seconds') && values['seconds'] is int) {
seconds = values['seconds'];
}
}
/**
* Returns a map with "primary" properties values.
*/
Map toMap() {
return {
'name': name,
'seconds' : seconds
};
}
/**
* Starts or stops time tracking.
*/
void toggleState([Event e]) {
if (!working) {
var started = new Date.now().subtract(new Duration(seconds: seconds));
_timer = new Timer.repeating(1000, (timer) {
seconds = (new Date.now()).difference(started).inSeconds;
watchers.dispatch();
});
working = true;
toggleStateLabel = 'Stop';
timeTracker.activeTasks++;
} else {
_timer.cancel();
working = false;
toggleStateLabel = 'Start';
timeTracker.activeTasks--;
}
}
/**
* Removes task.
*/
void delete([Event e]) {
if (working) {
toggleState();
}
timeTracker.tasks.removeAt(timeTracker.tasks.indexOf(this));
}
/**
* Returns time string in "H:MM:SS" format.
*/
String _formatTimeString(int seconds) {
var h = seconds ~/ (60*60);
var m = (seconds - h*60*60) ~/ 60;
var s = seconds % 60;
s = (s >= 10) ? s : '0${s}';
m = (m >= 10) ? m : '0${m}';
return '${h}:${m}:${s}';
}
/**
* Returns seconds count parsing them from time string in "H:MM:SS" format.
*/
int _parseTimeString(String time) {
var parts = time.split(':');
if (parts.length != 3) {
return -1;
}
var h = int.parse(parts[0]);
if (h.isNaN || h <0) {
return -1;
}
var m = int.parse(parts[1]);
if (m.isNaN || m < 0 || m > 60) {
return -1;
}
var s = int.parse(parts[2]);
if (s.isNaN || s < 0 || s > 60) {
return -1;
}
return h*60*60 + m*60 + s;
}
}