From ca5fc21ac8700421762a42a4ce75b24f145b4a21 Mon Sep 17 00:00:00 2001 From: czf <2455501+czf@users.noreply.github.com> Date: Sun, 24 Jan 2021 18:26:24 -0800 Subject: [PATCH] Adding update modal fix async for Artist_artistsWorksList remove boilerplate code clean up console statements --- .../ChartComponentHeaderButtons.razor.cs | 2 - .../DashboardPieChartComponent.razor.cs | 1 - .../Sitewide/ApplicationUpdateModal.razor | 21 ++++++ .../Sitewide/ApplicationUpdateModal.razor.cs | 32 ++++++++ .../Components/Sitewide/WelcomeModal.razor.cs | 24 +----- .../Interfaces/IUpdateService.cs | 12 +++ .../Pages/Artist_ArtistsWorksList.razor | 14 ++-- .../Pages/Artist_ArtistsWorksList.razor.cs | 9 ++- RadiocomDataViewApp/Pages/Counter.razor | 16 ---- RadiocomDataViewApp/Pages/FetchData.razor | 55 -------------- RadiocomDataViewApp/Program.cs | 7 ++ RadiocomDataViewApp/Services/UpdateService.cs | 74 +++++++++++++++++++ .../Shared/LayoutSiderContentNavMenu.razor | 1 - RadiocomDataViewApp/Shared/MainLayout.razor | 1 + RadiocomDataViewApp/wwwroot/appsettings.json | 1 - RadiocomDataViewApp/wwwroot/index.html | 9 +-- 16 files changed, 165 insertions(+), 114 deletions(-) create mode 100644 RadiocomDataViewApp/Components/Sitewide/ApplicationUpdateModal.razor create mode 100644 RadiocomDataViewApp/Components/Sitewide/ApplicationUpdateModal.razor.cs create mode 100644 RadiocomDataViewApp/Interfaces/IUpdateService.cs delete mode 100644 RadiocomDataViewApp/Pages/Counter.razor delete mode 100644 RadiocomDataViewApp/Pages/FetchData.razor create mode 100644 RadiocomDataViewApp/Services/UpdateService.cs diff --git a/RadiocomDataViewApp/Components/ChartComponentHeaderButtons.razor.cs b/RadiocomDataViewApp/Components/ChartComponentHeaderButtons.razor.cs index 88af05c..745bc2b 100644 --- a/RadiocomDataViewApp/Components/ChartComponentHeaderButtons.razor.cs +++ b/RadiocomDataViewApp/Components/ChartComponentHeaderButtons.razor.cs @@ -28,10 +28,8 @@ public partial class ChartComponentHeaderButtons : ComponentBase private async Task InvokeButtonStateEvent(MouseEventArgs mouseEventArgs, EventCallback callback, HeaderButtonState buttonState) { HeaderButtonState selected = null; - Console.WriteLine("invoke state event"); foreach (var state in HeaderButtonConfigs) { - Console.WriteLine($"text:{state.Text} color:{state.ButtonColor}"); state.ButtonColor = state == buttonState ? Color.Secondary : Color.Primary; state.Active = state == buttonState; if( state == buttonState) diff --git a/RadiocomDataViewApp/Components/DashboardPieChartComponent.razor.cs b/RadiocomDataViewApp/Components/DashboardPieChartComponent.razor.cs index 0d9a03e..23c8029 100644 --- a/RadiocomDataViewApp/Components/DashboardPieChartComponent.razor.cs +++ b/RadiocomDataViewApp/Components/DashboardPieChartComponent.razor.cs @@ -47,7 +47,6 @@ public DashboardPieChartComponent() public async Task RefreshChartData() { - Console.WriteLine("refresh picharet"); IEnumerable newDatas = await GenerateChartDatas.Invoke(); await Chart.Clear(); await Chart.AddLabels(newDatas.Select(x => x.Label).ToArray()); diff --git a/RadiocomDataViewApp/Components/Sitewide/ApplicationUpdateModal.razor b/RadiocomDataViewApp/Components/Sitewide/ApplicationUpdateModal.razor new file mode 100644 index 0000000..ec128e2 --- /dev/null +++ b/RadiocomDataViewApp/Components/Sitewide/ApplicationUpdateModal.razor @@ -0,0 +1,21 @@ + + + + + + + + + This application has an update. Reload the page to get it. + + + + + + + + + + + + diff --git a/RadiocomDataViewApp/Components/Sitewide/ApplicationUpdateModal.razor.cs b/RadiocomDataViewApp/Components/Sitewide/ApplicationUpdateModal.razor.cs new file mode 100644 index 0000000..1b67b7e --- /dev/null +++ b/RadiocomDataViewApp/Components/Sitewide/ApplicationUpdateModal.razor.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Blazorise; +using Microsoft.AspNetCore.Components; +using RadiocomDataViewApp.Interfaces; + +namespace RadiocomDataViewApp.Components.Sitewide +{ + public partial class ApplicationUpdateModal : ComponentBase + { + private Modal ApplicationUpdate; + + [Inject] + public IUpdateService UpdateService { get; set; } + + + protected override async Task OnParametersSetAsync() + { + + await base.OnParametersSetAsync(); + + UpdateService.OnWelcomeHasChanged += WelcomeHasChanged; + } + + private void WelcomeHasChanged() + { + ApplicationUpdate.Show(); + } + } +} diff --git a/RadiocomDataViewApp/Components/Sitewide/WelcomeModal.razor.cs b/RadiocomDataViewApp/Components/Sitewide/WelcomeModal.razor.cs index 39d317c..5e5de6a 100644 --- a/RadiocomDataViewApp/Components/Sitewide/WelcomeModal.razor.cs +++ b/RadiocomDataViewApp/Components/Sitewide/WelcomeModal.razor.cs @@ -19,27 +19,15 @@ public partial class WelcomeModal : ComponentBase private bool _hasVisited; - protected override void OnAfterRender(bool firstRender) - { - //base.OnAfterRender(firstRender); - //if (firstRender) - //{ - // Welcome.Show(); - //} - } - private Task ClosedModal() { Task visted = VisitService.SetVisitedAsync(); return visted; } - //private async Task ClosedModal() - //{ - // - //} - private void CloseClick() { + private void CloseClick() + { Welcome.Hide(); @@ -66,7 +54,6 @@ protected override async Task OnAfterRenderAsync(bool firstRender) await base.OnAfterRenderAsync(firstRender); if (!_hasVisited) { - Console.WriteLine("after"); Welcome.Show(); } @@ -75,12 +62,5 @@ protected override async Task OnAfterRenderAsync(bool firstRender) - //public override async Task SetParametersAsync(ParameterView parameters) - //{ - - // await base.SetParametersAsync(parameters); - // _hasVisited = await VisitService.HasVisitedAsync(); - - //} } } diff --git a/RadiocomDataViewApp/Interfaces/IUpdateService.cs b/RadiocomDataViewApp/Interfaces/IUpdateService.cs new file mode 100644 index 0000000..16541cf --- /dev/null +++ b/RadiocomDataViewApp/Interfaces/IUpdateService.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace RadiocomDataViewApp.Interfaces +{ + public interface IUpdateService + { + public event Action OnWelcomeHasChanged; + } +} diff --git a/RadiocomDataViewApp/Pages/Artist_ArtistsWorksList.razor b/RadiocomDataViewApp/Pages/Artist_ArtistsWorksList.razor index 4bc13df..9a869e6 100644 --- a/RadiocomDataViewApp/Pages/Artist_ArtistsWorksList.razor +++ b/RadiocomDataViewApp/Pages/Artist_ArtistsWorksList.razor @@ -1,7 +1,11 @@ @page "/artist/{ArtistId:int}/artistworks" -@{ -} -

All Songs by @_artistName

- +@if (_artistWorkInfos != null) +{ - \ No newline at end of file +

All Songs by @_artistName

+ + } + else + { +

Loading...

+ } \ No newline at end of file diff --git a/RadiocomDataViewApp/Pages/Artist_ArtistsWorksList.razor.cs b/RadiocomDataViewApp/Pages/Artist_ArtistsWorksList.razor.cs index 975e173..e540540 100644 --- a/RadiocomDataViewApp/Pages/Artist_ArtistsWorksList.razor.cs +++ b/RadiocomDataViewApp/Pages/Artist_ArtistsWorksList.razor.cs @@ -20,11 +20,12 @@ public partial class Artist_ArtistsWorksList : ComponentBase private string _artistName; private static readonly Func HrefGenerator = item => $"artistwork/{item.Id}"; private IEnumerable _artistWorkInfos; - protected override async Task OnParametersSetAsync() + protected override Task OnParametersSetAsync() { - await base.OnParametersSetAsync(); - _artistWorkInfos = await RadiocomArtistWorkRepository.GetArtist_ArtistWorks(ArtistId); - _artistName = _artistWorkInfos.FirstOrDefault()?.ArtistInfo.Name; + return base.OnParametersSetAsync().ContinueWith( + x => InvokeAsync(async () => _artistWorkInfos = await RadiocomArtistWorkRepository.GetArtist_ArtistWorks(ArtistId))) + .Unwrap() + .ContinueWith(x => _artistName = _artistWorkInfos.FirstOrDefault()?.ArtistInfo.Name); } } } diff --git a/RadiocomDataViewApp/Pages/Counter.razor b/RadiocomDataViewApp/Pages/Counter.razor deleted file mode 100644 index 8641f78..0000000 --- a/RadiocomDataViewApp/Pages/Counter.razor +++ /dev/null @@ -1,16 +0,0 @@ -@page "/counter" - -

Counter

- -

Current count: @currentCount

- - - -@code { - private int currentCount = 0; - - private void IncrementCount() - { - currentCount++; - } -} diff --git a/RadiocomDataViewApp/Pages/FetchData.razor b/RadiocomDataViewApp/Pages/FetchData.razor deleted file mode 100644 index 5824082..0000000 --- a/RadiocomDataViewApp/Pages/FetchData.razor +++ /dev/null @@ -1,55 +0,0 @@ -@page "/fetchdata" -@inject HttpClient Http - -

Weather forecast

- -

This component demonstrates fetching data from the server.

- -@if (forecasts == null) -{ -

Loading...

-} -else -{ - - - - - - - - - - - @foreach (var forecast in forecasts) - { - - - - - - - } - -
DateTemp. (C)Temp. (F)Summary
@forecast.Date.ToShortDateString()@forecast.TemperatureC@forecast.TemperatureF@forecast.Summary
-} - -@code { - private WeatherForecast[] forecasts; - - protected override async Task OnInitializedAsync() - { - forecasts = await Http.GetFromJsonAsync("sample-data/weather.json"); - } - - public class WeatherForecast - { - public DateTime Date { get; set; } - - public int TemperatureC { get; set; } - - public string Summary { get; set; } - - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); - } -} diff --git a/RadiocomDataViewApp/Program.cs b/RadiocomDataViewApp/Program.cs index ea7fe1b..f607679 100644 --- a/RadiocomDataViewApp/Program.cs +++ b/RadiocomDataViewApp/Program.cs @@ -44,6 +44,13 @@ public static async Task Main(string[] args) DateTime welcomeDate = builder.Configuration.GetValue(CONFIGKEY_WELCOME_DATE); return new VisitService(x.GetService(), welcomeDate); }); + builder.Services.AddScoped(x => + { + DateTime welcomeDate = builder.Configuration.GetValue(CONFIGKEY_WELCOME_DATE); + return new UpdateService(x.GetService(), welcomeDate); + }); + + builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }); builder.Services.AddBlazoredLocalStorage(); diff --git a/RadiocomDataViewApp/Services/UpdateService.cs b/RadiocomDataViewApp/Services/UpdateService.cs new file mode 100644 index 0000000..0c0fcb8 --- /dev/null +++ b/RadiocomDataViewApp/Services/UpdateService.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Net.Http.Json; +using System.Threading; +using System.Threading.Tasks; +using RadiocomDataViewApp.Interfaces; + +namespace RadiocomDataViewApp.Services +{ + public class UpdateService : IUpdateService, IAsyncDisposable, IDisposable + { + private readonly HttpClient _httpClient; + private readonly DateTime _welcomeTime; + + private readonly Timer _welcomeTimer; + private bool disposedValue; + private readonly Random _random; + + public UpdateService(HttpClient httpClient, DateTime welcomeTime) + { + _httpClient = httpClient; + _welcomeTime = welcomeTime; + _random = new Random(); + + _welcomeTimer = new Timer(HasApplicationUpdate, null, TimeSpan.FromHours(1), TimeSpan.FromHours(5)); + } + + public event Action OnWelcomeHasChanged; + + private async void HasApplicationUpdate(object state) + { + + bool welcomeHasChanged = await _httpClient.GetFromJsonAsync("/appsettings.json?r=" + _random.Next()) + .ContinueWith(x => _welcomeTime != x.Result.welcome); + if (welcomeHasChanged) + { + OnWelcomeHasChanged?.Invoke(); + } + } + + + + private class ApplicationUpdateCheck + { + public DateTime welcome { get; set; } + } + + protected virtual void Dispose(bool disposing) + { + if (!disposedValue) + { + if (disposing) + { + _welcomeTimer.Dispose(); + } + + disposedValue = true; + } + } + + public void Dispose() + { + Dispose(disposing: true); + GC.SuppressFinalize(this); + } + + public ValueTask DisposeAsync() + { + return ((IAsyncDisposable)_welcomeTimer).DisposeAsync(); + } + } +} diff --git a/RadiocomDataViewApp/Shared/LayoutSiderContentNavMenu.razor b/RadiocomDataViewApp/Shared/LayoutSiderContentNavMenu.razor index b452832..ea251cd 100644 --- a/RadiocomDataViewApp/Shared/LayoutSiderContentNavMenu.razor +++ b/RadiocomDataViewApp/Shared/LayoutSiderContentNavMenu.razor @@ -51,7 +51,6 @@ async Task AboutClick() { - Console.WriteLine("about"); await VisitService.ClearVisitedStateAsync(); StateHasChanged(); } diff --git a/RadiocomDataViewApp/Shared/MainLayout.razor b/RadiocomDataViewApp/Shared/MainLayout.razor index 0aa104c..5b2d4d2 100644 --- a/RadiocomDataViewApp/Shared/MainLayout.razor +++ b/RadiocomDataViewApp/Shared/MainLayout.razor @@ -17,6 +17,7 @@ @Body + diff --git a/RadiocomDataViewApp/wwwroot/appsettings.json b/RadiocomDataViewApp/wwwroot/appsettings.json index 64587b7..4d6c680 100644 --- a/RadiocomDataViewApp/wwwroot/appsettings.json +++ b/RadiocomDataViewApp/wwwroot/appsettings.json @@ -1,4 +1,3 @@ { - "version": 1201234, "welcome": "2021-01-24T00:00:00.000-08:00" } \ No newline at end of file diff --git a/RadiocomDataViewApp/wwwroot/index.html b/RadiocomDataViewApp/wwwroot/index.html index 34c120b..7f9d6ce 100644 --- a/RadiocomDataViewApp/wwwroot/index.html +++ b/RadiocomDataViewApp/wwwroot/index.html @@ -25,7 +25,7 @@ Reload 🗙 - + @@ -35,15 +35,10 @@ - +