-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEventTimer.cs
184 lines (154 loc) · 6 KB
/
EventTimer.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;
namespace healthy_reminders
{
public class EventTimer
{
public HealthEvent HealthEvent;
public bool OnEventMode = false;
public bool OnWaiting = true;
// Window and elements variables
private readonly MainWindow _mainWindow;
public Dictionary<string, System.Windows.Controls.Label> ControlLabels;
public Dictionary<string, System.Windows.Controls.Button> ControlButtons;
// Timer and time variables
public DispatcherTimer DispatcherTimer;
public TimeSpan DelayTime;
public TimeSpan EventTime;
public TimeSpan CountdownTime;
public EventTimer(
MainWindow window,
TimeSpan delayTime,
TimeSpan eventTime,
HealthEvent healthEvent,
Dictionary<string, System.Windows.Controls.Label> controlLabels,
Dictionary<string, System.Windows.Controls.Button> controlButtons
)
{
HealthEvent = healthEvent;
_mainWindow = window;
ControlLabels = controlLabels;
ControlButtons = controlButtons;
// Create and adjust the timer
DispatcherTimer = new DispatcherTimer();
DispatcherTimer.Tick += new EventHandler(DispatcherTimer_Tick);
DispatcherTimer.Interval = TimeSpan.FromSeconds(1);
// Time variables
DelayTime = delayTime;
CountdownTime = delayTime;
EventTime = eventTime;
}
// In every tick of the timer
public void DispatcherTimer_Tick(object? sender, EventArgs e)
{
// Reduce one second in the timer and show it
CountdownTime = CountdownTime.Subtract(TimeSpan.FromSeconds(1));
UpdateCountdown();
// Once the countdown reaches 00:00
if (CountdownTime.Ticks <= 0)
{
Reset(false);
if (OnEventMode)
{
OnEventMode = false;
UpdateControls();
ControlLabels["Countdown"].FontWeight = FontWeights.Normal;
ControlLabels["Countdown"].Foreground = System.Windows.Media.Brushes.Black;
_mainWindow.NotificationManager.ShowNotification(
HealthEvent.Name,
"Event ended!"
);
}
else
{
// Not event timer available for health event "PostureHealth"
if (HealthEvent.HasTimerEvent)
{
OnEventMode = true;
UpdateControls();
ControlLabels["Countdown"].FontWeight = FontWeights.Bold;
ControlLabels["Countdown"].Foreground = System.Windows.Media.Brushes.DarkGreen;
}
Random rand = new Random();
_mainWindow.NotificationManager.ShowNotification(
HealthEvent.Name,
HealthEvent.Alerts[rand.Next(HealthEvent.Alerts.Length)]
);
}
Reset();
if (!HealthEvent.HasTimerEvent)
{
Start();
}
}
}
// Start timer
public void Start()
{
OnWaiting = false;
UpdateControls();
UpdateCountdownType();
DispatcherTimer.Start();
ControlLabels["Countdown"].FontWeight = FontWeights.Normal;
ControlLabels["Countdown"].Foreground = System.Windows.Media.Brushes.Gray;
}
// Reset timer
public void Reset(bool resetEventMode = true)
{
OnWaiting = true;
OnEventMode = (resetEventMode ? false : OnEventMode);
UpdateControls();
UpdateCountdownType();
DispatcherTimer.Stop();
ControlLabels["Countdown"].FontWeight = FontWeights.Normal;
ControlLabels["Countdown"].Foreground = System.Windows.Media.Brushes.Black;
}
// Skip timer of event itself
public void Skip()
{
// Reset current timer
OnWaiting = true;
DispatcherTimer.Stop();
// Update labels and controls
ControlLabels["Countdown"].FontWeight = (OnEventMode ? FontWeights.Normal : FontWeights.Bold);
ControlLabels["Countdown"].Foreground = (OnEventMode ? System.Windows.Media.Brushes.Black : System.Windows.Media.Brushes.DarkGreen);
OnEventMode = !OnEventMode;
UpdateControls();
UpdateCountdownType();
}
// Update countdown timer
public void UpdateCountdown()
{
ControlLabels["Countdown"].Content = CountdownTime.ToString(
CountdownTime.Hours >= 1 ? @"hh\:mm\:ss" : @"mm\:ss"
);
}
// Update time goal in the countdown based on the type
public void UpdateCountdownType()
{
CountdownTime = OnEventMode ? EventTime : DelayTime;
UpdateCountdown();
}
// Update control buttons based on countdown states
public void UpdateControls()
{
ControlButtons["Play"].IsEnabled = (OnWaiting);
ControlButtons["Reset"].IsEnabled = (OnEventMode || !OnWaiting);
ControlButtons["Skip"].IsEnabled = (OnEventMode || !OnWaiting);
string message = "(";
message += OnWaiting ? "Waiting to start " : "Running ";
message += OnEventMode ? "event time" : "event delay";
message += ")";
ControlLabels["Status"].Content = message;
}
}
}