-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
143 lines (126 loc) · 5.39 KB
/
MainWindow.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
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;
namespace VacationCalculator
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public static DateTime DefaultStartDate { get; } = new DateTime(2020, 6, 17, 0, 0, 0);
public MainWindow()
{
InitializeComponent();
futureDatePicker.SelectedDate = DateTime.Now;
ToggleFutureVacationInformationVisibility();
startDatePicker.SelectedDate = DefaultStartDate;
totalHoursValueLabel.Content = Helpers.GetVacationHours(DefaultStartDate, DateTime.Now);
ToggleErrorMessage();
}
private void StartDateChanged(object sender, SelectionChangedEventArgs e)
{
totalHoursValueLabel.Content = Helpers.GetVacationHours(startDatePicker!.SelectedDate!.Value, DateTime.Now);
}
private void CurrentVacationHoursEntered(object sender, TextChangedEventArgs e)
{
if(double.TryParse(currentHoursTextBox.Text, out _))
{
ToggleErrorMessage(alwaysHidden: true);
ToggleFutureVacationInformationVisibility(alwaysVisible: true);
}
else
{
ToggleFutureVacationInformationVisibility(alwaysHidden: true);
ToggleErrorMessage(alwaysVisible: true, message: $"'{currentHoursTextBox.Text}' is not a valid number.");
}
}
private void FutureDateChanged(object sender, SelectionChangedEventArgs e)
{
GetFutureVacationTime();
}
private void PlannedTimeOffEntered(object sender, TextChangedEventArgs e)
{
GetFutureVacationTime();
}
private void ToggleErrorMessage(bool alwaysVisible = false, bool alwaysHidden = false, string message = "Something went wrong!")
{
// Hide
if(alwaysHidden || errorLabel.Visibility == Visibility.Visible && !alwaysVisible)
{
errorLabel.Content = "";
errorLabel.Visibility = Visibility.Hidden;
}
else
{
errorLabel.Content = message;
errorLabel.Visibility = Visibility.Visible;
}
}
private void GetFutureVacationTime()
{
if (startDatePicker is not null && futureDatePicker is not null)
if (startDatePicker.SelectedDate is not null && futureDatePicker.SelectedDate is not null)
{
if (double.TryParse(currentHoursTextBox.Text, out double currentHours))
{
if (double.TryParse(plannedTimeOffTextBox.Text, out double plannedTimeOff))
{
ToggleErrorMessage(alwaysHidden: true);
totalVacationLeftValueLabel.Content = Helpers.GetFutureVacationHorusFromCurrentVacationTime(
startDatePicker.SelectedDate.Value,
futureDatePicker.SelectedDate.Value,
currentHours,
plannedTimeOff);
}
else
{
ToggleErrorMessage(alwaysVisible: true, message: $"'{plannedTimeOffTextBox.Text}' is not a valid number.");
}
}
else
{
ToggleErrorMessage(alwaysVisible: true, message: $"'{currentHoursTextBox.Text}' is not a valid number.");
}
}
}
private void ToggleFutureVacationInformationVisibility(bool alwaysVisible = false, bool alwaysHidden = false)
{
// Hide
if (alwaysHidden || futureDateLabel.Visibility == Visibility.Visible && !alwaysVisible)
{
futureDateLabel.Visibility = Visibility.Hidden;
futureDatePicker.Visibility = Visibility.Hidden;
totalVacationLeftlabel.Visibility = Visibility.Hidden;
totalVacationLeftValueLabel.Visibility = Visibility.Hidden;
plannedTimeOffLabel.Visibility = Visibility.Hidden;
plannedTimeOffTextBox.Visibility = Visibility.Hidden;
}
// Show
else
{
futureDateLabel.Visibility = Visibility.Visible;
futureDatePicker.Visibility = Visibility.Visible;
totalVacationLeftlabel.Visibility = Visibility.Visible;
totalVacationLeftValueLabel.Visibility = Visibility.Visible;
plannedTimeOffLabel.Visibility = Visibility.Visible;
plannedTimeOffTextBox.Visibility = Visibility.Visible;
// Set values
futureDatePicker.SelectedDate = DateTime.Now;
totalVacationLeftValueLabel.Content = currentHoursTextBox.Text;
plannedTimeOffTextBox.Text = "0";
}
}
}
}