forked from Yadro/time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskStore.ts
More file actions
198 lines (170 loc) · 5.18 KB
/
TaskStore.ts
File metadata and controls
198 lines (170 loc) · 5.18 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
import { makeAutoObservable } from 'mobx';
import TaskService from './TaskService';
import TaskModel, { ITimeRangeModel } from '../../models/TaskModel';
import TasksByProject from '../../models/TasksByProject';
import TreeModelStoreHelper from '../../base/TreeModelStoreHelper';
import BadgeService from '../BadgeService';
export default class TaskStore {
tasks: TasksByProject = {};
activeTask: TaskModel | undefined;
private tasksService = new TaskService();
private interval: NodeJS.Timeout | undefined;
constructor() {
makeAutoObservable(this);
}
set(projectId: string, tasks: TaskModel[]) {
this.tasks[projectId] = tasks;
this.tasksService.save(this.tasks);
}
setTime(task: TaskModel, timeIndex: number, timeRange: ITimeRangeModel) {
task.time[timeIndex] = timeRange;
this.tasksService.save(this.tasks);
}
deleteTime(task: TaskModel, timeIndex: number) {
if (!task.time[timeIndex].end) {
task.stop();
}
task.time.splice(timeIndex, 1);
this.tasksService.save(this.tasks);
}
getTasks(projectId: string): TaskModel[] {
return this.tasks[projectId] || [];
}
getTaskByKey(taskKey: string): TaskModel | undefined {
function condition(task: TaskModel): boolean {
return task.key === taskKey;
}
for (const tasks of Object.values(this.tasks)) {
const found = TreeModelStoreHelper.getItemRecursive(tasks, condition);
if (found) {
return found;
}
}
return undefined;
}
getTasksByDate(date: Date): TaskModel[] {
const result: TaskModel[] = [];
function condition(task: TaskModel): boolean {
return task.wasActiveInDay(date);
}
for (const tasks of Object.values(this.tasks)) {
TreeModelStoreHelper.getFlatItemsRecursiveBase(tasks, condition, result);
}
return result;
}
add(task: TaskModel) {
const { projectId } = task;
if (!Array.isArray(this.tasks[projectId])) {
this.tasks[projectId] = [];
}
this.tasks[projectId].push(task);
this.tasks[projectId] = this.tasks[projectId].slice();
this.tasksService.save(this.tasks);
}
delete(task: TaskModel) {
function condition(_task: TaskModel) {
return _task.key === task.key;
}
if (task.active) {
this.stopTimer(task);
}
for (const projectKey in this.tasks) {
if (this.tasks.hasOwnProperty(projectKey)) {
this.tasks[projectKey] = TreeModelStoreHelper.deleteItems(
this.tasks[projectKey],
condition
);
}
}
this.tasksService.save(this.tasks);
}
deleteProjectTasks(projectKey: string) {
delete this.tasks[projectKey];
this.tasksService.save(this.tasks);
}
startTimer(task: TaskModel) {
if (this.activeTask) {
this.stopTimer(this.activeTask);
}
this.activeTask = task;
task.start();
this.setupReminder(task);
this.tasksService.save(this.tasks);
}
stopTimer(task: TaskModel) {
this.activeTask = undefined;
task.stop();
this.setupReminder();
this.tasksService.save(this.tasks);
}
restore() {
this.tasks = this.tasksService.getAll();
this.findAndSetActiveTask();
this.setupReminder(this.activeTask);
}
getCheckedKeys(projectId: string): string[] {
function condition(task: TaskModel): boolean {
return task.checked;
}
if (Array.isArray(this.tasks[projectId])) {
return TreeModelStoreHelper.getFlatItemsRecursive(
this.tasks[projectId],
condition
).map((task) => task.key);
}
return [];
}
checkTasks(projectId: string, taskIds: string[]) {
if (Array.isArray(this.tasks[projectId])) {
this.checkTasksRecursive(this.tasks[projectId], taskIds);
}
this.tasksService.save(this.tasks);
}
private findAndSetActiveTask() {
for (const tasks of Object.values(this.tasks)) {
const found = this.findActiveTaskRecursive(tasks);
if (found) {
this.activeTask = found;
break;
}
}
}
private findActiveTaskRecursive(tasks: TaskModel[]): TaskModel | undefined {
function condition(task: TaskModel): boolean {
return task.active;
}
return TreeModelStoreHelper.getItemRecursive(tasks, condition);
}
private checkTasksRecursive(tasks: TaskModel[], taskIds: string[]) {
tasks.forEach((task) => {
task.checked = taskIds.includes(task.key);
if (Array.isArray(task.children)) {
this.checkTasksRecursive(task.children, taskIds);
}
});
}
private setupReminder(task?: TaskModel) {
if (this.interval) {
clearInterval(this.interval);
}
if (task) {
BadgeService.setBadge(true);
console.log('Setup: Task in progress');
this.interval = setInterval(() => {
console.log('Task in progress');
new Notification('You are tracking time', {
body: `Task '${task.title}' in progress`,
});
}, 40 * 60 * 1000);
} else {
BadgeService.setBadge(false);
console.log('Setup: No tasks in progress');
this.interval = setInterval(() => {
console.log('No tasks in progress');
new Notification('You are not tracking time', {
body: 'There are not task that you track',
});
}, 15 * 60 * 1000);
}
}
}