using Avalonia.Collections; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Messaging; using Needlework.Net.Desktop.Messages; using SukiUI.Controls; using System; using System.Linq; using System.Net.Http; namespace Needlework.Net.Desktop.ViewModels { public partial class EndpointsViewModel : ObservableObject, IRecipient, ISukiStackPageTitleProvider { public HttpClient HttpClient { get; } public string Title => "Endpoints"; public Action OnClicked; [ObservableProperty] private IAvaloniaReadOnlyList _plugins = new AvaloniaList(); [ObservableProperty] private bool _isBusy = true; [ObservableProperty] private string _search = string.Empty; [ObservableProperty] private IAvaloniaReadOnlyList _query = new AvaloniaList(); [ObservableProperty] private string? _selectedQuery = string.Empty; public EndpointsViewModel(HttpClient httpClient, Action onClicked) { HttpClient = httpClient; OnClicked = onClicked; WeakReferenceMessenger.Default.Register(this); } public void Receive(DataReadyMessage message) { IsBusy = false; Plugins = new AvaloniaList([.. message.Value.Plugins.Keys]); Query = new AvaloniaList([.. Plugins]); } partial void OnSearchChanged(string value) { if (!string.IsNullOrEmpty(Search)) Query = new AvaloniaList(Plugins.Where(x => x.Contains(value))); else Query = Plugins; } partial void OnSelectedQueryChanged(string? value) { if (string.IsNullOrEmpty(value)) return; OnClicked.Invoke(new EndpointViewModel(value)); } } }