using System.Collections.ObjectModel; using System.Diagnostics; using System.Windows; 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 CheckReportWindowViewModel : ObservableObject, IRecipient, IRecipient { public event EventHandler? OnRequestClose; public ObservableCollection ReportList { get; } = []; public IPatientInfoManager PatientInfoManager { get; } public bool Processing { get => _processing; private set => SetProperty(ref _processing, value); } private string? _reportDataKey; public bool ShowDeleteButton { get; set; } = true; private bool _processing; private readonly IServerHandler _serverHandler; private readonly ILogger _logger; private readonly IWarningSystem _warningSystem; private CancellationTokenSource _cancellationTokenSource = new(); public CheckReportWindowViewModel(IServerHandler serverHandler, ILogger logger, IPatientInfoManager patientInfoManager, IWarningSystem warningSystem) { _serverHandler = serverHandler; _logger = logger; PatientInfoManager = patientInfoManager; _warningSystem = warningSystem; WeakReferenceMessenger.Default.RegisterAll(this); } private async Task Refresh() { if (string.IsNullOrEmpty(_reportDataKey)) return; _cancellationTokenSource = new CancellationTokenSource(); List list = await _serverHandler.GetReportList(_reportDataKey); Application.Current.Dispatcher.Invoke(delegate { ReportList.Clear(); ReportList.AddRange(list); }); } public async Task DeleteReport(int dataId) { if (string.IsNullOrEmpty(_reportDataKey)) return; Processing = true; await _serverHandler.DeleteReport(_reportDataKey, dataId).ConfigureAwait(false); await Refresh(); Processing = false; } public async Task DownloadReport(int dataId, string dialogSelectedPath) { if (string.IsNullOrEmpty(_reportDataKey)) return; Processing = true; _logger.LogInformation("Exporting report to {path}", dialogSelectedPath); (bool success, string messageIndex) = await _serverHandler.DownloadReport(_reportDataKey, dataId, dialogSelectedPath, _cancellationTokenSource.Token).ConfigureAwait(false); if (success) { if (!Settings.Default.OpenReportOnFinish) return; try { Process.Start("explorer.exe", dialogSelectedPath); } catch (Exception e) { _logger.LogError(e, "Error opening {path}", dialogSelectedPath); } } else { if (_cancellationTokenSource.Token.IsCancellationRequested) { _logger.LogInformation("Data processing canceled"); } else { _warningSystem.ShowDialog(WarningWindowKind.Warning, true, messageIndex); } } await Refresh(); Processing = false; } public async Task PushReport(int id) { if (string.IsNullOrEmpty(_reportDataKey)) return; Processing = true; (bool success, string messageIndex) = await _serverHandler.PushToPacs(_reportDataKey, id).ConfigureAwait(false); _warningSystem.ShowDialog(success ? WarningWindowKind.Info : WarningWindowKind.Warning, false, messageIndex); Processing = false; } public async Task LoadReportData(string dataKey, bool showDeleteButton) { ShowDeleteButton = showDeleteButton; _reportDataKey = dataKey; await Refresh(); } public void Cancel() { _cancellationTokenSource.Cancel(); } public void Receive(LogoutMessage message) { //TODO::not sure if it is safe if (!Processing) OnRequestClose?.Invoke(this, EventArgs.Empty); } public void Receive(IdleTimerTickMessage message) { if (Processing) message.IdleTimer.ResetTimer(); } public void Dispose() { WeakReferenceMessenger.Default.UnregisterAll(this); } }