Skip to content

Commit

Permalink
fix: Include Data to navigation for addressbar
Browse files Browse the repository at this point in the history
  • Loading branch information
eriklimakc committed Jan 16, 2025
1 parent b16e160 commit e175333
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ viewType is null ||

CurrentView?.SetNavigatorInstance(Region.Navigator()!);

var responseRequest = navSegment with { Qualifier = route.Qualifier, Data = route.Data };

var responseRequest = navSegment with { Qualifier = route.Qualifier };
return responseRequest;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,19 @@ public async Task When_AddressBar_HomePage_Wont_Navigate_Twice()

Assert.AreEqual("1", intanceCount);
}

[Test]
[ActivePlatforms(Platform.Browser)]
public void When_AddressBar_SecondPage_Query_Displayed()
{
InitTestSection(TestSections.Navigation_AddressBar);

App.WaitThenTap("AddressBarSecondButton");

App.WaitThenTap("GetUrlFromBrowser");

var url = App.GetText("TxtUrlFromBrowser");

StringAssert.Contains(url, "QueryUser.Id=8a5c5b2e-ff96-474b-9e4d-65bde598f6bc");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

public partial class AddressBarHomeModel
{
public IDictionary<string, object> UserId => new Dictionary<string, object>
{
{ "QueryUser.Id", new Guid("8a5c5b2e-ff96-474b-9e4d-65bde598f6bc") }
};

public static int InstanceCount
{
get => ApplicationData.Current.LocalSettings.Values.TryGetValue(Constants.HomeInstanceCountKey, out var value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
Text="{Binding InstanceCountProperty}" />
<TextBlock Text=" times" />
</StackPanel>
<StackPanel>
<Button Content="Go To Second"
AutomationProperties.AutomationId="AddressBarSecondButton"
uen:Navigation.Request="AddressBarSecond"
uen:Navigation.Data="{Binding UserId}" />
</StackPanel>

</StackPanel>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,34 @@ public AddressBarHostInit()
protected override void RegisterRoutes(IViewRegistry views, IRouteRegistry routes)
{
views.Register(
new ViewMap(ViewModel: typeof(ShellViewModel)),
new ViewMap<AddressBarHomePage, AddressBarHomeModel>(),
new ViewMap(ViewModel: typeof(ShellViewModel))
new DataViewMap<AddressBarSecondPage, AddressBarSecondModel, AddressBarUser>(
ToQuery: user => new Dictionary<string, string>
{
{ "QueryUser.Id", $"{user.UserId}" }
},
FromQuery: async (sp, query) =>
{
var userService = new AddressBarUserService();

if (Guid.TryParse($"{query["QueryUser.Id"]}", out var guid))
{
var user = userService.GetById(guid);
return user ?? new AddressBarUser(guid, "User not found");
}

return new AddressBarUser(guid, "User not found");
}
)
);

routes.Register(
new RouteMap("", View: views.FindByViewModel<ShellViewModel>(),
Nested:
[
new RouteMap("AddressBarHome", View: views.FindByViewModel<AddressBarHomeModel>(), IsDefault: true)
new RouteMap("AddressBarHome", View: views.FindByViewModel<AddressBarHomeModel>(), IsDefault: true),
new RouteMap("AddressBarSecond", View: views.FindByViewModel<AddressBarSecondModel>())
]
)
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace TestHarness.Ext.Navigation.AddressBar;

public class AddressBarSecondModel
{
public AddressBarUser User { get; set; }

public AddressBarSecondModel(AddressBarUser user)
{
User = user;
}
}

public class AddressBarUser(Guid id, string name)
{
public Guid UserId { get; set; } = id;
public string UserName { get; set; } = name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<Page x:Class="TestHarness.Ext.Navigation.AddressBar.AddressBarSecondPage"
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 SecondPage" />

<TextBlock HorizontalAlignment="Center"
Style="{StaticResource TitleLarge}">
<Run Text="User Id: " /><Run Text="{Binding User.UserId}" />
</TextBlock>

<TextBlock HorizontalAlignment="Center"
Style="{StaticResource TitleLarge}">
<Run Text="User name: " /><Run Text="{Binding User.UserName}" />
</TextBlock>

<TextBlock x:Name="TxtUrl"
AutomationProperties.AutomationId="TxtUrlFromBrowser" />

<Button AutomationProperties.AutomationId="GetUrlFromBrowser"
Click="GetUrlFromBrowser"
Content="Get URL" />
</StackPanel>

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

public sealed partial class AddressBarSecondPage : Page
{
public AddressBarSecondPage()
{
this.InitializeComponent();
}

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

TxtUrl.Text = url;
#else
TxtUrl.Text = "Not supported";
#endif
}
}
#if __WASM__
internal static partial class ImportsJS
{
[System.Runtime.InteropServices.JavaScript.JSImport("globalThis.Uno.Extensions.Hosting.getLocation")]
public static partial string GetUrl();
}
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace TestHarness.Ext.Navigation.AddressBar;

public class AddressBarUserService
{
private readonly List<AddressBarUser> _users;

public AddressBarUserService()
{
_users =
[
new(ConvertFromString("8a5c5b2e-ff96-474b-9e4d-65bde598f6bc"), "João Rodrigues"),
new(ConvertFromString("2b64071a-2c8a-45e4-9f48-3eb7d7aace41"), "Ross Polard")
];
}

public AddressBarUser? GetById(Guid id)
{
return _users.FirstOrDefault(user => user.UserId == id);
}

private static Guid ConvertFromString(string value)
{
var guid = Guid.Parse(value);
return guid;
}
}

0 comments on commit e175333

Please sign in to comment.