From 59619764c21fb7196076df2efc02b901adc42871 Mon Sep 17 00:00:00 2001 From: BlossomiShymae <87099578+BlossomiShymae@users.noreply.github.com> Date: Thu, 22 Aug 2024 20:15:13 -0500 Subject: [PATCH] Improve endpoint loading times by using lazy loading --- .../ViewModels/PathOperationViewModel.cs | 40 +++++-------------- .../ViewModels/ResponseViewModel.cs | 35 ++++++++++++++++ Needlework.Net/Views/EndpointView.axaml | 10 ++--- 3 files changed, 49 insertions(+), 36 deletions(-) create mode 100644 Needlework.Net/ViewModels/ResponseViewModel.cs diff --git a/Needlework.Net/ViewModels/PathOperationViewModel.cs b/Needlework.Net/ViewModels/PathOperationViewModel.cs index f179c53..aadd3c7 100644 --- a/Needlework.Net/ViewModels/PathOperationViewModel.cs +++ b/Needlework.Net/ViewModels/PathOperationViewModel.cs @@ -19,15 +19,12 @@ namespace Needlework.Net.ViewModels public SolidColorBrush Color { get; } public string Path { get; } public OperationViewModel Operation { get; } + public ProcessInfo? ProcessInfo { get; } [ObservableProperty] private bool _isBusy; - [ObservableProperty] private string? _responsePath; - [ObservableProperty] private string? _responseStatus; - [ObservableProperty] private string? _responseAuthentication; - [ObservableProperty] private string? _responseUsername; - [ObservableProperty] private string? _responsePassword; - [ObservableProperty] private string? _responseAuthorization; + + [ObservableProperty] private Lazy _response; public PathOperationViewModel(PathOperation pathOperation) { @@ -35,26 +32,7 @@ namespace Needlework.Net.ViewModels Color = new SolidColorBrush(GetColor(Method)); Path = pathOperation.Path; Operation = new OperationViewModel(pathOperation.Operation); - ProcessInfo = GetProcessInfo(); - if (ProcessInfo != null) - { - ResponsePath = $"https://127.0.0.1:{ProcessInfo.AppPort}{Path}"; - var riotAuth = new RiotAuthentication(ProcessInfo.RemotingAuthToken); - ResponseUsername = riotAuth.Username; - ResponsePassword = riotAuth.Password; - ResponseAuthorization = $"Basic {riotAuth.Value}"; - } - } - - private ProcessInfo? GetProcessInfo() - { - try - { - var processInfo = ProcessFinder.Get(); - return processInfo; - } - catch (Exception) { } - return null; + Response = new(() => new ResponseViewModel(pathOperation.Path)); } [RelayCommand] @@ -112,11 +90,11 @@ namespace Needlework.Net.ViewModels } else WeakReferenceMessenger.Default.Send(new EditorUpdateMessage(new(responseBody, "EndpointResponseEditor"))); - ResponseStatus = $"{(int)response.StatusCode} {response.StatusCode}"; - ResponsePath = $"https://127.0.0.1:{processInfo.AppPort}{uri}"; - ResponseAuthentication = $"Basic {riotAuthentication.Value}"; - ResponseUsername = riotAuthentication.Username; - ResponsePassword = riotAuthentication.Password; + Response.Value.Status = $"{(int)response.StatusCode} {response.StatusCode}"; + Response.Value.Path = $"https://127.0.0.1:{processInfo.AppPort}{uri}"; + Response.Value.Authentication = Response.Value.Authorization = $"Basic {riotAuthentication.Value}"; + Response.Value.Username = riotAuthentication.Username; + Response.Value.Password = riotAuthentication.Password; } catch (Exception ex) { diff --git a/Needlework.Net/ViewModels/ResponseViewModel.cs b/Needlework.Net/ViewModels/ResponseViewModel.cs new file mode 100644 index 0000000..815cb47 --- /dev/null +++ b/Needlework.Net/ViewModels/ResponseViewModel.cs @@ -0,0 +1,35 @@ +using BlossomiShymae.GrrrLCU; +using CommunityToolkit.Mvvm.ComponentModel; + +namespace Needlework.Net.ViewModels +{ + public partial class ResponseViewModel : ObservableObject + { + [ObservableProperty] private string? _path; + [ObservableProperty] private string? _status; + [ObservableProperty] private string? _authentication; + [ObservableProperty] private string? _username; + [ObservableProperty] private string? _password; + [ObservableProperty] private string? _authorization; + + public ResponseViewModel(string path) + { + Path = path; + var processInfo = GetProcessInfo(); + if (processInfo != null) + { + var riotAuthentication = new RiotAuthentication(processInfo.RemotingAuthToken); + Path = $"https://127.0.0.1:{processInfo.AppPort}{path}"; + Username = riotAuthentication.Username; + Password = riotAuthentication.Password; + Authorization = $"Basic {riotAuthentication.RawValue}"; + } + } + + private static ProcessInfo? GetProcessInfo() + { + if (ProcessFinder.IsActive()) return ProcessFinder.Get(); + return null; + } + } +} diff --git a/Needlework.Net/Views/EndpointView.axaml b/Needlework.Net/Views/EndpointView.axaml index 1bcc3bd..f870014 100644 --- a/Needlework.Net/Views/EndpointView.axaml +++ b/Needlework.Net/Views/EndpointView.axaml @@ -95,7 +95,7 @@ + Text="{Binding SelectedPathOperation.Response.Value.Username}" /> + Text="{Binding SelectedPathOperation.Response.Value.Password}"/> + Text="{Binding SelectedPathOperation.Response.Value.Authorization}"/> @@ -304,7 +304,7 @@ FontSize="10" Padding="12 4 12 4" Classes="Flat" - Content="{Binding SelectedPathOperation.ResponseStatus}"/> + Content="{Binding SelectedPathOperation.Response.Value.Status}"/>