[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 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 OpenOriginal() => Js.InvokeVoidAsync("open", Note.NoteResponse.Url, "_blank");
|
||||||
|
|
||||||
private void Like()
|
private void Like()
|
||||||
{
|
{
|
||||||
Note.Like();
|
_ = Note.ToggleLike();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Reply()
|
private void Reply()
|
||||||
|
@ -103,7 +103,7 @@
|
||||||
|
|
||||||
private void Renote()
|
private void Renote()
|
||||||
{
|
{
|
||||||
if (RenotePossible) Note.Renote();
|
if (RenotePossible) _ = Note.Renote();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Quote()
|
private void Quote()
|
||||||
|
@ -118,6 +118,6 @@
|
||||||
|
|
||||||
private void React(EmojiResponse emoji)
|
private void React(EmojiResponse emoji)
|
||||||
{
|
{
|
||||||
Note.React(emoji.Name, true, emoji.PublicUrl);
|
Note.React(emoji);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,6 +21,7 @@
|
||||||
|
|
||||||
private void React()
|
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 StreamingService StreamingService { get; set; } = null!;
|
||||||
[Inject] private StateService StateService { get; set; } = null!;
|
[Inject] private StateService StateService { get; set; } = null!;
|
||||||
|
|
||||||
private TimelineState State { get; set; } = new()
|
private TimelineState State { get; set; } = null!;
|
||||||
{
|
|
||||||
Timeline = [],
|
|
||||||
MaxId = null,
|
|
||||||
MinId = null
|
|
||||||
};
|
|
||||||
|
|
||||||
private VirtualScroller VirtualScroller { get; set; } = null!;
|
private VirtualScroller VirtualScroller { get; set; } = null!;
|
||||||
private bool LockFetch { get; set; }
|
private bool LockFetch { get; set; }
|
||||||
|
@ -28,6 +23,7 @@ public partial class TimelineComponent : IAsyncDisposable
|
||||||
{
|
{
|
||||||
StreamingService.NotePublished -= OnNotePublished;
|
StreamingService.NotePublished -= OnNotePublished;
|
||||||
await StreamingService.DisposeAsync();
|
await StreamingService.DisposeAsync();
|
||||||
|
StateService.Timeline.SetState("home", State);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task Initialize()
|
private async Task Initialize()
|
||||||
|
@ -42,18 +38,27 @@ public partial class TimelineComponent : IAsyncDisposable
|
||||||
// Returning false means the API has no more content.
|
// Returning false means the API has no more content.
|
||||||
private async Task<bool> FetchOlder()
|
private async Task<bool> FetchOlder()
|
||||||
{
|
{
|
||||||
if (LockFetch) return true;
|
try
|
||||||
LockFetch = true;
|
|
||||||
var pq = new PaginationQuery { Limit = 15, MaxId = State.MinId };
|
|
||||||
var res = await ApiService.Timelines.GetHomeTimeline(pq);
|
|
||||||
switch (res.Count)
|
|
||||||
{
|
{
|
||||||
case > 0:
|
if (LockFetch) return true;
|
||||||
State.MinId = res.Last().Id;
|
LockFetch = true;
|
||||||
State.Timeline.AddRange(res);
|
Console.WriteLine("Fetching older");
|
||||||
break;
|
var pq = new PaginationQuery { Limit = 15, MaxId = State.MinId };
|
||||||
case 0:
|
var res = await ApiService.Timelines.GetHomeTimeline(pq);
|
||||||
return false;
|
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;
|
LockFetch = false;
|
||||||
|
@ -62,16 +67,22 @@ public partial class TimelineComponent : IAsyncDisposable
|
||||||
|
|
||||||
private async Task FetchNewer()
|
private async Task FetchNewer()
|
||||||
{
|
{
|
||||||
if (LockFetch) return;
|
try
|
||||||
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;
|
if (LockFetch) return;
|
||||||
State.Timeline.InsertRange(0, res);
|
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;
|
LockFetch = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,21 +104,14 @@ public partial class TimelineComponent : IAsyncDisposable
|
||||||
{
|
{
|
||||||
if (firstRender)
|
if (firstRender)
|
||||||
{
|
{
|
||||||
try
|
State = StateService.Timeline.GetState("home");
|
||||||
{
|
if (State.Timeline.Count == 0)
|
||||||
var timeline = StateService.Timeline.GetState("home");
|
|
||||||
State = timeline;
|
|
||||||
_init = true;
|
|
||||||
StateHasChanged();
|
|
||||||
}
|
|
||||||
catch (ArgumentException)
|
|
||||||
{
|
{
|
||||||
|
Console.WriteLine("initializing");
|
||||||
await Initialize();
|
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 Iceshrimp.Shared.Schemas.Web;
|
||||||
|
using Microsoft.AspNetCore.Components;
|
||||||
|
|
||||||
namespace Iceshrimp.Frontend.Core.Services.StateServicePatterns;
|
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)
|
public void SetState(string id, TimelineState state)
|
||||||
{
|
{
|
||||||
|
@ -14,13 +17,44 @@ public class Timeline
|
||||||
public TimelineState GetState(string id)
|
public TimelineState GetState(string id)
|
||||||
{
|
{
|
||||||
States.TryGetValue(id, out var state);
|
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;
|
private MessageService MessageService { get; set; }
|
||||||
public required string? MinId;
|
public required string? MaxId;
|
||||||
public required List<NoteResponse> Timeline = [];
|
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