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: Check if navigating back is possible to update state #2462

Merged
merged 2 commits into from
Jul 29, 2024
Merged
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
5 changes: 2 additions & 3 deletions src/Uno.Extensions.Hosting.UI/AppHostingEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ internal class AppHostingEnvironment : HostingEnvironment, IAppHostEnvironment,
public Assembly? HostAssembly { get; init; }

#if __WASM__
public async Task UpdateAddressBar(Uri applicationUri)
public async Task UpdateAddressBar(Uri applicationUri, bool canGoBack)
{
CoreApplication.MainView?.DispatcherQueue.TryEnqueue(() =>
{
var state = 1;
if (PlatformHelper.IsWebAssembly)
{
var currentView = SystemNavigationManager.GetForCurrentView();
state = currentView?.AppViewBackButtonVisibility == AppViewBackButtonVisibility.Visible ? 1 : 0;
state = canGoBack ? 1 : 0;
}

var href = Imports.GetLocation();
Expand Down
5 changes: 4 additions & 1 deletion src/Uno.Extensions.Hosting/IHasAddressBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ public interface IHasAddressBar
/// <param name="applicationUri">
/// The URI to update the address bar with.
/// </param>
/// <param name="canGoBack">
/// Whether it is possible to navigate back or not.
/// </param>
/// <returns>
/// A task that completes when the address bar has been updated.
/// </returns>
Task UpdateAddressBar(Uri applicationUri);
Task UpdateAddressBar(Uri applicationUri, bool canGoBack);
}
4 changes: 3 additions & 1 deletion src/Uno.Extensions.Navigation.UI/BrowserAddressBarService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,14 @@ private async void RouteChanged(object? sender, RouteChangedEventArgs e)
return;
}

var canGoBack = rootRegion.Navigator() is { } navigator && await navigator.CanGoBack();

var url = new UriBuilder
{
Query = route.Query(),
Path = route.FullPath()?.Replace("+", "/")
};
await _addressbarHost!.UpdateAddressBar(url.Uri);
await _addressbarHost!.UpdateAddressBar(url.Uri, canGoBack);
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,75 +1,76 @@
<Page x:Class="TestHarness.Ext.Navigation.PageNavigation.PageNavigationOnePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TestHarness.Ext.Navigation.PageNavigation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:TestHarness.Ext.Navigation.PageNavigation"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
xmlns:uen="using:Uno.Extensions.Navigation.UI"
xmlns:utu="using:Uno.Toolkit.UI"
xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
mc:Ignorable="d">

<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<utu:NavigationBar Content="Page Navigation - One"
AutomationProperties.AutomationId="PageNavigationOne" />
<StackPanel HorizontalAlignment="Center"
VerticalAlignment="Center"
Grid.Row="1">
<utu:NavigationBar AutomationProperties.AutomationId="PageNavigationOne" Content="Page Navigation - One" />
<StackPanel Grid.Row="1"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<TextBlock>
<Run Text="Created on UI Thread: " /><Run Text="{Binding CreatedOnUIThread}" />
</TextBlock>
<Button AutomationProperties.AutomationId="OnePageToTwoPageButton"
Content="Two"
uen:Navigation.Request="PageNavigationTwo" />
<Button uen:Navigation.Request="PageNavigationTwo"
AutomationProperties.AutomationId="OnePageToTwoPageButton"
Content="Two" />
<Button AutomationProperties.AutomationId="OnePageToTwoPageCodebehindButton"
Content="Two (Codebehind)"
Click="OnePageToTwoPageCodebehindClick" />
Click="OnePageToTwoPageCodebehindClick"
Content="Two (Codebehind)" />
<Button AutomationProperties.AutomationId="OnePageToTwoPageViewModelButton"
Content="Two (ViewModel)"
Click="{x:Bind ViewModel.GoToTwo}" />
Click="{x:Bind ViewModel.GoToTwo}"
Content="Two (ViewModel)" />
<Button AutomationProperties.AutomationId="RapidSettingsWriteTestButton"
Content="Settings Write Test"
Click="{x:Bind ViewModel.SettingsWriteTest}" />
Click="{x:Bind ViewModel.SettingsWriteTest}"
Content="Settings Write Test" />
<TextBlock x:Name="TxtUrl" AutomationProperties.AutomationId="OnePageTxtUrlFromBrowser" />
<Button AutomationProperties.AutomationId="OnePageGetUrlFromBrowser"
Click="GetUrlFromBrowser"
Content="Get URL" />
</StackPanel>
<ScrollViewer Grid.Row="2">
<muxc:ItemsRepeater ItemsSource="{Binding Items}"
uen:Navigation.Request="PageNavigationTwo">
<muxc:ItemsRepeater uen:Navigation.Request="PageNavigationTwo" ItemsSource="{Binding Items}">
<muxc:ItemsRepeater.ItemTemplate>
<DataTemplate>
<!--<UserControl>-->
<Grid Height="200"
Background="Red">
<Border Background="Pink"
VerticalAlignment="Top"
HorizontalAlignment="Center">
<TextBlock Text="{Binding}" />
</Border>
<utu:CardContentControl Height="50"
Width="200"
Style="{StaticResource FilledCardContentControlStyle}"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch">
<utu:CardContentControl.ContentTemplate>
<DataTemplate>
<Border Background="Green"
<Grid Height="200" Background="Red">
<Border HorizontalAlignment="Center"
VerticalAlignment="Top"
Background="Pink">
<TextBlock Text="{Binding}" />
</Border>
<utu:CardContentControl Width="200"
Height="50"
Width="200">
<TextBlock Text="Inner card" />
</Border>
</DataTemplate>
</utu:CardContentControl.ContentTemplate>
</utu:CardContentControl>
<Button VerticalAlignment="Bottom"
Content="Go to Three"
uen:Navigation.Request="PageNavigationThree" />
</Grid>
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
Style="{StaticResource FilledCardContentControlStyle}">
<utu:CardContentControl.ContentTemplate>
<DataTemplate>
<Border Width="200"
Height="50"
Background="Green">
<TextBlock Text="Inner card" />
</Border>
</DataTemplate>
</utu:CardContentControl.ContentTemplate>
</utu:CardContentControl>
<Button VerticalAlignment="Bottom"
uen:Navigation.Request="PageNavigationThree"
Content="Go to Three" />
</Grid>
<!--</UserControl>-->
</DataTemplate>
</muxc:ItemsRepeater.ItemTemplate>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,22 @@ public async void OnePageToTwoPageCodebehindClick(object sender, RoutedEventArgs
await this.Navigator()!.NavigateViewAsync<PageNavigationTwoPage>(this);
}


public async void GetUrlFromBrowser(object sender, RoutedEventArgs e)
{
#if __WASM__
var url = Imports.GetLocation();

TxtUrl.Text = url;
#else
TxtUrl.Text = "Not supported";
#endif
}
}
#if __WASM__
internal static partial class Imports
{
[System.Runtime.InteropServices.JavaScript.JSImport("globalThis.Uno.Extensions.Hosting.getLocation")]
public static partial string GetLocation();
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
<ProjectGuid>6279c845-92f8-4333-ab99-3d213163593c</ProjectGuid>
<MinimumVisualStudioVersion>14.0</MinimumVisualStudioVersion>
</PropertyGroup>
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.Default.props" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.props" />
Expand Down Expand Up @@ -37,4 +40,4 @@
<_Globbled_Page Remove="Ext\Navigation\NavigationView\NavigationViewDataRecipeDetailsPage.xaml" />
<_Globbled_Page Remove="Ext\Navigation\NavigationView\NavigationViewDataRecipesPage.xaml" />
</ItemGroup>
</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,27 @@ private void OnePageToFivePage()
App.WaitThenTap("FourPageToFivePageButton");

}

[Test]
[ActivePlatforms(Platform.Browser)]
public void When_PageNavigationNavigateRootUpdateUrl()
{
InitTestSection(TestSections.Navigation_PageNavigation);

App.WaitThenTap("ShowOnePageButton");

App.WaitThenTap("OnePageGetUrlFromBrowser");
App.WaitElement("OnePageTxtUrlFromBrowser");
var urlBefore = App.GetText("OnePageTxtUrlFromBrowser");

App.WaitThenTap("OnePageToTwoPageButton");

App.WaitThenTap("TwoPageBackButton");

App.WaitThenTap("OnePageGetUrlFromBrowser");
App.WaitElement("OnePageTxtUrlFromBrowser");
var urlAfter = App.GetText("OnePageTxtUrlFromBrowser");

Assert.AreEqual(urlBefore, urlAfter);
}
}
Loading