fix: 使用 CommunityToolkit.Mvvm

This commit is contained in:
Shiqvlizi Name
2026-05-28 23:26:37 +08:00
parent 47572fefb8
commit ef61ae7e6b
12 changed files with 310 additions and 512 deletions

View File

@@ -1,20 +1,29 @@
using System.Collections.ObjectModel;
using CloudPrint.Client.Infrastructure;
using CloudPrint.Client.Models;
using CloudPrint.Client.Services;
using CloudPrint.Client.Services.Abstractions;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Win32;
namespace CloudPrint.Client.ViewModels;
public sealed class LibraryViewModel : ObservableObject
public sealed partial class LibraryViewModel : ObservableObject
{
private readonly IFileService _fileService;
private readonly IPrintBackendClient _backendClient;
private readonly ClientSettings _settings;
private LibraryCategory? _selectedCategory;
private CloudPrintDocument? _selectedDocument;
private string _statusMessage = "文库就绪";
[ObservableProperty]
private LibraryCategory? selectedCategory;
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(RemoveDocumentCommand))]
[NotifyCanExecuteChangedFor(nameof(SyncSelectedCommand))]
private CloudPrintDocument? selectedDocument;
[ObservableProperty]
private string statusMessage = "文库就绪";
public LibraryViewModel()
: this(new FileService(), new GrpcService(), new ClientSettings())
@@ -33,51 +42,18 @@ public sealed class LibraryViewModel : ObservableObject
Categories.Add(new LibraryCategory { Id = 4, Name = "其他文件", Icon = "📁", SortOrder = 99 });
SelectedCategory = Categories.FirstOrDefault();
AddDocumentCommand = new RelayCommand(AddDocument);
RemoveDocumentCommand = new RelayCommand(RemoveSelectedDocument, () => SelectedDocument is not null);
SyncSelectedCommand = new AsyncRelayCommand(SyncSelectedAsync, () => SelectedDocument is not null && _backendClient.IsConnected);
SyncAllCommand = new AsyncRelayCommand(SyncAllAsync, () => Documents.Any(document => !document.IsUploaded) && _backendClient.IsConnected);
}
public ObservableCollection<LibraryCategory> Categories { get; } = new();
public ObservableCollection<CloudPrintDocument> Documents { get; } = new();
public LibraryCategory? SelectedCategory
{
get => _selectedCategory;
set => SetProperty(ref _selectedCategory, value);
}
public CloudPrintDocument? SelectedDocument
{
get => _selectedDocument;
set
{
if (SetProperty(ref _selectedDocument, value))
{
RemoveDocumentCommand.RaiseCanExecuteChanged();
SyncSelectedCommand.RaiseCanExecuteChanged();
}
}
}
public string StatusMessage
{
get => _statusMessage;
private set => SetProperty(ref _statusMessage, value);
}
public RelayCommand AddDocumentCommand { get; }
public RelayCommand RemoveDocumentCommand { get; }
public AsyncRelayCommand SyncSelectedCommand { get; }
public AsyncRelayCommand SyncAllCommand { get; }
public void RefreshCommandState()
{
SyncSelectedCommand.RaiseCanExecuteChanged();
SyncAllCommand.RaiseCanExecuteChanged();
SyncSelectedCommand.NotifyCanExecuteChanged();
SyncAllCommand.NotifyCanExecuteChanged();
}
[RelayCommand]
private void AddDocument()
{
var dialog = new Microsoft.Win32.OpenFileDialog
@@ -114,7 +90,10 @@ public sealed class LibraryViewModel : ObservableObject
RefreshCommandState();
}
private void RemoveSelectedDocument()
private bool CanRemoveDocument() => SelectedDocument is not null;
[RelayCommand(CanExecute = nameof(CanRemoveDocument))]
private void RemoveDocument()
{
if (SelectedDocument is null)
{
@@ -128,6 +107,9 @@ public sealed class LibraryViewModel : ObservableObject
RefreshCommandState();
}
private bool CanSyncSelected() => SelectedDocument is not null && _backendClient.IsConnected;
[RelayCommand(CanExecute = nameof(CanSyncSelected))]
private async Task SyncSelectedAsync()
{
if (SelectedDocument is null)
@@ -140,6 +122,9 @@ public sealed class LibraryViewModel : ObservableObject
RefreshCommandState();
}
private bool CanSyncAll() => Documents.Any(document => !document.IsUploaded) && _backendClient.IsConnected;
[RelayCommand(CanExecute = nameof(CanSyncAll))]
private async Task SyncAllAsync()
{
var pendingDocuments = Documents.Where(document => !document.IsUploaded).ToList();
@@ -151,4 +136,4 @@ public sealed class LibraryViewModel : ObservableObject
StatusMessage = $"已同步 {pendingDocuments.Count} 个文库文件";
RefreshCommandState();
}
}
}