Skip to content

Commit

Permalink
fix: Image failing to load after disabling/enabling mod (Jorixon#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorixon authored Sep 30, 2023
1 parent be18025 commit 352de20
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 28 deletions.
9 changes: 5 additions & 4 deletions src/GIMI-ModManager.WinUI/Models/NewModModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public partial class NewModModel : ObservableObject, IEquatable<NewModModel>
[ObservableProperty] private string _author = string.Empty;
[ObservableProperty] private string _characterSkinOverride = string.Empty;

[ObservableProperty] private ObservableCollection<ModNotification> _modNotifications = new ();
[ObservableProperty] private ObservableCollection<ModNotification> _modNotifications = new();

public static readonly Uri PlaceholderImagePath =
new(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Assets\\ModPanePlaceholder.webp"));
Expand Down Expand Up @@ -122,13 +122,12 @@ public override string ToString()

// Due to some binding issues with datagrids, this is a hacky way to get the toggle button to work.
[RelayCommand]
private async Task ToggleModAsync()
private Task ToggleModAsync()
{
if (_toggleMod is not null)
try
{
await Task.Run(() => _toggleMod(this));
IsEnabled = !IsEnabled;
_toggleMod(this);
}
catch (Exception e)
{
Expand All @@ -137,6 +136,8 @@ private async Task ToggleModAsync()
$" the mod: {Name}",
e.ToString(), TimeSpan.MaxValue);
}

return Task.CompletedTask;
}

public bool Equals(NewModModel? other)
Expand Down
29 changes: 11 additions & 18 deletions src/GIMI-ModManager.WinUI/ViewModels/CharacterDetailsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,17 +243,18 @@ public async void OnNavigatedTo(object parameter)
if (lastSelectedSkin is not null) await SwitchCharacterSkin(lastSelectedSkin);
}


// Does not run on UI thread
private void ToggleMod(NewModModel mod)
// This function is called from the NewModModel _toggleMod delegate.
// This is a hacky way to get the toggle button to work.
private void ToggleMod(NewModModel thisMod)
{
var modList = _skinManagerService.GetCharacterModList(mod.Character);
if (mod.IsEnabled)
modList.DisableMod(mod.Id);

var modList = _skinManagerService.GetCharacterModList(thisMod.Character);
if (thisMod.IsEnabled)
modList.DisableMod(thisMod.Id);
else
modList.EnableMod(thisMod.Id);

modList.EnableMod(mod.Id);
thisMod.IsEnabled = !thisMod.IsEnabled;
thisMod.FolderName = _modList.Mods.First(mod => mod.Id == thisMod.Id).Mod.Name;
}

[ObservableProperty] private bool _isAddingModFolder = false;
Expand Down Expand Up @@ -541,21 +542,13 @@ public void ChangeModDetails(NewModModel newModModel)
TimeSpan.FromSeconds(10));
}

public void ModList_KeyHandler(IEnumerable<Guid> modEntryId, VirtualKey key)
public async Task ModList_KeyHandler(IEnumerable<Guid> modEntryId, VirtualKey key)
{
var selectedMods = ModListVM.Mods.Where(mod => modEntryId.Contains(mod.Id)).ToArray();

if (key == VirtualKey.Space)
foreach (var newModModel in selectedMods)
{
if (newModModel.IsEnabled)
_modList.DisableMod(newModModel.Id);
else
_modList.EnableMod(newModModel.Id);

newModModel.IsEnabled = !newModModel.IsEnabled;
newModModel.FolderName = _modList.Mods.First(mod => mod.Id == newModModel.Id).Mod.Name;
}
await newModModel.ToggleModCommand.ExecuteAsync(newModModel);
}


Expand Down
3 changes: 2 additions & 1 deletion src/GIMI-ModManager.WinUI/ViewModels/SubVms/ModPaneVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ private async Task ReloadModSettings(CancellationToken cancellationToken = defau
if (_selectedSkinMod is null || _backendModModel is null || SelectedModModel is null) return;
try
{
var skinModSettings = await _selectedSkinMod.ReadSkinModSettings(cancellationToken: cancellationToken);
var skinModSettings =
await _selectedSkinMod.ReadSkinModSettings(true, cancellationToken: cancellationToken);

_backendModModel.WithModSettings(skinModSettings);
SelectedModModel.WithModSettings(skinModSettings);
Expand Down
10 changes: 5 additions & 5 deletions src/GIMI-ModManager.WinUI/Views/CharacterDetailsPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Diagnostics;
using Windows.ApplicationModel.DataTransfer;
using Windows.ApplicationModel.DataTransfer;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.System;
Expand Down Expand Up @@ -277,7 +276,8 @@ private async void ModListGrid_OnKeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Space)
{
ViewModel.ModList_KeyHandler(ModListGrid.SelectedItems.OfType<NewModModel>().Select(mod => mod.Id), e.Key);
await ViewModel.ModList_KeyHandler(ModListGrid.SelectedItems.OfType<NewModModel>().Select(mod => mod.Id),
e.Key);
e.Handled = true;
}

Expand Down Expand Up @@ -383,8 +383,8 @@ private async void ModDetailsPaneImage_OnDrop(object sender, DragEventArgs e)

private void Image_OnImageFailed(object sender, ExceptionRoutedEventArgs e)
{
Log.Warning(e.ErrorMessage);
Debug.WriteLine(e.ErrorMessage);
Log.Error("Failed to load mod preview image: {Error}", e.ErrorMessage);
var image = (Image)sender;
}

private void ModNameCell_OnDoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
Expand Down

0 comments on commit 352de20

Please sign in to comment.