using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Artemis.UI.Shared.Screens.Dialogs; using Artemis.UI.Shared.Services.Interfaces; using MaterialDesignThemes.Wpf; using Ninject; using Ninject.Parameters; using Stylet; namespace Artemis.UI.Shared.Services.Dialog { public class DialogService : IDialogService { private readonly IKernel _kernel; private readonly IViewManager _viewManager; public DialogService(IKernel kernel, IViewManager viewManager) { _kernel = kernel; _viewManager = viewManager; } public async Task ShowConfirmDialog(string header, string text, string confirmText = "Confirm", string cancelText = "Cancel") { var arguments = new IParameter[] { new ConstructorArgument("header", header), new ConstructorArgument("text", text), new ConstructorArgument("confirmText", confirmText.ToUpper()), new ConstructorArgument("cancelText", cancelText.ToUpper()) }; var result = await ShowDialog(arguments); return (bool) result; } public async Task ShowConfirmDialogAt(string identifier, string header, string text, string confirmText = "Confirm", string cancelText = "Cancel") { var arguments = new IParameter[] { new ConstructorArgument("header", header), new ConstructorArgument("text", text), new ConstructorArgument("confirmText", confirmText.ToUpper()), new ConstructorArgument("cancelText", cancelText.ToUpper()) }; var result = await ShowDialogAt(identifier, arguments); return (bool) result; } public async Task ShowDialog() where T : DialogViewModelBase { return await ShowDialog("RootDialog", _kernel.Get()); } public Task ShowDialog(Dictionary parameters) where T : DialogViewModelBase { if (parameters == null) throw new ArgumentNullException(nameof(parameters)); var paramsArray = parameters.Select(kv => new ConstructorArgument(kv.Key, kv.Value)).Cast().ToArray(); return ShowDialog(paramsArray); } public async Task ShowDialog(IParameter[] parameters) where T : DialogViewModelBase { if (parameters == null) throw new ArgumentNullException(nameof(parameters)); return await ShowDialog("RootDialog", _kernel.Get(parameters)); } public async Task ShowDialogAt(string identifier) where T : DialogViewModelBase { return await ShowDialog(identifier, _kernel.Get()); } public async Task ShowDialogAt(string identifier, Dictionary parameters) where T : DialogViewModelBase { if (parameters == null) throw new ArgumentNullException(nameof(parameters)); var paramsArray = parameters.Select(kv => new ConstructorArgument(kv.Key, kv.Value)).Cast().ToArray(); return await ShowDialogAt(identifier, paramsArray); } public async Task ShowDialogAt(string identifier, IParameter[] parameters) where T : DialogViewModelBase { if (parameters == null) throw new ArgumentNullException(nameof(parameters)); return await ShowDialog(identifier, _kernel.Get(parameters)); } public async Task ShowExceptionDialog(string message, Exception exception) { var arguments = new IParameter[] { new ConstructorArgument("message", message), new ConstructorArgument("exception", exception) }; try { await Execute.OnUIThreadAsync(async () => await ShowDialog(arguments)); } catch (Exception) { //ignored } } private async Task ShowDialog(string identifier, DialogViewModelBase viewModel) { Task result = null; await Execute.OnUIThreadAsync(() => { var view = _viewManager.CreateViewForModel(viewModel); _viewManager.BindViewToModel(view, viewModel); if (identifier == null) result = DialogHost.Show(view, viewModel.OnDialogOpened, viewModel.OnDialogClosed); else result = DialogHost.Show(view, identifier, viewModel.OnDialogOpened, viewModel.OnDialogClosed); }); return await result; } } }