From 72a4b78afa34983c92bd95558770a0a266c893a4 Mon Sep 17 00:00:00 2001 From: Gordon Pedersen Date: Thu, 6 Jun 2024 15:20:09 +1000 Subject: [PATCH] We can now post pics! --- Components/NewPicDialog.razor | 82 ++++++++++++++++++++++ Components/Pages/Person.razor | 32 ++------- Components/Pages/Pics.razor | 12 +++- Components/Pages/StatuslogLatest.razor | 10 +-- Components/PicList.razor | 2 +- Models/Pic.cs | 3 +- Models/PostPic.cs | 11 +++ Models/PutPic.cs | 11 +++ Models/PutPicResponseData.cs | 15 ++++ Models/State.cs | 96 +++++++++++++++++++++++--- RestService.cs | 82 +++++++++++++++++----- 11 files changed, 295 insertions(+), 61 deletions(-) create mode 100644 Components/NewPicDialog.razor create mode 100644 Models/PostPic.cs create mode 100644 Models/PutPic.cs create mode 100644 Models/PutPicResponseData.cs diff --git a/Components/NewPicDialog.razor b/Components/NewPicDialog.razor new file mode 100644 index 0000000..5fd3bd9 --- /dev/null +++ b/Components/NewPicDialog.razor @@ -0,0 +1,82 @@ +@inject IJSRuntime JS +@inject State State + +
+ +
Share a picture
+
+
+ + + + +
+ @if(File != null){ + + @File.ContentType (@formatSizeUnits(File.Size)) + + } + +
+
+
+ + +
+
+ +
+ +@code { + private IBrowserFile? File { get; set; } + private string Description { get; set; } + private bool loading = false; + [Parameter] + public string id { get; set; } + + public async Task PostPic() { + loading = true; + await InvokeAsync(StateHasChanged); + + RestService api = new RestService(); + PutPicResponseData? response = await api.PutPic(State.SelectedAddressName, File); + if(!string.IsNullOrEmpty(Description) && response != null && !string.IsNullOrEmpty(response.Id)) { + await api.PostPicDescription(State.SelectedAddressName, response.Id, Description); + await State.RefreshPics(); + await InvokeAsync(StateHasChanged); + } + + await JS.InvokeVoidAsync("ui", "#" + id); + // clear input + File = null; + Description = string.Empty; + loading = false; + await InvokeAsync(StateHasChanged); + + } + + private async Task ChangeFile(InputFileChangeEventArgs e){ + File = e.File; + } + + private string formatSizeUnits(long bytes){ + string formatted = "0 bytes"; + if (bytes >= 1073741824) { formatted = $"{(bytes / 1073741824):.##} GB"; } + else if (bytes >= 1048576) { formatted = $"{(bytes / 1048576):.##} MB"; } + else if (bytes >= 1024) { formatted = $"{(bytes / 1024):.##} KB"; } + else if (bytes > 1) { formatted = $"{bytes} bytes"; } + else if (bytes == 1) { formatted = $"{bytes} byte"; } + + return formatted; + } +} diff --git a/Components/Pages/Person.razor b/Components/Pages/Person.razor index be77386..e06fdd6 100644 --- a/Components/Pages/Person.razor +++ b/Components/Pages/Person.razor @@ -1,4 +1,6 @@ @page "/person/{Address}" +@inject State State +

@Address

@@ -28,11 +30,11 @@
- +
- +
@@ -40,33 +42,9 @@ [Parameter] public string Address { get; set; } - private Status[] statuses; private MarkupString? bio; protected override async Task OnInitializedAsync() { - RestService api = new RestService(); - await GetBioAsync(api); - } - - private async Task GetBioAsync(RestService api) { - bio = await api.StatuslogBio(Address); - } - - public async ValueTask> GetStatuses(ItemsProviderRequest request) { - // TODO: request.cancellationToken - RestService api = new RestService(); - statuses = (await api.Statuslog(Address)).ToArray() ?? new Status[0]; - var numStatuses = Math.Min(request.Count, statuses.Length - request.StartIndex); - return new ItemsProviderResult(statuses.Skip(request.StartIndex).Take(numStatuses), statuses.Length); - } - - private List pics; - private async ValueTask> GetPics(ItemsProviderRequest request) { - // TODO: request.cancellationToken - RestService api = new RestService(); - if (pics == null || pics.Count == 0) pics = await api.SomePics(Address); - var numPics = Math.Min(request.Count, pics.Count - request.StartIndex); - - return new ItemsProviderResult(pics.Skip(request.StartIndex).Take(numPics), pics.Count); + bio = await State.GetBio(Address); } } diff --git a/Components/Pages/Pics.razor b/Components/Pages/Pics.razor index bb0433a..5c7186b 100644 --- a/Components/Pages/Pics.razor +++ b/Components/Pages/Pics.razor @@ -1,4 +1,5 @@ @page "/pics" +@inject State State

@@ -9,8 +10,17 @@

Sit back, relax, and look at some.pics

+ + + + + + +
- +
@code { diff --git a/Components/Pages/StatuslogLatest.razor b/Components/Pages/StatuslogLatest.razor index d2d9726..40a778a 100644 --- a/Components/Pages/StatuslogLatest.razor +++ b/Components/Pages/StatuslogLatest.razor @@ -21,13 +21,13 @@
- +
@code { - private Status[] statuses; + private List statuses; private string statusContent = string.Empty; private string? statusEmoji = null; @@ -38,9 +38,9 @@ private async ValueTask> GetStatuses(ItemsProviderRequest request) { // TODO: request.cancellationToken - statuses = (await State.GetStatuses()) ?? new Status[0]; - var numStatuses = Math.Min(request.Count, statuses.Length - request.StartIndex); - return new ItemsProviderResult(statuses.Skip(request.StartIndex).Take(numStatuses), statuses.Length); + statuses = (await State.GetStatuses()) ?? new List(); + var numStatuses = Math.Min(request.Count, statuses.Count - request.StartIndex); + return new ItemsProviderResult(statuses.Skip(request.StartIndex).Take(numStatuses), statuses.Count); } } diff --git a/Components/PicList.razor b/Components/PicList.razor index a3c7db9..f6e9d8f 100644 --- a/Components/PicList.razor +++ b/Components/PicList.razor @@ -4,7 +4,7 @@