Skip to content

Commit

Permalink
Made ModuleContext and renamed it to DefaultModuleContext
Browse files Browse the repository at this point in the history
  • Loading branch information
Aragas committed Oct 31, 2024
1 parent 2e4d9e8 commit cf81293
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 49 deletions.
54 changes: 54 additions & 0 deletions src/Bannerlord.LauncherManager/Utils/DefaultModuleContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Bannerlord.LauncherManager.Models;
using Bannerlord.ModuleManager;

using System.Collections.Generic;
using System.Linq;

namespace Bannerlord.LauncherManager.Utils;

public class DefaultModuleContext<TModuleViewModel> where TModuleViewModel : class, IModuleViewModel
{
private readonly IDictionary<string, TModuleViewModel> _lookup;
public DefaultModuleContext(IEnumerable<TModuleViewModel> moduleVMs)
{
_lookup = moduleVMs.ToDictionary(x => x.ModuleInfoExtended.Id, x => x);
}
public DefaultModuleContext(IDictionary<string, TModuleViewModel> lookup)
{
_lookup = lookup;
}

public bool GetIsValid(ModuleInfoExtended module)
{
if (FeatureIds.LauncherFeatures.Contains(module.Id))
return true;

return _lookup[module.Id].IsValid;
}
public bool GetIsSelected(ModuleInfoExtended module)
{
if (FeatureIds.LauncherFeatures.Contains(module.Id))
return false;

return _lookup[module.Id].IsSelected;
}
public void SetIsSelected(ModuleInfoExtended module, bool value)
{
if (FeatureIds.LauncherFeatures.Contains(module.Id))
return;
_lookup[module.Id].IsSelected = value;
}
public bool GetIsDisabled(ModuleInfoExtended module)
{
if (FeatureIds.LauncherFeatures.Contains(module.Id))
return false;

return _lookup[module.Id].IsDisabled;
}
public void SetIsDisabled(ModuleInfoExtended module, bool value)
{
if (FeatureIds.LauncherFeatures.Contains(module.Id))
return;
_lookup[module.Id].IsDisabled = value;
}
}
51 changes: 2 additions & 49 deletions src/Bannerlord.LauncherManager/Utils/SortHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,61 +9,14 @@ namespace Bannerlord.LauncherManager.Utils;

public static class SortHelper
{
private class ModuleContext<TModuleViewModel> where TModuleViewModel : class, IModuleViewModel
{
private readonly IDictionary<string, TModuleViewModel> _lookup;
public ModuleContext(IEnumerable<TModuleViewModel> moduleVMs)
{
_lookup = moduleVMs.ToDictionary(x => x.ModuleInfoExtended.Id, x => x);
}
public ModuleContext(IDictionary<string, TModuleViewModel> lookup)
{
_lookup = lookup;
}

public bool GetIsValid(ModuleInfoExtended module)
{
if (FeatureIds.LauncherFeatures.Contains(module.Id))
return true;

return _lookup[module.Id].IsValid;
}
public bool GetIsSelected(ModuleInfoExtended module)
{
if (FeatureIds.LauncherFeatures.Contains(module.Id))
return false;

return _lookup[module.Id].IsSelected;
}
public void SetIsSelected(ModuleInfoExtended module, bool value)
{
if (FeatureIds.LauncherFeatures.Contains(module.Id))
return;
_lookup[module.Id].IsSelected = value;
}
public bool GetIsDisabled(ModuleInfoExtended module)
{
if (FeatureIds.LauncherFeatures.Contains(module.Id))
return false;

return _lookup[module.Id].IsDisabled;
}
public void SetIsDisabled(ModuleInfoExtended module, bool value)
{
if (FeatureIds.LauncherFeatures.Contains(module.Id))
return;
_lookup[module.Id].IsDisabled = value;
}
}

/// <summary>
/// External<br/>
/// </summary>
public static void ToggleModuleSelection<TModuleViewModel>(IEnumerable<TModuleViewModel> moduleVMs, IDictionary<string, TModuleViewModel> lookup, TModuleViewModel moduleVM)
where TModuleViewModel : class, IModuleViewModel
{
var modules = moduleVMs.Select(x => x.ModuleInfoExtended).ToList();
var ctx = new ModuleContext<TModuleViewModel>(lookup);
var ctx = new DefaultModuleContext<TModuleViewModel>(lookup);

if (moduleVM.IsSelected)
ModuleUtilities.DisableModule(modules, moduleVM.ModuleInfoExtended, ctx.GetIsSelected, ctx.SetIsSelected, ctx.GetIsDisabled, ctx.SetIsDisabled);
Expand All @@ -78,7 +31,7 @@ public static IEnumerable<string> ValidateModule<TModuleViewModel>(IEnumerable<T
where TModuleViewModel : class, IModuleViewModel
{
var modules = moduleVMs.Select(x => x.ModuleInfoExtended).Concat(FeatureIds.LauncherFeatures.Select(x => new ModuleInfoExtended { Id = x })).ToList();
var ctx = new ModuleContext<TModuleViewModel>(lookup);
var ctx = new DefaultModuleContext<TModuleViewModel>(lookup);

return ModuleUtilities.ValidateModule(modules, moduleVM.ModuleInfoExtended, ctx.GetIsSelected, ctx.GetIsValid).Select(ModuleIssueRenderer.Render);
}
Expand Down

0 comments on commit cf81293

Please sign in to comment.