From 413051ad4c88dfd0bebd2a70a99a9e24c29ed180 Mon Sep 17 00:00:00 2001 From: Gordon Pedersen Date: Tue, 18 Jun 2024 11:44:18 +1000 Subject: [PATCH] Added refresh (no pull-to-refresh, yet) --- Components/Pages/Ephemeral.razor | 18 ++++++++++++++++++ Components/Pages/Now.razor | 18 ++++++++++++++++++ Components/Pages/Person.razor | 10 ++++++---- Components/Pages/Pics.razor | 4 +++- Components/Pages/StatuslogLatest.razor | 4 +++- Components/PicList.razor | 21 ++++++++++++++++++--- Components/RefreshButton.razor | 18 ++++++++++++++++++ Components/StatusList.razor | 21 ++++++++++++++++++--- Components/_Imports.razor | 3 ++- Models/State.cs | 21 ++++++++++++++++++++- 10 files changed, 124 insertions(+), 14 deletions(-) create mode 100644 Components/RefreshButton.razor diff --git a/Components/Pages/Ephemeral.razor b/Components/Pages/Ephemeral.razor index a36404a..e75d990 100644 --- a/Components/Pages/Ephemeral.razor +++ b/Components/Pages/Ephemeral.razor @@ -1,6 +1,10 @@ @page "/ephemeral" +@implements IDisposable @inject IJSRuntime JS @inject State State + + + Eph.emer.al is a place for fleeting thoughts. Everything on this page will disappear after a while. @@ -24,7 +28,21 @@ protected override async Task OnInitializedAsync() { await base.OnInitializedAsync(); if (messages == null || messages.Count == 0) messages = await State.GetEphemeralMessages(); + State.PropertyChanged += StateChanged; + State.CanRefresh = true; await InvokeAsync(StateHasChanged); await JS.InvokeVoidAsync("removeElementById", "ephemeral-loading"); } + + private async void StateChanged(object? sender, PropertyChangedEventArgs e) { + if (e.PropertyName == nameof(State.IsRefreshing) && State.IsRefreshing) { + messages = await State.GetEphemeralMessages(true); + State.IsRefreshing = false; + } + } + + public void Dispose() { + State.PropertyChanged -= StateChanged; + State.CanRefresh = false; + } } \ No newline at end of file diff --git a/Components/Pages/Now.razor b/Components/Pages/Now.razor index 750778f..f8f9feb 100644 --- a/Components/Pages/Now.razor +++ b/Components/Pages/Now.razor @@ -1,6 +1,10 @@ @page "/now" +@implements IDisposable @inject IJSRuntime JS @inject State State + + + Feel free to stroll through the now.garden and take a look at what people are up to. @@ -31,7 +35,21 @@ protected override async Task OnInitializedAsync() { await base.OnInitializedAsync(); if (garden == null || garden.Count == 0) garden = await State.GetNowGarden(); + State.PropertyChanged += StateChanged; + State.CanRefresh = true; await InvokeAsync(StateHasChanged); await JS.InvokeVoidAsync("removeElementById", "now-loading"); } + + private async void StateChanged(object? sender, PropertyChangedEventArgs e) { + if (e.PropertyName == nameof(State.IsRefreshing) && State.IsRefreshing) { + garden = await State.GetNowGarden(true); + State.IsRefreshing = false; + } + } + + public void Dispose() { + State.PropertyChanged -= StateChanged; + State.CanRefresh = false; + } } diff --git a/Components/Pages/Person.razor b/Components/Pages/Person.razor index 9c46b7f..8553890 100644 --- a/Components/Pages/Person.razor +++ b/Components/Pages/Person.razor @@ -3,6 +3,8 @@ @inject IJSRuntime JS @inject NavigationManager Nav + +

@Address

@@ -51,7 +53,7 @@ }
- + @if(Address == State.SelectedAddressName) { + +@code { + protected override async Task OnInitializedAsync() { + await base.OnInitializedAsync(); + State.PropertyChanged += StateChanged; + } + + private async void StateChanged(object? sender, PropertyChangedEventArgs e) { + if (e.PropertyName == nameof(State.IsRefreshing)) { + await InvokeAsync(StateHasChanged); + } + } +} \ No newline at end of file diff --git a/Components/StatusList.razor b/Components/StatusList.razor index e18035f..97c285f 100644 --- a/Components/StatusList.razor +++ b/Components/StatusList.razor @@ -1,4 +1,5 @@ -@inject IJSRuntime JS +@implements IDisposable +@inject IJSRuntime JS @inject State State @if (Editable) { @@ -13,7 +14,7 @@ @code { [Parameter] - public Func?>> StatusFunc { get; set; } + public Func?>> StatusFunc { get; set; } [Parameter] public bool Editable { get; set; } = false; @@ -23,8 +24,22 @@ protected override async Task OnInitializedAsync() { await base.OnInitializedAsync(); - if (statuses == null || statuses.Count == 0) statuses = await StatusFunc(); + if (statuses == null || statuses.Count == 0) statuses = await StatusFunc(false); + State.PropertyChanged += StateChanged; + State.CanRefresh = true; await InvokeAsync(StateHasChanged); await JS.InvokeVoidAsync("removeElementById", "statusLoading"); } + + private async void StateChanged(object? sender, PropertyChangedEventArgs e) { + if (e.PropertyName == nameof(State.IsRefreshing) && State.IsRefreshing) { + statuses = await StatusFunc(true); + State.IsRefreshing = false; + } + } + + public void Dispose() { + State.PropertyChanged -= StateChanged; + State.CanRefresh = false; + } } diff --git a/Components/_Imports.razor b/Components/_Imports.razor index 8ecaf89..6b25a70 100644 --- a/Components/_Imports.razor +++ b/Components/_Imports.razor @@ -1,4 +1,5 @@ -@using System.Net.Http +@using System.ComponentModel +@using System.Net.Http @using System.Net.Http.Json @using Microsoft.AspNetCore.Components.Forms @using Microsoft.AspNetCore.Components.Routing diff --git a/Models/State.cs b/Models/State.cs index 18b9601..74ca8dc 100644 --- a/Models/State.cs +++ b/Models/State.cs @@ -1,8 +1,9 @@ using Microsoft.AspNetCore.Components; +using System.ComponentModel; using System.Text.Json; namespace Neighbourhood.omg.lol.Models { - public class State { + public class State : INotifyPropertyChanged { // Main data lists public List? Statuses { get; set; } public List? Pics { get; set; } @@ -57,7 +58,25 @@ namespace Neighbourhood.omg.lol.Models { } } + // refreshing + public event PropertyChangedEventHandler? PropertyChanged; + private bool _isRefreshing; + public bool IsRefreshing { + get => _isRefreshing; + set { + _isRefreshing = value; + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsRefreshing))); + } + } + private bool _canRefresh; + public bool CanRefresh { + get => _canRefresh; + set { + _canRefresh = value; + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CanRefresh))); + } + } // api service private RestService api { get; set; }