Fixed and cleaned up ModViewModel

This commit is contained in:
Yoshi Askharoun
2022-08-14 15:18:25 -05:00
parent 52a60fe3fc
commit 17393effba
3 changed files with 45 additions and 42 deletions
+33 -26
View File
@@ -1,46 +1,39 @@
using Microsoft.Toolkit.Mvvm.ComponentModel;
using Microsoft.Toolkit.Mvvm.Input;
using OwlCore.AbstractUI.Models;
using OwlCore.AbstractUI.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ZuneModCore
{
public class ModViewModel : ObservableObject, IDisposable
{
public ModViewModel(Mod mod)
public ModViewModel()
{
Mod = mod;
_loadCommand = new AsyncRelayCommand(LoadAsync);
_applyCommand = new AsyncRelayCommand(Mod.ApplyWithDependencies);
_resetCommand = new AsyncRelayCommand(Mod.Reset);
}
if (Mod.OptionsUI != null)
OptionsViewModel = new(Mod.OptionsUI);
Mod.StatusChanged += OnStatusChanged;
public ModViewModel(Mod mod) : this()
{
UpdateMod(mod);
}
private Mod _mod;
private AbstractUICollectionViewModel? _optionsVm;
private IAsyncRelayCommand _loadCommand;
private IAsyncRelayCommand? _actionCommand;
private bool _hasOptions;
private bool _hasDependencies;
private string _actionButtonText;
private readonly IAsyncRelayCommand _applyCommand;
private readonly IAsyncRelayCommand _resetCommand;
private IAsyncRelayCommand _applyCommand;
private IAsyncRelayCommand _resetCommand;
public Mod Mod
{
get => _mod;
set
{
HasDependencies = _mod?.DependentMods != null && _mod.DependentMods.Count > 0;
UpdateMod(value);
SetProperty(ref _mod, value);
}
}
@@ -48,11 +41,7 @@ namespace ZuneModCore
public AbstractUICollectionViewModel? OptionsViewModel
{
get => _optionsVm;
set
{
HasOptions = _optionsVm != null;
SetProperty(ref _optionsVm, value);
}
set => SetProperty(ref _optionsVm, value);
}
public IAsyncRelayCommand LoadCommand
@@ -67,18 +56,19 @@ namespace ZuneModCore
set => SetProperty(ref _actionCommand, value);
}
public bool HasOptions
{
get => _hasOptions;
set => SetProperty(ref _hasOptions, value);
}
public bool HasDependencies
{
get => _hasDependencies;
set => SetProperty(ref _hasDependencies, value);
}
private IReadOnlyList<ModDependency>? _Dependencies;
public IReadOnlyList<ModDependency>? Dependencies
{
get => _Dependencies;
set => SetProperty(ref _Dependencies, value);
}
public string ActionButtonText
{
get => _actionButtonText;
@@ -108,5 +98,22 @@ namespace ZuneModCore
{
Mod.StatusChanged -= OnStatusChanged;
}
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;
}
}
}
+10 -6
View File
@@ -23,6 +23,10 @@
</ResourceDictionary>
</Window.Resources>
<Window.DataContext>
<mod:ModViewModel x:Name="ViewModel"/>
</Window.DataContext>
<Grid x:Name="OptionsUIGrid" Grid.RowSpan="2">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
@@ -31,9 +35,9 @@
<ScrollViewer>
<StackPanel Orientation="Vertical">
<StackPanel Margin="10,10,10,0" Visibility="{Binding ViewModel.HasDependencies}">
<StackPanel Margin="10,10,10,0" Visibility="{Binding HasDependencies, Converter={StaticResource BooleanToVisibilityConverter}}">
<TextBlock Text="Dependencies" FontSize="26"/>
<ItemsControl x:Name="DependenciesPresenter" ItemsSource="{Binding ViewModel.Mod.DependentMods}">
<ItemsControl x:Name="DependenciesPresenter" ItemsSource="{Binding Dependencies}">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type mod:ModDependency}">
<TextBlock Margin="4" FontSize="14">
@@ -50,9 +54,9 @@
Height="2" Margin="0,10,0,-7"/>
</StackPanel>
<absui:AbstractUIGroupPresenter x:Name="OptionsUIPresenter" ViewModel="{Binding ViewModel.OptionsViewModel}"
<absui:AbstractUIGroupPresenter x:Name="OptionsUIPresenter" ViewModel="{Binding OptionsViewModel}"
TemplateSelector="{StaticResource GroupTemplateSelector}"
Visibility="{Binding ViewModel.HasOptions}"/>
Visibility="{Binding OptionsViewModel, Converter={StaticResource NullToVisibilityConverter}}"/>
</StackPanel>
</ScrollViewer>
@@ -82,8 +86,8 @@
<Button x:Name="OptionsUICancelButton" Content="Cancel" Click="OptionsUICancelButton_Click"
Grid.Column="1" Style="{StaticResource SecondaryButton}"/>
<Button x:Name="OptionsUINextButton" Content="{Binding ViewModel.ActionButtonText}"
Command="{Binding ViewModel.ActionCommand}"
<Button x:Name="OptionsUINextButton" Content="{Binding ActionButtonText}"
Command="{Binding ActionCommand}"
Grid.Column="2" Style="{StaticResource PrimaryButton}"/>
</Grid>
</Border>
+2 -10
View File
@@ -16,8 +16,8 @@ namespace ZuneModdingHelper
{
InitializeComponent();
DataContext = this;
ViewModel = new(mod);
//DataContext = this;
ViewModel.Mod = mod;
Title = "Options | " + ViewModel.Mod.Title;
}
@@ -32,14 +32,6 @@ namespace ZuneModdingHelper
Close();
}
public ModViewModel ViewModel
{
get => (ModViewModel)GetValue(ViewModelProperty);
set => SetValue(ViewModelProperty, value);
}
public static readonly DependencyProperty ViewModelProperty = DependencyProperty.Register(
nameof(ViewModel), typeof(ModViewModel), typeof(OptionsUIDialog));
private async void Window_Loaded(object sender, RoutedEventArgs e)
{
await ViewModel.LoadAsync();