This repository was archived by the owner on Sep 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathTomatoClock.cs
More file actions
256 lines (236 loc) · 6.78 KB
/
TomatoClock.cs
File metadata and controls
256 lines (236 loc) · 6.78 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using System.Threading.Tasks;
namespace TomatoClock
{
public class TomatoClock
{
public class ClockCountChangedEventArgs : EventArgs
{
public readonly int oldValue;
public readonly int newValue;
public ClockCountChangedEventArgs(int oldValue, int newValue)
{
this.oldValue = oldValue;
this.newValue = newValue;
}
}
public class RemainSecondsChangedEventArgs : EventArgs
{
public readonly int oldValue;
public readonly int newValue;
public RemainSecondsChangedEventArgs(int oldValue, int newValue)
{
this.oldValue = oldValue;
this.newValue = newValue;
}
}
public class TimeIsUpEventArgs : EventArgs
{
public readonly bool tomatoTimeIsUp;
public TimeIsUpEventArgs(bool tomatoTimeIsUp)
{
this.tomatoTimeIsUp = tomatoTimeIsUp;
}
}
/// <summary>
/// 积攒的番茄数目修改事件
/// </summary>
public event EventHandler<ClockCountChangedEventArgs> ClockCountChanged;
public event EventHandler<RemainSecondsChangedEventArgs> RemainSecondsChanged;
public event EventHandler ResetTimer;
public event EventHandler<TimeIsUpEventArgs> TimeIsUp;
#region Attributes
protected int tomatoTime = 1; // mins
protected int breakTime = 1; // mins
protected int longBreakTime = 1; // mins
protected int clockCountToLongBreak = 2;
protected int clockCount;
protected int remainSeconds; // 剩余秒数
protected bool isInTomatoTime;
protected Timer timer;
/// <summary>
/// 当前积攒的番茄数目
/// </summary>
public int ClockCount
{
get { return clockCount; }
protected set
{
int oldValue = clockCount;
clockCount = value;
if (ClockCountChanged != null)
ClockCountChanged(this, new ClockCountChangedEventArgs(oldValue, value));
}
}
/// <summary>
/// 当前剩余的秒数
/// </summary>
public int RemainSeconds
{
get { return remainSeconds; }
protected set
{
int oldValue = remainSeconds;
remainSeconds = value;
if (RemainSecondsChanged != null)
RemainSecondsChanged(this, new RemainSecondsChangedEventArgs(oldValue, value));
}
}
/// <summary>
/// 番茄时间是否在工作
/// </summary>
public bool Enable
{
get { return timer.Enabled; }
}
/// <summary>
/// 工作时间(番茄时间)
/// </summary>
public int TomatoTime
{
get
{
return tomatoTime;
}
set
{
tomatoTime = value;
}
}
/// <summary>
/// 短休息时间
/// </summary>
public int BreakTime
{
get
{
return breakTime;
}
set
{
breakTime = value;
}
}
/// <summary>
/// 长休息时间
/// </summary>
public int LongBreakTime
{
get
{
return longBreakTime;
}
set
{
longBreakTime = value;
}
}
/// <summary>
/// 达到长休息所积攒的番茄数目
/// </summary>
public int ClockCountToLongBreak
{
get
{
return clockCountToLongBreak;
}
set
{
clockCountToLongBreak = value;
}
}
/// <summary>
/// 判断是否在工作时间(番茄时间)中
/// </summary>
public bool IsInTomatoTime
{
get
{
return isInTomatoTime;
}
}
#endregion
/// <summary>
///
/// </summary>
/// <param name="tomatoTime">工作时间</param>
/// <param name="breakTime">短休息时间</param>
/// <param name="longBreakTime">长休息时间</param>
/// <param name="clockCountToLongBreak">达到长休息时间所积攒的番茄数目</param>
public TomatoClock(int tomatoTime, int breakTime, int longBreakTime, int clockCountToLongBreak)
{
this.tomatoTime = tomatoTime;
this.breakTime = breakTime;
this.longBreakTime = longBreakTime;
this.clockCountToLongBreak = clockCountToLongBreak;
timer = new Timer(1000);
timer.Elapsed += Timer_Elapsed;
}
protected void resetTimer()
{
if (isInTomatoTime)
{
if (clockCount >= clockCountToLongBreak)
{
remainSeconds = longBreakTime * 60;
ClockCount = 0;
}
else
remainSeconds = breakTime * 60;
}
else
{
remainSeconds = tomatoTime * 60;
}
isInTomatoTime = !isInTomatoTime;
if (ResetTimer != null)
ResetTimer(this, new EventArgs());
}
/// <summary>
/// 开始
/// </summary>
public void start()
{
isInTomatoTime = false;
ClockCount = 0;
resetTimer();
timer.Start();
}
protected void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
if (RemainSeconds > 0)
{
// set the font color red when it remains 2 minutes
RemainSeconds -= 1;
}
else
{
// time is up
bool oldIsInTomatoTime = isInTomatoTime;
if (isInTomatoTime)
ClockCount += 1;
resetTimer();
if (TimeIsUp != null)
TimeIsUp(this, new TimeIsUpEventArgs(oldIsInTomatoTime));
}
}
/// <summary>
/// 暂停
/// </summary>
public void pause()
{
timer.Stop();
}
/// <summary>
/// 恢复
/// </summary>
public void resume()
{
timer.Start();
}
}
}