Files
ZuneModdingHelper/ZuneModCore/ModViewModel.cs
T

120 lines
3.1 KiB
C#
Raw Normal View History

2022-03-03 14:26:05 -06:00
using Microsoft.Toolkit.Mvvm.ComponentModel;
using Microsoft.Toolkit.Mvvm.Input;
using OwlCore.AbstractUI.ViewModels;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace ZuneModCore
{
2022-06-30 00:25:01 -05:00
public class ModViewModel : ObservableObject
2022-03-03 14:26:05 -06:00
{
2022-08-14 15:18:25 -05:00
public ModViewModel()
2022-03-03 14:26:05 -06:00
{
_loadCommand = new AsyncRelayCommand(LoadAsync);
2022-08-14 15:18:25 -05:00
}
2022-03-03 14:26:05 -06:00
2022-08-14 15:18:25 -05:00
public ModViewModel(Mod mod) : this()
{
UpdateMod(mod);
2022-03-03 14:26:05 -06:00
}
private Mod _mod;
private AbstractUICollectionViewModel? _optionsVm;
private IAsyncRelayCommand _loadCommand;
private IAsyncRelayCommand? _actionCommand;
private bool _hasDependencies;
private string _actionButtonText;
2022-08-14 15:18:25 -05:00
private IAsyncRelayCommand _applyCommand;
private IAsyncRelayCommand _resetCommand;
2022-03-03 14:26:05 -06:00
public Mod Mod
{
get => _mod;
set
{
2022-08-14 15:18:25 -05:00
UpdateMod(value);
2022-03-03 14:26:05 -06:00
SetProperty(ref _mod, value);
}
}
public AbstractUICollectionViewModel? OptionsViewModel
{
get => _optionsVm;
2022-08-14 15:18:25 -05:00
set => SetProperty(ref _optionsVm, value);
2022-03-03 14:26:05 -06:00
}
public IAsyncRelayCommand LoadCommand
{
get => _loadCommand;
set => SetProperty(ref _loadCommand, value);
}
public IAsyncRelayCommand? ActionCommand
{
get => _actionCommand;
set => SetProperty(ref _actionCommand, value);
}
public bool HasDependencies
{
get => _hasDependencies;
set => SetProperty(ref _hasDependencies, value);
}
2022-08-14 15:18:25 -05:00
private IReadOnlyList<ModDependency>? _Dependencies;
public IReadOnlyList<ModDependency>? Dependencies
{
get => _Dependencies;
set => SetProperty(ref _Dependencies, value);
}
2022-03-03 14:26:05 -06:00
public string ActionButtonText
{
get => _actionButtonText;
set => SetProperty(ref _actionButtonText, value);
}
public async Task LoadAsync()
{
OnStatusChanged(_mod, _mod.CheckApplied());
}
private void OnStatusChanged(Mod mod, bool status)
{
if (status)
{
ActionButtonText = "Reset";
ActionCommand = _resetCommand;
}
else
{
ActionButtonText = "Apply";
ActionCommand = _applyCommand;
}
}
public void Dispose()
{
Mod.StatusChanged -= OnStatusChanged;
}
2022-08-14 15:18:25 -05:00
private void UpdateMod(Mod mod)
{
if (mod == null) return;
_mod = mod;
_applyCommand = new AsyncRelayCommand(Mod.ApplyWithDependencies);
_resetCommand = new AsyncRelayCommand(Mod.Reset);
Dependencies = Mod.DependentMods;
HasDependencies = Dependencies != null && Dependencies.Count > 0;
if (Mod.OptionsUI != null)
OptionsViewModel = new(Mod.OptionsUI);
Mod.StatusChanged += OnStatusChanged;
}
2022-03-03 14:26:05 -06:00
}
}