166 lines
5.0 KiB
C#
166 lines
5.0 KiB
C#
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
using CommunityToolkit.Mvvm.Messaging;
|
|
using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using TodoList.Models;
|
|
using TodoList.Services;
|
|
|
|
namespace TodoList.ViewModels
|
|
{
|
|
public partial class MainViewModel : ObservableObject, IRecipient<TaskAddedMessage>
|
|
{
|
|
private readonly IDataService _dataService;
|
|
private readonly SettingsService _settingsService;
|
|
|
|
[ObservableProperty]
|
|
private ObservableCollection<TodoItem> tasks = new();
|
|
|
|
[ObservableProperty]
|
|
private bool showCompleted = false;
|
|
|
|
[ObservableProperty]
|
|
private string newContent;
|
|
|
|
[ObservableProperty]
|
|
private TodoPriority newPriority = TodoPriority.Medium;
|
|
|
|
[ObservableProperty]
|
|
private bool isSettingsOpen;
|
|
|
|
[ObservableProperty]
|
|
[NotifyPropertyChangedFor(nameof(FullShortcut))]
|
|
private string shortcutKey;
|
|
|
|
[ObservableProperty]
|
|
[NotifyPropertyChangedFor(nameof(FullShortcut))]
|
|
private string shortcutModifiers;
|
|
|
|
public string AppVersion => System.Reflection.Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "1.0.0";
|
|
|
|
public string FullShortcut
|
|
{
|
|
get
|
|
{
|
|
var mods = ShortcutModifiers?.Replace(",", " + ");
|
|
return string.IsNullOrEmpty(mods) ? ShortcutKey : $"{mods} + {ShortcutKey}";
|
|
}
|
|
}
|
|
|
|
public MainViewModel(IDataService dataService, SettingsService settingsService)
|
|
{
|
|
_dataService = dataService;
|
|
_settingsService = settingsService;
|
|
ShortcutKey = _settingsService.Settings.ShortcutKey;
|
|
ShortcutModifiers = _settingsService.Settings.ShortcutModifiers;
|
|
|
|
WeakReferenceMessenger.Default.Register(this);
|
|
LoadTasksCommand.Execute(null);
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void OpenSettings()
|
|
{
|
|
IsSettingsOpen = true;
|
|
ShortcutKey = _settingsService.Settings.ShortcutKey;
|
|
ShortcutModifiers = _settingsService.Settings.ShortcutModifiers;
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void CloseSettings()
|
|
{
|
|
IsSettingsOpen = false;
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void SaveSettings()
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(ShortcutKey))
|
|
{
|
|
_settingsService.Settings.ShortcutKey = ShortcutKey.ToUpper();
|
|
_settingsService.Settings.ShortcutModifiers = ShortcutModifiers;
|
|
_settingsService.SaveSettings();
|
|
}
|
|
IsSettingsOpen = false;
|
|
}
|
|
|
|
async partial void OnShowCompletedChanged(bool value)
|
|
{
|
|
await LoadTasksAsync();
|
|
}
|
|
|
|
[RelayCommand]
|
|
private async Task AddTaskAsync()
|
|
{
|
|
if (string.IsNullOrWhiteSpace(NewContent)) return;
|
|
|
|
var newTask = new TodoItem
|
|
{
|
|
Content = NewContent,
|
|
Priority = NewPriority,
|
|
IsCompleted = false,
|
|
SyncStatus = SyncStatus.Pending
|
|
};
|
|
|
|
await _dataService.SaveTaskAsync(newTask);
|
|
NewContent = string.Empty;
|
|
NewPriority = TodoPriority.Medium;
|
|
await LoadTasksAsync();
|
|
}
|
|
|
|
[RelayCommand]
|
|
private async Task LoadTasksAsync()
|
|
{
|
|
var allTasks = await _dataService.LoadTasksAsync();
|
|
|
|
var filtered = ShowCompleted
|
|
? allTasks
|
|
: allTasks.Where(t => !t.IsCompleted).ToList();
|
|
|
|
// Sort: Uncompleted first, then by priority (High -> Low), then date
|
|
var sorted = filtered
|
|
.OrderBy(t => t.IsCompleted)
|
|
.ThenByDescending(t => t.Priority)
|
|
.ThenByDescending(t => t.CreatedAt)
|
|
.ToList();
|
|
|
|
Tasks.Clear();
|
|
foreach (var t in sorted)
|
|
{
|
|
Tasks.Add(t);
|
|
}
|
|
}
|
|
|
|
[RelayCommand]
|
|
private async Task ToggleCompleteAsync(TodoItem item)
|
|
{
|
|
if (item == null) return;
|
|
// item.IsCompleted is already toggled by UI binding before this command if TwoWay binding
|
|
// But usually CheckBox command parameter is the item.
|
|
// Let's assume the binding updates the property.
|
|
|
|
item.SyncStatus = SyncStatus.Pending; // Mark as pending sync
|
|
await _dataService.SaveTaskAsync(item);
|
|
await LoadTasksAsync(); // Refresh list to apply filter
|
|
}
|
|
|
|
[RelayCommand]
|
|
private async Task DeleteAsync(TodoItem item)
|
|
{
|
|
if (item == null) return;
|
|
await _dataService.DeleteTaskAsync(item.Id);
|
|
Tasks.Remove(item);
|
|
}
|
|
|
|
public async void Receive(TaskAddedMessage message)
|
|
{
|
|
await LoadTasksAsync();
|
|
}
|
|
}
|
|
|
|
public class TaskAddedMessage
|
|
{
|
|
}
|
|
}
|