Skip to content

Commit

Permalink
Add: Add version check
Browse files Browse the repository at this point in the history
  • Loading branch information
whats2000 committed Sep 4, 2024
1 parent 3b14e43 commit 1edf720
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 17 deletions.
47 changes: 40 additions & 7 deletions Shared/SharedHeader.razor
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
@inject TranslateText TransText
@using PearlCalculatorBlazor.Localizer
@inject TranslateText TransText
@inject NotificationService Notice

@using PearlCalculatorBlazor.Localizer

<PageHeader>
<TitleTemplate>
<Flex Align="center" Gap="10">
<Image Preview="false" Height="40" src="Logo.png" alt="banner"/>
<Title Level="1" Style="margin-bottom: 0; font-size: 20px;">Pearl Calculator Blazor</Title>
<Tooltip Title="Check for updates">
<div @onclick="@CheckForUpdate">
@if (_currentVersion == "Loading...")
{
<Spin/>
}
else
{
<Text>@_currentVersion</Text>
}
</div>
</Tooltip>
</Flex>
</TitleTemplate>
<PageHeaderExtra>
Expand All @@ -18,9 +29,9 @@
<MenuItem @onclick="@(() => OnClickChangeLanguage("en"))" Key="en">@TranslateText.LanguageDict["en"]</MenuItem>
@foreach (var pair in TranslateText.LanguageDict)
{
if (pair.Key == "en")
continue;
<MenuItem @onclick="@(() => OnClickChangeLanguage(pair.Key))" @key=pair.Key>@pair.Value</MenuItem>
if (pair.Key == "en")
continue;
<MenuItem @onclick="@(() => OnClickChangeLanguage(pair.Key))" @key=pair.Key>@pair.Value</MenuItem>
}
</Menu>
</Overlay>
Expand Down Expand Up @@ -56,4 +67,26 @@
</Button>
</Flex>
</PageHeaderExtra>
</PageHeader>
</PageHeader>

@{
RenderFragment ModelFooterTemplate =
@<Template>
<Button OnClick="@HandleCancel" @key="@("back")">
Stay in this version
</Button>
<Button OnClick="@HandleOk"
@key="@("Refresh and Update")"
Type="@ButtonType.Primary">
Refresh and Update
</Button>
</Template>;
}

<Modal Title="New version available"
Visible="@_visible"
OnOk="@HandleOk"
OnCancel="@HandleCancel"
Footer="@ModelFooterTemplate">
<p>@_modalContent</p>
</Modal>
59 changes: 49 additions & 10 deletions Shared/SharedHeader.razor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AntDesign;
using System.Threading.Tasks;
using AntDesign;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using PearlCalculatorBlazor.Localizer;
Expand All @@ -11,9 +12,13 @@ public enum Theme
Dark
}

public partial class SharedHeader : ComponentBase
public partial class SharedHeader
{
private Theme _currentTheme = Theme.Light;
private string _currentVersion = "Loading...";
private string _latestVersion;
private string _modalContent = "";
private bool _visible = false;
[Inject] private IJSRuntime JsRuntime { get; set; }

private RenderFragment LightThemeIcon => builder =>
Expand Down Expand Up @@ -83,6 +88,25 @@ protected Theme CurrentTheme
}
}

private void ShowVersionModal()
{
_modalContent = $"New version: {_latestVersion} found.";
_visible = true;
StateHasChanged();
}

private async Task HandleOk()
{
_visible = false;
await JsRuntime.InvokeVoidAsync("localStorage.setItem", "PearlCalculatorBlazor_version", _latestVersion);
await JsRuntime.InvokeVoidAsync("ClearCacheAndReload");
}

private void HandleCancel()
{
_visible = false;
}

private RenderFragment GetThemeIcon()
{
return _currentTheme switch
Expand All @@ -108,7 +132,8 @@ private async void UpdateTheme()
_ => null
};

var url = fileName == null ? "" : $"/_content/AntDesign/css/{fileName}.css";
var baseUrl = await JsRuntime.InvokeAsync<string>("GetBaseUrl");
var url = fileName == null ? "" : $"{baseUrl}_content/AntDesign/css/{fileName}.css";
await JsRuntime.InvokeVoidAsync("AddElementToBody", url);
await JsRuntime.InvokeVoidAsync("localStorage.setItem", "PearlCalculatorBlazor_userTheme", theme);
}
Expand All @@ -119,6 +144,20 @@ private async void OnClickChangeLanguage(string language)
await JsRuntime.InvokeVoidAsync("localStorage.setItem", "PearlCalculatorBlazor_userLanguage", language);
}

private async void CheckForUpdate()
{
_currentVersion = "Loading...";
_latestVersion = await JsRuntime.InvokeAsync<string>("FetchVersionFromServer");
_currentVersion = await JsRuntime.InvokeAsync<string>("localStorage.getItem", "PearlCalculatorBlazor_version");
if (_currentVersion == _latestVersion)
{
StateHasChanged();
return;
}

ShowVersionModal();
}

protected override async void OnInitialized()
{
TranslateText.OnLanguageChange += RefreshPage;
Expand All @@ -128,13 +167,13 @@ protected override async void OnInitialized()

if (!string.IsNullOrEmpty(storedLanguage))
await TransText.LoadLanguageAsync(storedLanguage);
var storedTheme = await JsRuntime.InvokeAsync<string>("localStorage.getItem", "PearlCalculatorBlazor_userTheme");

if (!string.IsNullOrEmpty(storedTheme))
{
CurrentTheme = storedTheme == "dark" ? Theme.Dark : Theme.Light;
}

var storedTheme =
await JsRuntime.InvokeAsync<string>("localStorage.getItem", "PearlCalculatorBlazor_userTheme");

if (!string.IsNullOrEmpty(storedTheme)) CurrentTheme = storedTheme == "dark" ? Theme.Dark : Theme.Light;

CheckForUpdate();
}

private void RefreshPage()
Expand Down
1 change: 1 addition & 0 deletions wwwroot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<script src="theme.js?v=20240904"></script>
<script src="file-input-proxy.js?v=20240901"></script>
<script src="tnt-weight-slider.js"></script>
<script src="version.js?v=20240904"></script>
<script>navigator.serviceWorker.register('service-worker.js');</script>
<script src="_content/AntDesign/js/ant-design-blazor.js"></script>
<script src="_content/AntDesign.Charts/g2plot.min.js"></script>
Expand Down
4 changes: 4 additions & 0 deletions wwwroot/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@
}
link.href = url;
}

function GetBaseUrl() {
return window.location.origin + window.location.pathname;
}
23 changes: 23 additions & 0 deletions wwwroot/version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
async function FetchVersionFromServer() {
try {
// Append a timestamp to the URL to prevent caching
const timestamp = new Date().getTime();
const response = await fetch(`/version.json?t=${timestamp}`);
const data = await response.json();
return data.version;
} catch (error) {
console.error('Error fetching version:', error);
return null;
}
}

async function ClearCacheAndReload() {
if ('caches' in window) {
// Clear all the cache storage
const cacheNames = await caches.keys();
await Promise.all(cacheNames.map(cacheName => caches.delete(cacheName)));
}

// Reload the page
window.location.reload(true);
}
3 changes: 3 additions & 0 deletions wwwroot/version.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"version": "2.71"
}

0 comments on commit 1edf720

Please sign in to comment.