-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExerciseWindow.xaml.cs
More file actions
157 lines (135 loc) · 5.2 KB
/
ExerciseWindow.xaml.cs
File metadata and controls
157 lines (135 loc) · 5.2 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
using System;
using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using TriviaExercise.Models;
namespace TriviaExercise
{
public partial class ExerciseWindow : Window
{
private Exercise exercise;
private DispatcherTimer countdownTimer;
private int remainingSeconds;
public ExerciseWindow(bool isCorrect, Exercise exercise)
{
InitializeComponent();
this.exercise = exercise;
ResultTextBlock.Text = isCorrect ? "Correct! 🎉" : "Wrong answer 😅";
ResultTextBlock.Foreground = isCorrect ?
new SolidColorBrush(Colors.Green) :
new SolidColorBrush(Colors.Red);
ExerciseTextBlock.Text = exercise.Description;
// Load exercise image if available
LoadExerciseImage();
if (exercise.DurationSeconds.HasValue)
{
StartCountdown();
}
}
/// <summary>
/// Load and display the exercise image if specified
/// </summary>
private void LoadExerciseImage()
{
if (string.IsNullOrEmpty(exercise.ImageFileName))
{
// No image specified, keep image area hidden
ImageBorder.Visibility = Visibility.Collapsed;
return;
}
// Validate filename
if (!Helpers.ImageHelper.IsValidImageFileName(exercise.ImageFileName))
{
ShowImageError($"Invalid image filename: {exercise.ImageFileName}");
System.Diagnostics.Debug.WriteLine($"Invalid exercise image filename: '{exercise.ImageFileName}'");
return;
}
// Try to load the image using ImageHelper
var bitmap = Helpers.ImageHelper.LoadExerciseImage(exercise.ImageFileName);
if (bitmap != null)
{
// Successfully loaded image
ExerciseImage.Source = bitmap;
ImageBorder.Visibility = Visibility.Visible;
ImageErrorText.Visibility = Visibility.Collapsed;
System.Diagnostics.Debug.WriteLine($"Successfully loaded exercise image: {exercise.ImageFileName}");
}
else
{
// Failed to load image
string imagePath = Helpers.ImageHelper.GetImagePath(exercise.ImageFileName);
bool fileExists = Helpers.ImageHelper.ExerciseImageExists(exercise.ImageFileName);
if (!fileExists)
{
ShowImageError($"Image not found: {exercise.ImageFileName}");
System.Diagnostics.Debug.WriteLine($"Exercise image not found: {imagePath}");
}
else
{
ShowImageError("Failed to load image");
System.Diagnostics.Debug.WriteLine($"Failed to load exercise image: {imagePath}");
}
}
}
/// <summary>
/// Show an error message when image loading fails
/// </summary>
/// <param name="errorMessage">The error message to display</param>
private void ShowImageError(string errorMessage)
{
ImageErrorText.Text = errorMessage;
ImageErrorText.Visibility = Visibility.Visible;
ExerciseImage.Source = null;
ImageBorder.Visibility = Visibility.Visible; // Still show the border with error text
}
private void StartCountdown()
{
remainingSeconds = exercise.DurationSeconds.Value;
TimerTextBlock.Visibility = Visibility.Visible;
UpdateTimerDisplay();
countdownTimer = new DispatcherTimer();
countdownTimer.Interval = TimeSpan.FromSeconds(1);
countdownTimer.Tick += CountdownTimer_Tick;
countdownTimer.Start();
}
private void CountdownTimer_Tick(object sender, EventArgs e)
{
remainingSeconds--;
UpdateTimerDisplay();
if (remainingSeconds <= 0)
{
countdownTimer.Stop();
TimerTextBlock.Text = "Time's up!";
TimerTextBlock.Foreground = new SolidColorBrush(Colors.Green);
}
}
private void UpdateTimerDisplay()
{
int minutes = remainingSeconds / 60;
int seconds = remainingSeconds % 60;
TimerTextBlock.Text = $"{minutes:D2}:{seconds:D2}";
}
private void DoneButton_Click(object sender, RoutedEventArgs e)
{
Close();
}
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
countdownTimer?.Stop();
base.OnClosing(e);
}
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
Close();
}
private void Window_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (e.ButtonState == System.Windows.Input.MouseButtonState.Pressed)
{
DragMove();
}
}
}
}