Skip to content

Commit

Permalink
feat: make IPageDialogService a Singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
dansiegel committed Feb 14, 2024
1 parent 6bc48ea commit 81f37cb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Maui/Prism.Maui/PrismAppBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ private void RegisterDefaultRequiredTypes(IContainerRegistry containerRegistry)
{
containerRegistry.TryRegisterSingleton<IEventAggregator, EventAggregator>();
containerRegistry.TryRegisterSingleton<IKeyboardMapper, KeyboardMapper>();
containerRegistry.TryRegisterScoped<IPageDialogService, PageDialogService>();
containerRegistry.TryRegisterSingleton<IPageDialogService, PageDialogService>();
containerRegistry.TryRegisterScoped<IDialogService, DialogService>();
containerRegistry.TryRegister<IDialogViewRegistry, DialogViewRegistry>();
containerRegistry.RegisterDialogContainer<DialogContainerPage>();
Expand Down
21 changes: 12 additions & 9 deletions src/Maui/Prism.Maui/Services/PageDialogs/PageDialogService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Prism.AppModel;
using Prism.Common;
using Prism.Navigation;
using FlowDirection = Prism.AppModel.FlowDirection;
using MauiFlow = Microsoft.Maui.FlowDirection;

Expand All @@ -10,7 +11,7 @@ namespace Prism.Services;
/// </summary>
public class PageDialogService : IPageDialogService
{
private IPageAccessor _pageAccessor { get; }
private IWindowManager _windowManager { get; }
/// <summary>
/// Gets the <see cref="IKeyboardMapper"/>.
/// </summary>
Expand All @@ -19,11 +20,11 @@ public class PageDialogService : IPageDialogService
/// <summary>
/// Creates a new <see cref="IPageDialogService"/>
/// </summary>
/// <param name="pageAccessor">The <see cref="IPageAccessor"/>.</param>
/// <param name="windowManager">The <see cref="IWindowManager"/>.</param>
/// <param name="keyboardMapper">The <see cref="IKeyboardMapper"/>.</param>
public PageDialogService(IPageAccessor pageAccessor, IKeyboardMapper keyboardMapper)
public PageDialogService(IWindowManager windowManager, IKeyboardMapper keyboardMapper)
{
_pageAccessor = pageAccessor;
_windowManager = windowManager;
_keyboardMapper = keyboardMapper;
}

Expand Down Expand Up @@ -57,7 +58,7 @@ public virtual Task<bool> DisplayAlertAsync(string title, string message, string
/// <returns><c>true</c> if non-destructive button pressed; otherwise <c>false</c>/></returns>
public virtual Task<bool> DisplayAlertAsync(string title, string message, string acceptButton, string cancelButton, FlowDirection flowDirection)
{
return _pageAccessor.Page.DisplayAlert(title, message, acceptButton, cancelButton, (MauiFlow)flowDirection);
return GetPage().DisplayAlert(title, message, acceptButton, cancelButton, (MauiFlow)flowDirection);
}

/// <summary>
Expand All @@ -72,7 +73,7 @@ public virtual Task<bool> DisplayAlertAsync(string title, string message, string
/// <returns></returns>
public virtual Task DisplayAlertAsync(string title, string message, string cancelButton)
{
return _pageAccessor.Page.DisplayAlert(title, message, cancelButton);
return GetPage().DisplayAlert(title, message, cancelButton);
}

/// <summary>
Expand All @@ -88,7 +89,7 @@ public virtual Task DisplayAlertAsync(string title, string message, string cance
/// <returns></returns>
public virtual Task DisplayAlertAsync(string title, string message, string cancelButton, FlowDirection flowDirection)
{
return _pageAccessor.Page.DisplayAlert(title, message, cancelButton, (MauiFlow)flowDirection);
return GetPage().DisplayAlert(title, message, cancelButton, (MauiFlow)flowDirection);
}

/// <summary>
Expand All @@ -115,7 +116,7 @@ public virtual Task<string> DisplayActionSheetAsync(string title, string cancelB
/// <returns>Text for the pressed button</returns>
public virtual Task<string> DisplayActionSheetAsync(string title, string cancelButton, string destroyButton, FlowDirection flowDirection, params string[] otherButtons)
{
return _pageAccessor.Page.DisplayActionSheet(title, cancelButton, destroyButton, (MauiFlow)flowDirection, otherButtons);
return GetPage().DisplayActionSheet(title, cancelButton, destroyButton, (MauiFlow)flowDirection, otherButtons);
}

/// <summary>
Expand Down Expand Up @@ -177,6 +178,8 @@ public virtual async Task DisplayActionSheetAsync(string title, FlowDirection fl
public virtual Task<string> DisplayPromptAsync(string title, string message, string accept = "OK", string cancel = "Cancel", string placeholder = default, int maxLength = -1, KeyboardType keyboardType = KeyboardType.Default, string initialValue = "")
{
var keyboard = _keyboardMapper.Map(keyboardType);
return _pageAccessor.Page.DisplayPromptAsync(title, message, accept, cancel, placeholder, maxLength, keyboard, initialValue);
return GetPage().DisplayPromptAsync(title, message, accept, cancel, placeholder, maxLength, keyboard, initialValue);
}

private Page GetPage() => _windowManager.Windows.OfType<PrismWindow>().First().CurrentPage;
}

0 comments on commit 81f37cb

Please sign in to comment.