From 063fb6f12aea3a5b71c05beea22e7f6482c2f1cf Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Thu, 19 Oct 2023 07:21:41 +0200 Subject: Refactor, fix stuff --- MatrixRoomUtils.Web/Shared/RoomList.razor | 89 ++++++++++++++++++------------- 1 file changed, 52 insertions(+), 37 deletions(-) (limited to 'MatrixRoomUtils.Web/Shared/RoomList.razor') diff --git a/MatrixRoomUtils.Web/Shared/RoomList.razor b/MatrixRoomUtils.Web/Shared/RoomList.razor index 91ebb0b..705f68c 100644 --- a/MatrixRoomUtils.Web/Shared/RoomList.razor +++ b/MatrixRoomUtils.Web/Shared/RoomList.razor @@ -3,8 +3,10 @@ @using LibMatrix.Extensions @using ArcaneLibs.Extensions @using LibMatrix.EventTypes.Spec.State -@if(Rooms.Count != RoomsWithTypes.Sum(x=>x.Value.Count)) { -

Fetching room details... @RoomsWithTypes.Sum(x=>x.Value.Count) out of @Rooms.Count done!

+@using System.Collections.ObjectModel +@using _Imports = MatrixRoomUtils.Web._Imports +@if (!StillFetching) { +

Fetching room details... @RoomsWithTypes.Sum(x => x.Value.Count) out of @Rooms.Count done!

@foreach (var category in RoomsWithTypes.OrderBy(x => x.Value.Count)) {

@category.Key (@category.Value.Count)

} @@ -18,23 +20,35 @@ else { @code { [Parameter] - public List Rooms { get; set; } + public ObservableCollection Rooms { get; set; } + [Parameter] public ProfileResponseEventContent? GlobalProfile { get; set; } - Dictionary> RoomsWithTypes = new(); + [Parameter] + public bool StillFetching { get; set; } = true; + + [Parameter] + public EventCallback StillFetchingChanged { get; set; } - protected override async Task OnInitializedAsync() { + private Dictionary> RoomsWithTypes => Rooms is null ? new() : Rooms.GroupBy(x => GetRoomTypeName(x.CreationEventContent?.Type)).ToDictionary(x => x.Key, x => x.ToList()); + + protected override async Task OnParametersSetAsync() { var hs = await MRUStorage.GetCurrentSessionOrNavigate(); if (hs is null) return; + Rooms.CollectionChanged += (_, args) => { + foreach (RoomInfo item in args.NewItems) { + item.PropertyChanged += (_, args2) => { + Console.WriteLine(args2); + if(args2.PropertyName == nameof(item.CreationEventContent)) + StateHasChanged(); + }; + } + }; - GlobalProfile ??= await hs.GetProfileAsync(hs.WhoAmI.UserId); - if (RoomsWithTypes.Any()) return; - - var tasks = Rooms.Select(ProcessRoom); - await Task.WhenAll(tasks); + // GlobalProfile ??= await hs.GetProfileAsync(hs.WhoAmI.UserId); - await base.OnInitializedAsync(); + await base.OnParametersSetAsync(); } private string GetRoomTypeName(string? roomType) => roomType switch { @@ -45,32 +59,33 @@ else { _ => roomType }; + // private static SemaphoreSlim _semaphoreSlim = new(8, 8); - private static SemaphoreSlim _semaphoreSlim = new(8, 8); - private async Task ProcessRoom(RoomInfo room) { - await _semaphoreSlim.WaitAsync(); - string roomType; - try { - var createEvent = (await room.GetStateEvent("m.room.create")).TypedContent as RoomCreateEventContent; - roomType = GetRoomTypeName(createEvent.Type); - - if (roomType == "Room") { - var mjolnirData = await room.GetStateEvent("org.matrix.mjolnir.shortcode"); - if(mjolnirData?.RawContent?.ToJson(ignoreNull: true) is not null and not "{}") - roomType = "Legacy policy room"; - } - } - catch (MatrixException e) { - roomType = $"Error: {e.ErrorCode}"; - } - - if (!RoomsWithTypes.ContainsKey(roomType)) { - RoomsWithTypes.Add(roomType, new List()); - } - RoomsWithTypes[roomType].Add(room); - - StateHasChanged(); - _semaphoreSlim.Release(); - } + // private async Task ProcessRoom(RoomInfo room) { + // await _semaphoreSlim.WaitAsync(); + // string roomType; + // try { + // var createEvent = (await room.GetStateEvent("m.room.create")).TypedContent as RoomCreateEventContent; + // roomType = GetRoomTypeName(createEvent.Type); + // + // if (roomType == "Room") { + // var mjolnirData = await room.GetStateEvent("org.matrix.mjolnir.shortcode"); + // if (mjolnirData?.RawContent?.ToJson(ignoreNull: true) is not null and not "{}") + // roomType = "Legacy policy room"; + // } + // } + // catch (MatrixException e) { + // roomType = $"Error: {e.ErrorCode}"; + // } + // + // // if (!RoomsWithTypes.ContainsKey(roomType)) { + // // RoomsWithTypes.Add(roomType, new List()); + // // } + // // RoomsWithTypes[roomType].Add(room); + // + // StateHasChanged(); + // _semaphoreSlim.Release(); + // } } + -- cgit 1.4.1