From 65276d4984e08b2f3015031869eafd3c7b94fc33 Mon Sep 17 00:00:00 2001 From: Gordon Pedersen Date: Mon, 24 Jun 2024 14:51:48 +1000 Subject: [PATCH] Loading app settings and putting app name and version number in the app class on startup --- App.xaml.cs | 4 ++++ Components/Layout/AvatarMenu.razor | 6 ++---- LoginWebViewPage.xaml.cs | 1 + MauiProgram.cs | 13 +++++++++++-- Neighbourhood.omg.lol.csproj | 8 ++++++++ RestService.cs | 3 +-- appsettings.json | 5 +++++ wwwroot/css/style.css | 2 +- 8 files changed, 33 insertions(+), 9 deletions(-) create mode 100644 appsettings.json diff --git a/App.xaml.cs b/App.xaml.cs index 33ae922..147c72f 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -1,5 +1,9 @@ namespace Neighbourhood.omg.lol { public partial class App : Application { + + public static string Name { get; set; } + public static string Version { get; set; } + public App(NavigatorService navigatorService) { InitializeComponent(); diff --git a/Components/Layout/AvatarMenu.razor b/Components/Layout/AvatarMenu.razor index c90ce6e..ae358b2 100644 --- a/Components/Layout/AvatarMenu.razor +++ b/Components/Layout/AvatarMenu.razor @@ -1,5 +1,4 @@ -@using System.Reflection -@inject CustomAuthenticationStateProvider AuthStateProvider; +@inject CustomAuthenticationStateProvider AuthStateProvider; @inject State State; @@ -45,7 +44,7 @@ - @assembly.Name - @assembly.Version?.ToString() + @App.Name - @App.Version @@ -81,7 +80,6 @@ @code { - AssemblyName assembly = Assembly.GetExecutingAssembly().GetName(); public void changeAddress(AddressResponseData address) { State.SelectedAddress = address; } diff --git a/LoginWebViewPage.xaml.cs b/LoginWebViewPage.xaml.cs index 62e3cb0..7cec6ca 100644 --- a/LoginWebViewPage.xaml.cs +++ b/LoginWebViewPage.xaml.cs @@ -33,6 +33,7 @@ public partial class LoginWebViewPage : ContentPage if ( client_id != null && client_secret != null && redirect_uri != null) { + //this.loginwebview.UserAgent = new System.Net.Http.Headers.ProductInfoHeaderValue(App.Name, App.Version).ToString(); this.loginwebview.Source = $"https://home.omg.lol/oauth/authorize?client_id={client_id}&scope=everything&redirect_uri={redirect_uri}&response_type=code"; } } diff --git a/MauiProgram.cs b/MauiProgram.cs index 3f05c8f..5202399 100644 --- a/MauiProgram.cs +++ b/MauiProgram.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Components.Authorization; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using Neighbourhood.omg.lol.Models; +using System.Reflection; namespace Neighbourhood.omg.lol { public static class MauiProgram { @@ -22,8 +23,16 @@ namespace Neighbourhood.omg.lol { builder.Services.AddSingleton(); builder.Services.AddSingleton(); - ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); - builder.Services.AddSingleton(configurationBuilder.AddUserSecrets().Build()); + //ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); + //builder.Services.AddSingleton(configurationBuilder.AddUserSecrets().Build()); + var assembly = Assembly.GetExecutingAssembly(); + var details = assembly.GetName(); + App.Name = details.Name!; + App.Version = details.Version!.ToString(); + var appSettings = $"{App.Name}.appsettings.json"; + using var stream = assembly.GetManifestResourceStream(appSettings); + var config = new ConfigurationBuilder().AddJsonStream(stream).Build(); + builder.Configuration.AddConfiguration(config); builder.Services.AddAuthorizationCore(); builder.Services.AddScoped(); diff --git a/Neighbourhood.omg.lol.csproj b/Neighbourhood.omg.lol.csproj index ddb8e87..b24724a 100644 --- a/Neighbourhood.omg.lol.csproj +++ b/Neighbourhood.omg.lol.csproj @@ -90,6 +90,10 @@ + + + + @@ -195,6 +199,10 @@ + + + + diff --git a/RestService.cs b/RestService.cs index c83b9ea..540bfb6 100644 --- a/RestService.cs +++ b/RestService.cs @@ -19,8 +19,7 @@ namespace Neighbourhood.omg.lol { public RestService(string? token = null) { _client = new HttpClient(); _client.BaseAddress = new Uri(BaseUrl); - AssemblyName name = Assembly.GetExecutingAssembly().GetName(); - _client.DefaultRequestHeaders.UserAgent.Add(new System.Net.Http.Headers.ProductInfoHeaderValue(name.Name ?? "Neighbourhood.omg.lol", name.Version?.ToString())); + _client.DefaultRequestHeaders.UserAgent.Add(new System.Net.Http.Headers.ProductInfoHeaderValue(App.Name, App.Version)); _serializerOptions = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower, WriteIndented = true diff --git a/appsettings.json b/appsettings.json new file mode 100644 index 0000000..7062483 --- /dev/null +++ b/appsettings.json @@ -0,0 +1,5 @@ +{ + "client_id": "08656a9ade42b8ff16b868c4bb33379e", + "client_secret": "529e47904ca8900a4fb187ecee2f6221", + "redirect_uri": "https://auth.neighbourhood.omg.lol/" +} diff --git a/wwwroot/css/style.css b/wwwroot/css/style.css index 7bcd59b..1256de8 100644 --- a/wwwroot/css/style.css +++ b/wwwroot/css/style.css @@ -461,7 +461,7 @@ article { overflow-wrap: anywhere; } -nav label { +nav label:is(.checkbox, .radio, .switch) { white-space: break-spaces; flex: 1 1 100%; } \ No newline at end of file