2025-07-19 09:25:01 +08:00
|
|
|
using DMS.WPF.ViewModels.Dialogs;
|
|
|
|
|
using DMS.WPF.Views.Dialogs;
|
2025-07-27 21:08:58 +08:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows;
|
2025-09-03 18:22:01 +08:00
|
|
|
using DMS.WPF.Interfaces;
|
2025-08-23 16:01:30 +08:00
|
|
|
using DMS.WPF.Views;
|
2025-07-04 14:15:44 +08:00
|
|
|
using iNKORE.UI.WPF.Modern.Controls;
|
2025-06-12 13:15:55 +08:00
|
|
|
|
2025-07-27 21:08:58 +08:00
|
|
|
namespace DMS.WPF.Services
|
2025-06-12 13:15:55 +08:00
|
|
|
{
|
2025-07-27 21:08:58 +08:00
|
|
|
public class DialogService : IDialogService
|
|
|
|
|
{
|
|
|
|
|
private readonly IServiceProvider _serviceProvider;
|
|
|
|
|
private static readonly Dictionary<Type, Type> _viewModelViewMap = new Dictionary<Type, Type>
|
|
|
|
|
{
|
|
|
|
|
{ typeof(DeviceDialogViewModel), typeof(DeviceDialog) },
|
2025-08-24 11:31:07 +08:00
|
|
|
{ typeof(ConfirmDialogViewModel), typeof(ConfirmDialog) },
|
2025-07-30 12:54:14 +08:00
|
|
|
{ typeof(VariableTableDialogViewModel), typeof(VariableTableDialog) },
|
2025-08-22 20:24:09 +08:00
|
|
|
{ typeof(ImportExcelDialogViewModel), typeof(ImportExcelDialog) },
|
2025-08-25 21:26:18 +08:00
|
|
|
{ typeof(ImportOpcUaDialogViewModel), typeof(ImportOpcUaDialog) },
|
2025-08-23 20:52:23 +08:00
|
|
|
{ typeof(VariableDialogViewModel), typeof(VariableDialog) },
|
2025-09-02 17:42:11 +08:00
|
|
|
{ typeof(PollLevelDialogViewModel), typeof(PollLevelDialog) },
|
2025-09-02 18:29:58 +08:00
|
|
|
{ typeof(IsActiveDialogViewModel), typeof(IsActiveDialog) },
|
2025-09-06 19:10:25 +08:00
|
|
|
{ typeof(MqttDialogViewModel), typeof(MqttDialog) },
|
|
|
|
|
{ typeof(MqttSelectionDialogViewModel), typeof(MqttSelectionDialog) },
|
2025-09-12 08:57:41 +08:00
|
|
|
{ typeof(MqttAliasBatchEditDialogViewModel), typeof(MqttAliasBatchEditDialog) },
|
2025-09-12 12:26:57 +08:00
|
|
|
{ typeof(HistorySettingsDialogViewModel), typeof(HistorySettingsDialog) },
|
2025-09-13 19:08:43 +08:00
|
|
|
{ typeof(AlarmSettingsDialogViewModel), typeof(AlarmSettingsDialog) },
|
2025-09-13 19:39:18 +08:00
|
|
|
{ typeof(EmailAccountDialogViewModel), typeof(EmailAccountDialog) },
|
|
|
|
|
{ typeof(EmailTemplateDialogViewModel), typeof(EmailTemplateDialog) }
|
2025-09-06 19:10:25 +08:00
|
|
|
// Add other mappings here
|
2025-07-27 21:08:58 +08:00
|
|
|
// ... other dialogs
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public DialogService(IServiceProvider serviceProvider)
|
|
|
|
|
{
|
|
|
|
|
_serviceProvider = serviceProvider;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<TResult> ShowDialogAsync<TResult>(DialogViewModelBase<TResult> viewModel)
|
|
|
|
|
{
|
|
|
|
|
if (_viewModelViewMap.TryGetValue(viewModel.GetType(), out var viewType))
|
|
|
|
|
{
|
|
|
|
|
var tcs = new TaskCompletionSource<TResult>();
|
|
|
|
|
|
|
|
|
|
var dialog = (ContentDialog)_serviceProvider.GetService(viewType);
|
|
|
|
|
if (dialog == null)
|
|
|
|
|
{
|
|
|
|
|
// If not registered in DI, create an instance directly
|
|
|
|
|
dialog = (ContentDialog)Activator.CreateInstance(viewType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dialog.DataContext = viewModel;
|
|
|
|
|
|
|
|
|
|
Func<TResult, Task> closeHandler = null;
|
|
|
|
|
closeHandler = async (result) =>
|
|
|
|
|
{
|
|
|
|
|
viewModel.CloseRequested -= closeHandler;
|
|
|
|
|
dialog.Hide();
|
|
|
|
|
tcs.SetResult(result);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
viewModel.CloseRequested += closeHandler;
|
|
|
|
|
|
2025-07-28 13:06:36 +08:00
|
|
|
_ = await dialog.ShowAsync();
|
2025-07-27 21:08:58 +08:00
|
|
|
|
|
|
|
|
return await tcs.Task;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException($"No view registered for view model {viewModel.GetType().Name}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-06-12 13:15:55 +08:00
|
|
|
}
|