using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; using Avalonia.Threading; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using FluentAvalonia.UI.Controls; using StabilityMatrix.Avalonia.Animations; using StabilityMatrix.Avalonia.Controls; using StabilityMatrix.Avalonia.Languages; using StabilityMatrix.Avalonia.Services; using StabilityMatrix.Avalonia.ViewModels.Base; using StabilityMatrix.Avalonia.Views; using StabilityMatrix.Avalonia.Views.Dialogs; using StabilityMatrix.Core.Attributes; using StabilityMatrix.Core.Helper; using StabilityMatrix.Core.Helper.Factory; using StabilityMatrix.Core.Models; using StabilityMatrix.Core.Models.Packages; using StabilityMatrix.Core.Services; namespace StabilityMatrix.Avalonia.ViewModels.Dialogs; [View(typeof(InferenceConnectionHelpDialog))] public partial class InferenceConnectionHelpViewModel : ContentDialogViewModelBase { private readonly ISettingsManager settingsManager; private readonly INavigationService navigationService; private readonly IPackageFactory packageFactory; [ObservableProperty] private string title = "Hello"; [ObservableProperty] private IReadOnlyList installedPackages = Array.Empty(); [ObservableProperty] private InstalledPackage? selectedPackage; [ObservableProperty] private bool isFirstTimeWelcome; /// /// When the user has no Comfy packages, and we need to prompt to install /// [ObservableProperty] private bool isInstallMode; /// /// When the user has Comfy packages, and we need to prompt to launch /// [ObservableProperty] private bool isLaunchMode; public InferenceConnectionHelpViewModel( ISettingsManager settingsManager, INavigationService navigationService, IPackageFactory packageFactory ) { this.settingsManager = settingsManager; this.navigationService = navigationService; this.packageFactory = packageFactory; // Get comfy type installed packages var comfyPackages = this.settingsManager.Settings.InstalledPackages .Where(p => p.PackageName == "ComfyUI") .ToImmutableArray(); InstalledPackages = comfyPackages; // If there are none, use install mode if (comfyPackages.Length == 0) { IsInstallMode = true; } // Otherwise launch mode else { SelectedPackage = this.settingsManager.Settings.ActiveInstalledPackage; SelectedPackage ??= comfyPackages[0]; IsLaunchMode = true; } } /// /// Navigate to the package install page /// [RelayCommand] private void NavigateToInstall() { Dispatcher.UIThread.Post(() => { navigationService.NavigateTo( param: new PackageManagerPage.PackageManagerNavigationOptions { OpenInstallerDialog = true, InstallerSelectedPackage = packageFactory .GetAllAvailablePackages() .First(p => p is ComfyUI) } ); }); } /// /// Request launch of the selected package /// [RelayCommand] private void LaunchSelectedPackage() { if (SelectedPackage?.Id is { } id) { Dispatcher.UIThread.Post(() => { EventManager.Instance.OnPackageLaunchRequested(id); }); } } /// /// Create a better content dialog for this view model /// public BetterContentDialog CreateDialog() { var dialog = new BetterContentDialog { Content = new InferenceConnectionHelpDialog { DataContext = this }, PrimaryButtonCommand = IsInstallMode ? NavigateToInstallCommand : LaunchSelectedPackageCommand, PrimaryButtonText = IsInstallMode ? "Install" : "Launch", CloseButtonText = Resources.Action_Close, DefaultButton = ContentDialogButton.Primary }; return dialog; } }