You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
dotNetStudyWithGPT/MySolution/ToDoApp/MainWindow.xaml.cs

280 lines
7.4 KiB

2 years ago
using Microsoft.Extensions.DependencyInjection;
using System;
2 years ago
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;
2 years ago
using ToDoApp.Service;
using ToDoApp.SubControl;
using ToDoApp.SubWindow;
2 years ago
namespace ToDoApp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
2 years ago
private enum Mode
{
ToDo,
Today,
Planned,
Completed,
}
2 years ago
private User _user;
private Item _selectedItem;
2 years ago
private Mode _currentMode;
2 years ago
2 years ago
public MainWindow()
{
InitializeComponent();
if (!CheckDB())
{
MessageBox.Show("데이터베이스가 정상적이지 않아 프로그램을 종료합니다.");
Application.Current.Shutdown();
}
2 years ago
if (CheckLogin() != true)
{
Application.Current.Shutdown();
}
InitInstance();
}
private void InitInstance()
{
tbName.Text = _user.Name;
tbEmail.Text = _user.Email;
2 years ago
_currentMode = Mode.ToDo;
2 years ago
RefreshList();
2 years ago
}
private bool CheckDB()
{
try
{
2 years ago
DbService.Instance.EnsureCreated();
2 years ago
return true;
}
catch (Exception ex)
{
return false;
}
}
2 years ago
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()
{
2 years ago
string modeTitle = _currentMode.ToString();
List<Item> items = new List<Item>();
switch (_currentMode)
{
case Mode.ToDo:
default:
items = GetAllToDoItems();
break;
case Mode.Today:
items = GetTodayItems();
break;
case Mode.Planned:
items = GetPlannedItems();
break;
case Mode.Completed:
items = GetCompletedItems();
break;
}
tbTitle.Text = modeTitle;
SetItemList(items);
2 years ago
}
2 years ago
private List<Item> GetAllToDoItems()
2 years ago
{
return ItemService.Instance.FindAllNotCompletedItems(_user.Id);
}
2 years ago
private List<Item> GetTodayItems()
{
return ItemService.Instance.FindTodayItems(_user.Id);
}
private List<Item> GetPlannedItems()
{
return ItemService.Instance.FindPlannedItems(_user.Id);
}
private List<Item> GetCompletedItems()
{
return ItemService.Instance.FindCompletedItems(_user.Id);
}
private void SetItemList(List<Item> items)
2 years ago
{
List<ToDoListItem> list = new List<ToDoListItem>();
foreach (Item item in items)
{
2 years ago
ToDoListItem toDoItem = new ToDoListItem(item);
toDoItem.ItemUpdateHandler += ToDoItem_ItemUpdateHandler;
toDoItem.ItemRemoveHandler += ToDoItem_ItemRemoveHandler;
toDoItem.ItemListRefreshHandler += ToDoItem_ItemListRefreshHandler;
list.Add(toDoItem);
2 years ago
}
lbxContent.ItemsSource = list;
}
2 years ago
private void ToDoItem_ItemListRefreshHandler(object? sender, EventArgs e)
{
RefreshList();
}
private void ToDoItem_ItemRemoveHandler(object? sender, Item e)
{
ItemService.Instance.RemoveItem(e);
}
private void ToDoItem_ItemUpdateHandler(object? sender, Item e)
{
ItemService.Instance.UpdateItem(e);
}
2 years ago
private void SetSelectedItem(Item item)
{
_selectedItem = item;
tbxItemName.Text = _selectedItem.Title;
tbItemCreatedAt.Text = _selectedItem.CreateDate.ToString("yyyy년 MM월 dd일 ddd 생성됨");
2 years ago
chkItemToday.IsChecked = _selectedItem.IsToday == true ? true : false;
dpItemDueDate.SelectedDate = _selectedItem.DueDate;
tbxItemDescription.Text = _selectedItem.Description == null ? "" : _selectedItem.Description;
2 years ago
}
private void ClearSelectedItem()
{
_selectedItem = null;
tbxItemName.Text = "";
tbItemCreatedAt.Text = "";
dpItemDueDate.SelectedDate = null;
tbxItemDescription.Text = "";
}
private void tbxSearch_KeyDown(object sender, KeyEventArgs e)
{
}
private void btnUpdate_Click(object sender, RoutedEventArgs e)
{
2 years ago
_selectedItem.Title = tbxItemName.Text;
_selectedItem.IsToday = chkItemToday.IsChecked == true ? true : false;
_selectedItem.DueDate = dpItemDueDate.SelectedDate;
_selectedItem.Description = tbxItemDescription.Text;
2 years ago
2 years ago
ItemService.Instance.UpdateItem(_selectedItem);
RefreshList();
2 years ago
}
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);
}
2 years ago
private void btnToday_Click(object sender, RoutedEventArgs e)
{
_currentMode = Mode.Today;
RefreshList();
}
private void btnPlanned_Click(object sender, RoutedEventArgs e)
{
_currentMode = Mode.Planned;
RefreshList();
}
private void btnCompleted_Click(object sender, RoutedEventArgs e)
{
_currentMode = Mode.Completed;
RefreshList();
}
private void btnToDo_Click(object sender, RoutedEventArgs e)
{
_currentMode = Mode.ToDo;
RefreshList();
}
2 years ago
}
}