mirror of
https://github.com/BlossomiShymae/Needlework.Net.git
synced 2025-12-06 18:20:47 +01:00
Refactor request viewmodels
This commit is contained in:
@@ -1,8 +0,0 @@
|
|||||||
using CommunityToolkit.Mvvm.Messaging.Messages;
|
|
||||||
|
|
||||||
namespace Needlework.Net.Messages
|
|
||||||
{
|
|
||||||
public class ContentRequestMessage : RequestMessage<string>
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
using CommunityToolkit.Mvvm.Messaging.Messages;
|
|
||||||
|
|
||||||
namespace Needlework.Net.Messages
|
|
||||||
{
|
|
||||||
public class EditorUpdateMessage(EditorUpdate editorUpdate) : ValueChangedMessage<EditorUpdate>(editorUpdate)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public class EditorUpdate
|
|
||||||
{
|
|
||||||
public string Text { get; }
|
|
||||||
public string Key { get; }
|
|
||||||
|
|
||||||
public EditorUpdate(string text, string key)
|
|
||||||
{
|
|
||||||
Text = text;
|
|
||||||
Key = key;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,13 +1,9 @@
|
|||||||
using Avalonia.Collections;
|
using Avalonia.Collections;
|
||||||
using BlossomiShymae.GrrrLCU;
|
|
||||||
using CommunityToolkit.Mvvm.ComponentModel;
|
using CommunityToolkit.Mvvm.ComponentModel;
|
||||||
using CommunityToolkit.Mvvm.Input;
|
using CommunityToolkit.Mvvm.Input;
|
||||||
using CommunityToolkit.Mvvm.Messaging;
|
using CommunityToolkit.Mvvm.Messaging;
|
||||||
using Needlework.Net.Messages;
|
using Needlework.Net.Messages;
|
||||||
using Needlework.Net.ViewModels.MainWindow;
|
using Needlework.Net.ViewModels.Shared;
|
||||||
using System;
|
|
||||||
using System.Net.Http;
|
|
||||||
using System.Text.Json;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Needlework.Net.ViewModels.Pages;
|
namespace Needlework.Net.ViewModels.Pages;
|
||||||
@@ -18,13 +14,7 @@ public partial class ConsoleViewModel : PageBase, IRecipient<DataReadyMessage>
|
|||||||
public IAvaloniaList<string> RequestPaths { get; } = new AvaloniaList<string>();
|
public IAvaloniaList<string> RequestPaths { get; } = new AvaloniaList<string>();
|
||||||
|
|
||||||
[ObservableProperty] private bool _isBusy = true;
|
[ObservableProperty] private bool _isBusy = true;
|
||||||
[ObservableProperty] private bool _isRequestBusy = false;
|
[ObservableProperty] private LcuRequestViewModel _lcuRequest = new();
|
||||||
[ObservableProperty] private string? _requestMethodSelected = "GET";
|
|
||||||
[ObservableProperty] private string? _requestPath = null;
|
|
||||||
[ObservableProperty] private string? _requestBody = null;
|
|
||||||
[ObservableProperty] private string? _responsePath = null;
|
|
||||||
[ObservableProperty] private string? _responseStatus = null;
|
|
||||||
[ObservableProperty] private string? _responseAuthorization = null;
|
|
||||||
|
|
||||||
public ConsoleViewModel() : base("Console", "terminal", -200)
|
public ConsoleViewModel() : base("Console", "terminal", -200)
|
||||||
{
|
{
|
||||||
@@ -34,56 +24,7 @@ public partial class ConsoleViewModel : PageBase, IRecipient<DataReadyMessage>
|
|||||||
[RelayCommand]
|
[RelayCommand]
|
||||||
private async Task SendRequest()
|
private async Task SendRequest()
|
||||||
{
|
{
|
||||||
try
|
await LcuRequest.ExecuteAsync();
|
||||||
{
|
|
||||||
IsRequestBusy = true;
|
|
||||||
if (string.IsNullOrEmpty(RequestPath)) throw new Exception("Path is empty.");
|
|
||||||
|
|
||||||
var method = RequestMethodSelected switch
|
|
||||||
{
|
|
||||||
"GET" => HttpMethod.Get,
|
|
||||||
"POST" => HttpMethod.Post,
|
|
||||||
"PUT" => HttpMethod.Put,
|
|
||||||
"DELETE" => HttpMethod.Delete,
|
|
||||||
"HEAD" => HttpMethod.Head,
|
|
||||||
"PATCH" => HttpMethod.Patch,
|
|
||||||
"OPTIONS" => HttpMethod.Options,
|
|
||||||
"TRACE" => HttpMethod.Trace,
|
|
||||||
_ => throw new Exception("Method is not selected."),
|
|
||||||
};
|
|
||||||
|
|
||||||
var processInfo = ProcessFinder.Get();
|
|
||||||
var requestBody = WeakReferenceMessenger.Default.Send(new ContentRequestMessage(), "ConsoleRequestEditor").Response;
|
|
||||||
var content = new StringContent(requestBody, new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"));
|
|
||||||
var client = Connector.GetLcuHttpClientInstance();
|
|
||||||
var response = await client.SendAsync(new(method, RequestPath) { Content = content });
|
|
||||||
var riotAuthentication = new RiotAuthentication(processInfo.RemotingAuthToken);
|
|
||||||
var responseBody = await response.Content.ReadAsByteArrayAsync();
|
|
||||||
|
|
||||||
var body = responseBody.Length > 0 ? JsonSerializer.Serialize(JsonSerializer.Deserialize<object>(responseBody), App.JsonSerializerOptions) : string.Empty;
|
|
||||||
if (body.Length >= App.MaxCharacters)
|
|
||||||
{
|
|
||||||
WeakReferenceMessenger.Default.Send(new OopsiesDialogRequestedMessage(body));
|
|
||||||
WeakReferenceMessenger.Default.Send(new ResponseUpdatedMessage(string.Empty), nameof(ConsoleViewModel));
|
|
||||||
}
|
|
||||||
else WeakReferenceMessenger.Default.Send(new ResponseUpdatedMessage(body), nameof(ConsoleViewModel));
|
|
||||||
|
|
||||||
ResponseStatus = $"{(int)response.StatusCode} {response.StatusCode.ToString()}";
|
|
||||||
ResponsePath = $"https://127.0.0.1:{processInfo.AppPort}{RequestPath}";
|
|
||||||
ResponseAuthorization = $"Basic {riotAuthentication.Value}";
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
WeakReferenceMessenger.Default.Send(new InfoBarUpdateMessage(new InfoBarViewModel("Request Failed", true, ex.Message, FluentAvalonia.UI.Controls.InfoBarSeverity.Error, TimeSpan.FromSeconds(5))));
|
|
||||||
ResponseStatus = null;
|
|
||||||
ResponsePath = null;
|
|
||||||
ResponseAuthorization = null;
|
|
||||||
WeakReferenceMessenger.Default.Send(new ResponseUpdatedMessage(string.Empty), nameof(ConsoleViewModel));
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
IsRequestBusy = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Receive(DataReadyMessage message)
|
public void Receive(DataReadyMessage message)
|
||||||
|
|||||||
@@ -19,6 +19,8 @@ public partial class EndpointViewModel : ObservableObject
|
|||||||
[ObservableProperty] private string? _search;
|
[ObservableProperty] private string? _search;
|
||||||
public IAvaloniaList<PathOperationViewModel> FilteredPathOperations { get; }
|
public IAvaloniaList<PathOperationViewModel> FilteredPathOperations { get; }
|
||||||
|
|
||||||
|
public event EventHandler<string>? PathOperationSelected;
|
||||||
|
|
||||||
public EndpointViewModel(string endpoint)
|
public EndpointViewModel(string endpoint)
|
||||||
{
|
{
|
||||||
Endpoint = endpoint;
|
Endpoint = endpoint;
|
||||||
@@ -43,6 +45,6 @@ public partial class EndpointViewModel : ObservableObject
|
|||||||
partial void OnSelectedPathOperationChanged(PathOperationViewModel? value)
|
partial void OnSelectedPathOperationChanged(PathOperationViewModel? value)
|
||||||
{
|
{
|
||||||
if (value == null) return;
|
if (value == null) return;
|
||||||
WeakReferenceMessenger.Default.Send(new EditorUpdateMessage(new(value.Operation.RequestTemplate ?? string.Empty, "EndpointRequestEditor")));
|
PathOperationSelected?.Invoke(this, value.Operation.RequestTemplate ?? string.Empty);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,62 +1,34 @@
|
|||||||
using Avalonia.Media;
|
using CommunityToolkit.Mvvm.ComponentModel;
|
||||||
using BlossomiShymae.GrrrLCU;
|
|
||||||
using CommunityToolkit.Mvvm.ComponentModel;
|
|
||||||
using CommunityToolkit.Mvvm.Input;
|
using CommunityToolkit.Mvvm.Input;
|
||||||
using CommunityToolkit.Mvvm.Messaging;
|
|
||||||
using Needlework.Net.Messages;
|
|
||||||
using Needlework.Net.Models;
|
using Needlework.Net.Models;
|
||||||
using Needlework.Net.ViewModels.MainWindow;
|
using Needlework.Net.ViewModels.Shared;
|
||||||
using System;
|
using System;
|
||||||
using System.Net.Http;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.Json;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Needlework.Net.ViewModels.Pages.Endpoints;
|
namespace Needlework.Net.ViewModels.Pages.Endpoints;
|
||||||
|
|
||||||
public partial class PathOperationViewModel : ObservableObject
|
public partial class PathOperationViewModel : ObservableObject
|
||||||
{
|
{
|
||||||
public string Method { get; }
|
|
||||||
public SolidColorBrush Color { get; }
|
|
||||||
public string Path { get; }
|
public string Path { get; }
|
||||||
public OperationViewModel Operation { get; }
|
public OperationViewModel Operation { get; }
|
||||||
|
|
||||||
public ProcessInfo? ProcessInfo { get; }
|
|
||||||
|
|
||||||
[ObservableProperty] private bool _isBusy;
|
[ObservableProperty] private bool _isBusy;
|
||||||
|
[ObservableProperty] private Lazy<LcuRequestViewModel> _lcuRequest;
|
||||||
[ObservableProperty] private Lazy<ResponseViewModel> _response;
|
|
||||||
|
|
||||||
public PathOperationViewModel(PathOperation pathOperation)
|
public PathOperationViewModel(PathOperation pathOperation)
|
||||||
{
|
{
|
||||||
Method = pathOperation.Method.ToUpper();
|
|
||||||
Color = new SolidColorBrush(GetColor(Method));
|
|
||||||
Path = pathOperation.Path;
|
Path = pathOperation.Path;
|
||||||
Operation = new OperationViewModel(pathOperation.Operation);
|
Operation = new OperationViewModel(pathOperation.Operation);
|
||||||
Response = new(() => new ResponseViewModel(pathOperation.Path));
|
LcuRequest = new(() => new LcuRequestViewModel()
|
||||||
|
{
|
||||||
|
Method = pathOperation.Method.ToUpper()
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
[RelayCommand]
|
[RelayCommand]
|
||||||
public async Task SendRequest()
|
private async Task SendRequest()
|
||||||
{
|
{
|
||||||
try
|
|
||||||
{
|
|
||||||
IsBusy = true;
|
|
||||||
|
|
||||||
var method = Method switch
|
|
||||||
{
|
|
||||||
"GET" => HttpMethod.Get,
|
|
||||||
"POST" => HttpMethod.Post,
|
|
||||||
"PUT" => HttpMethod.Put,
|
|
||||||
"DELETE" => HttpMethod.Delete,
|
|
||||||
"HEAD" => HttpMethod.Head,
|
|
||||||
"PATCH" => HttpMethod.Patch,
|
|
||||||
"OPTIONS" => HttpMethod.Options,
|
|
||||||
"TRACE" => HttpMethod.Trace,
|
|
||||||
_ => throw new Exception("Method is missing.")
|
|
||||||
};
|
|
||||||
|
|
||||||
var processInfo = ProcessFinder.Get();
|
|
||||||
var sb = new StringBuilder(Path);
|
var sb = new StringBuilder(Path);
|
||||||
foreach (var pathParameter in Operation.PathParameters)
|
foreach (var pathParameter in Operation.PathParameters)
|
||||||
{
|
{
|
||||||
@@ -73,49 +45,8 @@ public partial class PathOperationViewModel : ObservableObject
|
|||||||
sb.Append($"{queryParameter.Name}={Uri.EscapeDataString(queryParameter.Value)}");
|
sb.Append($"{queryParameter.Name}={Uri.EscapeDataString(queryParameter.Value)}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var uri = sb.ToString();
|
|
||||||
|
|
||||||
var requestBody = WeakReferenceMessenger.Default.Send(new ContentRequestMessage(), "EndpointRequestEditor").Response;
|
LcuRequest.Value.RequestPath = sb.ToString();
|
||||||
var content = new StringContent(requestBody, new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"));
|
await LcuRequest.Value.ExecuteAsync();
|
||||||
|
|
||||||
var client = Connector.GetLcuHttpClientInstance();
|
|
||||||
var response = await client.SendAsync(new(method, uri) { Content = content });
|
|
||||||
var riotAuthentication = new RiotAuthentication(processInfo.RemotingAuthToken);
|
|
||||||
var responseBytes = await response.Content.ReadAsByteArrayAsync();
|
|
||||||
|
|
||||||
var responseBody = responseBytes.Length > 0 ? JsonSerializer.Serialize(JsonSerializer.Deserialize<object>(responseBytes), App.JsonSerializerOptions) : string.Empty;
|
|
||||||
if (responseBody.Length >= App.MaxCharacters)
|
|
||||||
{
|
|
||||||
WeakReferenceMessenger.Default.Send(new OopsiesDialogRequestedMessage(responseBody));
|
|
||||||
WeakReferenceMessenger.Default.Send(new EditorUpdateMessage(new(string.Empty, "EndpointResponseEditor")));
|
|
||||||
}
|
|
||||||
else WeakReferenceMessenger.Default.Send(new EditorUpdateMessage(new(responseBody, "EndpointResponseEditor")));
|
|
||||||
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
WeakReferenceMessenger.Default.Send(new InfoBarUpdateMessage(new InfoBarViewModel("Request Failed", true, ex.Message, FluentAvalonia.UI.Controls.InfoBarSeverity.Error, TimeSpan.FromSeconds(5))));
|
|
||||||
WeakReferenceMessenger.Default.Send(new EditorUpdateMessage(new(string.Empty, "EndpointResponseEditor")));
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
IsBusy = false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Color GetColor(string method) => method switch
|
|
||||||
{
|
|
||||||
"GET" => Avalonia.Media.Color.FromRgb(95, 99, 186),
|
|
||||||
"POST" => Avalonia.Media.Color.FromRgb(103, 186, 95),
|
|
||||||
"PUT" => Avalonia.Media.Color.FromRgb(186, 139, 95),
|
|
||||||
"DELETE" => Avalonia.Media.Color.FromRgb(186, 95, 95),
|
|
||||||
"HEAD" => Avalonia.Media.Color.FromRgb(136, 95, 186),
|
|
||||||
"PATCH" => Avalonia.Media.Color.FromRgb(95, 186, 139),
|
|
||||||
_ => throw new InvalidOperationException("Method does not have assigned color.")
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|||||||
112
Needlework.Net/ViewModels/Shared/LcuRequestViewModel.cs
Normal file
112
Needlework.Net/ViewModels/Shared/LcuRequestViewModel.cs
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
using Avalonia.Media;
|
||||||
|
using BlossomiShymae.GrrrLCU;
|
||||||
|
using CommunityToolkit.Mvvm.ComponentModel;
|
||||||
|
using CommunityToolkit.Mvvm.Messaging;
|
||||||
|
using Needlework.Net.Messages;
|
||||||
|
using Needlework.Net.ViewModels.MainWindow;
|
||||||
|
using System;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Needlework.Net.ViewModels.Shared;
|
||||||
|
|
||||||
|
public partial class LcuRequestViewModel : ObservableObject
|
||||||
|
{
|
||||||
|
[ObservableProperty] private string? _method = "GET";
|
||||||
|
[ObservableProperty] private SolidColorBrush _color = new(GetColor("GET"));
|
||||||
|
|
||||||
|
[ObservableProperty] private bool _isRequestBusy = false;
|
||||||
|
[ObservableProperty] private string? _requestPath = null;
|
||||||
|
[ObservableProperty] private string? _requestBody = null;
|
||||||
|
|
||||||
|
[ObservableProperty] private string? _responsePath = null;
|
||||||
|
[ObservableProperty] private string? _responseStatus = null;
|
||||||
|
[ObservableProperty] private string? _responseAuthentication = null;
|
||||||
|
[ObservableProperty] private string? _responseUsername = null;
|
||||||
|
[ObservableProperty] private string? _responsePassword = null;
|
||||||
|
[ObservableProperty] private string? _responseAuthorization = null;
|
||||||
|
|
||||||
|
public event EventHandler<LcuRequestViewModel>? RequestText;
|
||||||
|
public event EventHandler<string>? UpdateText;
|
||||||
|
|
||||||
|
partial void OnMethodChanged(string? oldValue, string? newValue)
|
||||||
|
{
|
||||||
|
if (newValue == null) return;
|
||||||
|
|
||||||
|
Color = new(GetColor(newValue));
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task ExecuteAsync()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
IsRequestBusy = true;
|
||||||
|
if (string.IsNullOrEmpty(RequestPath))
|
||||||
|
throw new Exception("Path is empty.");
|
||||||
|
|
||||||
|
var method = Method switch
|
||||||
|
{
|
||||||
|
"GET" => HttpMethod.Get,
|
||||||
|
"POST" => HttpMethod.Post,
|
||||||
|
"PUT" => HttpMethod.Put,
|
||||||
|
"DELETE" => HttpMethod.Delete,
|
||||||
|
"HEAD" => HttpMethod.Head,
|
||||||
|
"PATCH" => HttpMethod.Patch,
|
||||||
|
"OPTIONS" => HttpMethod.Options,
|
||||||
|
"TRACE" => HttpMethod.Trace,
|
||||||
|
_ => throw new Exception("Method is not selected or missing."),
|
||||||
|
};
|
||||||
|
|
||||||
|
var processInfo = ProcessFinder.Get();
|
||||||
|
RequestText?.Invoke(this, this);
|
||||||
|
var content = new StringContent(RequestBody ?? string.Empty, new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"));
|
||||||
|
var client = Connector.GetLcuHttpClientInstance();
|
||||||
|
var response = await client.SendAsync(new(method, RequestPath) { Content = content });
|
||||||
|
var riotAuthentication = new RiotAuthentication(processInfo.RemotingAuthToken);
|
||||||
|
var responseBody = await response.Content.ReadAsByteArrayAsync();
|
||||||
|
|
||||||
|
var body = responseBody.Length > 0 ? JsonSerializer.Serialize(JsonSerializer.Deserialize<object>(responseBody), App.JsonSerializerOptions) : string.Empty;
|
||||||
|
if (body.Length >= App.MaxCharacters)
|
||||||
|
{
|
||||||
|
WeakReferenceMessenger.Default.Send(new OopsiesDialogRequestedMessage(body));
|
||||||
|
UpdateText?.Invoke(this, string.Empty);
|
||||||
|
}
|
||||||
|
UpdateText?.Invoke(this, body);
|
||||||
|
|
||||||
|
ResponseStatus = $"{(int)response.StatusCode} {response.StatusCode.ToString()}";
|
||||||
|
ResponsePath = $"https://127.0.0.1:{processInfo.AppPort}{RequestPath}";
|
||||||
|
ResponseAuthentication = riotAuthentication.Value;
|
||||||
|
ResponseAuthorization = $"Basic {riotAuthentication.Value}";
|
||||||
|
ResponseUsername = riotAuthentication.Username;
|
||||||
|
ResponsePassword = riotAuthentication.Password;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
WeakReferenceMessenger.Default.Send(new InfoBarUpdateMessage(new InfoBarViewModel("Request Failed", true, ex.Message, FluentAvalonia.UI.Controls.InfoBarSeverity.Error, TimeSpan.FromSeconds(5))));
|
||||||
|
UpdateText?.Invoke(this, string.Empty);
|
||||||
|
|
||||||
|
ResponseStatus = null;
|
||||||
|
ResponsePath = null;
|
||||||
|
ResponseAuthentication = null;
|
||||||
|
ResponseAuthorization = null;
|
||||||
|
ResponseUsername = null;
|
||||||
|
ResponsePassword = null;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
IsRequestBusy = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Color GetColor(string method) => method switch
|
||||||
|
{
|
||||||
|
"GET" => Avalonia.Media.Color.FromRgb(95, 99, 186),
|
||||||
|
"POST" => Avalonia.Media.Color.FromRgb(103, 186, 95),
|
||||||
|
"PUT" => Avalonia.Media.Color.FromRgb(186, 139, 95),
|
||||||
|
"DELETE" => Avalonia.Media.Color.FromRgb(186, 95, 95),
|
||||||
|
"HEAD" => Avalonia.Media.Color.FromRgb(136, 95, 186),
|
||||||
|
"PATCH" => Avalonia.Media.Color.FromRgb(95, 186, 139),
|
||||||
|
_ => throw new InvalidOperationException("Method does not have assigned color.")
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -19,13 +19,13 @@
|
|||||||
<StackPanel Margin="0 0 0 16">
|
<StackPanel Margin="0 0 0 16">
|
||||||
<Grid RowDefinitions="auto" ColumnDefinitions="auto,*,auto">
|
<Grid RowDefinitions="auto" ColumnDefinitions="auto,*,auto">
|
||||||
<ComboBox ItemsSource="{Binding RequestMethods}"
|
<ComboBox ItemsSource="{Binding RequestMethods}"
|
||||||
SelectedItem="{Binding RequestMethodSelected}"
|
SelectedItem="{Binding LcuRequest.Method}"
|
||||||
Margin="0 0 8 0"
|
Margin="0 0 8 0"
|
||||||
Grid.Row="0"
|
Grid.Row="0"
|
||||||
Grid.Column="0"/>
|
Grid.Column="0"/>
|
||||||
<AutoCompleteBox
|
<AutoCompleteBox
|
||||||
ItemsSource="{Binding RequestPaths}"
|
ItemsSource="{Binding RequestPaths}"
|
||||||
Text="{Binding RequestPath}"
|
Text="{Binding LcuRequest.RequestPath}"
|
||||||
MaxDropDownHeight="400"
|
MaxDropDownHeight="400"
|
||||||
FilterMode="StartsWith"
|
FilterMode="StartsWith"
|
||||||
Grid.Row="0"
|
Grid.Row="0"
|
||||||
@@ -49,7 +49,7 @@
|
|||||||
<TextBox IsReadOnly="True"
|
<TextBox IsReadOnly="True"
|
||||||
Grid.Row="0"
|
Grid.Row="0"
|
||||||
Grid.Column="0"
|
Grid.Column="0"
|
||||||
Text="{Binding ResponsePath}"/>
|
Text="{Binding LcuRequest.ResponsePath}"/>
|
||||||
<avaloniaEdit:TextEditor
|
<avaloniaEdit:TextEditor
|
||||||
Name="RequestEditor"
|
Name="RequestEditor"
|
||||||
Text=""
|
Text=""
|
||||||
@@ -69,7 +69,7 @@
|
|||||||
<StackPanel Orientation="Horizontal"
|
<StackPanel Orientation="Horizontal"
|
||||||
Grid.Row="0"
|
Grid.Row="0"
|
||||||
Grid.Column="0">
|
Grid.Column="0">
|
||||||
<Button Content="{Binding ResponseStatus}"
|
<Button Content="{Binding LcuRequest.ResponseStatus}"
|
||||||
FontSize="12"
|
FontSize="12"
|
||||||
VerticalAlignment="Center"/>
|
VerticalAlignment="Center"/>
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
|
|||||||
@@ -2,15 +2,13 @@ using Avalonia;
|
|||||||
using Avalonia.Controls;
|
using Avalonia.Controls;
|
||||||
using Avalonia.Styling;
|
using Avalonia.Styling;
|
||||||
using AvaloniaEdit;
|
using AvaloniaEdit;
|
||||||
using CommunityToolkit.Mvvm.Messaging;
|
|
||||||
using Needlework.Net.Extensions;
|
using Needlework.Net.Extensions;
|
||||||
using Needlework.Net.Messages;
|
|
||||||
using Needlework.Net.ViewModels.Pages;
|
using Needlework.Net.ViewModels.Pages;
|
||||||
using TextMateSharp.Grammars;
|
using TextMateSharp.Grammars;
|
||||||
|
|
||||||
namespace Needlework.Net.Views.Pages;
|
namespace Needlework.Net.Views.Pages;
|
||||||
|
|
||||||
public partial class ConsoleView : UserControl, IRecipient<ResponseUpdatedMessage>, IRecipient<ContentRequestMessage>
|
public partial class ConsoleView : UserControl
|
||||||
{
|
{
|
||||||
private TextEditor? _responseEditor;
|
private TextEditor? _responseEditor;
|
||||||
private TextEditor? _requestEditor;
|
private TextEditor? _requestEditor;
|
||||||
@@ -20,16 +18,6 @@ public partial class ConsoleView : UserControl, IRecipient<ResponseUpdatedMessag
|
|||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Receive(ResponseUpdatedMessage message)
|
|
||||||
{
|
|
||||||
_responseEditor!.Text = message.Value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Receive(ContentRequestMessage message)
|
|
||||||
{
|
|
||||||
message.Reply(_requestEditor!.Text);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
|
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
|
||||||
{
|
{
|
||||||
base.OnAttachedToVisualTree(e);
|
base.OnAttachedToVisualTree(e);
|
||||||
@@ -39,17 +27,30 @@ public partial class ConsoleView : UserControl, IRecipient<ResponseUpdatedMessag
|
|||||||
_responseEditor?.ApplyJsonEditorSettings();
|
_responseEditor?.ApplyJsonEditorSettings();
|
||||||
_requestEditor?.ApplyJsonEditorSettings();
|
_requestEditor?.ApplyJsonEditorSettings();
|
||||||
|
|
||||||
WeakReferenceMessenger.Default.Register<ResponseUpdatedMessage, string>(this, nameof(ConsoleViewModel));
|
var vm = (ConsoleViewModel)DataContext!;
|
||||||
WeakReferenceMessenger.Default.Register<ContentRequestMessage, string>(this, "ConsoleRequestEditor");
|
vm.LcuRequest.RequestText += LcuRequest_RequestText; ;
|
||||||
|
vm.LcuRequest.UpdateText += LcuRequest_UpdateText;
|
||||||
|
|
||||||
OnBaseThemeChanged(Application.Current!.ActualThemeVariant);
|
OnBaseThemeChanged(Application.Current!.ActualThemeVariant);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void LcuRequest_RequestText(object? sender, ViewModels.Shared.LcuRequestViewModel e)
|
||||||
|
{
|
||||||
|
e.RequestBody = _requestEditor!.Text;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void LcuRequest_UpdateText(object? sender, string e)
|
||||||
|
{
|
||||||
|
_responseEditor!.Text = e;
|
||||||
|
}
|
||||||
|
|
||||||
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
|
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
|
||||||
{
|
{
|
||||||
base.OnDetachedFromVisualTree(e);
|
base.OnDetachedFromVisualTree(e);
|
||||||
|
|
||||||
WeakReferenceMessenger.Default.UnregisterAll(this);
|
var vm = (ConsoleViewModel)DataContext!;
|
||||||
|
vm.LcuRequest.RequestText -= LcuRequest_RequestText;
|
||||||
|
vm.LcuRequest.UpdateText -= LcuRequest_UpdateText;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnBaseThemeChanged(ThemeVariant currentTheme)
|
private void OnBaseThemeChanged(ThemeVariant currentTheme)
|
||||||
|
|||||||
@@ -59,8 +59,8 @@
|
|||||||
VerticalAlignment="Center"
|
VerticalAlignment="Center"
|
||||||
TextAlignment="Center"
|
TextAlignment="Center"
|
||||||
Margin="0 0 8 0"
|
Margin="0 0 8 0"
|
||||||
Text="{Binding Method}"
|
Text="{Binding LcuRequest.Value.Method}"
|
||||||
Background="{Binding Color}"
|
Background="{Binding LcuRequest.Value.Color}"
|
||||||
FontSize="8"
|
FontSize="8"
|
||||||
Width="50"
|
Width="50"
|
||||||
Padding="10 2 10 2"
|
Padding="10 2 10 2"
|
||||||
@@ -88,14 +88,14 @@
|
|||||||
ColumnDefinitions="auto,*,auto">
|
ColumnDefinitions="auto,*,auto">
|
||||||
<TextBox Grid.Row="0"
|
<TextBox Grid.Row="0"
|
||||||
Grid.Column="0"
|
Grid.Column="0"
|
||||||
Text="{Binding SelectedPathOperation.Method}"
|
Text="{Binding SelectedPathOperation.LcuRequest.Value.Method}"
|
||||||
FontSize="12"
|
FontSize="12"
|
||||||
IsReadOnly="True"
|
IsReadOnly="True"
|
||||||
Margin="0 0 8 0"/>
|
Margin="0 0 8 0"/>
|
||||||
<TextBox Grid.Row="0"
|
<TextBox Grid.Row="0"
|
||||||
Grid.Column="1"
|
Grid.Column="1"
|
||||||
FontSize="12"
|
FontSize="12"
|
||||||
Text="{Binding SelectedPathOperation.Response.Value.Path}"
|
Text="{Binding SelectedPathOperation.LcuRequest.Value.ResponsePath}"
|
||||||
IsReadOnly="True"/>
|
IsReadOnly="True"/>
|
||||||
<StackPanel Grid.Row="0"
|
<StackPanel Grid.Row="0"
|
||||||
Grid.Column="2"
|
Grid.Column="2"
|
||||||
@@ -189,7 +189,7 @@
|
|||||||
Grid.Column="1"
|
Grid.Column="1"
|
||||||
Margin="0 0 0 8"
|
Margin="0 0 0 8"
|
||||||
IsReadOnly="True"
|
IsReadOnly="True"
|
||||||
Text="{Binding SelectedPathOperation.Response.Value.Username}" />
|
Text="{Binding SelectedPathOperation.LcuRequest.Value.ResponseUsername}" />
|
||||||
<TextBlock FontSize="12"
|
<TextBlock FontSize="12"
|
||||||
Grid.Row="1"
|
Grid.Row="1"
|
||||||
Grid.Column="0"
|
Grid.Column="0"
|
||||||
@@ -201,7 +201,7 @@
|
|||||||
Grid.Column="1"
|
Grid.Column="1"
|
||||||
Margin="0 0 0 8"
|
Margin="0 0 0 8"
|
||||||
IsReadOnly="True"
|
IsReadOnly="True"
|
||||||
Text="{Binding SelectedPathOperation.Response.Value.Password}"/>
|
Text="{Binding SelectedPathOperation.LcuRequest.Value.ResponsePassword}"/>
|
||||||
<TextBlock FontSize="12"
|
<TextBlock FontSize="12"
|
||||||
Grid.Row="2"
|
Grid.Row="2"
|
||||||
Grid.Column="0"
|
Grid.Column="0"
|
||||||
@@ -212,7 +212,7 @@
|
|||||||
Grid.Row="2"
|
Grid.Row="2"
|
||||||
Grid.Column="1"
|
Grid.Column="1"
|
||||||
IsReadOnly="True"
|
IsReadOnly="True"
|
||||||
Text="{Binding SelectedPathOperation.Response.Value.Authorization}"/>
|
Text="{Binding SelectedPathOperation.LcuRequest.Value.ResponseAuthorization}"/>
|
||||||
</Grid>
|
</Grid>
|
||||||
</TabItem>
|
</TabItem>
|
||||||
<TabItem Header="Schemas">
|
<TabItem Header="Schemas">
|
||||||
@@ -304,7 +304,7 @@
|
|||||||
FontSize="10"
|
FontSize="10"
|
||||||
Padding="12 4 12 4"
|
Padding="12 4 12 4"
|
||||||
Classes="Flat"
|
Classes="Flat"
|
||||||
Content="{Binding SelectedPathOperation.Response.Value.Status}"/>
|
Content="{Binding SelectedPathOperation.LcuRequest.Value.ResponseStatus}"/>
|
||||||
|
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
<Grid Grid.Row="1" Grid.Column="4">
|
<Grid Grid.Row="1" Grid.Column="4">
|
||||||
|
|||||||
@@ -2,18 +2,18 @@ using Avalonia;
|
|||||||
using Avalonia.Controls;
|
using Avalonia.Controls;
|
||||||
using Avalonia.Styling;
|
using Avalonia.Styling;
|
||||||
using AvaloniaEdit;
|
using AvaloniaEdit;
|
||||||
using CommunityToolkit.Mvvm.Messaging;
|
|
||||||
using Needlework.Net.Extensions;
|
using Needlework.Net.Extensions;
|
||||||
using Needlework.Net.Messages;
|
|
||||||
using Needlework.Net.ViewModels.Pages.Endpoints;
|
using Needlework.Net.ViewModels.Pages.Endpoints;
|
||||||
|
using Needlework.Net.ViewModels.Shared;
|
||||||
using TextMateSharp.Grammars;
|
using TextMateSharp.Grammars;
|
||||||
|
|
||||||
namespace Needlework.Net.Views.Pages;
|
namespace Needlework.Net.Views.Pages;
|
||||||
|
|
||||||
public partial class EndpointView : UserControl, IRecipient<EditorUpdateMessage>, IRecipient<ContentRequestMessage>
|
public partial class EndpointView : UserControl
|
||||||
{
|
{
|
||||||
private TextEditor? _requestEditor;
|
private TextEditor? _requestEditor;
|
||||||
private TextEditor? _responseEditor;
|
private TextEditor? _responseEditor;
|
||||||
|
private LcuRequestViewModel? _lcuRequestVm;
|
||||||
|
|
||||||
public EndpointView()
|
public EndpointView()
|
||||||
{
|
{
|
||||||
@@ -24,23 +24,54 @@ public partial class EndpointView : UserControl, IRecipient<EditorUpdateMessage>
|
|||||||
{
|
{
|
||||||
base.OnAttachedToVisualTree(e);
|
base.OnAttachedToVisualTree(e);
|
||||||
|
|
||||||
var vm = (EndpointViewModel)DataContext!;
|
|
||||||
_requestEditor = this.FindControl<TextEditor>("EndpointRequestEditor");
|
_requestEditor = this.FindControl<TextEditor>("EndpointRequestEditor");
|
||||||
_responseEditor = this.FindControl<TextEditor>("EndpointResponseEditor");
|
_responseEditor = this.FindControl<TextEditor>("EndpointResponseEditor");
|
||||||
_requestEditor?.ApplyJsonEditorSettings();
|
_requestEditor?.ApplyJsonEditorSettings();
|
||||||
_responseEditor?.ApplyJsonEditorSettings();
|
_responseEditor?.ApplyJsonEditorSettings();
|
||||||
|
|
||||||
WeakReferenceMessenger.Default.Register<EditorUpdateMessage>(this);
|
var vm = (EndpointViewModel)DataContext!;
|
||||||
WeakReferenceMessenger.Default.Register<ContentRequestMessage, string>(this, "EndpointRequestEditor");
|
vm.PathOperationSelected += Vm_PathOperationSelected;
|
||||||
|
|
||||||
|
if (vm.SelectedPathOperation != null)
|
||||||
|
{
|
||||||
|
_lcuRequestVm = vm.SelectedPathOperation.LcuRequest.Value;
|
||||||
|
vm.SelectedPathOperation.LcuRequest.Value.RequestText += LcuRequest_RequestText;
|
||||||
|
vm.SelectedPathOperation.LcuRequest.Value.UpdateText += LcuRequest_UpdateText;
|
||||||
|
}
|
||||||
|
|
||||||
OnBaseThemeChanged(Application.Current!.ActualThemeVariant);
|
OnBaseThemeChanged(Application.Current!.ActualThemeVariant);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void Vm_PathOperationSelected(object? sender, string e)
|
||||||
|
{
|
||||||
|
var vm = (EndpointViewModel)DataContext!;
|
||||||
|
if (vm.SelectedPathOperation != null)
|
||||||
|
{
|
||||||
|
_requestEditor!.Text = e;
|
||||||
|
if (_lcuRequestVm != null)
|
||||||
|
{
|
||||||
|
_lcuRequestVm.RequestText -= LcuRequest_RequestText;
|
||||||
|
_lcuRequestVm.UpdateText -= LcuRequest_UpdateText;
|
||||||
|
}
|
||||||
|
vm.SelectedPathOperation.LcuRequest.Value.RequestText += LcuRequest_RequestText;
|
||||||
|
vm.SelectedPathOperation.LcuRequest.Value.UpdateText += LcuRequest_UpdateText;
|
||||||
|
_lcuRequestVm = vm.SelectedPathOperation.LcuRequest.Value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
|
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
|
||||||
{
|
{
|
||||||
base.OnDetachedFromVisualTree(e);
|
base.OnDetachedFromVisualTree(e);
|
||||||
|
|
||||||
WeakReferenceMessenger.Default.UnregisterAll(this);
|
var vm = (EndpointViewModel)DataContext!;
|
||||||
|
vm.PathOperationSelected -= Vm_PathOperationSelected;
|
||||||
|
|
||||||
|
if (_lcuRequestVm != null)
|
||||||
|
{
|
||||||
|
_lcuRequestVm.RequestText -= LcuRequest_RequestText;
|
||||||
|
_lcuRequestVm.UpdateText -= LcuRequest_UpdateText;
|
||||||
|
_lcuRequestVm = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnBaseThemeChanged(ThemeVariant currentTheme)
|
private void OnBaseThemeChanged(ThemeVariant currentTheme)
|
||||||
@@ -49,23 +80,14 @@ public partial class EndpointView : UserControl, IRecipient<EditorUpdateMessage>
|
|||||||
currentTheme == ThemeVariant.Dark ? ThemeName.DarkPlus : ThemeName.LightPlus);
|
currentTheme == ThemeVariant.Dark ? ThemeName.DarkPlus : ThemeName.LightPlus);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Receive(EditorUpdateMessage message)
|
private void LcuRequest_RequestText(object? sender, LcuRequestViewModel e)
|
||||||
{
|
{
|
||||||
switch (message.Value.Key)
|
e.RequestBody = _requestEditor!.Text;
|
||||||
{
|
|
||||||
case "EndpointRequestEditor":
|
|
||||||
_requestEditor!.Text = message.Value.Text;
|
|
||||||
break;
|
|
||||||
case "EndpointResponseEditor":
|
|
||||||
_responseEditor!.Text = message.Value.Text;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Receive(ContentRequestMessage message)
|
private void LcuRequest_UpdateText(object? sender, string e)
|
||||||
{
|
{
|
||||||
message.Reply(_requestEditor!.Text);
|
_responseEditor!.Text = e;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user