Neighbourhood.omg.lol/Components/StatusList.razor

75 lines
2 KiB
Text
Raw Permalink Normal View History

@inject IJSRuntime JS
@inject State State
@implements IDisposable
2024-06-13 10:02:51 +00:00
@if (Editable) {
<EditStatusDialog @ref="Dialog" id="EditStatusModal"></EditStatusDialog>
}
@if(statuses != null) foreach(Status status in statuses) {
2024-06-13 10:02:51 +00:00
<StatusCard Status="@status" Editable="Editable" Dialog="Dialog"></StatusCard>
}
<LoadingCard id="statusLoading" icon="fa-solid fa-message-smile"></LoadingCard>
2024-06-05 12:41:08 +00:00
@code {
[Parameter]
public Func<bool,Task<List<Status>?>> StatusFunc { get; set; }
[Parameter]
public bool Editable { get; set; } = false;
2024-06-13 10:02:51 +00:00
public EditStatusDialog? Dialog { get; set; }
private List<Status>? statuses;
private int count { get; set; } = 0;
private int pageSize { get; } = 50;
2024-06-13 00:26:43 +00:00
protected override async Task OnInitializedAsync() {
await base.OnInitializedAsync();
State.PropertyChanged += StateChanged;
State.CanRefresh = true;
if (statuses == null || statuses.Count == 0) await LoadNext();
await InvokeAsync(StateHasChanged);
await JS.InvokeVoidAsync("removeElementById", "statusLoading");
2024-06-13 00:26:43 +00:00
}
public async Task<bool> PullUp() {
int countBefore = count;
await LoadNext();
return (count != countBefore);
}
public async Task LoadNext(bool forceRefresh = false) {
if (statuses == null) statuses = new List<Status>();
if (forceRefresh) {
count = 0;
statuses.Clear();
}
var allStatuses = await StatusFunc(forceRefresh);
if (allStatuses != null && count < allStatuses.Count) {
statuses.AddRange(allStatuses.Skip(count).Take(pageSize));
count = statuses.Count;
}
await InvokeAsync(StateHasChanged);
}
private async void StateChanged(object? sender, PropertyChangedEventArgs e) {
if (e.PropertyName == nameof(State.IsRefreshing) && State.IsRefreshing) {
await LoadNext(true);
State.IsRefreshing = false;
}
if(e.PropertyName == nameof(State.AtBottom) && State.AtBottom) {
await LoadNext();
}
}
public void Dispose() {
State.PropertyChanged -= StateChanged;
State.CanRefresh = false;
}
2024-06-05 12:41:08 +00:00
}