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
/
time_tracker.dart
108 lines (100 loc) · 2.29 KB
/
time_tracker.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
part of time_tracker;
/**
* Time tracker.
*/
class TimeTracker {
final int dataSchemaVersion = 1;
String wrapperClass = 'idle';
List<Task> tasks = new List();
int _activeTasks = 0;
int get activeTasks {
return _activeTasks;
}
void set activeTasks(count) {
_activeTasks = count;
if (_activeTasks > 0) {
wrapperClass = 'working';
} else {
wrapperClass = 'idle';
}
}
String _url = window.location.toString();
/**
* Constructor.
*/
TimeTracker() {
// Load saved data from storage.
var data = _loadFromStorage();
// Restore window size. Position also could be restored, but it works
// unexpected on multi-monitors.
window.resizeTo(data['window']['width'], data['window']['height']);
// Restore tasks.
if (data['tasks'].isEmpty) {
createNewTask();
} else {
data['tasks'].forEach((values) {
tasks.add(new Task.fromMap(values));
});
}
// Initialize autosave.
new Timer.repeating(1000, (timer) => this._saveToStorage());
}
/**
* Creates new task.
*/
void createNewTask([Event e]) {
tasks.add(new Task());
}
/**
* Deletes all tasks.
*/
void deleteAllTasks([Event e]) {
tasks.clear();
createNewTask();
}
/**
* Saves all tasks to storage.
*/
void _saveToStorage() {
var _tasks = [];
tasks.forEach((task) {
_tasks.add(task.toMap());
});
window.localStorage[_url] = JSON.stringify({
'dataSchemaVersion': dataSchemaVersion,
'tasks': _tasks,
'window': _getWindowParams()
});
}
/**
* Loads saved tasks from storage.
*/
Map _loadFromStorage() {
if (window.localStorage.containsKey(_url)) {
try {
var data = JSON.parse(window.localStorage[_url]);
if (data is Map && data.containsKey('dataSchemaVersion')) {
if (data['dataSchemaVersion'] == dataSchemaVersion) {
return data;
} else {
// There could be data updates.
}
}
} on Exception catch (e) {}
}
// Defaults.
return {
'tasks': [],
'window': _getWindowParams()
};
}
/**
* Returns window parameters.
*/
Map _getWindowParams() {
return {
'width': window.outerWidth,
'height': window.outerHeight
};
}
}