using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; using CommunityToolkit.Mvvm.ComponentModel; using StabilityMatrix.Avalonia.Views.Dialogs; using StabilityMatrix.Core.Attributes; using StabilityMatrix.Core.Helper.Cache; using StabilityMatrix.Core.Models; namespace StabilityMatrix.Avalonia.ViewModels.Dialogs; [View(typeof(LaunchOptionsDialog))] public partial class LaunchOptionsViewModel : ContentDialogViewModelBase { private readonly LRUCache> cache = new(100); [ObservableProperty] private string title = "Launch Options"; [ObservableProperty] private bool isSearchBoxEnabled = true; [ObservableProperty] [NotifyPropertyChangedFor(nameof(FilteredCards))] private string searchText = string.Empty; [ObservableProperty] private IReadOnlyList? filteredCards; public IReadOnlyList? Cards { get; set; } /// /// Return cards that match the search text /// private IReadOnlyList? GetFilteredCards() { var text = SearchText; if (string.IsNullOrWhiteSpace(text) || text.Length < 2) { return Cards; } // Try cache if (cache.Get(text, out var cachedCards)) { return cachedCards!; } var searchCard = new LaunchOptionCard { Title = text.ToLowerInvariant(), Type = LaunchOptionType.Bool, Options = Array.Empty() }; var extracted = FuzzySharp.Process .ExtractTop(searchCard, Cards, c => c.Title.ToLowerInvariant()); var results = extracted .Where(r => r.Score > 40) .Select(r => r.Value) .ToImmutableList(); cache.Add(text, results); return results; } public void UpdateFilterCards() => FilteredCards = GetFilteredCards(); public override void OnLoaded() { base.OnLoaded(); UpdateFilterCards(); } /// /// Export the current cards options to a list of strings /// public List AsLaunchArgs() { var launchArgs = new List(); if (Cards is null) return launchArgs; foreach (var card in Cards) { launchArgs.AddRange(card.Options); } return launchArgs; } }