-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathItemDialog.xaml.cs
151 lines (126 loc) · 4.41 KB
/
ItemDialog.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
using System;
using System.Windows;
using System.Windows.Controls;
using TimelineCreator.Controls;
namespace TimelineCreator
{
public partial class ItemDialog : Window
{
private readonly TimeZoneInfo timeZone;
public TimelineItem Item { get; private set; }
public DateTime? TZeroTime { get; set; } = null;
public bool IsTZeroMode { get; set; } = false;
public bool WasDeleted { get; private set; } = false;
private readonly bool isEditing = false;
public ItemDialog(TimeZoneInfo timeZone)
{
this.timeZone = timeZone;
Item = new TimelineItem()
{
DateTime = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(UtcNowNoMillis(), timeZone.Id)
};
InitializeComponent();
}
public ItemDialog(TimeZoneInfo timeZone, TimelineItem item)
{
isEditing = true;
this.timeZone = timeZone;
Item = item;
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
if (isEditing)
{
Title = "Edit Timeline Item";
submitButton.Content = "Save Item";
deleteButton.Visibility = Visibility.Visible;
theTextBox.Text = Item.Text;
}
theTimeField.Value = Item.DateTime;
importantCheckBox.IsChecked = Item.IsImportant;
if (TZeroTime != null)
{
tZeroCheckBox.IsEnabled = true;
if (IsTZeroMode)
{
// TZeroCheckBox_CheckedChanged() will take care of the rest
tZeroCheckBox.IsChecked = true;
}
}
else
{
theTimeField.Focus();
}
}
private void TheTimeField_ValueChanged(object sender, DateTimeValueChangedEventArgs e)
{
submitButton.IsEnabled = e.Value != null;
}
private void TheTZeroField_ValueChanged(object sender, TZeroValueChangedEventArgs e)
{
submitButton.IsEnabled = e.Value != null;
}
private void Field_ValidationError(object sender, EventArgs e)
{
submitButton.IsEnabled = false;
}
private void NowButton_Click(object sender, RoutedEventArgs e)
{
DateTime now = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(UtcNowNoMillis(), timeZone.Id);
if (tZeroCheckBox.IsChecked == false)
{
theTimeField.Value = now;
}
else
{
theTZeroField.Value = now - TZeroTime;
}
}
private void TZeroCheckBox_CheckedChanged(object sender, RoutedEventArgs e)
{
if (((CheckBox)sender).IsChecked == true)
{
theTZeroField.Value = theTimeField.Value - TZeroTime;
theTimeField.Visibility = Visibility.Collapsed;
theTZeroField.Visibility = Visibility.Visible;
theTZeroField.Focus();
}
else
{
theTimeField.Value = TZeroTime + theTZeroField.Value;
theTZeroField.Visibility = Visibility.Collapsed;
theTimeField.Visibility = Visibility.Visible;
theTimeField.Focus();
}
}
private void SubmitButton_Click(object sender, RoutedEventArgs e)
{
if (tZeroCheckBox.IsChecked == true)
{
Item.DateTime = (DateTime)(TZeroTime + theTZeroField.Value)!;
}
else
{
Item.DateTime = (DateTime)theTimeField.Value!;
}
Item.Text = theTextBox.Text;
Item.IsImportant = importantCheckBox.IsChecked == true;
DialogResult = true;
}
private void DeleteButton_Click(object sender, RoutedEventArgs e)
{
WasDeleted = true;
DialogResult = true;
}
/// <summary>
/// Gets the current UTC time with the milliseconds zeroed out.
/// </summary>
private static DateTime UtcNowNoMillis()
{
DateTime now = DateTime.UtcNow;
return new DateTime(now.Year, now.Month, now.Day,
now.Hour, now.Minute, now.Second, now.Kind);
}
}
}