-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
201 lines (177 loc) · 6.57 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
using System;
using System.IO;
using System.Collections.ObjectModel;
using System.Linq;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace ToDoApp
{
public partial class MainWindow : Window
{
public ObservableCollection<TaskItem> TaskItems { get; set; }
public ObservableCollection<TaskItem> CompletedTaskItems { get; set; }
private System.Timers.Timer _timer;
public MainWindow()
{
InitializeComponent();
TaskItems = new ObservableCollection<TaskItem>();
CompletedTaskItems = new ObservableCollection<TaskItem>();
TaskListBox.ItemsSource = TaskItems;
CompletedTaskListBox.ItemsSource = CompletedTaskItems;
LoadTasks();
_timer = new System.Timers.Timer(1000);
_timer.Elapsed += TimerElapsed;
_timer.Start();
}
private void LoadTasks()
{
if (File.Exists("tasks.txt"))
{
var lines = File.ReadAllLines("tasks.txt");
foreach (var line in lines)
{
var parts = line.Split('|');
if (parts.Length == 3 && DateTime.TryParse(parts[1], out var deadline))
{
var taskItem = new TaskItem
{
Description = parts[0],
Deadline = deadline,
IsCompleted = bool.Parse(parts[2]),
ForegroundColor = System.Windows.Media.Brushes.Black
};
taskItem.TaskCompleted += TaskItem_TaskCompleted;
taskItem.TaskDeleted += TaskItem_TaskDeleted;
if (taskItem.IsCompleted)
{
CompletedTaskItems.Add(taskItem);
}
else
{
TaskItems.Add(taskItem);
}
}
}
}
}
private void SaveTasks()
{
var lines = TaskItems.Select(t => $"{t.Description}|{t.Deadline}|{t.IsCompleted}")
.Concat(CompletedTaskItems.Select(t => $"{t.Description}|{t.Deadline}|{t.IsCompleted}"))
.ToArray();
File.WriteAllLines("tasks.txt", lines); // Uložení úkolů do souboru
}
private void btnDelete_Click(object sender, RoutedEventArgs e)
{
if (sender is Button button && button.Tag is TaskItem taskItem)
{
taskItem.DeleteTask();
}
}
private void TaskItem_TaskDeleted(object sender, EventArgs e)
{
if (sender is TaskItem taskItem)
{
TaskItems.Remove(taskItem);
CompletedTaskItems.Remove(taskItem);
SaveTasks();
}
}
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DragMove();
}
private void btnMin_Click(object sender, RoutedEventArgs e)
{
WindowState = WindowState.Minimized;
}
private void btnMax_Click(object sender, RoutedEventArgs e)
{
if (WindowState == WindowState.Maximized) WindowState = WindowState.Normal;
else WindowState = WindowState.Maximized;
}
private void btnClose_Click(object sender, RoutedEventArgs e)
{
Close();
//Application.Current.Shutdown();
}
private void TimerElapsed(object sender, ElapsedEventArgs e)
{
Dispatcher.Invoke(() =>
{
foreach (var taskItem in TaskItems.ToList())
{
if (taskItem.Deadline <= DateTime.Now && !taskItem.IsCompleted)
{
taskItem.IsCompleted = true;
}
}
});
}
private void TaskInput_TextChanged(object sender, TextChangedEventArgs e)
{
if (string.IsNullOrEmpty(TaskInput.Text))
{
PlaceholderTextBlock.Visibility = Visibility.Visible;
}
else
{
PlaceholderTextBlock.Visibility = Visibility.Collapsed;
}
}
private void AddTaskButton_Click(object sender, RoutedEventArgs e)
{
if (!string.IsNullOrWhiteSpace(TaskInput.Text) && TaskDatePicker.SelectedDate.HasValue)
{
var time = ((ComboBoxItem)TaskTimePicker.SelectedItem)?.Content.ToString();
if (time == null)
{
MessageBox.Show("Please select a time for the task.");
return;
}
var deadline = DateTime.Parse(TaskDatePicker.SelectedDate.Value.ToString("yyyy-MM-dd") + " " + time);
var taskItem = new TaskItem
{
Description = TaskInput.Text,
Deadline = deadline,
ForegroundColor = System.Windows.Media.Brushes.Black
};
taskItem.TaskCompleted += TaskItem_TaskCompleted;
taskItem.TaskDeleted += TaskItem_TaskDeleted;
TaskItems.Add(taskItem);
SaveTasks();
TaskInput.Text = string.Empty;
TaskDatePicker.SelectedDate = null;
TaskTimePicker.SelectedIndex = -1;
}
else
{
MessageBox.Show("Please enter a task description and select a date.");
}
}
private void TaskItem_TaskCompleted(object sender, EventArgs e)
{
if (sender is TaskItem taskItem)
{
TaskItems.Remove(taskItem);
CompletedTaskItems.Add(taskItem);
SaveTasks();
}
}
private void ShowCompletedTasksButton_Click(object sender, RoutedEventArgs e)
{
if (CompletedTaskListBox.Visibility == Visibility.Collapsed)
{
CompletedTaskListBox.Visibility = Visibility.Visible;
ShowCompletedTasksButton.Content = "Hide Completed";
}
else
{
CompletedTaskListBox.Visibility = Visibility.Collapsed;
ShowCompletedTasksButton.Content = "Show Completed";
}
}
}
}