diff --git a/src/IronyModManager.Models.Common/IAppState.cs b/src/IronyModManager.Models.Common/IAppState.cs index 748f0bba..a0e35a4c 100644 --- a/src/IronyModManager.Models.Common/IAppState.cs +++ b/src/IronyModManager.Models.Common/IAppState.cs @@ -4,7 +4,7 @@ // Created : 03-03-2020 // // Last Modified By : Mario -// Last Modified On : 03-11-2020 +// Last Modified On : 04-26-2020 // *********************************************************************** // // Mario @@ -43,12 +43,6 @@ public interface IAppState : IModel /// The collection mods sort column. string CollectionModsSortColumn { get; set; } - /// - /// Gets or sets the collection mods sort mode. - /// - /// The collection mods sort mode. - int CollectionModsSortMode { get; set; } - /// /// Gets or sets the installed mods search term. /// diff --git a/src/IronyModManager.Models/AppState.cs b/src/IronyModManager.Models/AppState.cs index 4006f85d..5c16ab16 100644 --- a/src/IronyModManager.Models/AppState.cs +++ b/src/IronyModManager.Models/AppState.cs @@ -4,7 +4,7 @@ // Created : 03-03-2020 // // Last Modified By : Mario -// Last Modified On : 03-11-2020 +// Last Modified On : 04-26-2020 // *********************************************************************** // // Mario @@ -46,12 +46,6 @@ public class AppState : BaseModel, IAppState /// The collection mods sort column. public virtual string CollectionModsSortColumn { get; set; } - /// - /// Gets or sets the collection mods sort mode. - /// - /// The collection mods sort mode. - public virtual int CollectionModsSortMode { get; set; } - /// /// Gets or sets the installed mods search term. /// diff --git a/src/IronyModManager/ViewModels/Controls/CollectionModsControlViewModel.cs b/src/IronyModManager/ViewModels/Controls/CollectionModsControlViewModel.cs index 5b93f300..215ff818 100644 --- a/src/IronyModManager/ViewModels/Controls/CollectionModsControlViewModel.cs +++ b/src/IronyModManager/ViewModels/Controls/CollectionModsControlViewModel.cs @@ -4,7 +4,7 @@ // Created : 03-03-2020 // // Last Modified By : Mario -// Last Modified On : 04-17-2020 +// Last Modified On : 04-26-2020 // *********************************************************************** // // Mario @@ -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)); @@ -556,10 +547,41 @@ protected virtual void LoadModCollections() var selected = ModCollections?.FirstOrDefault(p => p.IsSelected); if (selected != null) { + RecognizeSortOrder(selected); SelectedModCollection = selected; } } + /// + /// Recognizes the sort order. + /// + /// The mod collection. + 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); + } + } + } + } + /// /// Called when [activated]. /// @@ -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); } @@ -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(); + } } } @@ -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); } @@ -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(); @@ -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;