Skip to content

Commit

Permalink
Add Dynamic VersionLabel Component
Browse files Browse the repository at this point in the history
  • Loading branch information
Psypher9 committed Jan 19, 2024
1 parent 40669a6 commit 62fde23
Show file tree
Hide file tree
Showing 14 changed files with 117 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<PackageReference Include="MudBlazor" Version="6.12.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Components" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
64 changes: 64 additions & 0 deletions src/AstroPanda.Blazor.Toolkit/Components/VersionLabel.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
@namespace AstroPanda.Blazor.Toolkit

@using AstroPanda.Blazor.Toolkit.Options
@using System.Reflection
@using System.Net.Http.Json

@if (!string.IsNullOrWhiteSpace(_version))
{
<div class="version-label">
<MudText Style="@Style" Class="@Class" Typo="Typo.caption">
v. @_version
</MudText>
</div>
}

@code {
[Inject]
private NavigationManager _navManager { get; set; }

[Parameter]
public string ManifestPath { get; set; } = "/manifest.json";

[Parameter]
public VersionFetch VersionFetchType { get; set; } = VersionFetch.Remote;

[Parameter]
public string Style { get; set; } = "position: absolute; bottom: 10px; right: 15px; text-transform:none;";

[Parameter]
public string Class { get; set; } = string.Empty;

private string _version { get; set; } = string.Empty;

protected override async Task OnInitializedAsync()
{
switch(VersionFetchType)
{
case VersionFetch.Remote:
var _http = new HttpClient()
{
BaseAddress = new Uri(_navManager.BaseUri)
};

var config = await _http.GetFromJsonAsync<Manifest>(ManifestPath);

if (config is null)
break;

if (config.Version is not null)
_version = config.Version;
else if (config.VersionName is not null)
_version = config.VersionName.Substring(0, 8);
break;
case VersionFetch.ExecutingAssembly:
var assemblyVersion = Assembly.GetExecutingAssembly().GetName().Version;
_version = assemblyVersion is null ? "0.0.0" : assemblyVersion.ToString();
break;
default:
break;
}

base.OnInitialized();
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using BlazorComponentBus;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using AstroPanda.Blazor.Toolkit;
using BlazorComponentBus;
using MudBlazor.Services;

namespace AstroPanda.Blazor.Toolkit;
public static class ServiceCollectionExtensions
{

/// <summary>
/// Registers the services from the Blazor Toolkit
/// DOM
Expand All @@ -20,8 +18,9 @@ public static IServiceCollection AddBlazorToolkit(this IServiceCollection servic
services.TryAddSingleton<IDownloadService, DownloadService>();

services.AddScoped<IComponentBus, ComponentBus>();
services.AddScoped<ComponentBus>(sp => sp.GetRequiredService<IComponentBus>() as ComponentBus);
services.AddScoped(sp => sp.GetRequiredService<IComponentBus>() as ComponentBus ?? new ComponentBus());

services.AddHttpClient();
services.AddMudServices();
return services;
}
Expand Down
18 changes: 18 additions & 0 deletions src/AstroPanda.Blazor.Toolkit/Models/Manifest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Text.Json.Serialization;

namespace AstroPanda.Blazor.Toolkit;

public class Manifest
{
[JsonPropertyName("name")]
public string Name { get; set; }

[JsonPropertyName("short_name")]
public string ShortName { get; set; }

[JsonPropertyName("version")]
public string Version { get; set; }

[JsonPropertyName("version_name")]
public string VersionName { get; set; }
}
7 changes: 7 additions & 0 deletions src/AstroPanda.Blazor.Toolkit/Options/VersionFetch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace AstroPanda.Blazor.Toolkit.Options;
public enum VersionFetch
{
Remote,

ExecutingAssembly
}
5 changes: 5 additions & 0 deletions src/AstroPanda.Blazor.Toolkit/Options/VersionOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace AstroPanda.Blazor.Toolkit.Options;
internal class VersionOptions
{
public const string ClientName = "Version";
}
4 changes: 2 additions & 2 deletions src/BlazorToolkit.Server/BlazorToolkit.Server.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>


Expand Down
7 changes: 5 additions & 2 deletions src/BlazorToolkit.Server/Components/Layout/NavMenu.razor
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<MudNavMenu>
@using AstroPanda.Blazor.Toolkit
@using AstroPanda.Blazor.Toolkit.Options
<MudNavMenu Style="height: 100%;">
<MudNavLink Href="" Match="NavLinkMatch.All" Icon="@Icons.Material.Filled.Home">Home</MudNavLink>
<MudNavLink Href="counter" Match="NavLinkMatch.Prefix" Icon="@Icons.Material.Filled.Add">Counter</MudNavLink>
<MudNavLink Href="weather" Match="NavLinkMatch.Prefix" Icon="@Icons.Material.Filled.List">Weather</MudNavLink>


<VersionLabel VersionFetchType="VersionFetch.ExecutingAssembly"/>
</MudNavMenu>
6 changes: 6 additions & 0 deletions src/BlazorToolkit.Wasm/BlazorToolkit.Wasm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@
<ServiceWorker Include="wwwroot\service-worker.js" PublishedContent="wwwroot\service-worker.published.js" />
</ItemGroup>

<ItemGroup>
<Content Update="wwwroot\manifest.webmanifest">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

</Project>
4 changes: 3 additions & 1 deletion src/BlazorToolkit.Wasm/Layout/NavMenu.razor
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<MudNavMenu>
@using AstroPanda.Blazor.Toolkit
<MudNavMenu Style="height: 100%;">
<MudNavLink Href="" Match="NavLinkMatch.All" Icon="@Icons.Material.Filled.Home">Home</MudNavLink>
<MudNavLink Href="counter" Match="NavLinkMatch.Prefix" Icon="@Icons.Material.Filled.Add">Counter</MudNavLink>
<MudNavLink Href="weather" Match="NavLinkMatch.Prefix" Icon="@Icons.Material.Filled.List">Weather</MudNavLink>

<VersionLabel ManifestPath="/manifest.webmanifest"/>
</MudNavMenu>
2 changes: 2 additions & 0 deletions src/BlazorToolkit.Wasm/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using AstroPanda.Blazor.Toolkit;
using BlazorToolkit.Wasm;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
Expand All @@ -10,5 +11,6 @@
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

builder.Services.AddMudServices();
builder.Services.AddBlazorToolkit();

await builder.Build().RunAsync();
1 change: 1 addition & 0 deletions src/BlazorToolkit.Wasm/wwwroot/manifest.webmanifest
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "BlazorToolkit.Wasm",
"short_name": "BlazorToolkit.Wasm",
"version": "pizza-the-hut",
"id": "./",
"start_url": "./",
"display": "standalone",
Expand Down

0 comments on commit 62fde23

Please sign in to comment.