Browse Source

fix some process exit stuff

pull/629/head
JT 9 months ago
parent
commit
91fb39f366
  1. 2
      StabilityMatrix.Avalonia/Services/RunningPackageService.cs
  2. 60
      StabilityMatrix.Avalonia/ViewModels/PackageManager/PackageCardViewModel.cs
  3. 21
      StabilityMatrix.Avalonia/ViewModels/RunningPackageViewModel.cs

2
StabilityMatrix.Avalonia/Services/RunningPackageService.cs

@ -27,6 +27,7 @@ public partial class RunningPackageService(
IPyRunner pyRunner
) : ObservableObject
{
// 🤔 what if we put the ConsoleViewModel inside the BasePackage? 🤔
[ObservableProperty]
private ObservableDictionary<Guid, RunningPackageViewModel> runningPackages = [];
@ -115,6 +116,7 @@ public partial class RunningPackageService(
var viewModel = new RunningPackageViewModel(
settingsManager,
notificationService,
this,
runningPackage,
console
);

60
StabilityMatrix.Avalonia/ViewModels/PackageManager/PackageCardViewModel.cs

@ -37,7 +37,15 @@ namespace StabilityMatrix.Avalonia.ViewModels.PackageManager;
[ManagedService]
[Transient]
public partial class PackageCardViewModel : ProgressViewModel
public partial class PackageCardViewModel(
ILogger<PackageCardViewModel> logger,
IPackageFactory packageFactory,
INotificationService notificationService,
ISettingsManager settingsManager,
INavigationService<NewPackageManagerViewModel> navigationService,
ServiceManager<ViewModelBase> vmFactory,
RunningPackageService runningPackageService
) : ProgressViewModel
{
private string webUiUrl = string.Empty;
@ -86,36 +94,6 @@ public partial class PackageCardViewModel : ProgressViewModel
[ObservableProperty]
private bool showWebUiButton;
private readonly ILogger<PackageCardViewModel> logger;
private readonly IPackageFactory packageFactory;
private readonly INotificationService notificationService;
private readonly ISettingsManager settingsManager;
private readonly INavigationService<NewPackageManagerViewModel> navigationService;
private readonly ServiceManager<ViewModelBase> vmFactory;
private readonly RunningPackageService runningPackageService;
/// <inheritdoc/>
public PackageCardViewModel(
ILogger<PackageCardViewModel> logger,
IPackageFactory packageFactory,
INotificationService notificationService,
ISettingsManager settingsManager,
INavigationService<NewPackageManagerViewModel> navigationService,
ServiceManager<ViewModelBase> vmFactory,
RunningPackageService runningPackageService
)
{
this.logger = logger;
this.packageFactory = packageFactory;
this.notificationService = notificationService;
this.settingsManager = settingsManager;
this.navigationService = navigationService;
this.vmFactory = vmFactory;
this.runningPackageService = runningPackageService;
runningPackageService.RunningPackages.CollectionChanged += RunningPackagesOnCollectionChanged;
}
private void RunningPackagesOnCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
if (
@ -167,6 +145,9 @@ public partial class PackageCardViewModel : ProgressViewModel
UseSharedOutput = Package?.UseSharedOutputFolder ?? false;
CanUseSharedOutput = basePackage?.SharedOutputFolders != null;
CanUseExtensions = basePackage?.SupportsExtensions ?? false;
runningPackageService.RunningPackages.CollectionChanged += RunningPackagesOnCollectionChanged;
EventManager.Instance.PackageRelaunchRequested += InstanceOnPackageRelaunchRequested;
}
}
@ -190,8 +171,6 @@ public partial class PackageCardViewModel : ProgressViewModel
if (Design.IsDesignMode || !settingsManager.IsLibraryDirSet || Package is not { } currentPackage)
return;
EventManager.Instance.PackageRelaunchRequested += InstanceOnPackageRelaunchRequested;
if (
packageFactory.FindPackageByName(currentPackage.PackageName)
is { } basePackage
@ -217,12 +196,27 @@ public partial class PackageCardViewModel : ProgressViewModel
}
IsUpdateAvailable = await HasUpdate();
if (
Package != null
&& !IsRunning
&& runningPackageService.RunningPackages.TryGetValue(Package.Id, out var runningPackageVm)
)
{
IsRunning = true;
runningPackageVm.RunningPackage.BasePackage.Exited += BasePackageOnExited;
runningPackageVm.RunningPackage.BasePackage.StartupComplete +=
RunningPackageOnStartupComplete;
webUiUrl = runningPackageVm.WebUiUrl;
ShowWebUiButton = !string.IsNullOrWhiteSpace(webUiUrl);
}
}
}
public override void OnUnloaded()
{
EventManager.Instance.PackageRelaunchRequested -= InstanceOnPackageRelaunchRequested;
runningPackageService.RunningPackages.CollectionChanged -= RunningPackagesOnCollectionChanged;
}
public async Task Launch()

21
StabilityMatrix.Avalonia/ViewModels/RunningPackageViewModel.cs

@ -20,7 +20,7 @@ namespace StabilityMatrix.Avalonia.ViewModels;
public partial class RunningPackageViewModel : PageViewModelBase, IDisposable, IAsyncDisposable
{
private readonly INotificationService notificationService;
private string webUiUrl = string.Empty;
private readonly RunningPackageService runningPackageService;
public PackagePair RunningPackage { get; }
public ConsoleViewModel Console { get; }
@ -33,6 +33,9 @@ public partial class RunningPackageViewModel : PageViewModelBase, IDisposable, I
[ObservableProperty]
private bool showWebUiButton;
[ObservableProperty]
private string webUiUrl = string.Empty;
[ObservableProperty]
private bool isRunning = true;
@ -40,11 +43,14 @@ public partial class RunningPackageViewModel : PageViewModelBase, IDisposable, I
public RunningPackageViewModel(
ISettingsManager settingsManager,
INotificationService notificationService,
RunningPackageService runningPackageService,
PackagePair runningPackage,
ConsoleViewModel console
)
{
this.notificationService = notificationService;
this.runningPackageService = runningPackageService;
RunningPackage = runningPackage;
Console = console;
Console.Document.LineCountChanged += DocumentOnLineCountChanged;
@ -66,12 +72,13 @@ public partial class RunningPackageViewModel : PageViewModelBase, IDisposable, I
Console.Document.LineCountChanged -= DocumentOnLineCountChanged;
RunningPackage.BasePackage.StartupComplete -= BasePackageOnStartupComplete;
RunningPackage.BasePackage.Exited -= BasePackageOnExited;
runningPackageService.RunningPackages.Remove(RunningPackage.InstalledPackage.Id);
}
private void BasePackageOnStartupComplete(object? sender, string url)
{
webUiUrl = url.Replace("0.0.0.0", "127.0.0.1");
ShowWebUiButton = !string.IsNullOrWhiteSpace(webUiUrl);
WebUiUrl = url.Replace("0.0.0.0", "127.0.0.1");
ShowWebUiButton = !string.IsNullOrWhiteSpace(WebUiUrl);
}
private void DocumentOnLineCountChanged(object? sender, EventArgs e)
@ -91,7 +98,7 @@ public partial class RunningPackageViewModel : PageViewModelBase, IDisposable, I
[RelayCommand]
private async Task Stop()
{
await RunningPackage.BasePackage.WaitForShutdown();
await runningPackageService.StopPackage(RunningPackage.InstalledPackage.Id);
Console.PostLine($"{Environment.NewLine}Stopped process at {DateTimeOffset.Now}");
await Console.StopUpdatesAsync();
IsRunning = false;
@ -100,13 +107,13 @@ public partial class RunningPackageViewModel : PageViewModelBase, IDisposable, I
[RelayCommand]
private void LaunchWebUi()
{
if (string.IsNullOrEmpty(webUiUrl))
if (string.IsNullOrEmpty(WebUiUrl))
return;
notificationService.TryAsync(
Task.Run(() => ProcessRunner.OpenUrl(webUiUrl)),
Task.Run(() => ProcessRunner.OpenUrl(WebUiUrl)),
"Failed to open URL",
$"{webUiUrl}"
$"{WebUiUrl}"
);
}

Loading…
Cancel
Save