[frontend] Wire up message service, refactor note rendering

This commit is contained in:
Lilian 2024-07-09 00:05:00 +02:00
parent 72bc021d5f
commit f5f43d6264
No known key found for this signature in database
4 changed files with 89 additions and 50 deletions

View file

@ -87,13 +87,13 @@
private void ToggleMenu() => Menu.Toggle();
private void Delete() => Note.Delete();
private void Delete() => _ = Note.Delete();
private void OpenOriginal() => Js.InvokeVoidAsync("open", Note.NoteResponse.Url, "_blank");
private void Like()
{
Note.Like();
_ = Note.ToggleLike();
}
private void Reply()
@ -103,7 +103,7 @@
private void Renote()
{
if (RenotePossible) Note.Renote();
if (RenotePossible) _ = Note.Renote();
}
private void Quote()
@ -118,6 +118,6 @@
private void React(EmojiResponse emoji)
{
Note.React(emoji.Name, true, emoji.PublicUrl);
Note.React(emoji);
}
}

View file

@ -21,6 +21,7 @@
private void React()
{
Note.React(Reaction.Name, !Reaction.Reacted);
if (Reaction.Reacted) _ = Note.RemoveReact(Reaction.Name);
else _ = Note.AddReact(Reaction.Name, Reaction.Url);
}
}

View file

@ -14,12 +14,7 @@ public partial class TimelineComponent : IAsyncDisposable
[Inject] private StreamingService StreamingService { get; set; } = null!;
[Inject] private StateService StateService { get; set; } = null!;
private TimelineState State { get; set; } = new()
{
Timeline = [],
MaxId = null,
MinId = null
};
private TimelineState State { get; set; } = null!;
private VirtualScroller VirtualScroller { get; set; } = null!;
private bool LockFetch { get; set; }
@ -28,6 +23,7 @@ public partial class TimelineComponent : IAsyncDisposable
{
StreamingService.NotePublished -= OnNotePublished;
await StreamingService.DisposeAsync();
StateService.Timeline.SetState("home", State);
}
private async Task Initialize()
@ -41,9 +37,12 @@ public partial class TimelineComponent : IAsyncDisposable
// Returning false means the API has no more content.
private async Task<bool> FetchOlder()
{
try
{
if (LockFetch) return true;
LockFetch = true;
Console.WriteLine("Fetching older");
var pq = new PaginationQuery { Limit = 15, MaxId = State.MinId };
var res = await ApiService.Timelines.GetHomeTimeline(pq);
switch (res.Count)
@ -55,12 +54,20 @@ public partial class TimelineComponent : IAsyncDisposable
case 0:
return false;
}
}
catch (HttpRequestException)
{
Console.WriteLine("Network Error");
return false;
}
LockFetch = false;
return true;
}
private async Task FetchNewer()
{
try
{
if (LockFetch) return;
LockFetch = true;
@ -71,7 +78,11 @@ public partial class TimelineComponent : IAsyncDisposable
State.MaxId = res.Last().Id;
State.Timeline.InsertRange(0, res);
}
}
catch (HttpRequestException)
{
Console.WriteLine("Network Error");
}
LockFetch = false;
}
@ -93,21 +104,14 @@ public partial class TimelineComponent : IAsyncDisposable
{
if (firstRender)
{
try
{
var timeline = StateService.Timeline.GetState("home");
State = timeline;
_init = true;
StateHasChanged();
}
catch (ArgumentException)
State = StateService.Timeline.GetState("home");
if (State.Timeline.Count == 0)
{
Console.WriteLine("initializing");
await Initialize();
}
_init = true;
StateHasChanged();
}
}
StateService.Timeline.SetState("home", State);
}
}

View file

@ -1,9 +1,12 @@
using System.Diagnostics.CodeAnalysis;
using Iceshrimp.Shared.Schemas.Web;
using Microsoft.AspNetCore.Components;
namespace Iceshrimp.Frontend.Core.Services.StateServicePatterns;
public class Timeline
internal class Timeline(MessageService messageService)
{
private MessageService MessageService { get; set; } = messageService;
private Dictionary<string, TimelineState> States { get; } = new();
public void SetState(string id, TimelineState state)
@ -14,13 +17,44 @@ public class Timeline
public TimelineState GetState(string id)
{
States.TryGetValue(id, out var state);
return state ?? throw new ArgumentException($"Requested state '{id}' does not exist.");
if (state != null) return state;
else
{
States[id] = new TimelineState([], null, null, MessageService);
return States[id];
}
}
}
public class TimelineState
internal class TimelineState : IDisposable
{
private MessageService MessageService { get; set; }
public required string? MaxId;
public required string? MinId;
public required List<NoteResponse> Timeline = [];
public required List<NoteResponse> Timeline;
[SetsRequiredMembers]
public TimelineState(List<NoteResponse> timeline, string? maxId, string? minId, MessageService messageService)
{
MaxId = maxId;
MinId = minId;
Timeline = timeline;
MessageService = messageService;
MessageService.AnyNoteChanged += OnNoteChanged;
}
private void OnNoteChanged(object? _, NoteResponse note)
{
var i = Timeline.FindIndex(p => p.Id == note.Id);
if (i >= 0)
{
Timeline[i].Liked = note.Liked;
}
}
public void Dispose()
{
MessageService.AnyNoteChanged -= OnNoteChanged;
}
}