Skip to content

Commit

Permalink
feat: add AppAction registration helpers.
Browse files Browse the repository at this point in the history
  • Loading branch information
dansiegel committed Nov 16, 2023
1 parent 0563e4d commit 25d2826
Showing 1 changed file with 71 additions and 2 deletions.
73 changes: 71 additions & 2 deletions src/Maui/Prism.Maui/PrismAppBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Prism.Ioc;
using Microsoft.Extensions.Logging;
using Prism.Modularity;
using Prism.Mvvm;
using Prism.Navigation;
using Microsoft.Extensions.Logging;

namespace Prism;

Expand Down Expand Up @@ -72,4 +72,73 @@ public static PrismAppBuilder ConfigureLogging(this PrismAppBuilder builder, Act
configureLogging(builder.MauiBuilder.Logging);
return builder;
}

public static PrismAppBuilder ConfigureViewTypeToViewModelTypeResolver(this PrismAppBuilder builder, Func<Type, Type> viewModelTypeResolver)
{
ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver(viewModelTypeResolver);
return builder;
}

/// <summary>
/// Registers an <see cref="AppAction"/> with a callback that will be invoked on the UI thread for the specified <see cref="AppAction"/>.
/// </summary>
/// <param name="builder">The <see cref="PrismAppBuilder"/>.</param>
/// <param name="appAction">An <see cref="AppAction"/></param>
/// <param name="callback">The callback to invoke when the <see cref="AppAction"/> is triggered.</param>
/// <returns>The <see cref="PrismAppBuilder"/>.</returns>
public static PrismAppBuilder RegisterAppAction(this PrismAppBuilder builder, AppAction appAction, Func<IContainerProvider, INavigationService, AppAction, Task> callback)
{
builder.MauiBuilder.ConfigureEssentials(essentials =>
{
essentials.AddAppAction(appAction)
.OnAppAction(async action =>
{
if (appAction.Id != action.Id)
return;

var app = Application.Current;
if (app?.Handler?.MauiContext?.Services is null || app.Dispatcher is null)
return;

var container = app.Handler.MauiContext.Services.GetRequiredService<IContainerProvider>();
var navigation = container.Resolve<INavigationService>();
await app.Dispatcher.DispatchAsync(() =>
{
return callback(container, navigation, action);
});
});
});
return builder;
}

/// <summary>
/// Registers an <see cref="AppAction"/> with a callback that will be invoked on the UI thread for the specified <see cref="AppAction"/>.
/// </summary>
/// <param name="builder">The <see cref="PrismAppBuilder"/>.</param>
/// <param name="appAction">An <see cref="AppAction"/></param>
/// <param name="callback">The callback to invoke when the <see cref="AppAction"/> is triggered.</param>
/// <returns>The <see cref="PrismAppBuilder"/>.</returns>
public static PrismAppBuilder RegisterAppAction(this PrismAppBuilder builder, AppAction appAction, Func<INavigationService, AppAction, Task> callback)
{
builder.MauiBuilder.ConfigureEssentials(essentials =>
{
essentials.AddAppAction(appAction)
.OnAppAction(async action =>
{
if (appAction.Id != action.Id)
return;

var app = Application.Current;
if (app?.Handler?.MauiContext?.Services is null || app.Dispatcher is null)
return;

var navigation = app.Handler.MauiContext.Services.GetRequiredService<INavigationService>();
await app.Dispatcher.DispatchAsync(() =>
{
return callback(navigation, action);
});
});
});
return builder;
}
}

0 comments on commit 25d2826

Please sign in to comment.