137 lines
4.3 KiB
C#
137 lines
4.3 KiB
C#
using CloudPrint.Client.Models;
|
|
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 partial class SettingsViewModel : ObservableObject
|
|
{
|
|
private readonly ISettingsService _settingsService;
|
|
private readonly IPrintBackendClient _backendClient;
|
|
private readonly IAppLogger _appLogger;
|
|
|
|
[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())
|
|
{
|
|
}
|
|
|
|
public SettingsViewModel(ClientSettings settings, ISettingsService settingsService, IPrintBackendClient backendClient, IAppLogger appLogger)
|
|
{
|
|
Settings = settings;
|
|
_settingsService = settingsService;
|
|
_backendClient = backendClient;
|
|
_appLogger = appLogger;
|
|
|
|
if (!string.IsNullOrWhiteSpace(Settings.AdminAccessToken))
|
|
{
|
|
_backendClient.SetAdminToken(Settings.AdminUsername, Settings.AdminAccessToken);
|
|
IsAdminLoggedIn = true;
|
|
}
|
|
}
|
|
|
|
public ClientSettings Settings { get; }
|
|
|
|
public string LogDirectory => _appLogger.LogDirectory;
|
|
|
|
public string AdminLoginStatus => IsAdminLoggedIn ? $"已登录:{Settings.AdminUsername}" : "未登录";
|
|
|
|
[RelayCommand]
|
|
private void Save()
|
|
{
|
|
_settingsService.Save(Settings);
|
|
StatusMessage = "设置已保存";
|
|
}
|
|
|
|
[RelayCommand]
|
|
private async Task LoginAdminAsync()
|
|
{
|
|
try
|
|
{
|
|
ConfigureBackendMode();
|
|
if (!_backendClient.IsConnected)
|
|
{
|
|
await _backendClient.ConnectAsync(Settings.ServerAddress, Settings.ApiKey).ConfigureAwait(true);
|
|
}
|
|
|
|
var session = await _backendClient.LoginAdminAsync(Settings.AdminUsername, AdminPassword).ConfigureAwait(true);
|
|
Settings.AdminUsername = session.Username;
|
|
Settings.AdminAccessToken = session.AccessToken;
|
|
AdminPassword = string.Empty;
|
|
IsAdminLoggedIn = true;
|
|
_settingsService.Save(Settings);
|
|
StatusMessage = $"管理员登录成功:{session.DisplayName}";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
IsAdminLoggedIn = false;
|
|
StatusMessage = $"管理员登录失败:{ex.Message}";
|
|
}
|
|
}
|
|
|
|
private bool CanLogoutAdmin() => IsAdminLoggedIn;
|
|
|
|
[RelayCommand(CanExecute = nameof(CanLogoutAdmin))]
|
|
private void LogoutAdmin()
|
|
{
|
|
_backendClient.LogoutAdmin();
|
|
Settings.AdminAccessToken = string.Empty;
|
|
AdminPassword = string.Empty;
|
|
IsAdminLoggedIn = false;
|
|
_settingsService.Save(Settings);
|
|
StatusMessage = "管理员已退出登录";
|
|
}
|
|
|
|
private void ConfigureBackendMode()
|
|
{
|
|
if (_backendClient is GrpcService grpcService)
|
|
{
|
|
grpcService.AllowInsecureGrpc = Settings.AllowInsecureGrpc;
|
|
}
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void SelectLibraryRoot()
|
|
{
|
|
using var dialog = new System.Windows.Forms.FolderBrowserDialog
|
|
{
|
|
Description = "选择本地文库目录",
|
|
UseDescriptionForTitle = true,
|
|
SelectedPath = string.IsNullOrWhiteSpace(Settings.LibraryRoot)
|
|
? Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
|
|
: Settings.LibraryRoot
|
|
};
|
|
|
|
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
|
|
{
|
|
Settings.LibraryRoot = dialog.SelectedPath;
|
|
StatusMessage = "已选择文库目录";
|
|
}
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void OpenLogDirectory()
|
|
{
|
|
Directory.CreateDirectory(_appLogger.LogDirectory);
|
|
var startInfo = new System.Diagnostics.ProcessStartInfo
|
|
{
|
|
FileName = _appLogger.LogDirectory,
|
|
UseShellExecute = true
|
|
};
|
|
System.Diagnostics.Process.Start(startInfo);
|
|
}
|
|
}
|