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

fix: Navigate once when starting app with same route as navigated #2639

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
19 changes: 12 additions & 7 deletions src/Uno.Extensions.Navigation.UI/FrameworkElementExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Microsoft.Extensions.DependencyInjection;

namespace Uno.Extensions.Navigation;
namespace Uno.Extensions.Navigation;

public static class FrameworkElementExtensions
{
Expand Down Expand Up @@ -35,11 +33,12 @@ public static Task HostAsync(
{
var initialNavigation = () => nav.NavigateRouteAsync(root, initialRoute ?? string.Empty);

Route? launchRoute = default;
var start = () => Task.CompletedTask;
var hostConfigOptions = sp.GetService<IOptions<HostConfiguration>>();
if (hostConfigOptions?.Value is { } hostConfig &&
hostConfig.LaunchRoute() is { } launchRoute &&
launchRoute.IsEmpty() == false)
(launchRoute = hostConfig.LaunchRoute()) is { } &&
!launchRoute.IsEmpty())
{
start = () => nav.NavigateRouteAsync(root, launchRoute.FullPath());
}
Expand All @@ -57,8 +56,14 @@ public static Task HostAsync(
}
var fullstart = async () =>
{
await initialNavigation();
await start();
var response = await initialNavigation();

if (launchRoute is null ||
!launchRoute.FullPath().Equals(response?.Route?.FullPath(), StringComparison.OrdinalIgnoreCase))

{
await start();
}
};
var startupTask = elementRegion.Services!.Startup(fullstart);
return startupTask;
Expand Down
1 change: 1 addition & 0 deletions testing/TestHarness/TestHarness.Core/TestSections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public enum TestSections
Navigation_NavigationView,
Navigation_TabBar,
Navigation_Reactive,
Navigation_AddressBar,
Apps_Chefs,
Apps_Commerce,
Apps_Commerce_ShellControl,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace TestHarness.UITest;

public class Given_AddressBar : NavigationTestBase
{
[Test]
public async Task When_AddressBar_HomePage_Wont_Navigate_Twice()
{
InitTestSection(TestSections.Navigation_AddressBar);

App.WaitElement("TbInstanceCountProperty");

var intanceCount = App.GetText("TbInstanceCountProperty");

Assert.AreEqual("1", intanceCount);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace TestHarness.Ext.Navigation.AddressBar;

public partial class AddressBarHomeModel
{
public static int InstanceCount
{
get => ApplicationData.Current.LocalSettings.Values.TryGetValue(Constants.HomeInstanceCountKey, out var value)
? (int)value
: 0;
private set => ApplicationData.Current.LocalSettings.Values[Constants.HomeInstanceCountKey] = value;
}

public int InstanceCountProperty { get; private set; }

public AddressBarHomeModel()
{
InstanceCountProperty = ++InstanceCount;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Page x:Class="TestHarness.Ext.Navigation.AddressBar.AddressBarHomePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TestHarness.Ext.Navigation.RoutesNavigation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:uen="using:Uno.Extensions.Navigation.UI"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">

<Grid>
<StackPanel Grid.Row="1"
HorizontalAlignment="Center"
Spacing="16">
<TextBlock Text="AddressBar HomePage" />
<StackPanel Orientation="Horizontal">
<TextBlock Text="This page was created: " />
<TextBlock AutomationProperties.AutomationId="TbInstanceCountProperty"
Text="{Binding InstanceCountProperty}" />
<TextBlock Text=" times" />
</StackPanel>

</StackPanel>

</Grid>
</Page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace TestHarness.Ext.Navigation.AddressBar;

public sealed partial class AddressBarHomePage : Page
{
public AddressBarHomePage()
{
this.InitializeComponent();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace TestHarness.Ext.Navigation.AddressBar;

public class AddressBarHostInit : BaseHostInitialization
{
protected override string[] ConfigurationFiles => new string[] { "TestHarness.Ext.Navigation.AddressBar.appsettings.addressbar.json" };

public AddressBarHostInit()
{
if (ApplicationData.Current.LocalSettings.Values.TryGetValue(Constants.HomeInstanceCountKey, out var value))
{
ApplicationData.Current.LocalSettings.Values[Constants.HomeInstanceCountKey] = 0;
}
}
protected override void RegisterRoutes(IViewRegistry views, IRouteRegistry routes)
{
views.Register(
new ViewMap<AddressBarHomePage, AddressBarHomeModel>(),
new ViewMap(ViewModel: typeof(ShellViewModel))
);

routes.Register(
new RouteMap("", View: views.FindByViewModel<ShellViewModel>(),
Nested:
[
new RouteMap("AddressBarHome", View: views.FindByViewModel<AddressBarHomeModel>(), IsDefault: true)
]
)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<testharness:BaseTestSectionPage xmlns:testharness="using:TestHarness"
x:Class="TestHarness.Ext.Navigation.AddressBar.AddressBarMainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TestHarness.Ext.Navigation.RoutesNavigation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:uen="using:Uno.Extensions.Navigation.UI"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Text="AddressBar Navigation Tests"
Margin="20"
FontSize="30" />

<ContentControl AutomationProperties.AutomationId="NavigationRoot"
x:Name="NavigationRoot"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
Grid.Row="1" />

<StackPanel Grid.Row="2"
Orientation="Horizontal"
HorizontalAlignment="Center">
<Button AutomationProperties.AutomationId="ShowAppButton"
Content="AddressBar App"
Click="ShowAppClick" />
</StackPanel>
</Grid>

</testharness:BaseTestSectionPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace TestHarness.Ext.Navigation.AddressBar;

[TestSectionRoot("AddressBar Navigation", TestSections.Navigation_AddressBar, typeof(AddressBarHostInit))]
public sealed partial class AddressBarMainPage : BaseTestSectionPage
{
public AddressBarMainPage()
{
this.InitializeComponent();
}

public async void ShowAppClick(object sender, RoutedEventArgs e)
{
await Navigator.NavigateRouteAsync(this, "");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace TestHarness.Ext.Navigation.AddressBar;

internal class Constants
{
public const string HomeInstanceCountKey = "HomeInstanceCount";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace TestHarness.Ext.Navigation.AddressBar;

public record ShellViewModel
{
public INavigator? Navigator { get; init; }

public ShellViewModel(INavigator navigator)
{
Navigator = navigator;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"HostConfiguration": {
"LaunchUrl": "http://localhost:5000/AddressBarHome"
}
}
1 change: 1 addition & 0 deletions testing/TestHarness/TestHarness/TestHarness.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<EmbeddedResource Include="Ext\Http\Endpoints\appsettings.httpendpoints.json" />
<EmbeddedResource Include="Ext\Http\Refit\appsettings.httprefit.json" />
<EmbeddedResource Include="Ext\Localization\appsettings.locale.json" />
<EmbeddedResource Include="Ext\Navigation\AddressBar\appsettings.addressbar.json" />
<EmbeddedResource Include="Ext\Navigation\Apps\Commerce\appsettings.logging.json" />
</ItemGroup>

Expand Down
Loading