Making refresh behaviour more solid

This commit is contained in:
Gordon Pedersen 2024-06-24 14:52:45 +10:00
parent 65276d4984
commit ce21244517
13 changed files with 70 additions and 17 deletions

View file

@ -72,6 +72,7 @@
if (!string.IsNullOrEmpty(Pic?.Id)) { if (!string.IsNullOrEmpty(Pic?.Id)) {
await api.DeletePic(State.SelectedAddressName, Pic.Id); await api.DeletePic(State.SelectedAddressName, Pic.Id);
await State.RefreshPics(); await State.RefreshPics();
State.SendRefresh();
await InvokeAsync(StateHasChanged); await InvokeAsync(StateHasChanged);
} }
@ -92,6 +93,7 @@
if(!string.IsNullOrEmpty(Pic?.Id)) { if(!string.IsNullOrEmpty(Pic?.Id)) {
await api.PostPicDescription(State.SelectedAddressName, Pic.Id, Description); await api.PostPicDescription(State.SelectedAddressName, Pic.Id, Description);
await State.RefreshPics(); await State.RefreshPics();
State.SendRefresh();
await InvokeAsync(StateHasChanged); await InvokeAsync(StateHasChanged);
} }

View file

@ -91,6 +91,7 @@
if (!string.IsNullOrEmpty(Status?.Id)) { if (!string.IsNullOrEmpty(Status?.Id)) {
await api.DeleteStatus(State.SelectedAddressName, Status.Id); await api.DeleteStatus(State.SelectedAddressName, Status.Id);
await State.RefreshStatuses(); await State.RefreshStatuses();
State.SendRefresh();
await InvokeAsync(StateHasChanged); await InvokeAsync(StateHasChanged);
} }
@ -112,6 +113,7 @@
if (!string.IsNullOrEmpty(Status?.Id)) { if (!string.IsNullOrEmpty(Status?.Id)) {
await api.PatchStatus(State.SelectedAddressName, Status.Id, Content, Emoji); await api.PatchStatus(State.SelectedAddressName, Status.Id, Content, Emoji);
await State.RefreshStatuses(); await State.RefreshStatuses();
State.SendRefresh();
await InvokeAsync(StateHasChanged); await InvokeAsync(StateHasChanged);
} }

View file

@ -19,13 +19,13 @@
} }
public async Task Reload() { public async Task Reload() {
if (Html == null){ // if (Html == null){
Html = await api.GetHtml(Url); Html = await api.GetHtml(Url);
string? HtmlString = Html?.ToString(); string? HtmlString = Html?.ToString();
HtmlString = HtmlString?.Replace("</head>", "<base target='_blank'></head>"); HtmlString = HtmlString?.Replace("</head>", "<base target='_blank'></head>");
HtmlString = HtmlString?.Replace("</body>", "<script src='https://cdn.jsdelivr.net/npm/@iframe-resizer/child'></script></body>"); HtmlString = HtmlString?.Replace("</body>", "<script src='https://cdn.jsdelivr.net/npm/@iframe-resizer/child'></script></body>");
Html = (MarkupString)HtmlString; Html = (MarkupString)HtmlString;
} // }
await InvokeAsync(StateHasChanged); await InvokeAsync(StateHasChanged);
} }
} }

View file

@ -78,6 +78,7 @@
if(!string.IsNullOrEmpty(Description) && response != null && !string.IsNullOrEmpty(response.Id)) { if(!string.IsNullOrEmpty(Description) && response != null && !string.IsNullOrEmpty(response.Id)) {
await api.PostPicDescription(State.SelectedAddressName, response.Id, Description); await api.PostPicDescription(State.SelectedAddressName, response.Id, Description);
await State.RefreshPics(); await State.RefreshPics();
State.SendRefresh();
await InvokeAsync(StateHasChanged); await InvokeAsync(StateHasChanged);
} }

View file

@ -81,8 +81,9 @@
var result = await api.StatusPost(State!.SelectedAddressName!, post); var result = await api.StatusPost(State!.SelectedAddressName!, post);
if(result != null){ if(result != null){
await State.RefreshStatuses(); await State.RefreshStatuses();
State.SendRefresh();
await InvokeAsync(StateHasChanged); await InvokeAsync(StateHasChanged);
navigationManager.NavigateTo("/statuslog/latest"); // navigationManager.NavigateTo("/statuslog/latest");
} }
this.Active = false; this.Active = false;

View file

@ -65,9 +65,11 @@ else {
private async void StateChanged(object? sender, PropertyChangedEventArgs e) { private async void StateChanged(object? sender, PropertyChangedEventArgs e) {
if (e.PropertyName == nameof(State.IsRefreshing) && State.IsRefreshing) { if (e.PropertyName == nameof(State.IsRefreshing) && State.IsRefreshing) {
addresses = await State.GetDirectory(true); using (State.GetRefreshToken()) {
GroupAddresses(); addresses = await State.GetDirectory(true);
State.IsRefreshing = false; GroupAddresses();
await InvokeAsync(StateHasChanged);
}
} }
} }

View file

@ -36,8 +36,10 @@
private async void StateChanged(object? sender, PropertyChangedEventArgs e) { private async void StateChanged(object? sender, PropertyChangedEventArgs e) {
if (e.PropertyName == nameof(State.IsRefreshing) && State.IsRefreshing) { if (e.PropertyName == nameof(State.IsRefreshing) && State.IsRefreshing) {
messages = await State.GetEphemeralMessages(true); using (State.GetRefreshToken()) {
State.IsRefreshing = false; messages = await State.GetEphemeralMessages(true);
await InvokeAsync(StateHasChanged);
}
} }
} }

View file

@ -43,8 +43,10 @@
private async void StateChanged(object? sender, PropertyChangedEventArgs e) { private async void StateChanged(object? sender, PropertyChangedEventArgs e) {
if (e.PropertyName == nameof(State.IsRefreshing) && State.IsRefreshing) { if (e.PropertyName == nameof(State.IsRefreshing) && State.IsRefreshing) {
garden = await State.GetNowGarden(true); using (State.GetRefreshToken()){
State.IsRefreshing = false; garden = await State.GetNowGarden(true);
await InvokeAsync(StateHasChanged);
}
} }
} }

View file

@ -129,6 +129,18 @@
if (fragment.EndsWith("now")) await ReloadNow(); if (fragment.EndsWith("now")) await ReloadNow();
else if (fragment.EndsWith("profile")) await ReloadProfile(); else if (fragment.EndsWith("profile")) await ReloadProfile();
bio = await State.GetBio(Address); bio = await State.GetBio(Address);
State.PropertyChanged += StateChanged;
}
private async void StateChanged(object? sender, PropertyChangedEventArgs e) {
if (e.PropertyName == nameof(State.IsRefreshing) && State.IsRefreshing) {
using (State.GetRefreshToken()){
await ReloadNow();
await ReloadProfile();
bio = await State.GetBio(Address);
await InvokeAsync(StateHasChanged);
}
}
} }
private async Task ReloadNow() { private async Task ReloadNow() {

View file

@ -34,8 +34,10 @@
private async void StateChanged(object? sender, PropertyChangedEventArgs e) { private async void StateChanged(object? sender, PropertyChangedEventArgs e) {
if (e.PropertyName == nameof(State.IsRefreshing) && State.IsRefreshing) { if (e.PropertyName == nameof(State.IsRefreshing) && State.IsRefreshing) {
pics = await PicsFunc(true); using (State.GetRefreshToken()){
State.IsRefreshing = false; pics = await PicsFunc(true);
await InvokeAsync(StateHasChanged);
}
} }
} }

View file

@ -1,6 +1,6 @@
@inject State State @inject State State
<button id="refreshButton" class="absolute transparent circle top right margin" @onclick="() => State.IsRefreshing = true"> <button id="refreshButton" class="absolute transparent circle top right margin" @onclick="() => State.SendRefresh()">
<i class="fa-solid fa-arrow-rotate-right @(State.IsRefreshing ? "fa-spin" : "")"></i> <i class="fa-solid fa-arrow-rotate-right @(State.IsRefreshing ? "fa-spin" : "")"></i>
</button> </button>

View file

@ -19,7 +19,7 @@
public bool Editable { get; set; } = false; public bool Editable { get; set; } = false;
public EditStatusDialog? Dialog { get; set; } public EditStatusDialog? Dialog { get; set; }
private List<Status>? statuses; private List<Status>? statuses;
protected override async Task OnInitializedAsync() { protected override async Task OnInitializedAsync() {
@ -33,8 +33,10 @@
private async void StateChanged(object? sender, PropertyChangedEventArgs e) { private async void StateChanged(object? sender, PropertyChangedEventArgs e) {
if (e.PropertyName == nameof(State.IsRefreshing) && State.IsRefreshing) { if (e.PropertyName == nameof(State.IsRefreshing) && State.IsRefreshing) {
statuses = await StatusFunc(true); using (State.GetRefreshToken()) {
State.IsRefreshing = false; statuses = await StatusFunc(true);
await InvokeAsync(StateHasChanged);
}
} }
} }

View file

@ -70,12 +70,37 @@ namespace Neighbourhood.omg.lol.Models {
private bool _isRefreshing; private bool _isRefreshing;
public bool IsRefreshing { public bool IsRefreshing {
get => _isRefreshing; get => _isRefreshing;
set { private set {
_isRefreshing = value; _isRefreshing = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsRefreshing))); PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsRefreshing)));
} }
} }
public void SendRefresh() => IsRefreshing = true;
private static int _refresherCount = 0;
private static Mutex mutex = new Mutex();
public class RefreshToken : IDisposable {
public event EventHandler? Disposed;
public void Dispose() => Disposed?.Invoke(this, EventArgs.Empty);
}
public RefreshToken GetRefreshToken() {
mutex.WaitOne();
_refresherCount++;
mutex.ReleaseMutex();
RefreshToken token = new RefreshToken();
token.Disposed += RefreshToken_Disposed;
return token;
}
private void RefreshToken_Disposed(object? sender, EventArgs e) {
mutex.WaitOne();
_refresherCount--;
if (_refresherCount == 0) IsRefreshing = false;
mutex.ReleaseMutex();
}
private bool _canRefresh; private bool _canRefresh;
public bool CanRefresh { public bool CanRefresh {
get => _canRefresh; get => _canRefresh;