124 lines
4.8 KiB
C#
124 lines
4.8 KiB
C#
using System.Collections.ObjectModel;
|
|
using System.Globalization;
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Messaging;
|
|
using Livia.Models;
|
|
using Livia.Models.Data;
|
|
using Livia.Properties;
|
|
using Livia.Utility;
|
|
using Livia.Views.Utility;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Livia.ViewModels;
|
|
|
|
public class LanguageComboBoxItemViewModel(string displayName, string id, string defaultFont) : ObservableObject
|
|
{
|
|
public string Id { get; } = id;
|
|
public string DefaultFont { get; set; } = defaultFont;
|
|
public string DisplayName { get; } = displayName;
|
|
|
|
public override string ToString()
|
|
{
|
|
return DisplayName;
|
|
}
|
|
}
|
|
|
|
public class SettingsWindowViewModel : ObservableObject
|
|
{
|
|
public IVersionUpdateManager VersionUpdateManager { get; }
|
|
public IServerHandler ServerHandler { get; }
|
|
public static bool LanguageSelectionEnabled { get; set; }
|
|
public static bool DisplaySettingsCardEnabled { get; set; } = true;
|
|
public static bool SecurityTabEnabled { get; set; } = true;
|
|
public static bool GeneralTabEnabled { get; set; } = true;
|
|
public static bool ShowExportReportSettings { get; set; } = true;
|
|
public ObservableCollection<LanguageComboBoxItemViewModel> Languages { get; } = [];
|
|
public ObservableCollection<string> UpdateChannels { get; }
|
|
public ObservableCollection<CultureInfo> CultureInfoCollection { get; } = [];
|
|
public bool ReadyToInstallUpdate { get => _readyToInstallUpdate; set => SetProperty(ref _readyToInstallUpdate, value); }
|
|
public bool ReadyToCheckForUpdate { get => _readyToCheckForUpdate; set => SetProperty(ref _readyToCheckForUpdate, value); }
|
|
public string DicomNodeHost { get => _dicomNodeHost; private set => SetProperty(ref _dicomNodeHost, value); }
|
|
public string DicomNodePort { get => _dicomNodePort; private set => SetProperty(ref _dicomNodePort, value); }
|
|
public string DicomNodeAet { get => _dicomNodeAet; private set => SetProperty(ref _dicomNodeAet, value); }
|
|
|
|
private bool _readyToCheckForUpdate = true;
|
|
private bool _readyToInstallUpdate = true;
|
|
private string _dicomNodeHost = string.Empty;
|
|
private string _dicomNodePort = string.Empty;
|
|
private string _dicomNodeAet = string.Empty;
|
|
|
|
private readonly IWarningSystem _warningSystem;
|
|
private readonly IServerHandler _serverHandler;
|
|
private readonly ILogger _logger;
|
|
|
|
public SettingsWindowViewModel(ILogger logger, IVersionUpdateManager versionUpdateManager, IServerHandler serverHandler, IWarningSystem warningSystem)
|
|
{
|
|
_logger = logger;
|
|
VersionUpdateManager = versionUpdateManager;
|
|
_serverHandler = serverHandler;
|
|
ServerHandler = serverHandler;
|
|
_warningSystem = warningSystem;
|
|
|
|
//manually set selected font index because somehow it is not initialized
|
|
foreach (LanguageComboBoxItemViewModel viewModel in ViewUtility.LanguageList)
|
|
{
|
|
Languages.Add(viewModel);
|
|
}
|
|
|
|
UpdateChannels = new ObservableCollection<string> { "stable", "beta" };
|
|
|
|
CultureInfoCollection.AddRange(CultureInfo.GetCultures(CultureTypes.AllCultures));
|
|
|
|
Task.Run(UpdateDicomNodeInfo);
|
|
}
|
|
|
|
private async void UpdateDicomNodeInfo()
|
|
{
|
|
if (!Settings.Default.ShowDicomNodeTab)
|
|
return;
|
|
|
|
DicomNodeInfo? info = await _serverHandler.GetDicomNodeInfo();
|
|
if (info == null)
|
|
return;
|
|
|
|
DicomNodeHost = info.Value.Host;
|
|
DicomNodePort = info.Value.Port.ToString();
|
|
DicomNodeAet = info.Value.Aet;
|
|
}
|
|
|
|
public async Task CheckUpdate()
|
|
{
|
|
_logger.LogInformation("Checking for update");
|
|
ReadyToCheckForUpdate = false;
|
|
await Task.Run(() => VersionUpdateManager.CheckForUpdate(true)).ConfigureAwait(false);
|
|
ReadyToCheckForUpdate = true;
|
|
}
|
|
|
|
public async Task InstallUpdate()
|
|
{
|
|
ReadyToInstallUpdate = false;
|
|
await Task.Run(() => VersionUpdateManager.UpdateApp()).ConfigureAwait(false);
|
|
ReadyToInstallUpdate = true;
|
|
}
|
|
|
|
public async Task<bool> EditPassword(string oldPassword, string newPassword, string confirmPassword)
|
|
{
|
|
if (newPassword != confirmPassword)
|
|
{
|
|
_warningSystem.ShowDialog(WarningWindowKind.Warning, false, "ConfirmPasswordFailedError");
|
|
return false;
|
|
}
|
|
|
|
(bool success, string messageIndex) = await _serverHandler.EditPassword(oldPassword, newPassword).ConfigureAwait(false);
|
|
if (success)
|
|
{
|
|
_warningSystem.ShowDialog(WarningWindowKind.Info, false, messageIndex);
|
|
WeakReferenceMessenger.Default.Send(new LogoutMessage());
|
|
_logger.LogInformation("Password changed");
|
|
return true;
|
|
}
|
|
|
|
_warningSystem.ShowDialog(WarningWindowKind.Warning, false, messageIndex);
|
|
return false;
|
|
}
|
|
} |