From 9fa7af1296e131b27e99a1c3f6419b27edec11b5 Mon Sep 17 00:00:00 2001 From: stavroskasidis Date: Sun, 11 Aug 2019 20:55:08 +0300 Subject: [PATCH] wip --- BlazorDialog/BlazorDialog.razor | 2 +- BlazorDialog/BlazorDialogBase.cs | 37 +++++++++++++++++ BlazorDialog/BlazorDialogService.cs | 1 - BlazorDialog/BlazorDialogService2.cs | 61 ++++++++++++++++++++++++++++ BlazorDialog/DialogState.cs | 31 -------------- 5 files changed, 99 insertions(+), 33 deletions(-) create mode 100644 BlazorDialog/BlazorDialogBase.cs create mode 100644 BlazorDialog/BlazorDialogService2.cs delete mode 100644 BlazorDialog/DialogState.cs diff --git a/BlazorDialog/BlazorDialog.razor b/BlazorDialog/BlazorDialog.razor index 2e663f5..ba1a9d2 100644 --- a/BlazorDialog/BlazorDialog.razor +++ b/BlazorDialog/BlazorDialog.razor @@ -1,6 +1,7 @@ @typeparam TInput @inject DialogStates states @inject IBlazorDialogService dialogService +@inherits BlazorDialogBase @implements IDisposable @if (Id != null && states.IsShowing.ContainsKey(Id) && states.IsShowing[Id]) @@ -22,7 +23,6 @@ } @code{ - [Parameter] protected string Id { get; set; } [Parameter] protected RenderFragment> ChildContent { get; set; } [Parameter] protected DialogSizes Size { get; set; } = DialogSizes.Normal; [Parameter] protected bool Centered { get; set; } diff --git a/BlazorDialog/BlazorDialogBase.cs b/BlazorDialog/BlazorDialogBase.cs new file mode 100644 index 0000000..96f1b54 --- /dev/null +++ b/BlazorDialog/BlazorDialogBase.cs @@ -0,0 +1,37 @@ +using Microsoft.AspNetCore.Components; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace BlazorDialog +{ + public abstract class BlazorDialogBase : ComponentBase + { + [Parameter] public string Id { get; protected set; } + [Parameter] protected bool IsShowing { get; set; } + + + public async Task Show() + { + this.IsShowing = true; + await Invoke(() => StateHasChanged()); + + } + + //public Task Show(TInput input) + //{ + // return dialogService.ShowDialog(this.Id, input); + //} + + //public void Hide(TResult result) + //{ + // dialogService.HideDialog(this.Id, result); + //} + + //public void Hide() + //{ + // dialogService.HideDialog(this.Id); + //} + } +} diff --git a/BlazorDialog/BlazorDialogService.cs b/BlazorDialog/BlazorDialogService.cs index e25160d..3db6344 100644 --- a/BlazorDialog/BlazorDialogService.cs +++ b/BlazorDialog/BlazorDialogService.cs @@ -7,7 +7,6 @@ namespace BlazorDialog { public class BlazorDialogService : IBlazorDialogService { - private readonly DialogStates _dialogStates; private Dictionary> taskCompletionSources = new Dictionary>(); public BlazorDialogService(DialogStates states) diff --git a/BlazorDialog/BlazorDialogService2.cs b/BlazorDialog/BlazorDialogService2.cs new file mode 100644 index 0000000..b8e1337 --- /dev/null +++ b/BlazorDialog/BlazorDialogService2.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace BlazorDialog +{ + public class BlazorDialogService2 + { + private Dictionary registeredDialogs = new Dictionary(); + + internal void Register(BlazorDialogBase blazorDialog) + { + registeredDialogs[blazorDialog.Id] = blazorDialog; + } + + + internal void Unregister(BlazorDialogBase blazorDialog) + { + registeredDialogs.Remove(blazorDialog.Id); + } + + public void HideDialog(string dialogId) + { + registeredDialogs[dialogId].Hide(); + } + + public void HideDialog(string dialogId, object result) + { + registeredDialogs[dialogId].Hide(result); + } + + public async Task ShowDialog(string dialogId) + { + await ShowDialog(dialogId, null); + } + + public async Task ShowDialog(string dialogId) + { + return await ShowDialog(dialogId, null); + } + + public async Task ShowDialog(string dialogId, object input) + { + await ShowDialog(dialogId, input); + } + + public async Task ShowDialog(string dialogId, object input) + { + //if (taskCompletionSources.ContainsKey(dialogId)) + //{ + // taskCompletionSources[dialogId].SetCanceled(); + //} + //taskCompletionSources[dialogId] = new TaskCompletionSource(); + //_dialogStates.ShowDialog(dialogId, input); + //return (TResult)await taskCompletionSources[dialogId].Task; + + return (TResult)await registeredDialogs[dialogId].Show(input); + } + } +} diff --git a/BlazorDialog/DialogState.cs b/BlazorDialog/DialogState.cs deleted file mode 100644 index 0cd7cce..0000000 --- a/BlazorDialog/DialogState.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace BlazorDialog -{ - public class DialogStates - { - internal Dictionary Inputs { get; set; } = new Dictionary(); - internal Dictionary IsShowing { get; set; } = new Dictionary(); - internal void ShowDialog(string dialogId,object input) - { - Inputs[dialogId] = input; - IsShowing[dialogId] = true; - NotifyDialogStateChange(dialogId); - } - - internal void HideDialog(string dialogId, object result) - { - IsShowing[dialogId] = false; - NotifyDialogStateChange(dialogId); - NotifyDialogHidden(dialogId, result); - } - - internal Action OnDialogHide; - protected void NotifyDialogHidden(string dialogId, object result) => OnDialogHide?.Invoke(dialogId, result); - internal Action OnDialogStateChange; - protected void NotifyDialogStateChange(string dialogId) => OnDialogStateChange?.Invoke(); - } -}