VaultSmpInstaller/ViewModels/MainWindowViewModel.cs

111 lines
4.8 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net.Http;
using System.Reactive.Linq;
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;
using Avalonia.Media;
using ReactiveUI;
using VaultSmpInstaller.Data;
namespace VaultSmpInstaller.ViewModels;
public class MainWindowViewModel : ViewModelBase
{
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<ProfileWindow1ViewModel, ProfileWindow2ViewModel.InstanceInfo?> ShowProfileSelectionDialog { get; }
public ICommand SelectProfileCommand { get; }
public MainWindowViewModel()
{
ShowProfileSelectionDialog = new Interaction<ProfileWindow1ViewModel, ProfileWindow2ViewModel.InstanceInfo?>();
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<InstanceConfig>(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; } = null;
public InstanceConfig? LatestInstanceConfig { get; set; } = null;
public Dictionary<string, ModProfile> EnabledModProfiles => LatestInstanceConfig?.ModProfiles.Where(pair => pair.Value.IsEnabled).ToDictionary() ?? new Dictionary<string, ModProfile>();
public List<string> EnabledModProfileNames => EnabledModProfiles.Keys.ToList();
public Dictionary<string, ModProfile> DisabledModProfiles => LatestInstanceConfig?.ModProfiles.Where(pair => !pair.Value.IsEnabled).ToDictionary() ?? new Dictionary<string, ModProfile>();
public List<string> DisabledModProfileNames => DisabledModProfiles.Keys.ToList();
public Dictionary<string, ModProfile> InstalledModProfiles => InstalledInstanceConfig?.ModProfiles.Where(pair => pair.Value.IsEnabled).ToDictionary() ?? new Dictionary<string, ModProfile>();
public List<string> 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;
}