Browse Source

Launch options save / load to user settings

pull/5/head
Ionite 1 year ago
parent
commit
e5188a4fa9
No known key found for this signature in database
  1. 15
      StabilityMatrix/Helper/DialogFactory.cs
  2. 2
      StabilityMatrix/Helper/IDialogFactory.cs
  3. 6
      StabilityMatrix/Helper/ISettingsManager.cs
  4. 19
      StabilityMatrix/Helper/SettingsManager.cs
  5. 2
      StabilityMatrix/LaunchOptionsDialog.xaml
  6. 6
      StabilityMatrix/LaunchOptionsDialog.xaml.cs
  7. 3
      StabilityMatrix/Models/InstalledPackage.cs
  8. 10
      StabilityMatrix/Models/LaunchOption.cs
  9. 11
      StabilityMatrix/Models/LaunchOptionCard.cs
  10. 50
      StabilityMatrix/ViewModels/LaunchOptionsDialogViewModel.cs
  11. 17
      StabilityMatrix/ViewModels/LaunchViewModel.cs

15
StabilityMatrix/Helper/DialogFactory.cs

@ -9,18 +9,25 @@ public class DialogFactory : IDialogFactory
{ {
private readonly IContentDialogService contentDialogService; private readonly IContentDialogService contentDialogService;
private readonly LaunchOptionsDialogViewModel launchOptionsDialogViewModel; private readonly LaunchOptionsDialogViewModel launchOptionsDialogViewModel;
private readonly LaunchViewModel launchViewModel; private readonly ISettingsManager settingsManager;
private readonly IPackageFactory packageFactory;
public DialogFactory(IContentDialogService contentDialogService, LaunchOptionsDialogViewModel launchOptionsDialogViewModel) public DialogFactory(IContentDialogService contentDialogService, LaunchOptionsDialogViewModel launchOptionsDialogViewModel, ISettingsManager settingsManager)
{ {
this.contentDialogService = contentDialogService; this.contentDialogService = contentDialogService;
this.launchOptionsDialogViewModel = launchOptionsDialogViewModel; this.launchOptionsDialogViewModel = launchOptionsDialogViewModel;
this.settingsManager = settingsManager;
} }
public LaunchOptionsDialog CreateLaunchOptionsDialog(BasePackage selectedPackage) public LaunchOptionsDialog CreateLaunchOptionsDialog(BasePackage selectedPackage, InstalledPackage installedPackage)
{ {
var definitions = selectedPackage.LaunchOptions;
launchOptionsDialogViewModel.SelectedPackage = selectedPackage; launchOptionsDialogViewModel.SelectedPackage = selectedPackage;
launchOptionsDialogViewModel.Cards.Clear();
// Create cards
launchOptionsDialogViewModel.CardsFromDefinitions(definitions);
// Load user settings
var userLaunchArgs = settingsManager.GetLaunchArgs(installedPackage.Id);
launchOptionsDialogViewModel.LoadFromLaunchArgs(userLaunchArgs);
return new LaunchOptionsDialog(contentDialogService, launchOptionsDialogViewModel); return new LaunchOptionsDialog(contentDialogService, launchOptionsDialogViewModel);
} }

2
StabilityMatrix/Helper/IDialogFactory.cs

@ -4,5 +4,5 @@ namespace StabilityMatrix.Helper;
public interface IDialogFactory public interface IDialogFactory
{ {
LaunchOptionsDialog CreateLaunchOptionsDialog(BasePackage selectedPackage); LaunchOptionsDialog CreateLaunchOptionsDialog(BasePackage selectedPackage, InstalledPackage installedPackage);
} }

6
StabilityMatrix/Helper/ISettingsManager.cs

@ -1,4 +1,6 @@
using StabilityMatrix.Models; using System;
using System.Collections.Generic;
using StabilityMatrix.Models;
namespace StabilityMatrix.Helper; namespace StabilityMatrix.Helper;
@ -12,4 +14,6 @@ public interface ISettingsManager
void SetHasInstalledVenv(bool hasInstalledVenv); void SetHasInstalledVenv(bool hasInstalledVenv);
void SetNavExpanded(bool navExpanded); void SetNavExpanded(bool navExpanded);
void UpdatePackageVersionNumber(string packageName, string newVersion); void UpdatePackageVersionNumber(string packageName, string newVersion);
List<string> GetLaunchArgs(Guid packageId);
void SaveLaunchArgs(Guid packageId, List<string> launchArgs);
} }

19
StabilityMatrix/Helper/SettingsManager.cs

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text.Json; using System.Text.Json;
@ -87,6 +88,24 @@ public class SettingsManager : ISettingsManager
SaveSettings(); SaveSettings();
} }
public List<string> GetLaunchArgs(Guid packageId)
{
var packageData = Settings.InstalledPackages.FirstOrDefault(x => x.Id == packageId);
return packageData?.LaunchArgs ?? new List<string>();
}
public void SaveLaunchArgs(Guid packageId, List<string> launchArgs)
{
var packageData = Settings.InstalledPackages.FirstOrDefault(x => x.Id == packageId);
if (packageData == null)
{
return;
}
packageData.LaunchArgs = launchArgs;
SaveSettings();
}
private void LoadSettings() private void LoadSettings()
{ {
var settingsContent = File.ReadAllText(SettingsPath); var settingsContent = File.ReadAllText(SettingsPath);

2
StabilityMatrix/LaunchOptionsDialog.xaml

@ -38,7 +38,7 @@
<ItemsControl ItemsSource="{Binding Options}"> <ItemsControl ItemsSource="{Binding Options}">
<ItemsControl.ItemTemplate> <ItemsControl.ItemTemplate>
<DataTemplate> <DataTemplate>
<CheckBox Content="{Binding}" /> <CheckBox Content="{Binding Name}" IsChecked="{Binding Selected}" />
</DataTemplate> </DataTemplate>
</ItemsControl.ItemTemplate> </ItemsControl.ItemTemplate>
</ItemsControl> </ItemsControl>

6
StabilityMatrix/LaunchOptionsDialog.xaml.cs

@ -1,5 +1,7 @@
using System.Windows; using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Documents;
using StabilityMatrix.ViewModels; using StabilityMatrix.ViewModels;
using Wpf.Ui.Contracts; using Wpf.Ui.Contracts;
using Wpf.Ui.Controls.ContentDialogControl; using Wpf.Ui.Controls.ContentDialogControl;
@ -10,6 +12,8 @@ public partial class LaunchOptionsDialog : ContentDialog
{ {
private readonly LaunchOptionsDialogViewModel viewModel; private readonly LaunchOptionsDialogViewModel viewModel;
public List<string> AsLaunchArgs() => viewModel.AsLaunchArgs();
public LaunchOptionsDialog(IContentDialogService dialogService, LaunchOptionsDialogViewModel viewModel) : base( public LaunchOptionsDialog(IContentDialogService dialogService, LaunchOptionsDialogViewModel viewModel) : base(
dialogService.GetContentPresenter()) dialogService.GetContentPresenter())
{ {

3
StabilityMatrix/Models/InstalledPackage.cs

@ -1,6 +1,5 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Windows.Documents;
namespace StabilityMatrix.Models; namespace StabilityMatrix.Models;
@ -20,5 +19,5 @@ public class InstalledPackage
// Install path // Install path
public string? Path { get; set; } public string? Path { get; set; }
public string? LaunchCommand { get; set; } public string? LaunchCommand { get; set; }
public HashSet<string>? LaunchArgs { get; set; } public List<string>? LaunchArgs { get; set; }
} }

10
StabilityMatrix/Models/LaunchOption.cs

@ -0,0 +1,10 @@
using CommunityToolkit.Mvvm.ComponentModel;
namespace StabilityMatrix.Models;
public partial class LaunchOption : ObservableObject
{
public string Name { get; set; }
[ObservableProperty] private bool selected = false;
}

11
StabilityMatrix/Models/LaunchOptionCard.cs

@ -1,5 +1,8 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Documents; using System.Windows.Documents;
using ABI.Windows.Data.Xml.Dom;
using StabilityMatrix.Helper;
namespace StabilityMatrix.Models; namespace StabilityMatrix.Models;
@ -7,11 +10,15 @@ public class LaunchOptionCard
{ {
public string Title { get; set; } public string Title { get; set; }
public string? Description { get; set; } public string? Description { get; set; }
public List<string> Options { get; set; } public ObservableCollection<LaunchOption> Options { get; set; } = new();
public LaunchOptionCard(LaunchOptionDefinition definition) public LaunchOptionCard(LaunchOptionDefinition definition)
{ {
Title = definition.Name; Title = definition.Name;
Options = definition.Options; foreach (var optionName in definition.Options)
{
var option = new LaunchOption {Name = optionName};
Options.Add(option);
}
} }
} }

50
StabilityMatrix/ViewModels/LaunchOptionsDialogViewModel.cs

@ -1,8 +1,9 @@
using System; using System;
using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Diagnostics; using System.Diagnostics;
using System.Linq;
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel;
using StabilityMatrix.Helper;
using StabilityMatrix.Models; using StabilityMatrix.Models;
namespace StabilityMatrix.ViewModels; namespace StabilityMatrix.ViewModels;
@ -14,29 +15,44 @@ public partial class LaunchOptionsDialogViewModel : ObservableObject
[ObservableProperty] [ObservableProperty]
private BasePackage? selectedPackage; private BasePackage? selectedPackage;
public void OnLoad() /// <summary>
/// Export the current cards options to a list of strings
/// </summary>
public List<string> AsLaunchArgs()
{ {
Debug.WriteLine("In LaunchOptions OnLoad"); return (
// Populate Cards using the selected package from card in Cards from option in card.Options
Cards.Clear(); where option.Selected select option.Name).ToList();
}
var package = SelectedPackage; /// <summary>
if (package == null) /// Create cards using definitions
/// </summary>
public void CardsFromDefinitions(List<LaunchOptionDefinition> definitions)
{ {
Debug.WriteLine($"selectedPackage is null"); foreach (var definition in definitions)
return;
}
var definitions = package.LaunchOptions;
if (definitions == null)
{ {
Debug.WriteLine($"definitions is null"); Cards.Add(new LaunchOptionCard(definition));
return; }
} }
foreach (var definition in definitions) /// <summary>
/// Import the current cards options from a list of strings
/// </summary>
public void LoadFromLaunchArgs(IEnumerable<string> launchArgs)
{ {
Cards.Add(new LaunchOptionCard(definition)); var launchArgsSet = new HashSet<string>(launchArgs);
foreach (var card in Cards)
{
foreach (var option in card.Options)
{
option.Selected = launchArgsSet.Contains(option.Name);
}
}
} }
Debug.WriteLine($"Cards: {Cards.Count}");
public void OnLoad()
{
Debug.WriteLine("In LaunchOptions OnLoad");
} }
} }

17
StabilityMatrix/ViewModels/LaunchViewModel.cs

@ -12,6 +12,7 @@ using Microsoft.Toolkit.Uwp.Notifications;
using StabilityMatrix.Helper; using StabilityMatrix.Helper;
using StabilityMatrix.Models; using StabilityMatrix.Models;
using Wpf.Ui.Contracts; using Wpf.Ui.Contracts;
using Wpf.Ui.Controls.ContentDialogControl;
namespace StabilityMatrix.ViewModels; namespace StabilityMatrix.ViewModels;
@ -120,8 +121,9 @@ public partial class LaunchViewModel : ObservableObject
[RelayCommand] [RelayCommand]
public async Task ConfigAsync() public async Task ConfigAsync()
{ {
var name = SelectedPackage?.Name; var activeInstall = SelectedPackage;
if (name == null) var name = activeInstall?.Name;
if (name == null || activeInstall == null)
{ {
logger.LogWarning($"Selected package is null"); logger.LogWarning($"Selected package is null");
return; return;
@ -135,11 +137,18 @@ public partial class LaunchViewModel : ObservableObject
} }
// Open a config page // Open a config page
var dialog = dialogFactory.CreateLaunchOptionsDialog(package); var dialog = dialogFactory.CreateLaunchOptionsDialog(package, activeInstall);
dialog.IsPrimaryButtonEnabled = true; dialog.IsPrimaryButtonEnabled = true;
dialog.PrimaryButtonText = "Save"; dialog.PrimaryButtonText = "Save";
dialog.CloseButtonText = "Cancel"; dialog.CloseButtonText = "Cancel";
await dialog.ShowAsync(); var result = await dialog.ShowAsync();
if (result == ContentDialogResult.Primary)
{
// Save config
var args = dialog.AsLaunchArgs();
settingsManager.SaveLaunchArgs(activeInstall.Id, args);
}
} }
private void RunningPackageOnStartupComplete(object? sender, string url) private void RunningPackageOnStartupComplete(object? sender, string url)

Loading…
Cancel
Save