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

feat: Enabling deep link navigation by default #2039

Merged
merged 6 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion samples/Playground/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
-->
<PackageVersion Include="Microsoft.NETCore.UniversalWindowsPlatform" Version="6.2.13" />
<PackageVersion Include="Microsoft.UI.Xaml" Version="2.7.1" />
<PackageVersion Include="Microsoft.Windows.Compatibility" Version="6.0.0" />
<PackageVersion Include="Microsoft.Windows.Compatibility" Version="7.0.3" />
<PackageVersion Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.756" />
<PackageVersion Include="Microsoft.WindowsAppSDK" Version="1.4.231008000" />
<PackageVersion Include="Refit" Version="6.3.2" />
Expand Down
708 changes: 0 additions & 708 deletions samples/Playground/Playground.sln

Large diffs are not rendered by default.

22 changes: 16 additions & 6 deletions src/Uno.Extensions.Hosting.UI/AppHostingEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,30 @@ internal class AppHostingEnvironment : HostingEnvironment, IAppHostEnvironment,
#if __WASM__
public async Task UpdateAddressBar(Uri applicationUri)
{
// Note: This is a hack to avoid error being thrown when loading products async
await Task.Delay(1000).ConfigureAwait(false);
CoreApplication.MainView?.DispatcherQueue.TryEnqueue(() =>
{
var href = WebAssemblyRuntime.InvokeJS("window.location.href");
var href = Imports.GetLocation();
var appUriBuilder = new UriBuilder(applicationUri);
var url = new UriBuilder(href);
url.Query = appUriBuilder.Query;
url.Path = appUriBuilder.Path;
var webUri = url.Uri.OriginalString;
var js = $"window.history.pushState(\"{webUri}\",\"\", \"{webUri}\");";
Console.WriteLine($"JS:{js}");
var result = WebAssemblyRuntime.InvokeJS(js);
var result = Imports.PushState($"{webUri}", "", $"{webUri}");
});
}


#endif
}

#if __WASM__
internal static partial class Imports
{
[System.Runtime.InteropServices.JavaScript.JSImport("globalThis.Uno.Extensions.Hosting.getLocation")]
public static partial string GetLocation();


[System.Runtime.InteropServices.JavaScript.JSImport("globalThis.window.history.pushState")]
public static partial string PushState(string state, string title, string url);
}
#endif
nickrandolph marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
<DefineConstants>$(DefineConstants);WINUI</DefineConstants>
</PropertyGroup>

<ItemGroup>
<Compile Remove="WasmScripts\**" />
<EmbeddedResource Remove="WasmScripts\**" />
<None Remove="WasmScripts\**" />
</ItemGroup>


<ItemGroup>
<PackageReference Include="Uno.WinUI" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,17 @@

<PropertyGroup>
<DefineConstants>$(DefineConstants);WINUI</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<None Remove="WasmScripts\helpers.js" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="WasmScripts\helpers.js" />
</ItemGroup>


<ItemGroup>
<PackageReference Include="Uno.WinUI.Runtime.WebAssembly" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<Project Sdk="MSBuild.Sdk.Extras" ToolsVersion="15.0">
<Import Project="..\tfms-ui-winui.props" />
<ItemGroup>
<Compile Remove="WasmScripts\**" />
<EmbeddedResource Remove="WasmScripts\**" />
<None Remove="WasmScripts\**" />
</ItemGroup>

<ItemGroup>
<UnoRuntimeProjectReference Include="Uno.Extensions.Hosting.WinUI.Wasm.csproj" />
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.Extensions.Hosting.UI/UnoHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ internal static IHostBuilder CreateDefaultBuilder(Assembly applicationAssembly,
{
if (Foundation.WebAssemblyRuntime.IsWebAssembly)
{
var href = Foundation.WebAssemblyRuntime.InvokeJS("window.location.href");
var href = Imports.GetLocation();
var appsettingsPrefix = new Dictionary<string, string>
{
{ HostingConstants.AppConfigPrefixKey, "local" },
Expand Down
13 changes: 13 additions & 0 deletions src/Uno.Extensions.Hosting.UI/WasmScripts/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var Uno;
(function (Uno) {
var Extensions;
(function (Extensions) {
class Hosting {
static getLocation() {
return window.location.href;
}

}
Extensions.Hosting = Hosting;
})(Extensions = Uno.Extensions || (Uno.Extensions = {}));
})(Uno || (Uno = {}));
15 changes: 9 additions & 6 deletions src/Uno.Extensions.Navigation.UI/BrowserAddressBarService.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
using Uno.Extensions.Hosting;

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

internal class BrowserAddressBarService : IHostedService
{
private readonly ILogger _logger;
private readonly IRouteNotifier _notifier;
private readonly IHasAddressBar? _addressbarHost;
private readonly NavigationConfig? _config;
public BrowserAddressBarService(
ILogger<BrowserAddressBarService> logger,
IRouteNotifier notifier,
NavigationConfig? config,
IHasAddressBar? host = null)
{
_logger = logger;
_notifier = notifier;
_addressbarHost = host;
_config = config;
Expand All @@ -20,7 +21,7 @@ public BrowserAddressBarService(

public Task StartAsync(CancellationToken cancellationToken)
{
if (_addressbarHost is not null && (_config?.AddressBarUpdateEnabled??true))
if (_addressbarHost is not null && (_config?.AddressBarUpdateEnabled ?? true))
{
_notifier.RouteChanged += RouteChanged;
}
Expand Down Expand Up @@ -54,11 +55,13 @@ private async void RouteChanged(object? sender, RouteChangedEventArgs e)
url.Query = route.Query();
url.Path = route.FullPath()?.Replace("+", "/");
await _addressbarHost!.UpdateAddressBar(url.Uri);

}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
if (_logger.IsEnabled(LogLevel.Warning))
{
_logger.LogWarning($"Error encountered updating address bar on route changed event - {ex.Message}");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@ public static Task HostAsync(
var initialNavigation = () => nav.NavigateRouteAsync(root, initialRoute ?? string.Empty);

var start = () => Task.CompletedTask;
if (initialNavigate is not null)
var hostConfigOptions = sp.GetService<IOptions<HostConfiguration>>();
if (hostConfigOptions?.Value is { } hostConfig &&
hostConfig.LaunchRoute() is { } launchRoute &&
launchRoute.IsEmpty() == false)
{
start = () => nav.NavigateRouteAsync(root, launchRoute.FullPath());
}
else if (initialNavigate is not null)
{
start = () => initialNavigate(services, nav);
}
Expand Down
Loading