[frontend] Wire up message service, refactor note rendering
This commit is contained in:
parent
72bc021d5f
commit
f5f43d6264
4 changed files with 89 additions and 50 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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()
|
||||
|
@ -42,18 +38,27 @@ public partial class TimelineComponent : IAsyncDisposable
|
|||
// Returning false means the API has no more content.
|
||||
private async Task<bool> FetchOlder()
|
||||
{
|
||||
if (LockFetch) return true;
|
||||
LockFetch = true;
|
||||
var pq = new PaginationQuery { Limit = 15, MaxId = State.MinId };
|
||||
var res = await ApiService.Timelines.GetHomeTimeline(pq);
|
||||
switch (res.Count)
|
||||
try
|
||||
{
|
||||
case > 0:
|
||||
State.MinId = res.Last().Id;
|
||||
State.Timeline.AddRange(res);
|
||||
break;
|
||||
case 0:
|
||||
return false;
|
||||
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)
|
||||
{
|
||||
case > 0:
|
||||
State.MinId = res.Last().Id;
|
||||
State.Timeline.AddRange(res);
|
||||
break;
|
||||
case 0:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch (HttpRequestException)
|
||||
{
|
||||
Console.WriteLine("Network Error");
|
||||
return false;
|
||||
}
|
||||
|
||||
LockFetch = false;
|
||||
|
@ -62,16 +67,22 @@ public partial class TimelineComponent : IAsyncDisposable
|
|||
|
||||
private async Task FetchNewer()
|
||||
{
|
||||
if (LockFetch) return;
|
||||
LockFetch = true;
|
||||
var pq = new PaginationQuery { Limit = 15, MinId = State.MaxId };
|
||||
var res = await ApiService.Timelines.GetHomeTimeline(pq);
|
||||
if (res.Count > 0)
|
||||
try
|
||||
{
|
||||
State.MaxId = res.Last().Id;
|
||||
State.Timeline.InsertRange(0, res);
|
||||
if (LockFetch) return;
|
||||
LockFetch = true;
|
||||
var pq = new PaginationQuery { Limit = 15, MinId = State.MaxId };
|
||||
var res = await ApiService.Timelines.GetHomeTimeline(pq);
|
||||
if (res.Count > 0)
|
||||
{
|
||||
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();
|
||||
}
|
||||
_init = true;
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
StateService.Timeline.SetState("home", State);
|
||||
}
|
||||
}
|
|
@ -1,10 +1,13 @@
|
|||
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 Dictionary<string, TimelineState> States { get; } = new();
|
||||
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
|
||||
{
|
||||
public required string? MaxId;
|
||||
public required string? MinId;
|
||||
public required List<NoteResponse> Timeline = [];
|
||||
private MessageService MessageService { get; set; }
|
||||
public required string? MaxId;
|
||||
public required string? MinId;
|
||||
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;
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue