fix: 使用 CommunityToolkit.Mvvm
This commit is contained in:
@@ -1,18 +1,28 @@
|
||||
using CloudPrint.Client.Models;
|
||||
using CloudPrint.Client.Infrastructure;
|
||||
using CloudPrint.Client.Services;
|
||||
using CloudPrint.Client.Services.Abstractions;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using System.IO;
|
||||
|
||||
namespace CloudPrint.Client.ViewModels;
|
||||
|
||||
public sealed class SettingsViewModel : ObservableObject
|
||||
public sealed partial class SettingsViewModel : ObservableObject
|
||||
{
|
||||
private readonly ISettingsService _settingsService;
|
||||
private readonly IPrintBackendClient _backendClient;
|
||||
private readonly IAppLogger _appLogger;
|
||||
private string _statusMessage = "设置就绪";
|
||||
private string _adminPassword = string.Empty;
|
||||
|
||||
[ObservableProperty]
|
||||
private string statusMessage = "设置就绪";
|
||||
|
||||
[ObservableProperty]
|
||||
private string adminPassword = string.Empty;
|
||||
|
||||
[ObservableProperty]
|
||||
[NotifyPropertyChangedFor(nameof(AdminLoginStatus))]
|
||||
[NotifyCanExecuteChangedFor(nameof(LogoutAdminCommand))]
|
||||
private bool isAdminLoggedIn;
|
||||
|
||||
public SettingsViewModel()
|
||||
: this(new JsonSettingsService().Load(), new JsonSettingsService(), new GrpcService(), new FileAppLogger())
|
||||
@@ -25,11 +35,6 @@ public sealed class SettingsViewModel : ObservableObject
|
||||
_settingsService = settingsService;
|
||||
_backendClient = backendClient;
|
||||
_appLogger = appLogger;
|
||||
SaveCommand = new RelayCommand(Save);
|
||||
SelectLibraryRootCommand = new RelayCommand(SelectLibraryRoot);
|
||||
LoginAdminCommand = new AsyncRelayCommand(LoginAdminAsync);
|
||||
LogoutAdminCommand = new RelayCommand(LogoutAdmin, () => IsAdminLoggedIn);
|
||||
OpenLogDirectoryCommand = new RelayCommand(OpenLogDirectory);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(Settings.AdminAccessToken))
|
||||
{
|
||||
@@ -40,47 +45,18 @@ public sealed class SettingsViewModel : ObservableObject
|
||||
|
||||
public ClientSettings Settings { get; }
|
||||
|
||||
public string StatusMessage
|
||||
{
|
||||
get => _statusMessage;
|
||||
private set => SetProperty(ref _statusMessage, value);
|
||||
}
|
||||
|
||||
public RelayCommand SaveCommand { get; }
|
||||
public RelayCommand SelectLibraryRootCommand { get; }
|
||||
public AsyncRelayCommand LoginAdminCommand { get; }
|
||||
public RelayCommand LogoutAdminCommand { get; }
|
||||
public RelayCommand OpenLogDirectoryCommand { get; }
|
||||
public string LogDirectory => _appLogger.LogDirectory;
|
||||
|
||||
private bool _isAdminLoggedIn;
|
||||
public bool IsAdminLoggedIn
|
||||
{
|
||||
get => _isAdminLoggedIn;
|
||||
private set
|
||||
{
|
||||
if (SetProperty(ref _isAdminLoggedIn, value))
|
||||
{
|
||||
OnPropertyChanged(nameof(AdminLoginStatus));
|
||||
LogoutAdminCommand.RaiseCanExecuteChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public string AdminLoginStatus => IsAdminLoggedIn ? $"已登录:{Settings.AdminUsername}" : "未登录";
|
||||
|
||||
public string AdminPassword
|
||||
{
|
||||
get => _adminPassword;
|
||||
set => SetProperty(ref _adminPassword, value);
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private void Save()
|
||||
{
|
||||
_settingsService.Save(Settings);
|
||||
StatusMessage = "设置已保存";
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private async Task LoginAdminAsync()
|
||||
{
|
||||
try
|
||||
@@ -106,6 +82,9 @@ public sealed class SettingsViewModel : ObservableObject
|
||||
}
|
||||
}
|
||||
|
||||
private bool CanLogoutAdmin() => IsAdminLoggedIn;
|
||||
|
||||
[RelayCommand(CanExecute = nameof(CanLogoutAdmin))]
|
||||
private void LogoutAdmin()
|
||||
{
|
||||
_backendClient.LogoutAdmin();
|
||||
@@ -124,6 +103,7 @@ public sealed class SettingsViewModel : ObservableObject
|
||||
}
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private void SelectLibraryRoot()
|
||||
{
|
||||
using var dialog = new System.Windows.Forms.FolderBrowserDialog
|
||||
@@ -142,6 +122,7 @@ public sealed class SettingsViewModel : ObservableObject
|
||||
}
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private void OpenLogDirectory()
|
||||
{
|
||||
Directory.CreateDirectory(_appLogger.LogDirectory);
|
||||
@@ -152,4 +133,4 @@ public sealed class SettingsViewModel : ObservableObject
|
||||
};
|
||||
System.Diagnostics.Process.Start(startInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user