From c69e5c790b2b277d9b11265b8f0883e9f90fe3b9 Mon Sep 17 00:00:00 2001 From: Rory& Date: Fri, 22 Mar 2024 17:47:29 +0100 Subject: Changes --- MatrixUtils.Abstractions/RoomInfo.cs | 59 ++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 26 deletions(-) (limited to 'MatrixUtils.Abstractions') diff --git a/MatrixUtils.Abstractions/RoomInfo.cs b/MatrixUtils.Abstractions/RoomInfo.cs index 53acbee..5c258a4 100644 --- a/MatrixUtils.Abstractions/RoomInfo.cs +++ b/MatrixUtils.Abstractions/RoomInfo.cs @@ -11,6 +11,20 @@ using LibMatrix.RoomTypes; namespace MatrixUtils.Abstractions; public class RoomInfo : NotifyPropertyChanged { + public RoomInfo(GenericRoom room) { + Room = room; + _fallbackIcon = identiconGenerator.GenerateAsDataUri(room.RoomId); + RegisterEventListener(); + } + + public RoomInfo(GenericRoom room, List? stateEvents) { + Room = room; + _fallbackIcon = identiconGenerator.GenerateAsDataUri(room.RoomId); + if (stateEvents is { Count: > 0 }) StateEvents = new(stateEvents!); + RegisterEventListener(); + ProcessNewItems(stateEvents!); + } + public readonly GenericRoom Room; public ObservableCollection StateEvents { get; private set; } = new(); @@ -131,37 +145,30 @@ public class RoomInfo : NotifyPropertyChanged { set => SetField(ref _ownMembership, value); } - public RoomInfo(GenericRoom room) { - Room = room; - _fallbackIcon = identiconGenerator.GenerateAsDataUri(room.RoomId); - registerEventListener(); - } - - public RoomInfo(GenericRoom room, List? stateEvents) { - Room = room; - _fallbackIcon = identiconGenerator.GenerateAsDataUri(room.RoomId); - if (stateEvents is { Count: > 0 }) StateEvents = new(stateEvents!); - registerEventListener(); - } - - private void registerEventListener() { + private void RegisterEventListener() { StateEvents.CollectionChanged += (_, args) => { if (args.NewItems is { Count: > 0 }) - foreach (StateEventResponse? newState in args.NewItems) { - // TODO: switch statement benchmark? - if (newState is null) continue; - if (newState.Type == RoomNameEventContent.EventId && newState.TypedContent is RoomNameEventContent roomNameContent) - RoomName = roomNameContent.Name; - else if (newState is { Type: RoomAvatarEventContent.EventId, TypedContent: RoomAvatarEventContent roomAvatarContent }) - RoomIcon = roomAvatarContent.Url; - else if (newState is { Type: RoomCreateEventContent.EventId, TypedContent: RoomCreateEventContent roomCreateContent }) { - CreationEventContent = roomCreateContent; - RoomCreator = newState.Sender; - } - } + ProcessNewItems(args.NewItems.OfType()); }; } + private void ProcessNewItems(IEnumerable newItems) { + foreach (StateEventResponse? newState in newItems) { + if (newState is null) continue; + // TODO: Benchmark switch statement + + if(newState.StateKey != "") continue; + if (newState.Type == RoomNameEventContent.EventId && newState.TypedContent is RoomNameEventContent roomNameContent) + RoomName = roomNameContent.Name; + else if (newState is { Type: RoomAvatarEventContent.EventId, TypedContent: RoomAvatarEventContent roomAvatarContent }) + RoomIcon = roomAvatarContent.Url; + else if (newState is { Type: RoomCreateEventContent.EventId, TypedContent: RoomCreateEventContent roomCreateContent }) { + CreationEventContent = roomCreateContent; + RoomCreator = newState.Sender; + } + } + } + public async Task FetchAllStateAsync() { var stateEvents = Room.GetFullStateAsync(); await foreach (var stateEvent in stateEvents) { -- cgit 1.4.1