Skip to content

Commit

Permalink
🆕 feat: Upgrade Masa.Blazor to 1.0.2 (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
capdiem authored Aug 21, 2023
1 parent 632d1a0 commit d805814
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 24 deletions.
55 changes: 38 additions & 17 deletions Masa.Blazor.Pro/Global/Config/GlobalConfig.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
namespace Masa.Blazor.Pro.Global;
using System.Globalization;

namespace Masa.Blazor.Pro.Global;

public class GlobalConfig
{
private readonly CookieStorage? _cookieStorage;
private readonly I18n _i18n;
private readonly CookieStorage _cookieStorage;

private string? _pageMode;
private bool _expandOnHover;
private string? _favorite;
private string? _navigationStyle;

public GlobalConfig(CookieStorage cookieStorage, IHttpContextAccessor httpContextAccessor)
public GlobalConfig(CookieStorage cookieStorage, I18n i18n)
{
_i18n = i18n;
_cookieStorage = cookieStorage;
if (httpContextAccessor.HttpContext is not null) Initialization(httpContextAccessor.HttpContext.Request.Cookies);
}

public static string PageModeKey { get; set; } = "GlobalConfig_PageMode";
public static string PageModeKey => "GlobalConfig_PageMode";

public static string NavigationStyleKey => "GlobalConfig_NavigationStyle";

public static string NavigationStyleKey { get; set; } = "GlobalConfig_NavigationStyle";
public static string ExpandOnHoverCookieKey => "GlobalConfig_ExpandOnHover";

public static string ExpandOnHoverCookieKey { get; set; } = "GlobalConfig_ExpandOnHover";
public static string FavoriteCookieKey => "GlobalConfig_Favorite";

public static string FavoriteCookieKey { get; set; } = "GlobalConfig_Favorite";
public static string LangCookieKey => "GlobalConfig_Lang";

public EventHandler? NavigationStyleChanged { get; set; }

Expand All @@ -31,7 +36,7 @@ public string PageMode
set
{
_pageMode = value;
_cookieStorage?.SetItemAsync(PageModeKey, value);
_cookieStorage.SetAsync(PageModeKey, value);
}
}

Expand All @@ -42,7 +47,7 @@ public string NavigationStyle
{
_navigationStyle = value;
NavigationStyleChanged?.Invoke(this, EventArgs.Empty);
_cookieStorage?.SetItemAsync(NavigationStyleKey, value);
_cookieStorage.SetAsync(NavigationStyleKey, value);
}
}

Expand All @@ -52,7 +57,7 @@ public bool ExpandOnHover
set
{
_expandOnHover = value;
_cookieStorage?.SetItemAsync(ExpandOnHoverCookieKey, value);
_cookieStorage.SetAsync(ExpandOnHoverCookieKey, value);
}
}

Expand All @@ -62,15 +67,31 @@ public string? Favorite
set
{
_favorite = value;
_cookieStorage?.SetItemAsync(FavoriteCookieKey, value);
_cookieStorage.SetAsync(FavoriteCookieKey, value);
}
}

public void Initialization(IRequestCookieCollection cookies)
public CultureInfo Culture
{
_pageMode = cookies[PageModeKey];
_navigationStyle = cookies[NavigationStyleKey];
_expandOnHover = Convert.ToBoolean(cookies[ExpandOnHoverCookieKey]);
_favorite = cookies[FavoriteCookieKey];
get => _i18n.Culture;
set
{
_cookieStorage.SetAsync(LangCookieKey, value.Name);
_i18n.SetCulture(value);
}
}

public async Task InitFromStorage()
{
_pageMode = await _cookieStorage.GetAsync(PageModeKey);
_navigationStyle = await _cookieStorage.GetAsync(NavigationStyleKey);
_expandOnHover = Convert.ToBoolean(await _cookieStorage.GetAsync(ExpandOnHoverCookieKey));
_favorite = await _cookieStorage.GetAsync(FavoriteCookieKey);

var lang = await _cookieStorage.GetAsync(LangCookieKey);
if (!string.IsNullOrWhiteSpace(lang))
{
_i18n.SetCulture(new CultureInfo(lang));
}
}
}
30 changes: 30 additions & 0 deletions Masa.Blazor.Pro/Global/CookieStorage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Microsoft.JSInterop;

namespace Masa.Blazor.Pro;

public class CookieStorage
{
private readonly IJSRuntime _jsRuntime;

public CookieStorage(IJSRuntime jsRuntime)
{
_jsRuntime = jsRuntime;
}

public async Task<string> GetAsync(string key)
{
return await _jsRuntime.InvokeAsync<string>(JsInteropConstants.GetCookie, key);
}

public async void SetAsync<T>(string key, T? value)
{
try
{
await _jsRuntime.InvokeVoidAsync(JsInteropConstants.SetCookie, key, value?.ToString());
}
catch
{
// ignored
}
}
}
2 changes: 2 additions & 0 deletions Masa.Blazor.Pro/Global/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public static IServiceCollection AddGlobalForServer(this IServiceCollection serv
{
var basePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? throw new Exception("Get the assembly root directory exception!");
services.AddNav(Path.Combine(basePath, $"wwwroot/nav/nav.json"));
services.AddScoped<CookieStorage>();
services.AddScoped<GlobalConfig>();

return services;
Expand All @@ -16,6 +17,7 @@ public static async Task<IServiceCollection> AddGlobalForWasmAsync(this IService
using var httpclient = new HttpClient();
var navList = await httpclient.GetFromJsonAsync<List<NavModel>>(Path.Combine(baseUri, $"nav/nav.json")) ?? throw new Exception("please configure the Navigation!");
services.AddNav(navList);
services.AddScoped<CookieStorage>();
services.AddScoped<GlobalConfig>();

return services;
Expand Down
2 changes: 1 addition & 1 deletion Masa.Blazor.Pro/Masa.Blazor.Pro.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Masa.Blazor" Version="1.0.0" />
<PackageReference Include="Masa.Blazor" Version="1.0.2" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Masa.Blazor.Pro.Pages.Authentication.Components;
using Microsoft.AspNetCore.Components.Web;

namespace Masa.Blazor.Pro.Pages.Authentication.Components;

public partial class Login
{
Expand Down
16 changes: 13 additions & 3 deletions Masa.Blazor.Pro/Shared/MainLayout.razor
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
@inherits LayoutComponentBase
@inject I18n I18n
@inject GlobalConfig GlobalConfig

<MApp>
<PPageTabsProvider>
<CascadingValue Value="I18n.Culture.Name" Name="CultureName">
<CascadingValue Value="GlobalConfig.Culture.Name" Name="CultureName">
<MAppBar Elevation=0 App Height="100" Class="default-app-bar mx-6">
<div class="default-app-bar__actions @PageModeClass">
<Favorite />
Expand Down Expand Up @@ -62,9 +62,19 @@

private string PageModeClass => _pageTab == PageModes.PageTab ? "page-mode--tab" : "page-mode--breadcrumb";

protected override async Task OnAfterRenderAsync(bool firstRender)
{
await base.OnAfterRenderAsync(firstRender);

if (firstRender)
{
await GlobalConfig.InitFromStorage();
}
}

void OnLanguageChanged(CultureInfo culture)
{
I18n.SetCulture(culture);
GlobalConfig.Culture = culture;
}

}
5 changes: 3 additions & 2 deletions Masa.Blazor.Pro/_Imports.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
global using BlazorComponent;
global using BlazorComponent.I18n;
global using Masa.Blazor.Pro;
global using Masa.Blazor.Pro.Data.App.ECommerce;
global using Masa.Blazor.Pro.Data.App.ECommerce.Dto;
global using Masa.Blazor.Pro.Data.App.Invoice;
Expand All @@ -14,13 +15,13 @@
global using Masa.Blazor.Pro.Data.Dashboard.Analytics;
global using Masa.Blazor.Pro.Data.Others.AccountSettings;
global using Masa.Blazor.Pro.Data.Others.AccountSettings.Dto;
global using Masa.Blazor.Pro.Data.Shared.Favorite;
global using Masa.Blazor.Pro.Global;
global using Masa.Blazor.Pro.Global.Config;
global using Masa.Blazor.Pro.Global.Nav.Model;
global using Masa.Blazor.Pro.Data.Shared.Favorite;
global using Microsoft.AspNetCore.Components;
global using Microsoft.AspNetCore.Components.Web;
global using System.ComponentModel;
global using System.ComponentModel.DataAnnotations;
global using System.Reflection;
global using System.Text.Json;

0 comments on commit d805814

Please sign in to comment.