forked from Study/Blazor
75 lines
2.2 KiB
C#
75 lines
2.2 KiB
C#
using BlazorApp.PrefixCSharp;
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
namespace BlazorApp.Components.Pages.TodoPage
|
|
{
|
|
|
|
public partial class Todo : ComponentBase
|
|
{
|
|
private List<TodoItem> todos = new();
|
|
private List<TodoItem> doneTodos = new();
|
|
private string? newTodo;
|
|
|
|
private void AddTodo()
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(newTodo))
|
|
{
|
|
todos.Add(new TodoItem
|
|
{
|
|
Id = (new Func<int>(delegate
|
|
{
|
|
int result = 0;
|
|
|
|
if (doneTodos.Count > 0)
|
|
result = doneTodos[0].Id + 1;
|
|
|
|
if (todos.Count > 0)
|
|
result = todos[todos.Count - 1].Id + 1 > result ? todos[todos.Count - 1].Id + 1 : result;
|
|
|
|
return result;
|
|
|
|
}))(),
|
|
Title = newTodo,
|
|
InputDate = DateTime.Now.ToString("yyyy-MM-dd")
|
|
});
|
|
newTodo = string.Empty;
|
|
}
|
|
}
|
|
|
|
private void ToggleTodo(TodoItem todo, int index)
|
|
{
|
|
|
|
if (todo.IsDone)
|
|
{
|
|
todo.SuccessDate = string.Empty;
|
|
todo.IsDone = Prefix.Toggle(todo.IsDone);
|
|
|
|
doneTodos.RemoveAt(index);
|
|
todos.Add(todo);
|
|
todos = SortList(todos);
|
|
}
|
|
else
|
|
{
|
|
todo.SuccessDate = DateTime.Now.ToString("yyyy-MM-dd");
|
|
todo.IsDone = Prefix.Toggle(todo.IsDone);
|
|
|
|
todos.RemoveAt(index);
|
|
doneTodos.Add(todo);
|
|
doneTodos = SortList(doneTodos, true);
|
|
}
|
|
}
|
|
|
|
private List<TodoItem> SortList(List<TodoItem> list, bool reverse = false)
|
|
{
|
|
return reverse ? list.OrderByDescending(todo => todo.Id).ToList() : list.OrderBy(todo => todo.Id).ToList();
|
|
}
|
|
|
|
private void RemoveList(TodoItem todo, int index)
|
|
{
|
|
if (todo.IsDone)
|
|
doneTodos.RemoveAt(index);
|
|
else
|
|
todos.RemoveAt(index);
|
|
}
|
|
}
|
|
} |