Skip to content

Commit

Permalink
fix(templates): resolve issues of universal link in MAUI client of Bo…
Browse files Browse the repository at this point in the history
…ilerplate #8903 (#8904)
  • Loading branch information
ysmoradi authored Oct 15, 2024
1 parent 209d019 commit eb2f2d9
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public partial class AppInitializer : AppComponentBase
[AutoInject] private IStorageService storageService = default!;
[AutoInject] private CultureInfoManager cultureInfoManager = default!;
[AutoInject] private ILogger<AuthenticationManager> authLogger = default!;
[AutoInject] private NavigationManager navigationManager = default!;

protected async override Task OnInitAsync()
{
Expand All @@ -33,8 +34,9 @@ protected async override Task OnInitAsync()
{
if (CultureInfoManager.MultilingualEnabled)
{
cultureInfoManager.SetCurrentCulture(await storageService.GetItem("Culture") ?? // 1- User settings
CultureInfo.CurrentUICulture.Name); // 2- OS settings
cultureInfoManager.SetCurrentCulture(new Uri(navigationManager.Uri).GetCulture() ?? // 1- Culture query string OR Route data request culture
await storageService.GetItem("Culture") ?? // 2- User settings
CultureInfo.CurrentUICulture.Name); // 3- OS settings
}

await SetupBodyClasses();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private void SetCurrentDir()

private void SetCurrentUrl()
{
var path = navigationManager.GetPath();
var path = navigationManager.GetUriPath();

currentUrl = Urls.All.SingleOrDefault(pageUrl =>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,74 +1,14 @@
using System.Web;

namespace Microsoft.AspNetCore.Components;
namespace Microsoft.AspNetCore.Components;

public static partial class NavigationManagerExtensions
{
public static string GetUriWithoutQueryParameter(this NavigationManager navigationManager, string key)
{
var url = navigationManager.Uri;

var uri = new Uri(url);

// this gets all the query string key value pairs as a collection
var newQueryString = HttpUtility.ParseQueryString(uri.Query);

// this removes the key if exists
newQueryString.Remove(key);

// this gets the page path from root without QueryString
string pagePathWithoutQueryString = uri.GetLeftPart(UriPartial.Path);

return newQueryString.Count > 0
? string.Format("{0}?{1}", pagePathWithoutQueryString, newQueryString)
: pagePathWithoutQueryString;
}

/// <summary>
/// Reads culture from either route segment or query string.
/// https://adminpanel.bitpaltform.dev/en-US/categories
/// https://adminpanel.bitpaltform.dev/categories?culture=en-US
/// </summary>
public static string? GetCultureFromUri(this NavigationManager navigationManager)
{
var uri = new Uri(navigationManager.Uri);

var culture = HttpUtility.ParseQueryString(uri.Query)["culture"];

if (string.IsNullOrEmpty(culture) is false)
return culture;

foreach (var segment in uri.Segments.Take(2))
{
var segmentValue = segment.Trim('/');
if (CultureInfoManager.SupportedCultures.Any(sc => string.Equals(sc.Culture.Name, segmentValue, StringComparison.InvariantCultureIgnoreCase)))
{
return segmentValue;
}
}

return null;
}

public static string GetUriWithoutCulture(this NavigationManager navigationManager)
{
var uri = navigationManager.GetUriWithoutQueryParameter("culture");

var culture = navigationManager.GetCultureFromUri();

if (string.IsNullOrEmpty(culture) is false)
{
uri = uri
.Replace($"{culture}/", string.Empty)
.Replace(culture, string.Empty);
}

return uri;
return new Uri(navigationManager.Uri).GetUrlWithoutQueryParameter(key);
}

public static string GetPath(this NavigationManager navigationManager)
public static string GetUriPath(this NavigationManager navigationManager)
{
var uriBuilder = new UriBuilder(navigationManager.GetUriWithoutCulture()) { Query = string.Empty, Fragment = string.Empty };
return uriBuilder.Path;
return new Uri(navigationManager.Uri).GetPath();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ await Task.Run(async () =>
}
});

if (CultureInfoManager.MultilingualEnabled && forceLoad == false)
{
var currentCulture = CultureInfo.CurrentUICulture.Name;
var uri = new Uri(url);
var urlCulture = uri.GetCulture();
forceLoad = urlCulture is not null && currentCulture != urlCulture;
}

universalLinksNavigationManager!.NavigateTo(url, forceLoad, replace);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ await cookie.Set(new()
});
}

navigationManager.NavigateTo(navigationManager.GetUriWithoutCulture(), forceLoad: true, replace: true);
navigationManager.NavigateTo(new Uri(navigationManager.Uri).GetUrlWithoutCulture(), forceLoad: true, replace: true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static async Task Main(string[] args)

var navigationManager = host.Services.GetRequiredService<NavigationManager>();

var culture = navigationManager.GetCultureFromUri() ?? // 1- Culture query string OR Route data request culture
var culture = new Uri(navigationManager.Uri).GetCulture() ?? // 1- Culture query string OR Route data request culture
cultureCookie ?? // 2- User settings
CultureInfo.CurrentUICulture.Name; // 3- OS/Browser settings

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System.Web;

namespace System;

public static partial class UriExtensions
{
public static string GetUrlWithoutQueryParameter(this Uri uri, string key)
{
// this gets all the query string key value pairs as a collection
var newQueryString = HttpUtility.ParseQueryString(uri.Query);

// this removes the key if exists
newQueryString.Remove(key);

// this gets the page path from root without QueryString
string pagePathWithoutQueryString = uri.GetLeftPart(UriPartial.Path);

return newQueryString.Count > 0
? string.Format("{0}?{1}", pagePathWithoutQueryString, newQueryString)
: pagePathWithoutQueryString;
}

/// <summary>
/// Reads culture from either route segment or query string.
/// https://adminpanel.bitpaltform.dev/en-US/categories
/// https://adminpanel.bitpaltform.dev/categories?culture=en-US
/// </summary>
public static string? GetCulture(this Uri uri)
{
var culture = HttpUtility.ParseQueryString(uri.Query)["culture"];

if (string.IsNullOrEmpty(culture) is false)
return culture;

foreach (var segment in uri.Segments.Take(2))
{
var segmentValue = segment.Trim('/');
if (CultureInfoManager.SupportedCultures.Any(sc => string.Equals(sc.Culture.Name, segmentValue, StringComparison.InvariantCultureIgnoreCase)))
{
return segmentValue;
}
}

return null;
}

public static string GetUrlWithoutCulture(this Uri uri)
{
uri = new Uri(uri.GetUrlWithoutQueryParameter("culture"));

var culture = uri.GetCulture();

if (string.IsNullOrEmpty(culture) is false)
{
uri = new Uri(uri.ToString()
.Replace($"{culture}/", string.Empty)
.Replace(culture, string.Empty));
}

return uri.ToString();
}

public static string GetPath(this Uri uri)
{
var uriBuilder = new UriBuilder(uri.GetUrlWithoutCulture()) { Query = string.Empty, Fragment = string.Empty };
return uriBuilder.Path;
}
}

0 comments on commit eb2f2d9

Please sign in to comment.