From ede3857084bc7c6e65b7d36cbf913b09596e2787 Mon Sep 17 00:00:00 2001 From: Rory& Date: Mon, 8 Jan 2024 13:55:15 +0100 Subject: Internal changes to policy list viewer (extensibility), fix duplicating change handler for room list page (performance), use /state in room list page before sync --- MatrixRoomUtils.Desktop/App.axaml.cs | 12 +++--- .../Components/NavigationStack.axaml | 12 +++--- .../Components/NavigationStack.axaml.cs | 24 +++++++++-- .../Components/Pages/RoomList.axaml | 21 ++++++++++ .../Components/Pages/RoomList.axaml.cs | 15 +++++++ .../Components/RoomListEntry.axaml | 9 +++- .../Components/RoomListEntry.axaml.cs | 31 +++++++------- MatrixRoomUtils.Desktop/FileStorageProvider.cs | 49 ---------------------- MatrixRoomUtils.Desktop/LoginWindow.axaml | 18 +++++--- MatrixRoomUtils.Desktop/LoginWindow.axaml.cs | 1 + MatrixRoomUtils.Desktop/MRUStorageWrapper.cs | 6 ++- MatrixRoomUtils.Desktop/MainWindow.axaml.cs | 13 ++++-- .../MatrixRoomUtils.Desktop.csproj | 29 ++++++------- .../Properties/launchSettings.json | 3 +- MatrixRoomUtils.Desktop/RoomInfo.cs | 40 ------------------ MatrixRoomUtils.Desktop/SentryService.cs | 2 +- 16 files changed, 140 insertions(+), 145 deletions(-) create mode 100644 MatrixRoomUtils.Desktop/Components/Pages/RoomList.axaml create mode 100644 MatrixRoomUtils.Desktop/Components/Pages/RoomList.axaml.cs delete mode 100644 MatrixRoomUtils.Desktop/FileStorageProvider.cs delete mode 100644 MatrixRoomUtils.Desktop/RoomInfo.cs (limited to 'MatrixRoomUtils.Desktop') diff --git a/MatrixRoomUtils.Desktop/App.axaml.cs b/MatrixRoomUtils.Desktop/App.axaml.cs index 3963be6..aeb154c 100644 --- a/MatrixRoomUtils.Desktop/App.axaml.cs +++ b/MatrixRoomUtils.Desktop/App.axaml.cs @@ -1,7 +1,9 @@ using Avalonia; using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Markup.Xaml; +using Avalonia.Styling; using LibMatrix.Services; +using MatrixRoomUtils.Abstractions; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; @@ -10,10 +12,6 @@ namespace MatrixRoomUtils.Desktop; public partial class App : Application { public IHost host { get; set; } - public override void Initialize() { - AvaloniaXamlLoader.Load(this); - } - public override void OnFrameworkInitializationCompleted() { host = Host.CreateDefaultBuilder().ConfigureServices((ctx, services) => { services.AddSingleton(); @@ -42,6 +40,10 @@ public partial class App : Application { var scope = scopeFac.CreateScope(); desktop.MainWindow = scope.ServiceProvider.GetRequiredService(); } + + if(Environment.GetEnvironmentVariable("AVALONIA_THEME")?.Equals("dark", StringComparison.OrdinalIgnoreCase) ?? false) + RequestedThemeVariant = ThemeVariant.Dark; + base.OnFrameworkInitializationCompleted(); } -} +} \ No newline at end of file diff --git a/MatrixRoomUtils.Desktop/Components/NavigationStack.axaml b/MatrixRoomUtils.Desktop/Components/NavigationStack.axaml index c773b8d..bc6b75d 100644 --- a/MatrixRoomUtils.Desktop/Components/NavigationStack.axaml +++ b/MatrixRoomUtils.Desktop/Components/NavigationStack.axaml @@ -4,9 +4,9 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="MatrixRoomUtils.Desktop.Components.NavigationStack"> - - - - - - + + + + + + \ No newline at end of file diff --git a/MatrixRoomUtils.Desktop/Components/NavigationStack.axaml.cs b/MatrixRoomUtils.Desktop/Components/NavigationStack.axaml.cs index d6343e2..92c617b 100644 --- a/MatrixRoomUtils.Desktop/Components/NavigationStack.axaml.cs +++ b/MatrixRoomUtils.Desktop/Components/NavigationStack.axaml.cs @@ -1,4 +1,5 @@ using Avalonia.Controls; +using Avalonia.Interactivity; using Avalonia.Markup.Xaml; namespace MatrixRoomUtils.Desktop.Components; @@ -8,12 +9,24 @@ public partial class NavigationStack : UserControl { InitializeComponent(); } - private void InitializeComponent() { - AvaloniaXamlLoader.Load(this); + // private void InitializeComponent() { + // AvaloniaXamlLoader.Load(this); + // buildView(); + // } + + protected override void OnLoaded(RoutedEventArgs e) { + base.OnLoaded(e); buildView(); } - + private void buildView() { + if (navPanel is null) { + Console.WriteLine("NavigationStack buildView called while navpanel is null!"); + // await Task.Delay(100); + // if (navPanel is null) + // await buildView(); + // else Console.WriteLine("navpanel is not null!"); + } navPanel.Children.Clear(); foreach (var item in _stack) { Button btn = new() { @@ -25,7 +38,7 @@ public partial class NavigationStack : UserControl { }; navPanel.Children.Add(btn); } - content = Current?.View ?? new UserControl(); + content.Content = Current?.View ?? new UserControl(); } @@ -44,13 +57,16 @@ public partial class NavigationStack : UserControl { Name = name, View = view }); + buildView(); } public void Pop() { _stack.RemoveAt(_stack.Count - 1); + buildView(); } public void PopTo(int index) { _stack.RemoveRange(index, _stack.Count - index); + buildView(); } } diff --git a/MatrixRoomUtils.Desktop/Components/Pages/RoomList.axaml b/MatrixRoomUtils.Desktop/Components/Pages/RoomList.axaml new file mode 100644 index 0000000..0e43d99 --- /dev/null +++ b/MatrixRoomUtils.Desktop/Components/Pages/RoomList.axaml @@ -0,0 +1,21 @@ + + + + + + + + + diff --git a/MatrixRoomUtils.Desktop/Components/Pages/RoomList.axaml.cs b/MatrixRoomUtils.Desktop/Components/Pages/RoomList.axaml.cs new file mode 100644 index 0000000..53c3063 --- /dev/null +++ b/MatrixRoomUtils.Desktop/Components/Pages/RoomList.axaml.cs @@ -0,0 +1,15 @@ +using System.Collections.ObjectModel; +using Avalonia; +using Avalonia.Controls; +using Avalonia.Markup.Xaml; +using MatrixRoomUtils.Abstractions; + +namespace MatrixRoomUtils.Desktop.Components.Pages; + +public partial class RoomList : UserControl { + private ObservableCollection Rooms { get; set; } = new(); + + public RoomList() { + InitializeComponent(); + } +} \ No newline at end of file diff --git a/MatrixRoomUtils.Desktop/Components/RoomListEntry.axaml b/MatrixRoomUtils.Desktop/Components/RoomListEntry.axaml index 09fe52b..db22ccc 100644 --- a/MatrixRoomUtils.Desktop/Components/RoomListEntry.axaml +++ b/MatrixRoomUtils.Desktop/Components/RoomListEntry.axaml @@ -2,10 +2,15 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" + xmlns:components="clr-namespace:MatrixRoomUtils.Desktop.Components" mc:Ignorable="d" d:DesignWidth="250" d:DesignHeight="32" - x:Class="MatrixRoomUtils.Desktop.Components.RoomListEntry"> + x:Class="MatrixRoomUtils.Desktop.Components.RoomListEntry" + + x:DataType="components:RoomListEntry" + DataContext="{Binding $self}" + > - + diff --git a/MatrixRoomUtils.Desktop/Components/RoomListEntry.axaml.cs b/MatrixRoomUtils.Desktop/Components/RoomListEntry.axaml.cs index 69458aa..73115a2 100644 --- a/MatrixRoomUtils.Desktop/Components/RoomListEntry.axaml.cs +++ b/MatrixRoomUtils.Desktop/Components/RoomListEntry.axaml.cs @@ -7,29 +7,28 @@ using LibMatrix.EventTypes.Spec.State.RoomInfo; using LibMatrix.Helpers; using LibMatrix.Interfaces.Services; using LibMatrix.Services; +using MatrixRoomUtils.Abstractions; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; namespace MatrixRoomUtils.Desktop.Components; public partial class RoomListEntry : UserControl { - private readonly IServiceScopeFactory _serviceScopeFactory; - private readonly RoomInfo _roomInfo; + public RoomInfo Room { get; set; } - public RoomListEntry(IServiceScopeFactory serviceScopeFactory, RoomInfo roomInfo) { - _serviceScopeFactory = serviceScopeFactory; - _roomInfo = roomInfo; + public RoomListEntry() { InitializeComponent(); } protected override void OnLoaded(RoutedEventArgs e) { base.OnLoaded(e); - RoomName.Content = _roomInfo.Room.RoomId; + RoomName.Content = Room.Room.RoomId; Task.WhenAll(GetRoomName(), GetRoomIcon()); } private async Task GetRoomName() { try { - var nameEvent = await _roomInfo.GetStateEvent("m.room.name"); + var nameEvent = await Room.GetStateEvent("m.room.name"); if (nameEvent?.TypedContent is RoomNameEventContent nameData) RoomName.Content = nameData.Name; } @@ -41,18 +40,22 @@ public partial class RoomListEntry : UserControl { private async Task GetRoomIcon() { try { - var avatarEvent = await _roomInfo.GetStateEvent("m.room.avatar"); + using var hc = new HttpClient(); + var avatarEvent = await Room.GetStateEvent("m.room.avatar"); if (avatarEvent?.TypedContent is RoomAvatarEventContent avatarData) { var mxcUrl = avatarData.Url; - await using var svc = _serviceScopeFactory.CreateAsyncScope(); - var hs = await svc.ServiceProvider.GetService()?.GetCurrentSessionOrPrompt()!; - var hsResolver = svc.ServiceProvider.GetService(); - var storage = svc.ServiceProvider.GetService()?.CacheStorageProvider; - var resolvedUrl = await hsResolver.ResolveMediaUri(hs.ServerName, mxcUrl); + var resolvedUrl = await Room.Room.GetResolvedRoomAvatarUrlAsync(); + + // await using var svc = _serviceScopeFactory.CreateAsyncScope(); + // var hs = await svc.ServiceProvider.GetService()?.GetCurrentSessionOrPrompt()!; + // var hsResolver = svc.ServiceProvider.GetService(); + // var storage = svc.ServiceProvider.GetService()?.CacheStorageProvider; + // var resolvedUrl = await hsResolver.ResolveMediaUri(hs.ServerName, mxcUrl); + var storage = new FileStorageProvider("cache"); var storageKey = $"media/{mxcUrl.Replace("mxc://", "").Replace("/", ".")}"; try { if (!await storage.ObjectExistsAsync(storageKey)) - await storage.SaveStreamAsync(storageKey, await hs.ClientHttpClient.GetStreamAsync(resolvedUrl)); + await storage.SaveStreamAsync(storageKey, await hc.GetStreamAsync(resolvedUrl)); RoomIcon.Source = new Bitmap(await storage.LoadStreamAsync(storageKey) ?? throw new NullReferenceException()); } diff --git a/MatrixRoomUtils.Desktop/FileStorageProvider.cs b/MatrixRoomUtils.Desktop/FileStorageProvider.cs deleted file mode 100644 index 0429d1a..0000000 --- a/MatrixRoomUtils.Desktop/FileStorageProvider.cs +++ /dev/null @@ -1,49 +0,0 @@ -using System.Diagnostics.CodeAnalysis; -using System.Text.Json; -using ArcaneLibs.Extensions; -using LibMatrix.Extensions; -using LibMatrix.Interfaces.Services; -using Microsoft.Extensions.Logging; - -namespace MatrixRoomUtils.Desktop; - -public class FileStorageProvider : IStorageProvider { - private readonly ILogger _logger; - - public string TargetPath { get; } - - /// - /// Creates a new instance of . - /// - /// - public FileStorageProvider(string targetPath) { - new Logger(new LoggerFactory()).LogInformation("test"); - Console.WriteLine($"Initialised FileStorageProvider with path {targetPath}"); - TargetPath = targetPath; - if (!Directory.Exists(targetPath)) { - Directory.CreateDirectory(targetPath); - } - } - - public async Task SaveObjectAsync(string key, T value) => await File.WriteAllTextAsync(Path.Join(TargetPath, key), value?.ToJson()); - - [RequiresUnreferencedCode("This API uses reflection to deserialize JSON")] - public async Task LoadObjectAsync(string key) => JsonSerializer.Deserialize(await File.ReadAllTextAsync(Path.Join(TargetPath, key))); - - public Task ObjectExistsAsync(string key) => Task.FromResult(File.Exists(Path.Join(TargetPath, key))); - - public Task> GetAllKeysAsync() => Task.FromResult(Directory.GetFiles(TargetPath).Select(Path.GetFileName).ToList()); - - public Task DeleteObjectAsync(string key) { - File.Delete(Path.Join(TargetPath, key)); - return Task.CompletedTask; - } - - public async Task SaveStreamAsync(string key, Stream stream) { - Directory.CreateDirectory(Path.GetDirectoryName(Path.Join(TargetPath, key)) ?? throw new InvalidOperationException()); - await using var fileStream = File.Create(Path.Join(TargetPath, key)); - await stream.CopyToAsync(fileStream); - } - - public Task LoadStreamAsync(string key) => Task.FromResult(File.Exists(Path.Join(TargetPath, key)) ? File.OpenRead(Path.Join(TargetPath, key)) : null); -} diff --git a/MatrixRoomUtils.Desktop/LoginWindow.axaml b/MatrixRoomUtils.Desktop/LoginWindow.axaml index a4600d5..fc0ee6f 100644 --- a/MatrixRoomUtils.Desktop/LoginWindow.axaml +++ b/MatrixRoomUtils.Desktop/LoginWindow.axaml @@ -4,14 +4,22 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:desktop="clr-namespace:MatrixRoomUtils.Desktop" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" - x:Class="MatrixRoomUtils.Desktop.LoginWindow" Title="LoginWindow" + x:Class="MatrixRoomUtils.Desktop.LoginWindow" x:DataType="desktop:LoginWindow" DataContext="{Binding $self}" - > + SizeToContent="WidthAndHeight" CanResize="False" + MinWidth="250"> - - + + + + + + + + + - + \ No newline at end of file diff --git a/MatrixRoomUtils.Desktop/LoginWindow.axaml.cs b/MatrixRoomUtils.Desktop/LoginWindow.axaml.cs index 1f31b05..183c46b 100644 --- a/MatrixRoomUtils.Desktop/LoginWindow.axaml.cs +++ b/MatrixRoomUtils.Desktop/LoginWindow.axaml.cs @@ -2,6 +2,7 @@ using Avalonia; using Avalonia.Controls; using Avalonia.Interactivity; using Avalonia.Markup.Xaml; +using Avalonia.VisualTree; namespace MatrixRoomUtils.Desktop; diff --git a/MatrixRoomUtils.Desktop/MRUStorageWrapper.cs b/MatrixRoomUtils.Desktop/MRUStorageWrapper.cs index 8a44518..b69c50d 100644 --- a/MatrixRoomUtils.Desktop/MRUStorageWrapper.cs +++ b/MatrixRoomUtils.Desktop/MRUStorageWrapper.cs @@ -1,3 +1,4 @@ +using Avalonia; using LibMatrix; using LibMatrix.Homeservers; using LibMatrix.Responses; @@ -74,8 +75,11 @@ public class MRUStorageWrapper(TieredStorageService storageService, HomeserverPr if (session is null) { // _navigationManager.NavigateTo("/Login"); var wnd = new LoginWindow(this); + wnd.Position = MainWindow.Instance.Position + new PixelPoint(50, 50); await wnd.ShowDialog(MainWindow.Instance); - while (wnd.IsVisible) await Task.Delay(100); + while (wnd.IsVisible) { + await Task.Delay(100); + } session = await GetCurrentSession(); } diff --git a/MatrixRoomUtils.Desktop/MainWindow.axaml.cs b/MatrixRoomUtils.Desktop/MainWindow.axaml.cs index 9db59c5..6ef573e 100644 --- a/MatrixRoomUtils.Desktop/MainWindow.axaml.cs +++ b/MatrixRoomUtils.Desktop/MainWindow.axaml.cs @@ -1,5 +1,7 @@ using Avalonia.Controls; using Avalonia.Interactivity; +using MatrixRoomUtils.Abstractions; +using MatrixRoomUtils.Desktop.Components; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -26,7 +28,6 @@ public partial class MainWindow : Window { _logger.LogInformation("Cache location: {}", _configuration.CacheStoragePath); _logger.LogInformation("Data location: {}", _configuration.DataStoragePath); - // for (int i = 0; i < 100; i++) { // roomList.Children.Add(new RoomListEntry()); // } @@ -39,12 +40,18 @@ public partial class MainWindow : Window { var rooms = await hs.GetJoinedRooms(); foreach (var room in rooms) { // roomList.Children.Add(new RoomListEntry(_scopeFactory, new RoomInfo(room))); + + windowContent.Push("home", new RoomListEntry() { + Room = new RoomInfo() { + Room = room + } + }); + base.OnLoaded(e); } - base.OnLoaded(e); } // public Command // protected void LoadedCommand() { // _logger.LogInformation("async command"); // } -} +} \ No newline at end of file diff --git a/MatrixRoomUtils.Desktop/MatrixRoomUtils.Desktop.csproj b/MatrixRoomUtils.Desktop/MatrixRoomUtils.Desktop.csproj index 6d9fc0e..94bf245 100644 --- a/MatrixRoomUtils.Desktop/MatrixRoomUtils.Desktop.csproj +++ b/MatrixRoomUtils.Desktop/MatrixRoomUtils.Desktop.csproj @@ -10,29 +10,27 @@ preview enable true - true - true - true - true - true - true + + + + + + - - - - + + + + - + - - - + @@ -46,4 +44,7 @@ Always + + + diff --git a/MatrixRoomUtils.Desktop/Properties/launchSettings.json b/MatrixRoomUtils.Desktop/Properties/launchSettings.json index 997e294..36405e8 100644 --- a/MatrixRoomUtils.Desktop/Properties/launchSettings.json +++ b/MatrixRoomUtils.Desktop/Properties/launchSettings.json @@ -12,7 +12,8 @@ "commandName": "Project", "dotnetRunMessages": true, "environmentVariables": { - "DOTNET_ENVIRONMENT": "Development" + "DOTNET_ENVIRONMENT": "Development", + "AVALONIA_THEME": "Dark" } }, "Local config": { diff --git a/MatrixRoomUtils.Desktop/RoomInfo.cs b/MatrixRoomUtils.Desktop/RoomInfo.cs deleted file mode 100644 index a562086..0000000 --- a/MatrixRoomUtils.Desktop/RoomInfo.cs +++ /dev/null @@ -1,40 +0,0 @@ -using LibMatrix; -using LibMatrix.EventTypes; -using LibMatrix.Interfaces; -using LibMatrix.Responses; -using LibMatrix.RoomTypes; - -namespace MatrixRoomUtils.Desktop; - -public class RoomInfo { - public RoomInfo() { } - - public RoomInfo(GenericRoom room) { - Room = room; - } - - public GenericRoom Room { get; set; } - public List StateEvents { get; init; } = new(); - - public async Task GetStateEvent(string type, string stateKey = "") { - var @event = StateEvents.FirstOrDefault(x => x.Type == type && x.StateKey == stateKey); - if (@event is not null) return @event; - @event = new StateEventResponse { - RoomId = Room.RoomId, - Type = type, - StateKey = stateKey, - Sender = null, //TODO: implement - EventId = null - }; - try { - @event.TypedContent = await Room.GetStateAsync(type, stateKey); - } - catch (MatrixException e) { - if (e is { ErrorCode: "M_NOT_FOUND" }) @event.TypedContent = default!; - else throw; - } - - StateEvents.Add(@event); - return @event; - } -} diff --git a/MatrixRoomUtils.Desktop/SentryService.cs b/MatrixRoomUtils.Desktop/SentryService.cs index 648946c..26212fa 100644 --- a/MatrixRoomUtils.Desktop/SentryService.cs +++ b/MatrixRoomUtils.Desktop/SentryService.cs @@ -6,7 +6,7 @@ namespace MatrixRoomUtils.Desktop; public class SentryService : IDisposable { private IDisposable? _sentrySdkDisposable; - public SentryService(IServiceScopeFactory scopeFactory, ILogger logger) { + public SentryService(IServiceScopeFactory scopeFactory, ILogger logger) { var config = scopeFactory.CreateScope().ServiceProvider.GetRequiredService(); if (config.SentryDsn is null) { logger.LogWarning("Sentry DSN is not set, skipping Sentry initialisation"); -- cgit 1.5.1