diff --git a/src/SharedMauiCoreLibrary.Licensing/Hosting/AppHostBuilderExtensions.cs b/src/SharedMauiCoreLibrary.Licensing/Hosting/AppHostBuilderExtensions.cs index f1932b4..267d6fa 100644 --- a/src/SharedMauiCoreLibrary.Licensing/Hosting/AppHostBuilderExtensions.cs +++ b/src/SharedMauiCoreLibrary.Licensing/Hosting/AppHostBuilderExtensions.cs @@ -14,16 +14,17 @@ public static MauiAppBuilder ConfigureLicensing(this MauiAppBuilder builder, Uri builder.Services.AddSingleton(manager); return builder; } - public static MauiAppBuilder ConfigureAppUpdater(this MauiAppBuilder builder, ILicenseManager licenseManager, IDispatcher dispatcher) + public static MauiAppBuilder ConfigureAppUpdater(this MauiAppBuilder builder, ILicenseManager licenseManager, IDispatcher? dispatcher = null) { + dispatcher ??= builder.Services.BuildServiceProvider().GetService(); AppUpdateManager manager = new(dispatcher, licenseManager); builder.Services.AddSingleton(manager); return builder; } - public static MauiAppBuilder ConfigureAppUpdater(this MauiAppBuilder builder) + public static MauiAppBuilder ConfigureAppUpdater(this MauiAppBuilder builder, IDispatcher? dispatcher = null) { ILicenseManager? licenseManager = builder.Services.BuildServiceProvider().GetService(); - IDispatcher? dispatcher = builder.Services.BuildServiceProvider().GetService(); + dispatcher ??= builder.Services.BuildServiceProvider().GetService(); if (licenseManager is not null && dispatcher is not null) { builder.ConfigureAppUpdater(licenseManager, dispatcher); diff --git a/src/SharedMauiCoreLibrary/Hosting/AppHostBuilderExtensions.cs b/src/SharedMauiCoreLibrary/Hosting/AppHostBuilderExtensions.cs index 23dedfb..694b232 100644 --- a/src/SharedMauiCoreLibrary/Hosting/AppHostBuilderExtensions.cs +++ b/src/SharedMauiCoreLibrary/Hosting/AppHostBuilderExtensions.cs @@ -1,5 +1,6 @@ using AndreasReitberger.Shared.Core.Interfaces; using AndreasReitberger.Shared.Core.Localization; +using AndreasReitberger.Shared.Core.NavigationManager; using CommunityToolkit.Maui; using System.Runtime.Versioning; @@ -37,5 +38,14 @@ public static MauiAppBuilder ConfigureLocalizationManager(this MauiAppBuilder bu builder.Services.AddSingleton(manager); return builder; } + + public static MauiAppBuilder ConfigureShellNavigator(this MauiAppBuilder builder, string rootPage, List? entryPages = null, IDispatcher? dispatcher = null) + { + dispatcher ??= builder.Services.BuildServiceProvider().GetService(); + ShellNavigator navigator = dispatcher is not null ? new(rootPage, dispatcher) : new(rootPage); + navigator.AvailableEntryPages = [.. entryPages ?? [ rootPage]]; + builder.Services.AddSingleton(navigator); + return builder; + } } } diff --git a/src/SharedMauiCoreLibrary/Interfaces/IShellNavigator.cs b/src/SharedMauiCoreLibrary/Interfaces/IShellNavigator.cs index b21796a..3d33ced 100644 --- a/src/SharedMauiCoreLibrary/Interfaces/IShellNavigator.cs +++ b/src/SharedMauiCoreLibrary/Interfaces/IShellNavigator.cs @@ -18,6 +18,8 @@ public interface IShellNavigator public Task GoToRootAsync(string target, Dictionary? parameters = null, bool? flyoutIsPresented = null, int delay = -1, bool animate = false, string rootPrefix = "///"); public Task GoBackAsync(Dictionary? parameters = null, bool? flyoutIsPresented = null, int delay = -1, bool animate = false, bool confirm = false, Func>? confirmFunction = null); bool IsCurrentPathRoot(); + public void SubscribeNavigated(); + public void UnsubscribeNavigated(); #endregion #region Events diff --git a/src/SharedMauiCoreLibrary/Models/NavigationManager/ShellNavigator.cs b/src/SharedMauiCoreLibrary/Models/NavigationManager/ShellNavigator.cs index e49677f..2b81bc7 100644 --- a/src/SharedMauiCoreLibrary/Models/NavigationManager/ShellNavigator.cs +++ b/src/SharedMauiCoreLibrary/Models/NavigationManager/ShellNavigator.cs @@ -1,5 +1,6 @@ using AndreasReitberger.Shared.Core.Events; using AndreasReitberger.Shared.Core.Interfaces; +using System.Diagnostics; namespace AndreasReitberger.Shared.Core.NavigationManager { @@ -58,7 +59,7 @@ public static IShellNavigator Instance public ShellNavigator() { Dispatcher = DispatcherProvider.Current.GetForCurrentThread(); - Shell.Current.Navigated += (a, b) => + Shell.Current?.Navigated += (a, b) => { string msg = $"Navigation: From '{b.Previous?.Location}' to '{b.Current?.Location}'. Source = '{b.Source}'"; OnNavigationDone(new NavigationDoneEventArgs() @@ -221,7 +222,36 @@ string GetCurrentRoute() return string.Empty; } } + public void SubscribeNavigated() + { + if (Shell.Current is not null) + { + UnsubscribeNavigated(); + Shell.Current.Navigated += OnNavigated; + } + } + public void UnsubscribeNavigated() + { + if (Shell.Current is not null) + { + Shell.Current.Navigated -= OnNavigated; + } + } + private void OnNavigated(object? sender, ShellNavigatedEventArgs e) + { +#if DEBUG + string msg = $"Navigation: From '{e.Previous?.Location}' to '{e.Current?.Location}'. Source = '{e.Source}'"; + Debug.WriteLine(msg); +#endif + OnNavigationDone(new NavigationDoneEventArgs() + { + Arguments = e, + NavigatedFrom = e.Previous?.Location, + NavigatedTo = e.Current?.Location, + Source = e.Source, + }); + } #endregion }