Skip to content

Commit

Permalink
chore: rename to DialogCloseClallback
Browse files Browse the repository at this point in the history
  • Loading branch information
dansiegel committed Jul 9, 2023
1 parent 089679d commit 6ec3730
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 101 deletions.
26 changes: 15 additions & 11 deletions src/Forms/Prism.Forms/Dialogs/DialogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ public async void ShowDialog(string name, IDialogParameters parameters, DialogCa

dialogAware.RequestClose = new(DialogAware_RequestClose);

async Task DialogAware_RequestClose(IDialogParameters outParameters)
async Task DialogAware_RequestClose(IDialogResult outResult)
{
try
{
var result = await CloseDialogAsync(outParameters ?? new DialogParameters(), currentPage, dialogModal);
var result = await CloseDialogAsync(outResult ?? new DialogResult(), currentPage, dialogModal);
dialogModal.RaiseDialogResult(result);
if (result.Exception is DialogException de && de.Message == DialogException.CanCloseIsFalse)
{
Expand Down Expand Up @@ -110,15 +110,21 @@ async Task DialogAware_RequestClose(IDialogParameters outParameters)
}
}

private async System.Threading.Tasks.Task<IDialogResult> CloseDialogAsync(IDialogParameters parameters, ContentPage currentPage, DialogPage dialogModal)
private async System.Threading.Tasks.Task<IDialogResult> CloseDialogAsync(IDialogResult result, ContentPage currentPage, DialogPage dialogModal)
{
try
{
PageNavigationService.NavigationSource = PageNavigationSource.DialogService;

if (parameters is null)
result ??= new DialogResult();
if (result.Parameters is null)
{
parameters = new DialogParameters();
result = new DialogResult
{
Exception = result.Exception,
Parameters = new DialogParameters(),
Result = result.Result
};
}

var view = dialogModal.DialogView;
Expand All @@ -135,10 +141,7 @@ private async System.Threading.Tasks.Task<IDialogResult> CloseDialogAsync(IDialo
PageUtilities.InvokeViewAndViewModelAction<IActiveAware>(currentPage, aa => aa.IsActive = true);
dialogAware.OnDialogClosed();

return new DialogResult
{
Parameters = parameters
};
return result;
}
catch (DialogException)
{
Expand All @@ -149,7 +152,8 @@ private async System.Threading.Tasks.Task<IDialogResult> CloseDialogAsync(IDialo
return new DialogResult
{
Exception = ex,
Parameters = parameters
Parameters = result.Parameters,
Result = result.Result
};
}
finally
Expand Down Expand Up @@ -246,7 +250,7 @@ private ContentPage GetCurrentPage(Page page = null)
}
}

private async void InsertPopupViewInCurrentPage(ContentPage currentPage, DialogPage modalPage, View popupView, bool hideOnBackgroundTapped, DialogCloseEvent closeEvent)
private async void InsertPopupViewInCurrentPage(ContentPage currentPage, DialogPage modalPage, View popupView, bool hideOnBackgroundTapped, DialogCloseCallback closeEvent)
{
View mask = DialogLayout.GetMask(popupView);

Expand Down
38 changes: 27 additions & 11 deletions src/Maui/Prism.Maui/Dialogs/DialogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,19 @@ public sealed class DialogService : IDialogService
private readonly IContainerProvider _container;
private readonly IPageAccessor _pageAccessor;

/// <summary>
/// Creates a new instance of the <see cref="DialogService"/> for Maui Applications
/// </summary>
/// <param name="container">The <see cref="IContainerProvider"/> that will be used to help resolve the Dialog Views.</param>
/// <param name="pageAccessor">The <see cref="IPageAccessor"/> used to determine where in the Navigation Stack we need to process the Dialog.</param>
/// <exception cref="ArgumentNullException">Throws when any constructor arguments are null.</exception>
public DialogService(IContainerProvider container, IPageAccessor pageAccessor)
{
_container = container ?? throw new ArgumentNullException(nameof(container));
_pageAccessor = pageAccessor ?? throw new ArgumentNullException(nameof(pageAccessor));
}

/// <inheritdoc/>
public void ShowDialog(string name, IDialogParameters parameters, DialogCallback callback)
{
try
Expand All @@ -35,11 +42,11 @@ public void ShowDialog(string name, IDialogParameters parameters, DialogCallback
var dialogModal = _container.Resolve<IDialogContainer>();
var dialogAware = GetDialogController(view);

async Task DialogAware_RequestClose(IDialogParameters outParameters)
async Task DialogAware_RequestClose(IDialogResult outResult)
{
try
{
var result = await CloseDialogAsync(outParameters ?? new DialogParameters(), currentPage, dialogModal);
var result = await CloseDialogAsync(outResult ?? new DialogResult(), currentPage, dialogModal);
if (result.Exception is DialogException de && de.Message == DialogException.CanCloseIsFalse)
{
return;
Expand All @@ -53,7 +60,8 @@ async Task DialogAware_RequestClose(IDialogParameters outParameters)
var result = new DialogResult
{
Exception = dex,
Parameters = parameters
Parameters = parameters,
Result = ButtonResult.None
};

if (dex.Message != DialogException.CanCloseIsFalse)
Expand Down Expand Up @@ -100,18 +108,28 @@ private async Task InvokeError(DialogCallback callback, Exception exception, IDi
var result = new DialogResult
{
Parameters = parameters,
Exception = exception
Exception = exception,
Result = ButtonResult.None
};
await callback.Invoke(result);
}

private static async Task<IDialogResult> CloseDialogAsync(IDialogParameters parameters, Page currentPage, IDialogContainer dialogModal)
private static async Task<IDialogResult> CloseDialogAsync(IDialogResult result, Page currentPage, IDialogContainer dialogModal)
{
try
{
PageNavigationService.NavigationSource = PageNavigationSource.DialogService;

parameters ??= new DialogParameters();
result ??= new DialogResult();
if (result.Parameters is null)
{
result = new DialogResult
{
Exception = result.Exception,
Parameters = new DialogParameters(),
Result = result.Result
};
}

var view = dialogModal.DialogView;
var dialogAware = GetDialogController(view);
Expand All @@ -129,10 +147,7 @@ private static async Task<IDialogResult> CloseDialogAsync(IDialogParameters para
MvvmHelpers.InvokeViewAndViewModelAction<IActiveAware>(currentPage, aa => aa.IsActive = true);
dialogAware.OnDialogClosed();

return new DialogResult
{
Parameters = parameters
};
return result;
}
catch (DialogException)
{
Expand All @@ -143,7 +158,8 @@ private static async Task<IDialogResult> CloseDialogAsync(IDialogParameters para
return new DialogResult
{
Exception = ex,
Parameters = parameters
Parameters = result.Parameters,
Result = result.Result
};
}
finally
Expand Down
80 changes: 80 additions & 0 deletions src/Prism.Core/Dialogs/DialogCloseCallback.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System;
using System.ComponentModel;
using System.Threading.Tasks;

#nullable enable
namespace Prism.Dialogs;

/// <summary>
/// This is set by the <see cref="IDialogService"/> on your <see cref="IDialogAware"/> ViewModel. This can then
/// be invoked by either the DialogService or your code to initiate closing the Dialog.
/// </summary>
public struct DialogCloseCallback
{
private readonly MulticastDelegate _callback;

/// <summary>
/// Creates a default instance of the <see cref="DialogCloseCallback"/>
/// </summary>
public DialogCloseCallback()
{
_callback = () => { };
}

/// <summary>
/// Creates an instance of the <see cref="DialogCloseCallback"/> with an <see cref="Action{IDialogResult}"/> callback.
/// </summary>
/// <param name="callback">The callback to invoke.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
public DialogCloseCallback(Action<IDialogResult> callback)
{
_callback = callback;
}

/// <summary>
/// Creates an instance of the <see cref="DialogCloseCallback"/> with an <see cref="Func{IDialogResult, Task}"/> asynchronous callback.
/// </summary>
/// <param name="callback"></param>
[EditorBrowsable(EditorBrowsableState.Never)]
public DialogCloseCallback(Func<IDialogResult, Task> callback)
{
_callback = callback;
}

/// <summary>
/// Invokes the initialized delegate with no <see cref="IDialogResult"/>.
/// </summary>
public void Invoke() =>
Invoke(new DialogResult());

/// <summary>
/// Invokes the initialized delegate with the specified <see cref="IDialogParameters"/>.
/// </summary>
/// <param name="parameters">The <see cref="IDialogParameters"/>.</param>
/// <param name="result">The <see cref="ButtonResult"/>.</param>
public void Invoke(IDialogParameters parameters, ButtonResult result = ButtonResult.None) =>
Invoke(new DialogResult
{
Parameters = parameters,
Result = result
});

/// <summary>
/// Invokes the initialized delegate with the specified <see cref="IDialogResult"/>
/// </summary>
/// <param name="result"></param>
public async void Invoke(IDialogResult result)
{
switch(_callback)
{
case Action<IDialogResult> actionCallback:
actionCallback(result);
break;
case Func<IDialogResult, Task> taskCallback:
await taskCallback(result);
break;
default:
throw new InvalidOperationException("The DialogCloseCallback has not been properly initialized. This must be initialized by the DialogService, and should not be set by user code.");
}
}
}
65 changes: 0 additions & 65 deletions src/Prism.Core/Dialogs/DialogCloseEvent.cs

This file was deleted.

4 changes: 2 additions & 2 deletions src/Prism.Core/Dialogs/IDialogAware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public interface IDialogAware
void OnDialogOpened(IDialogParameters parameters);

/// <summary>
/// The <see cref="DialogCloseEvent"/> will be set by the <see cref="IDialogService"/> and can be called to
/// The <see cref="DialogCloseCallback"/> will be set by the <see cref="IDialogService"/> and can be called to
/// invoke the close of the Dialog.
/// </summary>
DialogCloseEvent RequestClose { get; set; }
DialogCloseCallback RequestClose { get; set; }
}
7 changes: 3 additions & 4 deletions src/Uno/Prism.Uno/Dialogs/DialogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,9 @@ void ConfigureDialogWindowEvents(IDialogWindow contentDialog, DialogCallback cal
{
IDialogResult result = null;

Action<IDialogParameters> requestCloseHandler = null;
requestCloseHandler = (p) =>
Action<IDialogResult> requestCloseHandler = (r) =>
{
result = new DialogResult { Parameters = p };
result = r ?? new DialogResult();
contentDialog.Hide();
};

Expand All @@ -72,7 +71,7 @@ void ConfigureDialogWindowEvents(IDialogWindow contentDialog, DialogCallback cal
if (contentDialog.DataContext is IDialogAware dialogAware)
{
dialogAware.RequestClose = new DialogCloseEvent(requestCloseHandler);
dialogAware.RequestClose = new DialogCloseCallback(requestCloseHandler);
}
};

Expand Down
7 changes: 3 additions & 4 deletions src/Wpf/Prism.Wpf/Dialogs/DialogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,17 @@ protected virtual void ConfigureDialogWindowContent(string dialogName, IDialogWi
/// <param name="callback">The action to perform when the dialog is closed.</param>
protected virtual void ConfigureDialogWindowEvents(IDialogWindow dialogWindow, DialogCallback callback)
{
Action<IDialogParameters> requestCloseHandler = null;
requestCloseHandler = (p) =>
Action<IDialogResult> requestCloseHandler = (r) =>
{
dialogWindow.Result = new DialogResult { Parameters = p };
dialogWindow.Result = r;
dialogWindow.Close();
};

RoutedEventHandler loadedHandler = null;
loadedHandler = (o, e) =>
{
dialogWindow.Loaded -= loadedHandler;
dialogWindow.GetDialogViewModel().RequestClose = new DialogCloseEvent(requestCloseHandler);
dialogWindow.GetDialogViewModel().RequestClose = new DialogCloseCallback(requestCloseHandler);
};
dialogWindow.Loaded += loadedHandler;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using Prism.AppModel;
using Prism.Dialogs;
using Prism.Mvvm;
using Prism.Dialogs;

namespace Prism.Forms.Tests.Services.Mocks.Dialogs
{
Expand All @@ -14,7 +12,7 @@ public string Title
set => SetProperty(ref _title, value);
}

public DialogCloseEvent RequestClose { get; set; }
public DialogCloseCallback RequestClose { get; set; }

public bool CanClose { get; set; } = true;

Expand Down

0 comments on commit 6ec3730

Please sign in to comment.