forked from canton7/timer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TaskList.cs
162 lines (137 loc) · 4.32 KB
/
TaskList.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace timer {
public class TaskList {
private List<Task> tasks = new List<Task>();
public List<Task> Tasks {
get { return this.tasks; }
}
public Task CurrentTask {
get { return this.tasks[0]; }
}
public bool HaveTasks {
get { return this.tasks.Count > 0; }
}
public Task.States CurrentState {
get { return this.tasks[0].State; }
}
public int Count {
get { return this.tasks.Count; }
}
private List<string> projects = new List<string>();
public List<string> Projects {
get { return this.projects; }
}
public string CurrentProject {
get { return (this.tasks.Count > 0) ? this.tasks[0].Project : ""; }
}
// Cache of duration for all tasks in current project except current task
private TimeSpan currentProjectFinishedTime;
public TimeSpan CurrentProjectTime {
get { return this.currentProjectFinishedTime + this.tasks[0].Duration; }
}
public class SerializedForm {
public Task.SerializedForm[] Tasks;
}
public TaskList() {
}
public TaskList(SerializedForm serializedForm) {
if (serializedForm == null)
return;
this.Unserialize(serializedForm);
this.generateProjects();
this.currentProjectFinishedTime = this.calcCurrentProjectFinishedTime();
}
public void AddTask(string project, string description, TimeSpan duration) {
// Don't replace the top-of-the-list task if it's currently running
int index = (this.tasks.Count > 0 && this.tasks[0].State == Task.States.IN_PROGRESS) ? 1 : 0;
this.tasks.Insert(index, new Task(project, description, duration));
if (!this.projects.Contains(project))
this.projects.Insert(index, project);
this.currentProjectFinishedTime = this.calcCurrentProjectFinishedTime();
}
public void StartCurrent() {
this.tasks[0].Start();
}
public void StopCurrent() {
if (this.tasks.Count > 0)
this.tasks[0].Stop();
}
private TimeSpan calcCurrentProjectFinishedTime() {
TimeSpan timespan = new TimeSpan(0);
foreach (Task task in this.tasks) {
if (task.Project != this.CurrentProject)
continue;
// Don't include current task, as this is updated so can't be cached
if (task == this.tasks[0])
continue;
timespan += task.Duration;
}
return timespan;
}
public TimeSpan GetProjectTime(string project) {
TimeSpan timespan = new TimeSpan(0);
foreach (Task task in this.tasks) {
if (task.Project != project)
continue;
timespan += task.Duration;
}
return timespan;
}
public Task[] GetProjectTasks(string project) {
List<Task> tasks = new List<Task>();
foreach (Task task in this.tasks) {
if (task.Project != project)
continue;
tasks.Add(task);
}
return tasks.ToArray();
}
public void SetCurrentTask(Task task) {
if (!this.tasks.Contains(task))
throw new Exception("Can't find that task when setting it as current");
// Take the task and stick it at the top
this.tasks.Remove(task);
this.tasks.Insert(0, task);
this.currentProjectFinishedTime = this.calcCurrentProjectFinishedTime();
}
public void DeleteTask(Task task) {
if (!this.tasks.Contains(task))
throw new Exception("Can't find that task when deleteing");
this.tasks.Remove(task);
this.generateProjects();
this.currentProjectFinishedTime = this.calcCurrentProjectFinishedTime();
}
public void ReplaceTask(int old_index, Task task) {
//int old_index = this.tasks.
this.tasks.RemoveAt(old_index);
this.tasks.Insert(old_index, task);
if (!this.projects.Contains(task.Project))
this.projects.Insert(0, task.Project);
this.generateProjects();
}
private void generateProjects() {
this.projects.Clear();
foreach (Task task in this.tasks) {
if (!this.projects.Contains(task.Project))
this.projects.Add(task.Project);
}
}
public SerializedForm Serialize() {
SerializedForm serializedForm = new SerializedForm();
List<Task.SerializedForm> tasks = new List<Task.SerializedForm>();
foreach (Task task in this.tasks) {
tasks.Add(task.Serialize());
}
serializedForm.Tasks = tasks.ToArray();
return serializedForm;
}
public void Unserialize(SerializedForm serializedForm) {
foreach (Task.SerializedForm serializedTask in serializedForm.Tasks) {
this.tasks.Add(new Task(serializedTask));
}
}
}
}