Skip to content

Commit 612c40b

Browse files
committed
Add test project and rename OneCallPerCache -> OneCallPerLocalStorage
1 parent 22b18ac commit 612c40b

32 files changed

Lines changed: 649 additions & 14 deletions
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<Router AppAssembly="@typeof(App).Assembly" NotFoundPage="typeof(Pages.NotFound)">
2+
<Found Context="routeData">
3+
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)"/>
4+
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
5+
</Found>
6+
</Router>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net10.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<OverrideHtmlAssetPlaceholders>true</OverrideHtmlAssetPlaceholders>
8+
<ServiceWorkerAssetsManifest>service-worker-assets.js</ServiceWorkerAssetsManifest>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="10.*" />
13+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="10.0.7" PrivateAssets="all" />
14+
<PackageReference Include="MudBlazor" Version="9.*" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<ServiceWorker Include="wwwroot\\service-worker.js" PublishedContent="wwwroot\\service-worker.published.js" />
19+
</ItemGroup>
20+
21+
<ItemGroup>
22+
<ProjectReference Include="..\..\Src\Drogecode.Blazor.ExpireStorage\Drogecode.Blazor.ExpireStorage.csproj" />
23+
</ItemGroup>
24+
25+
</Project>
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
@inherits LayoutComponentBase
2+
3+
<MudThemeProvider Theme="@_theme" IsDarkMode="_isDarkMode" />
4+
<MudPopoverProvider />
5+
<MudDialogProvider />
6+
<MudSnackbarProvider />
7+
8+
<MudLayout>
9+
<MudAppBar Elevation="1">
10+
<MudIconButton Icon="@Icons.Material.Filled.Menu" Color="Color.Inherit" Edge="Edge.Start" OnClick="@DrawerToggle" />
11+
<MudText Typo="Typo.h6" Class="ml-2">Application</MudText>
12+
<MudSpacer />
13+
<MudIconButton Icon="@(DarkLightModeButtonIcon)" Color="Color.Inherit" OnClick="@DarkModeToggle" />
14+
<MudLink Href="https://learn.microsoft.com/aspnet/core/" Target="_blank" Color="Color.Inherit" Underline="Underline.None">
15+
About
16+
</MudLink>
17+
</MudAppBar>
18+
19+
<MudDrawer @bind-Open="_drawerOpen" ClipMode="DrawerClipMode.Always" Elevation="2">
20+
<NavMenu />
21+
</MudDrawer>
22+
23+
<MudMainContent Class="pt-16 pa-4">
24+
@Body
25+
</MudMainContent>
26+
</MudLayout>
27+
28+
@code {
29+
private bool _drawerOpen = true;
30+
private bool _isDarkMode = true;
31+
private MudTheme? _theme;
32+
33+
protected override void OnInitialized()
34+
{
35+
base.OnInitialized();
36+
37+
_theme = new()
38+
{
39+
PaletteLight = _lightPalette,
40+
PaletteDark = _darkPalette,
41+
LayoutProperties = new LayoutProperties()
42+
};
43+
}
44+
45+
private void DrawerToggle()
46+
{
47+
_drawerOpen = !_drawerOpen;
48+
}
49+
50+
private void DarkModeToggle()
51+
{
52+
_isDarkMode = !_isDarkMode;
53+
}
54+
55+
private readonly PaletteLight _lightPalette = new()
56+
{
57+
Black = "#110e2d",
58+
AppbarText = "#424242",
59+
AppbarBackground = "rgba(255,255,255,0.8)",
60+
DrawerBackground = "#ffffff",
61+
GrayLight = "#e8e8e8",
62+
GrayLighter = "#f9f9f9",
63+
};
64+
65+
private readonly PaletteDark _darkPalette = new()
66+
{
67+
Primary = "#7e6fff",
68+
Surface = "#1e1e2d",
69+
Background = "#1a1a27",
70+
BackgroundGray = "#151521",
71+
AppbarText = "#92929f",
72+
AppbarBackground = "rgba(26,26,39,0.8)",
73+
DrawerBackground = "#1a1a27",
74+
ActionDefault = "#74718e",
75+
ActionDisabled = "#9999994d",
76+
ActionDisabledBackground = "#605f6d4d",
77+
TextPrimary = "#b2b0bf",
78+
TextSecondary = "#92929f",
79+
TextDisabled = "#ffffff33",
80+
DrawerIcon = "#92929f",
81+
DrawerText = "#92929f",
82+
GrayLight = "#2a2833",
83+
GrayLighter = "#1e1e2d",
84+
Info = "#4a86ff",
85+
Success = "#3dcb6c",
86+
Warning = "#ffb545",
87+
Error = "#ff3f5f",
88+
LinesDefault = "#33323e",
89+
TableLines = "#33323e",
90+
Divider = "#292838",
91+
OverlayLight = "#1e1e2d80",
92+
};
93+
94+
public string DarkLightModeButtonIcon => _isDarkMode switch
95+
{
96+
true => Icons.Material.Rounded.AutoMode,
97+
false => Icons.Material.Outlined.DarkMode,
98+
};
99+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
.page {
2+
position: relative;
3+
display: flex;
4+
flex-direction: column;
5+
}
6+
7+
main {
8+
flex: 1;
9+
}
10+
11+
.sidebar {
12+
background-image: linear-gradient(180deg, rgb(5, 39, 103) 0%, #3a0647 70%);
13+
}
14+
15+
.top-row {
16+
background-color: #f7f7f7;
17+
border-bottom: 1px solid #d6d5d5;
18+
justify-content: flex-end;
19+
height: 3.5rem;
20+
display: flex;
21+
align-items: center;
22+
}
23+
24+
.top-row ::deep a, .top-row ::deep .btn-link {
25+
white-space: nowrap;
26+
margin-left: 1.5rem;
27+
text-decoration: none;
28+
}
29+
30+
.top-row ::deep a:hover, .top-row ::deep .btn-link:hover {
31+
text-decoration: underline;
32+
}
33+
34+
.top-row ::deep a:first-child {
35+
overflow: hidden;
36+
text-overflow: ellipsis;
37+
}
38+
39+
@media (max-width: 640.98px) {
40+
.top-row {
41+
justify-content: space-between;
42+
}
43+
44+
.top-row ::deep a, .top-row ::deep .btn-link {
45+
margin-left: 0;
46+
}
47+
}
48+
49+
@media (min-width: 641px) {
50+
.page {
51+
flex-direction: row;
52+
}
53+
54+
.sidebar {
55+
width: 250px;
56+
height: 100vh;
57+
position: sticky;
58+
top: 0;
59+
}
60+
61+
.top-row {
62+
position: sticky;
63+
top: 0;
64+
z-index: 1;
65+
}
66+
67+
.top-row.auth ::deep a:first-child {
68+
flex: 1;
69+
text-align: right;
70+
width: 0;
71+
}
72+
73+
.top-row, article {
74+
padding-left: 2rem !important;
75+
padding-right: 1.5rem !important;
76+
}
77+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<MudNavMenu>
2+
<MudNavLink Href="" Match="NavLinkMatch.All" Icon="@Icons.Material.Filled.Home">Home</MudNavLink>
3+
</MudNavMenu>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using Drogecode.Blazor.ExpireStorage;
2+
3+
namespace ExpireStorage.Demo.PWA.Models;
4+
5+
public class DemoModelForStorage : ICacheableResponse
6+
{
7+
public HandledBy HandledBy { get; set; }
8+
public string? Data { get; set; } = "Some text to store in the cache.";
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace ExpireStorage.Demo.PWA.Models;
2+
3+
public class StorageSettings
4+
{
5+
public string Key { get; set; } = "StorageKey";
6+
public int LocalStorageDaysInFuture { get; set; } = 7;
7+
public int SessionStorageMinutesInFuture { get; set; } = 10;
8+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
@page "/"
2+
@using Drogecode.Blazor.ExpireStorage
3+
4+
<PageTitle>Demo ExireStorage NuGet</PageTitle>
5+
6+
<MudText Typo="Typo.h3" GutterBottom="true">Welcome and lets play with ExpireStorage!</MudText>
7+
8+
<MudText>You can play a bit with the settings in the form below.</MudText>
9+
<MudText>Remember your key and close and open the browser to see the workings</MudText>
10+
11+
<MudForm @ref="_form">
12+
<MudTextField T="string" @bind-Value="_storageSettings.Key" Label="Key to save with" />
13+
<MudTextField T="string" @bind-Value="_model.Data" Label="Text to save" />
14+
<MudSwitch T="bool" @bind-Value="_cachedRequest.OneCallPerSession" Label="One call per session" />
15+
<MudSwitch T="bool" @bind-Value="_cachedRequest.OneCallPerLocalStorage" Label="One call per cache" />
16+
<MudNumericField T="int" @bind-Value="@_storageSettings.SessionStorageMinutesInFuture" Label="Expire session date (minutes)" />
17+
<MudNumericField T="int" @bind-Value="@_storageSettings.LocalStorageDaysInFuture" Label="Expire local storage (days)" />
18+
<MudSwitch T="bool" @bind-Value="_cachedRequest.IgnoreCache" Label="Ignore cache" />
19+
<MudSwitch T="bool" @bind-Value="_cachedRequest.CachedAndReplace" Label="Cached and replace" />
20+
<MudSwitch T="bool" @bind-Value="_cachedRequest.AlwaysCacheWhenOffline" Label="Always cache when offline" />
21+
<MudSwitch T="bool" @bind-Value="_cachedRequest.RetryOnJsonException" Label="Retry on JSON exception" />
22+
</MudForm>
23+
24+
<MudButton Variant="Variant.Filled" Color="Color.Primary" DropShadow="false" OnClick="@Save">Save</MudButton>
25+
26+
<MudText>Offline: @ExpireStorageService.IsOffline</MudText>
27+
<MudText>Response: @_response</MudText>
28+
@if (_handledBy != HandledBy.None)
29+
{
30+
<MudText>Handled by: @_handledBy</MudText>
31+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
using Drogecode.Blazor.ExpireStorage;
3+
using ExpireStorage.Demo.PWA.Models;
4+
using Microsoft.AspNetCore.Components;
5+
using MudBlazor;
6+
7+
namespace ExpireStorage.Demo.PWA.Pages;
8+
9+
public sealed partial class Home : IDisposable
10+
{
11+
[Inject, NotNull] private IExpireStorageService? StorageService { get; set; }
12+
13+
private readonly CancellationTokenSource _cls = new();
14+
private DemoModelForStorage _model = new();
15+
private StorageSettings _storageSettings = new();
16+
private CachedRequest _cachedRequest = new();
17+
private MudForm _form;
18+
19+
private string _response = string.Empty;
20+
private HandledBy _handledBy = HandledBy.None;
21+
22+
private async Task Save()
23+
{
24+
_cachedRequest.ExpireLocalStorage = DateTime.UtcNow.AddDays(_storageSettings.LocalStorageDaysInFuture);
25+
_cachedRequest.ExpireSession = DateTime.UtcNow.AddMinutes(_storageSettings.SessionStorageMinutesInFuture);
26+
27+
var value = await StorageService.CachedRequestAsync(_storageSettings.Key,
28+
async () => await FunctionToCall(),
29+
_cachedRequest,
30+
new DemoModelForStorage(),
31+
_cls.Token);
32+
_response = value?.Data ?? "No data";
33+
_handledBy = value?.HandledBy ?? HandledBy.None;
34+
}
35+
36+
// This function could be a call to a server side API.
37+
private async Task<DemoModelForStorage> FunctionToCall()
38+
{
39+
return _model;
40+
}
41+
42+
public void Dispose()
43+
{
44+
_cls.Cancel();
45+
// Do not use _cls.Dispose() because it could throw an exception.
46+
}
47+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@page "/not-found"
2+
@layout MainLayout
3+
4+
<PageTitle>Not Found</PageTitle>
5+
6+
<MudText Typo="Typo.h3" GutterBottom="true">404 - Page Not Found</MudText>
7+
<MudText Class="mb-8">Sorry, the content you are looking for does not exist.</MudText>
8+
9+
<MudButton Variant="Variant.Filled" Color="Color.Primary" Href="/">Go to Home</MudButton>

0 commit comments

Comments
 (0)