using Microsoft.Extensions.DependencyInjection; 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; using ToDoApp.Context; using ToDoApp.Model; using ToDoApp.Service; using ToDoApp.SubControl; using ToDoApp.SubWindow; namespace ToDoApp { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { private User _user; private Item _selectedItem; public MainWindow() { InitializeComponent(); if (!CheckDB()) { MessageBox.Show("데이터베이스가 정상적이지 않아 프로그램을 종료합니다."); Application.Current.Shutdown(); } if (CheckLogin() != true) { Application.Current.Shutdown(); } InitInstance(); } private void InitInstance() { tbName.Text = _user.Name; tbEmail.Text = _user.Email; RefreshList(); } private bool CheckDB() { try { DbService.Instance.EnsureCreated(); return true; } catch (Exception ex) { return false; } } private bool? CheckLogin() { try { LoginWindow dlg = new LoginWindow(); bool? isOk = dlg.ShowDialog(); _user = dlg.User; return isOk; } catch (Exception ex) { return false; } } private void RefreshList() { List items = GetAllItems(); SetToDoList(items); } private List GetAllItems() { return ItemService.Instance.FindAllNotCompletedItems(_user.Id); } private void SetToDoList(List items) { List list = new List(); foreach (Item item in items) { list.Add(new ToDoListItem(item)); } lbxContent.ItemsSource = list; } private void SetSelectedItem(Item item) { _selectedItem = item; tbxItemName.Text = _selectedItem.Title; tbItemCreatedAt.Text = _selectedItem.CreateDate.ToString("yyyy년 MM월 dd일 ddd 생성됨"); if (_selectedItem.DueDate != null) dpItemDueDate.SelectedDate = _selectedItem.DueDate; if (_selectedItem.Description != null) tbxItemDescription.Text = _selectedItem.Description; } private void ClearSelectedItem() { _selectedItem = null; tbxItemName.Text = ""; tbItemCreatedAt.Text = ""; dpItemDueDate.SelectedDate = null; tbxItemDescription.Text = ""; } private void tbxNewItemName_KeyDown(object sender, KeyEventArgs e) { } private void tbxSearch_KeyDown(object sender, KeyEventArgs e) { } private void btnUpdate_Click(object sender, RoutedEventArgs e) { } private void btnDelete_Click(object sender, RoutedEventArgs e) { if (_selectedItem == null) return; ItemService.Instance.RemoveItem(_selectedItem.Id); ClearSelectedItem(); RefreshList(); } private void tbxNewItem_KeyDown(object sender, KeyEventArgs e) { if (e.Key != Key.Enter) return; PlaceholderTextBox textBox = sender as PlaceholderTextBox; if (textBox == null || string.IsNullOrEmpty(textBox.Text)) return; Item newItem = new Item() { UserId = _user.Id, Title = textBox.Text, }; ItemService.Instance.CreateItem(newItem); textBox.Text = ""; RefreshList(); } private void lbxContent_SelectionChanged(object sender, SelectionChangedEventArgs e) { ToDoListItem toDoListItem = lbxContent.SelectedItem as ToDoListItem; if (toDoListItem == null) return; SetSelectedItem(toDoListItem.Item); } } }