From ae07ea6f354dc559427d028a79fea7ca0606195b Mon Sep 17 00:00:00 2001 From: Nick Randolph Date: Mon, 22 Jan 2024 18:17:33 +1100 Subject: [PATCH] fix: Adding default routes for hard coded regions --- src/Uno.Extensions.Navigation.UI/Navigator.cs | 1 + .../Navigators/PanelVisiblityNavigator.cs | 18 +++++++++++++++++- .../RouteResolver.cs | 10 ++++++++++ .../IRouteResolver.cs | 2 ++ .../Ext/Navigation/TabBar/TabBarHostInit.cs | 7 ++++++- 5 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/Uno.Extensions.Navigation.UI/Navigator.cs b/src/Uno.Extensions.Navigation.UI/Navigator.cs index 9785631273..7d66e2d938 100644 --- a/src/Uno.Extensions.Navigation.UI/Navigator.cs +++ b/src/Uno.Extensions.Navigation.UI/Navigator.cs @@ -549,6 +549,7 @@ protected virtual async Task RegionCanNavigate(Route route, RouteInfo? rou if (currentRouteMap != null || routeMap != null) { if (routeMap?.Parent is not null && + currentRouteMap?.Parent is not null && currentRouteMap?.Parent != routeMap.Parent) { return false; diff --git a/src/Uno.Extensions.Navigation.UI/Navigators/PanelVisiblityNavigator.cs b/src/Uno.Extensions.Navigation.UI/Navigators/PanelVisiblityNavigator.cs index 5e7e4d4a6b..caef86e33b 100644 --- a/src/Uno.Extensions.Navigation.UI/Navigators/PanelVisiblityNavigator.cs +++ b/src/Uno.Extensions.Navigation.UI/Navigators/PanelVisiblityNavigator.cs @@ -14,6 +14,22 @@ public PanelVisiblityNavigator( RegionControlProvider controlProvider) : base(logger, dispatcher, region, resolver, controlProvider.RegionControl as Grid) { + if (region.View is not null) + { + region.View.Loaded += PanelLoaded; + } + } + + private void PanelLoaded(object sender, RoutedEventArgs e) { + if (Control is null) + { + return; + } + Control.Loaded -= PanelLoaded; + + var existingRoutes = + Control.Children.OfType().Select(x => x.GetName()).Where(x => x is { Length: > 0 }); + existingRoutes.ForEach(r => Resolver.InsertRoute(new RouteInfo(r))).ToArray(); } protected override async Task RegionCanNavigate(Route route, RouteInfo? routeMap) @@ -23,7 +39,7 @@ protected override async Task RegionCanNavigate(Route route, RouteInfo? ro return false; } - if(routeMap?.RenderView?.IsSubclassOf(typeof(FrameworkElement)) ?? false) + if (routeMap?.RenderView?.IsSubclassOf(typeof(FrameworkElement)) ?? false) { return true; } diff --git a/src/Uno.Extensions.Navigation.UI/RouteResolver.cs b/src/Uno.Extensions.Navigation.UI/RouteResolver.cs index e1ac28f3df..a7aea99c61 100644 --- a/src/Uno.Extensions.Navigation.UI/RouteResolver.cs +++ b/src/Uno.Extensions.Navigation.UI/RouteResolver.cs @@ -232,6 +232,16 @@ protected virtual RouteInfo[] InternalFindByView(Type? viewType) return BestNavigatorRouteInfo(maps, navigator); } + /// + public void InsertRoute(RouteInfo route) + { + var path = route.Path; + if (Mappings.FirstOrDefault(x => x.Path == path) is not { }) + { + Mappings.Add(route); + } + } + private RouteInfo[] FindRouteByType(Type? typeToFind, Func mapType) { return FindByInheritedTypes(Mappings, typeToFind, mapType); diff --git a/src/Uno.Extensions.Navigation/IRouteResolver.cs b/src/Uno.Extensions.Navigation/IRouteResolver.cs index 08d99a10ac..15c2871fdf 100644 --- a/src/Uno.Extensions.Navigation/IRouteResolver.cs +++ b/src/Uno.Extensions.Navigation/IRouteResolver.cs @@ -11,4 +11,6 @@ public interface IRouteResolver RouteInfo? FindByData(Type? dataType, INavigator? navigator); RouteInfo? FindByResultData(Type? resultDataType, INavigator? navigator); + + void InsertRoute(RouteInfo routeInfo); } diff --git a/testing/TestHarness/TestHarness.Shared/Ext/Navigation/TabBar/TabBarHostInit.cs b/testing/TestHarness/TestHarness.Shared/Ext/Navigation/TabBar/TabBarHostInit.cs index 375706b5de..240845311f 100644 --- a/testing/TestHarness/TestHarness.Shared/Ext/Navigation/TabBar/TabBarHostInit.cs +++ b/testing/TestHarness/TestHarness.Shared/Ext/Navigation/TabBar/TabBarHostInit.cs @@ -21,7 +21,12 @@ protected override void RegisterRoutes(IViewRegistry views, IRouteRegistry route new RouteMap("", Nested: new[] { - new RouteMap("Home", View: views.FindByViewModel()), + new RouteMap("Home", View: views.FindByViewModel(), Nested: + new RouteMap[] + { + new RouteMap("Products"), + + }), new RouteMap("List", View: views.FindByViewModel(), Nested: new RouteMap[] {