Skip to content

Commit

Permalink
Merge pull request #2130 from unoplatform/dev/nr/noreflection
Browse files Browse the repository at this point in the history
fix: Adding caching for generated route maps
  • Loading branch information
nickrandolph authored Jan 24, 2024
2 parents 282cf8d + 48f24b5 commit 8caf092
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 72 deletions.
53 changes: 53 additions & 0 deletions src/Uno.Extensions.Navigation.UI/RouteInfoExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
namespace Uno.Extensions.Navigation;

internal static class RouteInfoExtensions
{
internal static RouteInfo[] Ancestors(this RouteInfo routeInfo, IRouteResolver resolver)
{
var routes = new List<RouteInfo>();
routeInfo.NavigatorAncestors(resolver, routes);
return routes.ToArray();
}

private static void NavigatorAncestors(this RouteInfo routeInfo, IRouteResolver resolver, IList<RouteInfo> routes)
{
routes.Insert(0, routeInfo);

while (routeInfo?.DependsOnRoute is { } dependee)
{
routes.Insert(0, dependee);
routeInfo = dependee;
}

if (routeInfo?.Parent is { } parent)
{
parent.NavigatorAncestors(resolver, routes);
}
}

internal static RouteInfo[] Ancestors(this INavigator navigator, IRouteResolver resolver)
{
var routes = new List<RouteInfo>();
navigator.NavigatorAncestors(resolver, routes);
return routes.ToArray();
}

private static void NavigatorAncestors(this INavigator navigator, IRouteResolver resolver, IList<RouteInfo> routes)
{
var route = (navigator is IStackNavigator deepNav) ? deepNav.FullRoute : navigator?.Route;
while (!(route?.IsEmpty() ?? true))
{
var info = resolver.FindByPath(route.Base);
if (info is not null)
{
routes.Insert(0, info);
}
route = route.Next();
}

if (navigator?.GetParent() is { } parent)
{
parent.NavigatorAncestors(resolver, routes);
}
}
}
78 changes: 8 additions & 70 deletions src/Uno.Extensions.Navigation.UI/RouteResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ protected static bool IsDialogViewType(Type? viewType = null)
{
if (viewType is null)
{
return false; ;
return false;
}

return viewType == typeof(MessageDialog) ||
Expand All @@ -147,9 +147,7 @@ protected static bool IsDialogViewType(Type? viewType = null)
}

public RouteInfo? FindByPath(string? path)
{
return InternalFindByPath(path);
}
=> InternalFindByPath(path);

protected virtual RouteInfo? InternalFindByPath(string? path)
{
Expand All @@ -175,7 +173,8 @@ protected static bool IsDialogViewType(Type? viewType = null)
{
return default;
}
else if (maps.Length == 1 ||

if (maps.Length == 1 ||
navigator is null)
{
return maps[0];
Expand Down Expand Up @@ -205,9 +204,7 @@ protected static bool IsDialogViewType(Type? viewType = null)
}

protected virtual RouteInfo[] InternalFindByViewModel(Type? viewModelType)
{
return FindRouteByType(viewModelType, map => map.ViewModel);
}
=> FindRouteByType(viewModelType, map => map.ViewModel);

public RouteInfo? FindByView(Type? viewType, INavigator? navigator)
{
Expand All @@ -216,9 +213,7 @@ protected virtual RouteInfo[] InternalFindByViewModel(Type? viewModelType)
}

protected virtual RouteInfo[] InternalFindByView(Type? viewType)
{
return FindRouteByType(viewType, map => map.RenderView);
}
=> FindRouteByType(viewType, map => map.RenderView);

public RouteInfo? FindByData(Type? dataType, INavigator? navigator)
{
Expand All @@ -243,65 +238,8 @@ public void InsertRoute(RouteInfo route)
}

private RouteInfo[] FindRouteByType(Type? typeToFind, Func<RouteInfo, Type?> mapType)
{
return FindByInheritedTypes(Mappings, typeToFind, mapType);
}
=> FindByInheritedTypes(Mappings, typeToFind, mapType);

private TMap[] FindByInheritedTypes<TMap>(IList<TMap> mappings, Type? typeToFind, Func<TMap, Type?> mapType)
{
return mappings.FindByInheritedTypes(typeToFind, mapType);
}
}

public static class TempHelpers
{
internal static RouteInfo[] Ancestors(this RouteInfo routeInfo, IRouteResolver resolver)
{
var routes = new List<RouteInfo>();
routeInfo.NavigatorAncestors(resolver, routes);
return routes.ToArray();
}

private static void NavigatorAncestors(this RouteInfo routeInfo, IRouteResolver resolver, IList<RouteInfo> routes)
{
routes.Insert(0, routeInfo);

while (routeInfo?.DependsOnRoute is { } dependee)
{
routes.Insert(0, dependee);
routeInfo = dependee;
}

if (routeInfo?.Parent is { } parent)
{
parent.NavigatorAncestors(resolver, routes);
}
}


internal static RouteInfo[] Ancestors(this INavigator navigator, IRouteResolver resolver)
{
var routes = new List<RouteInfo>();
navigator.NavigatorAncestors(resolver, routes);
return routes.ToArray();
}

private static void NavigatorAncestors(this INavigator navigator, IRouteResolver resolver, IList<RouteInfo> routes)
{
var route = (navigator is IStackNavigator deepNav) ? deepNav.FullRoute : navigator?.Route;
while (!(route?.IsEmpty() ?? true))
{
var info = resolver.FindByPath(route.Base);
if (info is not null)
{
routes.Insert(0, info);
}
route = route.Next();
}

if (navigator?.GetParent() is { } parent)
{
parent.NavigatorAncestors(resolver, routes);
}
}
=> mappings.FindByInheritedTypes(typeToFind, mapType);
}
2 changes: 2 additions & 0 deletions src/Uno.Extensions.Navigation.UI/RouteResolverDefault.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ view is null &&
});
Mappings.Add(defaultMapFromViewMap);
if (Logger.IsEnabled(LogLevel.Debug)) Logger.LogDebugMessage($"Created default mapping from viewmap - Path '{defaultMapFromViewMap.Path}'");
return defaultMapFromViewMap;
}

if (Logger.IsEnabled(LogLevel.Information)) Logger.LogInformationMessage($"For better performance (avoid reflection), create mapping for for path '{path}', view '{view?.Name}', view model '{viewModel?.Name}'");
Expand All @@ -134,6 +135,7 @@ view is null &&
return IsDialogViewType(view);
});
if (Logger.IsEnabled(LogLevel.Debug)) Logger.LogDebugMessage($"Created default mapping - Path '{defaultMap.Path}'");
Mappings.Add(defaultMap);
return defaultMap;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
</Grid.Resources>

<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition />
Expand All @@ -29,8 +30,9 @@
Grid.Row="1"
x:Name="CurrentTabBarItemText"
AutomationProperties.AutomationId="CurrentTabBarItemTextBlock" />
<ToggleButton Grid.Row="2" Content="ImplicitMapping" IsChecked="{Binding ImplicitMappingEnabled}" Command="{Binding ToggleIsCheckedCommand}" />
<Grid uen:Region.Attached="true"
Grid.Row="2" >
Grid.Row="3" >
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
namespace TestHarness.Ext.Navigation.TabBar;

public record TabBarHomeViewModel(INavigator Navigator)
public partial class TabBarHomeViewModel : ObservableObject
{
private RouteResolverDefault routeResolver;
public TabBarHomeViewModel(IServiceProvider services)
{
routeResolver = services.GetRequiredService<RouteResolverDefault>();
ImplicitMappingEnabled = routeResolver.ReturnImplicitMapping;
}

[ObservableProperty]
private bool _implicitMappingEnabled;

[RelayCommand]
public void ToggleIsChecked()
{
routeResolver.ReturnImplicitMapping = !routeResolver.ReturnImplicitMapping;
ImplicitMappingEnabled = routeResolver.ReturnImplicitMapping;
}
}

0 comments on commit 8caf092

Please sign in to comment.