-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApplicationTimer.cs
75 lines (63 loc) · 1.85 KB
/
ApplicationTimer.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
using System;
using System.Threading;
namespace InteractiveOfficeClient
{
public class ApplicationTimer
{
public void ChangeState(AppState state)
{
switch (state)
{
case AppState.Working:
case AppState.Break:
ResetTimer();
break;
}
}
private void ResetTimer()
{
if (_app.IsWorking)
{
_timeLeft = Interval25MinAsSeconds;
}
else
{
_timeLeft = Interval5MinAsSeconds;
}
}
private static readonly TimeSpan TimeSpanTickInterval = TimeSpan.FromSeconds(1);
#if DEBUG
private static readonly double IntervalMinute = 1.0;
#else
private static readonly double IntervalMinute = 60.0;
#endif
private static readonly int Interval5MinAsSeconds = (int) (5 * IntervalMinute);
private static readonly int Interval25MinAsSeconds = 5 * Interval5MinAsSeconds;
private int _timeLeft = 0;
private readonly InteractiveOfficeClient _app;
public ApplicationTimer(InteractiveOfficeClient app)
{
_app = app;
new System.Threading.Timer(new TimerCallback(OnTimerCallback), AppState.Paused, TimeSpan.Zero,
TimeSpanTickInterval);
}
private void OnTimerCallback(object state)
{
if (_app.IsPaused || _app.IsNotifying)
{
return;
}
_timeLeft = _timeLeft - 1;
if (_timeLeft <= 0)
{
_timeLeft = 0;
_app.TriggerNotification();
}
_app.TimeLeft = _timeLeft;
}
public void Snooze()
{
_timeLeft = Interval5MinAsSeconds;
}
}
}