Skip to content

Commit

Permalink
feat: add dark mode
Browse files Browse the repository at this point in the history
  • Loading branch information
manuel-rw committed Jan 27, 2024
1 parent 8e64079 commit 1c5f638
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions GrocyScanner.Service/GrocyScanner.Service.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Blazored.LocalStorage" Version="4.4.0" />
<PackageReference Include="Humanizer" Version="2.14.1" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="7.0.15" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
Expand Down
2 changes: 1 addition & 1 deletion GrocyScanner.Service/Pages/_Host.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<component type="typeof(HeadOutlet)" render-mode="ServerPrerendered"/>
</head>
<body>
<component type="typeof(App)" render-mode="ServerPrerendered"/>
<component type="typeof(App)" render-mode="Server"/>

<div id="blazor-error-ui">
<environment include="Staging,Production">
Expand Down
2 changes: 2 additions & 0 deletions GrocyScanner.Service/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Blazored.LocalStorage;
using GrocyScanner.Core.Configurations;
using GrocyScanner.Core.GrocyClient;
using GrocyScanner.Core.Providers;
Expand Down Expand Up @@ -34,6 +35,7 @@
builder.Services.AddHttpClient();
builder.Services.AddMudServices();
builder.Services.AddScoped<IDialogService, DialogService>();
builder.Services.AddBlazoredLocalStorage();

WebApplication app = builder.Build();

Expand Down
26 changes: 24 additions & 2 deletions GrocyScanner.Service/Shared/MainLayout.razor
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
@inherits LayoutComponentBase
@using Blazored.LocalStorage
@inherits LayoutComponentBase
@inject ILocalStorageService LocalStorageService

<MudThemeProvider/>
<MudThemeProvider @bind-IsDarkMode="_isDarkTheme"/>
<MudDialogProvider/>
<MudSnackbarProvider/>

Expand All @@ -9,6 +11,7 @@
<MudIconButton Icon="@Icons.Material.Filled.Menu" Color="Color.Inherit" Edge="Edge.Start" OnClick="@((e) => DrawerToggle())"/>
<MudText Typo="Typo.h5" Class="ml-3">Grocy Scanner</MudText>
<MudSpacer/>
<MudIconButton Icon="@(_isDarkTheme ? Icons.Material.Filled.LightMode : Icons.Material.Filled.DarkMode)" OnClick="ToggleDarkTheme"></MudIconButton>
<MudIconButton Icon="@Icons.Material.Filled.MoreVert" Color="Color.Inherit" Edge="Edge.End"/>
</MudAppBar>
<MudDrawer @bind-Open="_drawerOpen" ClipMode="DrawerClipMode.Always" Elevation="2">
Expand All @@ -27,10 +30,29 @@

@code {
bool _drawerOpen = true;
bool _isDarkTheme;

private const string IsDarkModeKey = "theme.is-dark-mode";

void DrawerToggle()
{
_drawerOpen = !_drawerOpen;
}

private async Task ToggleDarkTheme()
{
_isDarkTheme = !_isDarkTheme;
await LocalStorageService.SetItemAsync(IsDarkModeKey, _isDarkTheme);
}

protected override async Task OnInitializedAsync()
{
if (!await LocalStorageService.ContainKeyAsync(IsDarkModeKey))
{
return;
}

_isDarkTheme = await LocalStorageService.GetItemAsync<bool>(IsDarkModeKey);
}

}

0 comments on commit 1c5f638

Please sign in to comment.