Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AppAction registration helpers. #3009

Merged
merged 1 commit into from
Nov 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading