mirror of
https://github.com/BlossomiShymae/Needlework.Net.git
synced 2025-12-06 18:20:47 +01:00
WIP
This commit is contained in:
9
Needlework.Net.Desktop/ViewModels/AboutViewModel.cs
Normal file
9
Needlework.Net.Desktop/ViewModels/AboutViewModel.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace Needlework.Net.Desktop.ViewModels
|
||||
{
|
||||
public class AboutViewModel : PageBase
|
||||
{
|
||||
public AboutViewModel() : base("About", Material.Icons.MaterialIconKind.InfoCircle)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
75
Needlework.Net.Desktop/ViewModels/ConsoleViewModel.cs
Normal file
75
Needlework.Net.Desktop/ViewModels/ConsoleViewModel.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
using Avalonia.Collections;
|
||||
using BlossomiShymae.GrrrLCU;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using SukiUI.Controls;
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Needlework.Net.Desktop.ViewModels
|
||||
{
|
||||
public partial class ConsoleViewModel : PageBase
|
||||
{
|
||||
public IAvaloniaReadOnlyList<string> RequestMethods { get; } = new AvaloniaList<string>(["GET", "POST", "PUT", "DELETE", "HEAD", "PATCH", "OPTIONS", "TRACE"]);
|
||||
|
||||
[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? _responseAuthentication = null;
|
||||
|
||||
public event EventHandler<TextUpdatedEventArgs>? ResponseBodyUpdated;
|
||||
|
||||
public ConsoleViewModel() : base("Console", Material.Icons.MaterialIconKind.Console, -100)
|
||||
{
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private async Task SendRequest()
|
||||
{
|
||||
try
|
||||
{
|
||||
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 = Connector.GetProcessInfo();
|
||||
var response = await Connector.SendAsync(method, RequestPath) ?? throw new Exception("Response is null.");
|
||||
var riotAuthentication = new RiotAuthentication(processInfo.RemotingAuthToken);
|
||||
var body = await response.Content.ReadAsStringAsync();
|
||||
|
||||
Avalonia.Threading.Dispatcher.UIThread.Post(() =>
|
||||
{
|
||||
ResponseStatus = response.StatusCode.ToString();
|
||||
ResponsePath = $"https://127.0.0.1/{processInfo.AppPort}{RequestPath}";
|
||||
ResponseAuthentication = riotAuthentication.Value;
|
||||
ResponseBodyUpdated?.Invoke(this, new(body));
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await SukiHost.ShowToast("Request Failed", ex.Message, SukiUI.Enums.NotificationType.Error);
|
||||
Avalonia.Threading.Dispatcher.UIThread.Post(() =>
|
||||
{
|
||||
ResponseStatus = null;
|
||||
ResponsePath = null;
|
||||
ResponseAuthentication = null;
|
||||
ResponseBodyUpdated?.Invoke(this, new(string.Empty));
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
10
Needlework.Net.Desktop/ViewModels/EndpointViewModel.cs
Normal file
10
Needlework.Net.Desktop/ViewModels/EndpointViewModel.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
|
||||
namespace Needlework.Net.Desktop.ViewModels
|
||||
{
|
||||
public partial class EndpointViewModel(string endpoint) : ObservableObject
|
||||
{
|
||||
public string Endpoint { get; } = endpoint;
|
||||
public string Title => $"Needlework.Net - {Endpoint}";
|
||||
}
|
||||
}
|
||||
57
Needlework.Net.Desktop/ViewModels/EndpointsViewModel.cs
Normal file
57
Needlework.Net.Desktop/ViewModels/EndpointsViewModel.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using Needlework.Net.Core;
|
||||
using Needlework.Net.Desktop.Services;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Needlework.Net.Desktop.ViewModels
|
||||
{
|
||||
public partial class EndpointsViewModel : PageBase
|
||||
{
|
||||
public HttpClient HttpClient { get; }
|
||||
|
||||
public DialogService DialogService { get; }
|
||||
|
||||
[ObservableProperty] private List<string> _plugins = [];
|
||||
[ObservableProperty] private bool _isBusy = true;
|
||||
[ObservableProperty] private string _search = string.Empty;
|
||||
[ObservableProperty] private List<string> _query = [];
|
||||
[ObservableProperty] private string? _selectedQuery = string.Empty;
|
||||
|
||||
public EndpointsViewModel(HttpClient httpClient, DialogService dialogService) : base("Endpoints", Material.Icons.MaterialIconKind.Hub, -500)
|
||||
{
|
||||
HttpClient = httpClient;
|
||||
DialogService = dialogService;
|
||||
|
||||
Task.Run(InitializeAsync);
|
||||
}
|
||||
|
||||
private async Task InitializeAsync()
|
||||
{
|
||||
var handler = new LcuSchemaHandler(await Resources.GetOpenApiDocumentAsync(HttpClient));
|
||||
Avalonia.Threading.Dispatcher.UIThread.Post(() =>
|
||||
{
|
||||
Plugins = [.. handler.Plugins.Keys];
|
||||
Query = [.. Plugins];
|
||||
IsBusy = false;
|
||||
});
|
||||
}
|
||||
|
||||
partial void OnSearchChanged(string value)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(Search))
|
||||
Query = Plugins.Where(x => x.Contains(value)).ToList();
|
||||
else
|
||||
Query = Plugins;
|
||||
}
|
||||
|
||||
partial void OnSelectedQueryChanged(string? value)
|
||||
{
|
||||
if (string.IsNullOrEmpty(value))
|
||||
return;
|
||||
DialogService.ShowEndpoint(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
55
Needlework.Net.Desktop/ViewModels/HomeViewModel.cs
Normal file
55
Needlework.Net.Desktop/ViewModels/HomeViewModel.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using Avalonia.Media;
|
||||
using BlossomiShymae.GrrrLCU;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Needlework.Net.Desktop.ViewModels
|
||||
{
|
||||
public partial class HomeViewModel : PageBase
|
||||
{
|
||||
[ObservableProperty] private string _statusText = string.Empty;
|
||||
[ObservableProperty] private IBrush? _statusForeground;
|
||||
[ObservableProperty] private string _statusAddress = string.Empty;
|
||||
|
||||
public HomeViewModel() : base("Home", Material.Icons.MaterialIconKind.Home, int.MinValue)
|
||||
{
|
||||
Task.Run(async () => { while (true) { SetStatus(); await Task.Delay(TimeSpan.FromSeconds(5)); } });
|
||||
}
|
||||
|
||||
private void SetStatus()
|
||||
{
|
||||
void Set(string text, Color color, string address)
|
||||
{
|
||||
StatusText = text;
|
||||
StatusForeground = new SolidColorBrush(color.ToUInt32());
|
||||
StatusAddress = address;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var processInfo = Connector.GetProcessInfo();
|
||||
Avalonia.Threading.Dispatcher.UIThread.Post(() => Set("Online", Colors.Green, $"https://127.0.0.1:{processInfo.AppPort}/"));
|
||||
}
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
Avalonia.Threading.Dispatcher.UIThread.Post(() => Set("Offline", Colors.Red, "N/A"));
|
||||
}
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private void OpenUrl(string url)
|
||||
{
|
||||
var process = new Process()
|
||||
{
|
||||
StartInfo = new ProcessStartInfo(url)
|
||||
{
|
||||
UseShellExecute = true
|
||||
}
|
||||
};
|
||||
process.Start();
|
||||
}
|
||||
}
|
||||
}
|
||||
41
Needlework.Net.Desktop/ViewModels/MainWindowViewModel.cs
Normal file
41
Needlework.Net.Desktop/ViewModels/MainWindowViewModel.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using Avalonia.Collections;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Needlework.Net.Desktop.ViewModels
|
||||
{
|
||||
public partial class MainWindowViewModel : ObservableObject
|
||||
{
|
||||
public IAvaloniaReadOnlyList<PageBase> Pages { get; }
|
||||
|
||||
public string Version { get; } = Assembly.GetEntryAssembly()?.GetName().Version?.ToString() ?? "0.0.0.0";
|
||||
|
||||
public MainWindowViewModel(IEnumerable<PageBase> pages)
|
||||
{
|
||||
Pages = new AvaloniaList<PageBase>(pages.OrderBy(x => x.Index).ThenBy(x => x.DisplayName));
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private void OpenUrl(string url)
|
||||
{
|
||||
var process = new Process()
|
||||
{
|
||||
StartInfo = new ProcessStartInfo(url)
|
||||
{
|
||||
UseShellExecute = true
|
||||
}
|
||||
};
|
||||
process.Start();
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private void OpenConsole()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
13
Needlework.Net.Desktop/ViewModels/PageBase.cs
Normal file
13
Needlework.Net.Desktop/ViewModels/PageBase.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using Material.Icons;
|
||||
|
||||
namespace Needlework.Net.Desktop.ViewModels
|
||||
{
|
||||
|
||||
public abstract partial class PageBase(string displayName, MaterialIconKind icon, int index = 0) : ObservableValidator
|
||||
{
|
||||
[ObservableProperty] private string _displayName = displayName;
|
||||
[ObservableProperty] private MaterialIconKind _icon = icon;
|
||||
[ObservableProperty] private int _index = index;
|
||||
}
|
||||
}
|
||||
7
Needlework.Net.Desktop/ViewModels/PluginViewModel.cs
Normal file
7
Needlework.Net.Desktop/ViewModels/PluginViewModel.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Needlework.Net.Desktop.ViewModels
|
||||
{
|
||||
public class PluginViewModel
|
||||
{
|
||||
public PluginViewModel() { }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user