using System.Collections.Generic; using System.IO; using System.Linq; using System.Reactive.Linq; using System.Text.Json; using System.Windows.Input; using Avalonia.Media; using ReactiveUI; using VaultSmpInstaller.Data; namespace VaultSmpInstaller.ViewModels; public class MainWindowViewModel : ReactiveObject { public static Brush Background => SolidColorBrush.Parse("#282A36"); public static Brush SecondaryBackground => SolidColorBrush.Parse("#44475A"); public static Brush ButtonBackground => SolidColorBrush.Parse("#6272A4"); public static Brush TextColor => SolidColorBrush.Parse("#F8F8F2"); public Interaction ShowProfileSelectionDialog { get; } public ICommand SelectProfileCommand { get; } public MainWindowViewModel() { ShowProfileSelectionDialog = new Interaction(); SelectProfileCommand = ReactiveCommand.CreateFromTask(async () => { var profileWindowModel = new ProfileWindow1ViewModel(); SelectedInstance = await ShowProfileSelectionDialog.Handle(profileWindowModel); this.RaisePropertyChanged(nameof(SelectedInstanceName)); this.RaisePropertyChanged(nameof(SelectedInstance)); this.RaisePropertyChanged(nameof(IsInstanceSelected)); if (SelectedInstance != null) { if (File.Exists(Path.Combine(SelectedInstance.InstancePath, "installedInstance.json"))) { await using FileStream fs = new FileStream(Path.Combine(SelectedInstance.InstancePath, "installedInstance.json"), FileMode.Open); InstalledInstanceConfig = await JsonSerializer.DeserializeAsync(fs, JsonContext.Default.InstanceConfig); if (InstalledInstanceConfig != null) { InstalledInstanceConfig.InstancePath = SelectedInstance.InstancePath; InstalledVersion = InstalledInstanceConfig.Version; } } else { InstalledInstanceConfig = null; InstalledVersion = "None"; } this.RaisePropertyChanged(nameof(InstalledInstanceConfig)); this.RaisePropertyChanged(nameof(InstalledVersion)); this.RaisePropertyChanged(nameof(InstalledModProfiles)); this.RaisePropertyChanged(nameof(InstalledModProfileNames)); } }); } public InstanceConfig? InstalledInstanceConfig { get; set; } public InstanceConfig? LatestInstanceConfig { get; set; } public Dictionary EnabledModProfiles => LatestInstanceConfig?.ModProfiles.Where(pair => pair.Value.IsEnabled).ToDictionary() ?? new Dictionary(); public List EnabledModProfileNames => EnabledModProfiles.Keys.ToList(); public Dictionary DisabledModProfiles => LatestInstanceConfig?.ModProfiles.Where(pair => !pair.Value.IsEnabled).ToDictionary() ?? new Dictionary(); public List DisabledModProfileNames => DisabledModProfiles.Keys.ToList(); public Dictionary InstalledModProfiles => InstalledInstanceConfig?.ModProfiles.Where(pair => pair.Value.IsEnabled).ToDictionary() ?? new Dictionary(); public List InstalledModProfileNames => InstalledModProfiles.Keys.ToList(); public string SelectedInstanceName => SelectedInstance == null ? "No profile selected" : $"Selected Profile: {SelectedInstance.InstanceName}"; public ProfileWindow2ViewModel.InstanceInfo? SelectedInstance { get; set; } private string _installedVersion = "None"; public string InstalledVersion { set { _installedVersion = value; this.RaisePropertyChanged(nameof(IsLatestVersionText)); this.RaisePropertyChanged(); } get => _installedVersion; } private string _latestVersion = "Downloading"; public string LatestVersion { set { _latestVersion = value; this.RaisePropertyChanged(nameof(IsLatestVersionText)); this.RaisePropertyChanged(); } get => _latestVersion; } public string IsLatestVersionText => InstalledVersion == LatestVersion ? "the latest version!" : "an outdated version."; public bool IsInstanceSelected => SelectedInstance != null; }