-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
206 lines (182 loc) · 6.89 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Threading;
using System.Collections.ObjectModel;
using TimeManager.View;
using TimeManager.Core;
using TimeManager.Model;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
namespace TimeManager
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
DispatcherTimer dispatcherTimer = new DispatcherTimer();
SqlServer sql = new SqlServer();
private int hour, minute, second;
private bool _start = false;
private ObservableCollection<ProjectNames> projects = new();
public MainWindow()
{
InitializeComponent();
ProjectListComboBox.SelectedValuePath = "Name";
ProjectListComboBox.ItemsSource = projects;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
dispatcherTimer.Interval = TimeSpan.FromSeconds(1);
dispatcherTimer.Tick += dispatcherTimer_Tick;
hour = 0;
minute = 0;
second = 0;
Pause.IsEnabled = false;
if (sql.connect())
{
List<string> lastProject = sql.search("SELECT NAME FROM last");
List<string> projectNames = sql.search("SELECT NAME FROM project");
for (int size = 0; size < projectNames.Count; size++)
{
projects.Add(new ProjectNames() { Name = projectNames[size] });
}
if (lastProject.Count > 0)
{
ProjectListComboBox.SelectedValue = lastProject[0];
ViewMessageUpdate(lastProject[0]);
}
}
}
private void ViewMessageUpdate(string itemText)
{
List<ProjectMessages> message = sql.searchMessage("SELECT * FROM project WHERE NAME=\"" +
itemText + "\"");
if (message.Count > 0)
{
CountTime.Content = message[0].CountTime;
Times.Content = message[0].Times;
}
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Pause_Click(null, null);
if (MessageBox.Show("确定要退出吗?", Title.ToString(), MessageBoxButton.YesNo,
MessageBoxImage.Question) == MessageBoxResult.Yes)
{
e.Cancel = false;
if (ProjectListComboBox.Text != String.Empty)
sql.UpdateLast(ProjectListComboBox.Text);
Stop.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent));
sql.close();
}
else
{
Start_Click(null, null);
e.Cancel = true;
}
}
private void Start_Click(object sender, RoutedEventArgs e)
{
if (!ProjectListComboBox.HasItems)
{
MessageBox.Show("无计时项目!", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
dispatcherTimer.Start();
_start = true;
Start.IsEnabled = false;
Pause.IsEnabled = true;
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
second++;
if (second == 60)
{
minute++;
if (minute == 60)
{
hour++;
minute = 0;
}
second = 0;
}
NowTime.Content = $"{hour.ToString().PadLeft(4,'0')}:{minute.ToString().PadLeft(2, '0')}:" +
$"{second.ToString().PadLeft(2, '0')}";
}
private void Pause_Click(object sender, RoutedEventArgs e)
{
dispatcherTimer.Stop();
Start.IsEnabled = true;
Pause.IsEnabled = false;
}
private void Stop_Click(object sender, RoutedEventArgs e)
{
dispatcherTimer.Stop();
Start.IsEnabled = true;
Pause.IsEnabled = false;
if (!_start)
return;
_start = false;
if (minute < 1)
{
MessageBox.Show("计时未到1分钟,无法打卡,本次不会记录", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
initTime();
return;
}
else
{
MessageBox.Show("打卡成功", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
}
string? nowTime = NowTime.Content.ToString();
initTime();
sql.Update(ProjectListComboBox.Text, nowTime);
ViewMessageUpdate(ProjectListComboBox.Text);
}
private void initTime()
{
NowTime.Content = "0000:00:00";
hour = 0;
minute = 0;
second = 0;
}
private void NewProject_Click(object sender, RoutedEventArgs e)
{
NewProjectWin newProjectWin = new NewProjectWin(ref sql);
newProjectWin.CreateSuccess += new MainCreateSuccess(MainAddProjectName);
newProjectWin.Show();
}
public void MainAddProjectName(string projectName)
{
projects.Add(new ProjectNames() { Name = projectName });
ProjectListComboBox.SelectedValue = projectName;
}
private void DeleteProject_Click(object sender, RoutedEventArgs e)
{
deleteProjectWin deleteProjectWin = new deleteProjectWin();
deleteProjectWin.DeleteList.ItemsSource = projects;
deleteProjectWin.DeleteList.SelectedValue = ProjectListComboBox.Text;
deleteProjectWin.DeleteSuccess += new MainDeleteSuccess(MainDeleteProjectName);
deleteProjectWin.Show();
}
private void ProjectListComboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if ((sender as ComboBox).SelectedValue == null)
ViewMessageUpdate((ProjectListComboBox.SelectedIndex = 0).ToString());
else
ViewMessageUpdate((sender as ComboBox).SelectedValue.ToString());
}
public void MainDeleteProjectName(object item)
{
if (projects.Count > 0)
sql.delete("DELETE FROM project WHERE NAME=\"" + (item as ProjectNames).Name + "\"");
projects.Remove(item as ProjectNames);
}
private void ProjectList_Click(object sender, RoutedEventArgs e)
{
ProjectManageWin projectManageWin = new ProjectManageWin();
projectManageWin.Show();
}
}
}