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

Maui Cleanup #3042

Merged
merged 3 commits into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
chore: adding xml docs
  • Loading branch information
dansiegel committed Jan 6, 2024
commit 23452b5530a9fcd562b502513a30d4a57d7165e4
2 changes: 1 addition & 1 deletion src/Maui/Prism.Maui/Navigation/PrismWindowManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public Window CreateWindow(Application app, IActivationState activationState)
else if (app.Windows.OfType<PrismWindow>().Any())
return _initialWindow = app.Windows.OfType<PrismWindow>().First();

activationState.Context.Services.GetRequiredService<PrismAppBuilder>().OnAppStarted();
activationState.Context.Services.GetRequiredService<PrismAppBuilder>().OnCreateWindow();

return _initialWindow ?? throw new InvalidNavigationException("Expected Navigation Failed. No Root Window has been created.");
}
Expand Down
16 changes: 8 additions & 8 deletions src/Maui/Prism.Maui/PrismAppBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public sealed class PrismAppBuilder
private List<Action<IContainerRegistry>> _registrations { get; }
private List<Action<IContainerProvider>> _initializations { get; }
private IContainerProvider _container { get; }
private Func<IContainerProvider, INavigationService, Task> _onAppStarted;
private Func<IContainerProvider, INavigationService, Task> _createWindow;
private Action<RegionAdapterMappings> _configureAdapters;
private Action<IRegionBehaviorFactory> _configureBehaviors;

Expand Down Expand Up @@ -181,26 +181,26 @@ internal void OnInitialized()
}
}

internal void OnAppStarted()
internal void OnCreateWindow()
{
if (_onAppStarted is null)
throw new ArgumentException("You must call OnAppStart on the PrismAppBuilder.");
if (_createWindow is null)
throw new ArgumentException("You must call CreateWindow on the PrismAppBuilder.");

// Ensure that this is executed before we navigate.
OnInitialized();
var onStart = _onAppStarted(_container, _container.Resolve<INavigationService>());
var onStart = _createWindow(_container, _container.Resolve<INavigationService>());
onStart.Wait();
}

/// <summary>
/// When the <see cref="Application"/> is started and the native platform calls <see cref="IApplication.CreateWindow(IActivationState?)"/>
/// this delegate will be invoked to do your initial Navigation.
/// </summary>
/// <param name="onAppStarted">The Navigation Delegate.</param>
/// <param name="createWindow">The Navigation Delegate.</param>
/// <returns>The <see cref="PrismAppBuilder"/>.</returns>
public PrismAppBuilder CreateWindow(Func<IContainerProvider, INavigationService, Task> onAppStarted)
public PrismAppBuilder CreateWindow(Func<IContainerProvider, INavigationService, Task> createWindow)
{
_onAppStarted = onAppStarted;
_createWindow = createWindow;
return this;
}

Expand Down
95 changes: 87 additions & 8 deletions src/Maui/Prism.Maui/PrismAppBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,33 @@

namespace Prism;

/// <summary>
/// Common extensions and overloads for the <see cref="PrismAppBuilder"/>
/// </summary>
public static class PrismAppBuilderExtensions
{
private static bool s_didRegisterModules = false;

/// <summary>
/// Configures the <see cref="MauiAppBuilder"/> to use Prism with a callback for the <see cref="PrismAppBuilder"/>
/// </summary>
/// <param name="builder">The <see cref="MauiAppBuilder"/>.</param>
/// <param name="containerExtension">The instance of the <see cref="IContainerExtension"/> Prism should use.</param>
/// <param name="configurePrism">A delegate callback for the <see cref="PrismAppBuilder"/></param>
/// <returns>The <see cref="MauiAppBuilder"/>.</returns>
public static MauiAppBuilder UsePrism(this MauiAppBuilder builder, IContainerExtension containerExtension, Action<PrismAppBuilder> configurePrism)
{
var prismBuilder = new PrismAppBuilder(containerExtension, builder);
configurePrism(prismBuilder);
return builder;
}

/// <summary>
/// Provides a Delegate to invoke when the App is initialized.
/// </summary>
/// <param name="builder">The <see cref="PrismAppBuilder"/>.</param>
/// <param name="action">The delegate to invoke.</param>
/// <returns>The <see cref="PrismAppBuilder"/>.</returns>
public static PrismAppBuilder OnInitialized(this PrismAppBuilder builder, Action action)
{
return builder.OnInitialized(_ => action());
Expand Down Expand Up @@ -45,9 +61,24 @@ public static PrismAppBuilder ConfigureModuleCatalog(this PrismAppBuilder builde
});
}

/// <summary>
/// When the <see cref="Application"/> is started and the native platform calls <see cref="IApplication.CreateWindow(IActivationState?)"/>
/// this delegate will be invoked to do your initial Navigation.
/// </summary>
/// <param name="builder">The <see cref="PrismAppBuilder"/>.</param>
/// <param name="uri">The initial Navigation Uri.</param>
/// <returns>The <see cref="PrismAppBuilder"/>.</returns>
public static PrismAppBuilder CreateWindow(this PrismAppBuilder builder, string uri) =>
builder.CreateWindow(navigation => navigation.NavigateAsync(uri));

/// <summary>
/// When the <see cref="Application"/> is started and the native platform calls <see cref="IApplication.CreateWindow(IActivationState?)"/>
/// this delegate will be invoked to do your initial Navigation.
/// </summary>
/// <param name="builder">The <see cref="PrismAppBuilder"/>.</param>
/// <param name="uri">The intial Navigation Uri.</param>
/// <param name="onError">A delegate callback if the navigation fails.</param>
/// <returns>The <see cref="PrismAppBuilder"/>.</returns>
public static PrismAppBuilder CreateWindow(this PrismAppBuilder builder, string uri, Action<Exception> onError) =>
builder.CreateWindow(async navigation =>
{
Expand All @@ -56,30 +87,78 @@ public static PrismAppBuilder ConfigureModuleCatalog(this PrismAppBuilder builde
onError(result.Exception);
});

public static PrismAppBuilder CreateWindow(this PrismAppBuilder builder, Func<IContainerProvider, INavigationService, Task> CreateWindowed) =>
builder.CreateWindow((c, n) => CreateWindowed(c, n));
/// <summary>
/// When the <see cref="Application"/> is started and the native platform calls <see cref="IApplication.CreateWindow(IActivationState?)"/>
/// this delegate will be invoked to do your initial Navigation.
/// </summary>
/// <param name="builder">The <see cref="PrismAppBuilder"/>.</param>
/// <param name="createWindow">The Navigation Delegate.</param>
/// <returns>The <see cref="PrismAppBuilder"/>.</returns>
public static PrismAppBuilder CreateWindow(this PrismAppBuilder builder, Func<IContainerProvider, INavigationService, Task> createWindow) =>
builder.CreateWindow((c, n) => createWindow(c, n));

public static PrismAppBuilder CreateWindow(this PrismAppBuilder builder, Func<INavigationService, Task> CreateWindowed) =>
builder.CreateWindow((_, n) => CreateWindowed(n));
/// <summary>
/// When the <see cref="Application"/> is started and the native platform calls <see cref="IApplication.CreateWindow(IActivationState?)"/>
/// this delegate will be invoked to do your initial Navigation.
/// </summary>
/// <param name="builder">The <see cref="PrismAppBuilder"/>.</param>
/// <param name="createWindow">The Navigation Delegate.</param>
/// <returns>The <see cref="PrismAppBuilder"/>.</returns>
public static PrismAppBuilder CreateWindow(this PrismAppBuilder builder, Func<INavigationService, Task> createWindow) =>
builder.CreateWindow((_, n) => createWindow(n));

public static PrismAppBuilder CreateWindow(this PrismAppBuilder builder, Func<INavigationService, INavigationBuilder> CreateWindowed) =>
builder.CreateWindow(n => CreateWindowed(n).NavigateAsync());
/// <summary>
/// When the <see cref="Application"/> is started and the native platform calls <see cref="IApplication.CreateWindow(IActivationState?)"/>
/// this delegate will be invoked to do your initial Navigation.
/// </summary>
/// <param name="builder">The <see cref="PrismAppBuilder"/>.</param>
/// <param name="createWindow">The Navigation Delegate.</param>
/// <returns>The <see cref="PrismAppBuilder"/>.</returns>
public static PrismAppBuilder CreateWindow(this PrismAppBuilder builder, Func<INavigationService, INavigationBuilder> createWindow) =>
builder.CreateWindow(n => createWindow(n).NavigateAsync());

/// <summary>
/// When the <see cref="Application"/> is started and the native platform calls <see cref="IApplication.CreateWindow(IActivationState?)"/>
/// this delegate will be invoked to do your initial Navigation.
/// </summary>
/// <param name="builder">The <see cref="PrismAppBuilder"/>.</param>
/// <param name="createWindow">The Navigation Delegate.</param>
/// <returns>The <see cref="PrismAppBuilder"/>.</returns>
public static PrismAppBuilder CreateWindow(this PrismAppBuilder builder, Func<IContainerProvider, INavigationService, INavigationBuilder> createWindow) =>
builder.CreateWindow((c, n) => createWindow(c, n).NavigateAsync());

public static PrismAppBuilder CreateWindow(this PrismAppBuilder builder, Func<IContainerProvider, INavigationService, INavigationBuilder> CreateWindowed) =>
builder.CreateWindow((c, n) => CreateWindowed(c, n).NavigateAsync());

/// <summary>
/// Provides a configuration delegate to add services to the <see cref="MauiAppBuilder.Services"/>
/// </summary>
/// <param name="builder">The <see cref="PrismAppBuilder"/>.</param>
/// <param name="configureServices">Configuration Delegate</param>
/// <returns>The <see cref="PrismAppBuilder"/>.</returns>
public static PrismAppBuilder ConfigureServices(this PrismAppBuilder builder, Action<IServiceCollection> configureServices)
{
configureServices(builder.MauiBuilder.Services);
return builder;
}

/// <summary>
/// Provides a delegate to configure Logging within the Maui application
/// </summary>
/// <param name="builder">The <see cref="PrismAppBuilder"/>.</param>
/// <param name="configureLogging"></param>
/// <returns>The <see cref="PrismAppBuilder"/>.</returns>
public static PrismAppBuilder ConfigureLogging(this PrismAppBuilder builder, Action<ILoggingBuilder> configureLogging)
{
configureLogging(builder.MauiBuilder.Logging);
return builder;
}

/// <summary>
/// Provides a configuration Delegate to the <see cref="ViewModelLocationProvider"/> to set the
/// DefaultViewTypeToViewModelTypeResolver.
/// </summary>
/// <param name="builder">The <see cref="PrismAppBuilder"/>.</param>
/// <param name="viewModelTypeResolver">The Configuration Delegate for the Default ViewType to ViewModelType Resolver.</param>
/// <returns>The <see cref="PrismAppBuilder"/>.</returns>
public static PrismAppBuilder ConfigureViewTypeToViewModelTypeResolver(this PrismAppBuilder builder, Func<Type, Type> viewModelTypeResolver)
{
ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver(viewModelTypeResolver);
Expand Down
Loading