Skip to content

Commit

Permalink
Add collection sort order recognition, also fixes #35
Browse files Browse the repository at this point in the history
  • Loading branch information
bcssov committed Apr 26, 2020
1 parent 0ba14f3 commit ebbdec6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 31 deletions.
8 changes: 1 addition & 7 deletions src/IronyModManager.Models.Common/IAppState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created : 03-03-2020
//
// Last Modified By : Mario
// Last Modified On : 03-11-2020
// Last Modified On : 04-26-2020
// ***********************************************************************
// <copyright file="IAppState.cs" company="Mario">
// Mario
Expand Down Expand Up @@ -43,12 +43,6 @@ public interface IAppState : IModel
/// <value>The collection mods sort column.</value>
string CollectionModsSortColumn { get; set; }

/// <summary>
/// Gets or sets the collection mods sort mode.
/// </summary>
/// <value>The collection mods sort mode.</value>
int CollectionModsSortMode { get; set; }

/// <summary>
/// Gets or sets the installed mods search term.
/// </summary>
Expand Down
8 changes: 1 addition & 7 deletions src/IronyModManager.Models/AppState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created : 03-03-2020
//
// Last Modified By : Mario
// Last Modified On : 03-11-2020
// Last Modified On : 04-26-2020
// ***********************************************************************
// <copyright file="AppState.cs" company="Mario">
// Mario
Expand Down Expand Up @@ -46,12 +46,6 @@ public class AppState : BaseModel, IAppState
/// <value>The collection mods sort column.</value>
public virtual string CollectionModsSortColumn { get; set; }

/// <summary>
/// Gets or sets the collection mods sort mode.
/// </summary>
/// <value>The collection mods sort mode.</value>
public virtual int CollectionModsSortMode { get; set; }

/// <summary>
/// Gets or sets the installed mods search term.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created : 03-03-2020
//
// Last Modified By : Mario
// Last Modified On : 04-17-2020
// Last Modified On : 04-26-2020
// ***********************************************************************
// <copyright file="CollectionModsControlViewModel.cs" company="Mario">
// Mario
Expand Down Expand Up @@ -532,15 +532,6 @@ protected virtual void InitSortersAndFilters(IAppState state)
SearchMods.WatermarkText = SearchModsWatermark;
SearchMods.Text = state?.CollectionModsSearchTerm;
ModNameSortOrder.Text = ModName;
if (!string.IsNullOrWhiteSpace(state.CollectionModsSortColumn) && Enum.IsDefined(typeof(SortOrder), state.CollectionModsSortMode))
{
var sort = (SortOrder)state.CollectionModsSortMode;
ModNameSortOrder.SortOrder = sort;
}
else
{
ModNameSortOrder.SortOrder = SortOrder.Asc;
}
if (!string.IsNullOrWhiteSpace(state.CollectionModsSelectedMod) && SelectedMods != null)
{
SelectedMod = SelectedMods.FirstOrDefault(p => p.DescriptorFile.Equals(state.CollectionModsSelectedMod));
Expand All @@ -556,10 +547,41 @@ protected virtual void LoadModCollections()
var selected = ModCollections?.FirstOrDefault(p => p.IsSelected);
if (selected != null)
{
RecognizeSortOrder(selected);
SelectedModCollection = selected;
}
}

/// <summary>
/// Recognizes the sort order.
/// </summary>
/// <param name="modCollection">The mod collection.</param>
protected virtual void RecognizeSortOrder(IModCollection modCollection)
{
// modCollection sort order
if (modCollection?.Mods.Count() > 0 && Mods?.Count() > 0)
{
var mods = Mods.Where(p => modCollection.Mods.Any(a => a.Equals(p.DescriptorFile, StringComparison.OrdinalIgnoreCase)));
if (mods.Count() > 0)
{
var ascending = mods.OrderBy(p => p.Name).Select(p => p.DescriptorFile);
var descending = mods.OrderByDescending(p => p.Name).Select(p => p.DescriptorFile);
if (ascending.SequenceEqual(modCollection.Mods))
{
ModNameSortOrder.SetSortOrder(SortOrder.Asc);
}
else if (descending.SequenceEqual(modCollection.Mods))
{
ModNameSortOrder.SetSortOrder(SortOrder.Desc);
}
else
{
ModNameSortOrder.SetSortOrder(SortOrder.None);
}
}
}
}

/// <summary>
/// Called when [activated].
/// </summary>
Expand Down Expand Up @@ -809,14 +831,14 @@ protected virtual async Task ReorderSelectedItemsAsync(IMod mod, CancellationTok
var index = SelectedMods.IndexOf(swapItem);
SelectedMods.Remove(mod);
SelectedMods.Insert(index, mod);
SetSelectedMods(SelectedMods);
ModNameSortOrder.SetSortOrder(SortOrder.None);
SetSelectedMods(SelectedMods);
SelectedMod = mod;
if (!string.IsNullOrWhiteSpace(SelectedModCollection?.Name))
{
SaveSelectedCollection();
}
SaveState();
RecognizeSortOrder(SelectedModCollection);
}
ModReordered?.Invoke(mod);
}
Expand All @@ -832,9 +854,12 @@ protected virtual void SaveSelectedCollection()
if (collection != null && SelectedModCollection != null)
{
collection.Name = SelectedModCollection.Name;
collection.Mods = SelectedMods?.Where(p => p.IsSelected).Select(p => p.DescriptorFile);
collection.Mods = SelectedMods?.Where(p => p.IsSelected).Select(p => p.DescriptorFile).ToList();
collection.IsSelected = true;
modCollectionService.Save(collection);
if (modCollectionService.Save(collection))
{
SelectedModCollection.Mods = collection.Mods.ToList();
}
}
}

Expand All @@ -847,7 +872,6 @@ protected virtual void SaveState()
state.CollectionModsSelectedMod = SelectedMod?.DescriptorFile;
state.CollectionModsSearchTerm = SearchMods.Text;
state.CollectionModsSortColumn = ModNameKey;
state.CollectionModsSortMode = (int)ModNameSortOrder.SortOrder;
appStateService.Save(state);
}

Expand Down Expand Up @@ -911,8 +935,7 @@ protected virtual void SubscribeToMods()
{
if (!SelectedMods.Contains(s.Sender))
{
SelectedMods.Add(s.Sender);
ModNameSortOrder.SetSortOrder(SortOrder.None);
SelectedMods.Add(s.Sender);
if (!string.IsNullOrWhiteSpace(SelectedModCollection?.Name))
{
SaveState();
Expand All @@ -934,6 +957,7 @@ protected virtual void SubscribeToMods()
SaveSelectedCollection();
}
SetSelectedMods(SelectedMods, false);
RecognizeSortOrder(SelectedModCollection);
}
AllModsEnabled = SelectedMods?.Count() > 0 && SelectedMods.All(p => p.IsSelected);
skipReorder = false;
Expand Down

0 comments on commit ebbdec6

Please sign in to comment.