From 03313562d21d5db9bf6a14ebbeab80e06c883d3a Mon Sep 17 00:00:00 2001 From: Rory& Date: Wed, 24 Jan 2024 02:31:56 +0100 Subject: MRU->RMU, fixes, cleanup --- .../Components/NavigationStack.axaml | 12 ++++ .../Components/NavigationStack.axaml.cs | 72 +++++++++++++++++++++ .../Components/Pages/RoomList.axaml | 21 ++++++ .../Components/Pages/RoomList.axaml.cs | 15 +++++ MatrixUtils.Desktop/Components/RoomListEntry.axaml | 16 +++++ .../Components/RoomListEntry.axaml.cs | 74 ++++++++++++++++++++++ 6 files changed, 210 insertions(+) create mode 100644 MatrixUtils.Desktop/Components/NavigationStack.axaml create mode 100644 MatrixUtils.Desktop/Components/NavigationStack.axaml.cs create mode 100644 MatrixUtils.Desktop/Components/Pages/RoomList.axaml create mode 100644 MatrixUtils.Desktop/Components/Pages/RoomList.axaml.cs create mode 100644 MatrixUtils.Desktop/Components/RoomListEntry.axaml create mode 100644 MatrixUtils.Desktop/Components/RoomListEntry.axaml.cs (limited to 'MatrixUtils.Desktop/Components') diff --git a/MatrixUtils.Desktop/Components/NavigationStack.axaml b/MatrixUtils.Desktop/Components/NavigationStack.axaml new file mode 100644 index 0000000..b24895d --- /dev/null +++ b/MatrixUtils.Desktop/Components/NavigationStack.axaml @@ -0,0 +1,12 @@ + + + + + + + \ No newline at end of file diff --git a/MatrixUtils.Desktop/Components/NavigationStack.axaml.cs b/MatrixUtils.Desktop/Components/NavigationStack.axaml.cs new file mode 100644 index 0000000..632ae3c --- /dev/null +++ b/MatrixUtils.Desktop/Components/NavigationStack.axaml.cs @@ -0,0 +1,72 @@ +using Avalonia.Controls; +using Avalonia.Interactivity; +using Avalonia.Markup.Xaml; + +namespace MatrixUtils.Desktop.Components; + +public partial class NavigationStack : UserControl { + public NavigationStack() { + InitializeComponent(); + } + + // 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() { + Content = item.Name + }; + btn.Click += (_, _) => { + PopTo(_stack.IndexOf(item)); + buildView(); + }; + navPanel.Children.Add(btn); + } + content.Content = Current?.View ?? new UserControl(); + } + + + public class NavigationStackItem { + public string Name { get; set; } + public string Description { get; set; } = ""; + public UserControl View { get; set; } + } + + private List _stack = new(); + + public NavigationStackItem? Current => _stack.LastOrDefault(); + + public void Push(string name, UserControl view) { + _stack.Add(new NavigationStackItem { + 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/MatrixUtils.Desktop/Components/Pages/RoomList.axaml b/MatrixUtils.Desktop/Components/Pages/RoomList.axaml new file mode 100644 index 0000000..45778f3 --- /dev/null +++ b/MatrixUtils.Desktop/Components/Pages/RoomList.axaml @@ -0,0 +1,21 @@ + + + + + + + + + diff --git a/MatrixUtils.Desktop/Components/Pages/RoomList.axaml.cs b/MatrixUtils.Desktop/Components/Pages/RoomList.axaml.cs new file mode 100644 index 0000000..a0c9fcc --- /dev/null +++ b/MatrixUtils.Desktop/Components/Pages/RoomList.axaml.cs @@ -0,0 +1,15 @@ +using System.Collections.ObjectModel; +using Avalonia; +using Avalonia.Controls; +using Avalonia.Markup.Xaml; +using MatrixUtils.Abstractions; + +namespace MatrixUtils.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/MatrixUtils.Desktop/Components/RoomListEntry.axaml b/MatrixUtils.Desktop/Components/RoomListEntry.axaml new file mode 100644 index 0000000..97e6fdc --- /dev/null +++ b/MatrixUtils.Desktop/Components/RoomListEntry.axaml @@ -0,0 +1,16 @@ + + + + + + diff --git a/MatrixUtils.Desktop/Components/RoomListEntry.axaml.cs b/MatrixUtils.Desktop/Components/RoomListEntry.axaml.cs new file mode 100644 index 0000000..1e4a127 --- /dev/null +++ b/MatrixUtils.Desktop/Components/RoomListEntry.axaml.cs @@ -0,0 +1,74 @@ +using Avalonia.Controls; +using Avalonia.Interactivity; +using Avalonia.Media.Imaging; +using LibMatrix; +using LibMatrix.EventTypes.Spec.State; +using LibMatrix.EventTypes.Spec.State.RoomInfo; +using LibMatrix.Helpers; +using LibMatrix.Interfaces.Services; +using LibMatrix.Services; +using MatrixUtils.Abstractions; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; + +namespace MatrixUtils.Desktop.Components; + +public partial class RoomListEntry : UserControl { + public RoomInfo Room { get; set; } + + public RoomListEntry() { + InitializeComponent(); + } + + protected override void OnLoaded(RoutedEventArgs e) { + base.OnLoaded(e); + RoomName.Content = Room.Room.RoomId; + Task.WhenAll(GetRoomName(), GetRoomIcon()); + } + + private async Task GetRoomName() { + try { + var nameEvent = await Room.GetStateEvent("m.room.name"); + if (nameEvent?.TypedContent is RoomNameEventContent nameData) + RoomName.Content = nameData.Name; + } + catch (MatrixException e) { + if (e.ErrorCode != "M_NOT_FOUND") + throw; + } + } + + private async Task GetRoomIcon() { + try { + using var hc = new HttpClient(); + var avatarEvent = await Room.GetStateEvent("m.room.avatar"); + if (avatarEvent?.TypedContent is RoomAvatarEventContent avatarData) { + var mxcUrl = avatarData.Url; + 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 hc.GetStreamAsync(resolvedUrl)); + + RoomIcon.Source = new Bitmap(await storage.LoadStreamAsync(storageKey) ?? throw new NullReferenceException()); + } + catch (IOException) { } + catch (MatrixException e) { + if (e.ErrorCode != "M_UNKNOWN") + throw; + } + } + } + catch (MatrixException e) { + if (e.ErrorCode != "M_NOT_FOUND") + throw; + } + } +} -- cgit 1.4.1