-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainPage.xaml.cs
191 lines (162 loc) · 5.32 KB
/
MainPage.xaml.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
185
186
187
188
189
190
191
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace MyPomodoroApp
{
/// <summary>
/// Interaction logic for MainPage.xaml
/// </summary>
public partial class MainPage : Page
{
// Create the Timer
private static DispatcherTimer timer = new DispatcherTimer();
// Store the time remaining for the current session
private int minutes = 25;
private int seconds = 0;
// Store the statuses for the sessions
public static string workStatus = "Work";
private string longBreakStatus = "Long Break";
private string shortBreakStatus = "Short Break";
// Store the duration of each session
private int pomodoroTime = 25;
private int longBreakTime = 15;
private int shortBreakTime = 5;
// Store the current session number and maximum number of sessions
private int curSession = 1;
private int maxSessions = 4;
public MainPage()
{
InitializeComponent();
}
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
// Set the initial values for the timer label and status label
UpdateTimerLabel();
StatusLabel.Content = workStatus;
// Set the interval of the timer to 1 second
timer.Interval = TimeSpan.FromSeconds(1);
// Attach the Tick event handler to the timer
timer.Tick += Timer_Tick;
}
private void Timer_Tick(object sender, EventArgs e)
{
// Decrement a second per tick
seconds--;
// If the number of seconds is less than 0, decrement the minutes and reset the number of seconds
if (seconds < 0)
{
minutes--;
seconds = 59;
}
// If the number of minutes and seconds is 0, move to the next stage.
if (minutes == 0 && seconds == 0)
{
NextStage();
timer.Start();
}
else
{
// Update Timer Label
UpdateTimerLabel();
}
}
private void ToggleButton_Click(object sender, RoutedEventArgs e)
{
// If the timer is currently running stop it
if (timer.IsEnabled)
{
timer.Stop();
ToggleButton.Content = "Start";
}
else
{
// If the timer is not currently running, start it
timer.Start();
ToggleButton.Content = "Stop";
}
}
private void ResetButton_Click(object sender, RoutedEventArgs e)
{
Reset();
}
private void SkipButton_Click(object sender, RoutedEventArgs e)
{
// Move to the next stage
NextStage();
// Update timer label
UpdateTimerLabel();
}
private void Reset()
{
// Stop the timer
timer.Stop();
// Reset the number of minutes and seconds
minutes = pomodoroTime;
seconds = 0;
// Update label
UpdateTimerLabel();
// Update Status Label
StatusLabel.Content = workStatus;
// Reset Current Session and session label
curSession = 1;
UpdateSessionLabel();
}
private void NextStage()
{
// Stop the timer
timer.Stop();
// Reset the number of seconds
seconds = 0;
// Update the toggle button
ToggleButton.Content = "Start";
// Check the status of the timer and swithc to the next stage accordingly
if (StatusLabel.Content.ToString() == workStatus)
{
// If max sessions have been completed, start long break
if (curSession >= maxSessions)
{
minutes = longBreakTime;
StatusLabel.Content = longBreakStatus;
}
else
{
minutes = shortBreakTime;
StatusLabel.Content = shortBreakStatus;
}
}
else if (StatusLabel.Content.ToString() == longBreakStatus)
{
Reset();
}
else
{
minutes = pomodoroTime;
StatusLabel.Content = workStatus;
// One cycle has been completed
curSession++;
}
// Update the session label
UpdateSessionLabel();
}
private void UpdateTimerLabel()
{
TimerLabel.Content = string.Format("{0:00}:{1:00}", minutes, seconds);
}
private void UpdateSessionLabel()
{
SessionLabel.Content = string.Format("{0}/{1}", curSession, maxSessions);
}
}
}