Skip to content

Commit

Permalink
Merge pull request #61 from Piotrekol/collectionReordering
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotrekol authored Jun 2, 2022
2 parents e9897d7 + a9359ff commit a84c211
Show file tree
Hide file tree
Showing 14 changed files with 193 additions and 22 deletions.
25 changes: 22 additions & 3 deletions App/Presenters/Controls/CollectionListingPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using System.Windows.Forms;
using System.Collections.Specialized;
using App.Misc;
using Common;
using App.Properties;

namespace App.Presenters.Controls
{
Expand All @@ -15,7 +17,8 @@ public class CollectionListingPresenter
ICollectionListingView _view;

private Collections _collections;
private ICollectionListingModel _model;
private readonly ICollectionListingModel _model;
private readonly IUserDialogs _userDialogs;

public Collections Collections
{
Expand All @@ -32,23 +35,39 @@ public Collections Collections
_view.SelectedCollection = selectedCollection;
}
}
public CollectionListingPresenter(ICollectionListingView view, ICollectionListingModel model)
public CollectionListingPresenter(ICollectionListingView view, ICollectionListingModel model, IUserDialogs userDialogs)
{
_view = view;
_view.RightClick += _view_RightClick;
_view.SelectedCollectionsChanged += ViewOnSelectedCollectionsChanged;
_view.BeatmapsDropped += ViewOnBeatmapsDropped;
_view.OnCollectionReorder += ViewOnCollectionReorder;
_model = model;
_userDialogs = userDialogs;
_model.CollectionsChanged += ModelOnCollectionsChanged;
Collections = _model.GetCollections();
}

private void ViewOnCollectionReorder(object sender, Collections collections, Collection targetCollection, bool placeBefore)
{
if (!Settings.Default.DontAskAboutReorderingCollections)
{
var result = _userDialogs.YesNoMessageBox($"Reordering collections will rename all loaded collections, proceed?", "Reordering", MessageBoxType.Question,
"Don't ask me again");
Settings.Default.DontAskAboutReorderingCollections = result.doNotAskAgain;
Settings.Default.Save();
if (!result.Result)
return;
}

_model.EmitCollectionEditing(CollectionEditArgs.ReorderCollections(collections, targetCollection, placeBefore));
}

private void ViewOnBeatmapsDropped(object sender, Beatmaps args, string collectionName)
{
_model.EmitCollectionEditing(CollectionEditArgs.AddBeatmaps(collectionName, args));
}


private void ViewOnSelectedCollectionsChanged(object sender, EventArgs eventArgs)
{
Collections selectedCollections = new Collections();
Expand Down
6 changes: 4 additions & 2 deletions App/Presenters/Controls/CombinedListingPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ public class CombinedListingPresenter
private readonly ICollectionListingView _collectionsView;
public readonly IBeatmapListingModel BeatmapListingModel;
private readonly IWebCollectionProvider _webCollectionProvider;
private readonly IUserDialogs _userDialogs;
private readonly ICollectionListingModel _collectionListingModel;

public CombinedListingPresenter(ICombinedListingView view, ICollectionListingModel collectionListingModel, IBeatmapListingModel beatmapListingModel, IWebCollectionProvider webCollectionProvider)
public CombinedListingPresenter(ICombinedListingView view, ICollectionListingModel collectionListingModel, IBeatmapListingModel beatmapListingModel, IWebCollectionProvider webCollectionProvider, IUserDialogs userDialogs)
{
_view = view;
_beatmapsView = _view.beatmapListingView;
Expand All @@ -26,8 +27,9 @@ public CombinedListingPresenter(ICombinedListingView view, ICollectionListingMod
BeatmapListingModel = beatmapListingModel;

_webCollectionProvider = webCollectionProvider;
_userDialogs = userDialogs;
new BeatmapListingPresenter(_beatmapsView, BeatmapListingModel);
new CollectionListingPresenter(_collectionsView, collectionListingModel);
new CollectionListingPresenter(_collectionsView, collectionListingModel, userDialogs);

BeatmapListingModel.SelectedBeatmapsChanged += BeatmapListingModelOnSelectedBeatmapsChanged;
_collectionsView.SelectedCollectionChanged += CollectionsViewOnSelectedCollectionChanged;
Expand Down
2 changes: 1 addition & 1 deletion App/Presenters/Forms/MainFormPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public MainFormPresenter(IMainFormView view, IMainFormModel mainFormModel, IInfo
CollectionListingModel = new CollectionListingModel(Initalizer.LoadedCollections, _mainFormModel.GetCollectionEditor());
CollectionListingModel.CollectionEditing += CollectionListing_CollectionEditing;
CollectionListingModel.SelectedCollectionsChanged += CollectionListing_SelectedCollectionsChanged;
new CombinedListingPresenter(_view.CombinedListingView, CollectionListingModel, BeatmapListingModel, webCollectionProvider);
new CombinedListingPresenter(_view.CombinedListingView, CollectionListingModel, BeatmapListingModel, webCollectionProvider, mainFormModel.GetUserDialogs());

//Beatmap preview stuff (images, beatmap info like ar,cs,stars...)
_combinedBeatmapPreviewModel = new CombinedBeatmapPreviewModel();
Expand Down
14 changes: 13 additions & 1 deletion App/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions App/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@
<Setting Name="DownloadManager_DownloaderSettings" Type="System.String" Scope="User">
<Value Profile="(Default)">{}</Value>
</Setting>
<Setting Name="DontAskAboutReorderingCollections" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
</Settings>
</SettingsFile>
3 changes: 3 additions & 0 deletions App/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
<setting name="DownloadManager_DownloaderSettings" serializeAs="String">
<value>{}</value>
</setting>
<setting name="DontAskAboutReorderingCollections" serializeAs="String">
<value>False</value>
</setting>
</App.Properties.Settings>
</userSettings>
</configuration>
2 changes: 1 addition & 1 deletion CollectionManagerDll/DataTypes/Collection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class Collection : IEnumerable, ICollection
/// </summary>
public Beatmaps KnownBeatmaps { get; } = new Beatmaps();


public override string ToString() => $"osu! map Collection: \"{Name}\" Count: {BeatmapHashes.Count}";

/// <summary>
/// Total number of beatmaps contained in this collection
Expand Down
1 change: 1 addition & 0 deletions CollectionManagerDll/Enums/CollectionEdit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ public enum CollectionEdit
Intersect=9,
Inverse=10,
Difference=11,
Reorder=12,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ public class CollectionEditArgs : EventArgs
public string OrginalName { get; private set; }
public string NewName { get; set; }
public Collections Collections { get; private set; }
public Collection TargetCollection { get; private set; }
public Beatmaps Beatmaps { get; private set; }
public IList<string> CollectionNames { get; private set; }
public bool PlaceCollectionsBefore { get; private set; }
public CollectionEditArgs(CollectionEdit action)
{
Action = action;
Expand Down Expand Up @@ -114,6 +116,17 @@ public static CollectionEditArgs AddBeatmaps(string collectionName, Beatmaps bea
};
}

#endregion
#region Reorder collections using special characters placed at the begining of the name, this modifies ALL collection names
public static CollectionEditArgs ReorderCollections(Collections collections, Collection targetCollection, bool placeBefore)
{
return new CollectionEditArgs(CollectionEdit.Reorder)
{
Collections = collections,
TargetCollection = targetCollection,
PlaceCollectionsBefore = placeBefore
};
}
#endregion
#region Remove beatmaps from collection
public static CollectionEditArgs RemoveBeatmaps(string collectionName, Beatmaps beatmaps)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App.Interfaces;
using CollectionManager.DataTypes;
using CollectionManager.Enums;
Expand All @@ -12,10 +13,18 @@ public class CollectionsManager : ICollectionEditor, ICollectionNameValidator
{
public readonly Collections LoadedCollections = new Collections();
public Beatmaps LoadedBeatmaps;
private readonly char[] ReorderChars;
private readonly char[] ReorderCharsWithSeparator;
private readonly string ReorderCharsString;
private const string reorderSeparator = "| ";
public CollectionsManager(Beatmaps loadedBeatmaps)
{
LoadedBeatmaps = loadedBeatmaps;
ReorderCharsString = "0123456789";//!$)]},.? ABCDEFGHIJKLMNOPQRSTUVWXYZ @#%^&*(+[{;':\\\"<>/
ReorderChars = ReorderCharsString.ToArray();
ReorderCharsWithSeparator = ReorderChars.Concat(reorderSeparator).ToArray();
}

/*
add collection
remove collection
Expand All @@ -25,10 +34,10 @@ intersect x collections
inverse map sum of x collections
difference x collections
clear collections
reorder collections
add beatmaps to collection
remove beatmaps from collection
*/

private void EditCollection(CollectionEditArgs args, bool suspendRefresh = false)
{
var action = args.Action;
Expand Down Expand Up @@ -151,6 +160,36 @@ private void EditCollection(CollectionEditArgs args, bool suspendRefresh = false
{
LoadedCollections.Clear();
}
else if (action == CollectionEdit.Reorder)
{
var targetCollection = args.TargetCollection;
var collectionsToReorder = args.Collections.OrderBy(x => x.Name).ToList();
var orderedLoadedCollections = LoadedCollections.OrderBy(x => x.Name).ToList();
foreach (var coll in collectionsToReorder)
orderedLoadedCollections.Remove(coll);

var targetCollectionIndex = orderedLoadedCollections.IndexOf(targetCollection);
orderedLoadedCollections.InsertRange(args.PlaceCollectionsBefore ? targetCollectionIndex : targetCollectionIndex + 1, collectionsToReorder);
var amountOfCharactersRequired = 0;
var variations = 0;
while (orderedLoadedCollections.Count() > variations)
variations = Enumerable.Range(1, ++amountOfCharactersRequired).Aggregate(0, (acc, i) => Convert.ToInt32(Math.Pow(ReorderChars.Length, i)) + acc);

List<string> reorderStrings = new List<string>(variations);
for (int i = 1; i <= amountOfCharactersRequired; i++)
reorderStrings.AddRange(CombinationsWithRepetition(ReorderCharsString, i));

reorderStrings.Sort();
var collectionIndex = 0;
foreach (var collection in orderedLoadedCollections)
{

if (collection.Name.Contains(reorderSeparator))
collection.Name = collection.Name.TrimStart(ReorderCharsWithSeparator);

collection.Name = $"{reorderStrings[collectionIndex++]}{reorderSeparator} {collection.Name}";
}
}
else
{
var collection = GetCollectionByName(args.OrginalName);
Expand Down Expand Up @@ -243,5 +282,26 @@ public bool IsCollectionNameValid(string name)
{
return !CollectionNameExists(name);
}

private static string Combinations(string symbols, int number, int stringLength)
{
var stringBuilder = new StringBuilder();
var len = symbols.Length;
var nullSym = symbols[0];
while (number > 0)
{
var index = number % len;
number = number / len;
stringBuilder.Insert(0, symbols[index]);
}

return stringBuilder.ToString().PadLeft(stringLength, nullSym);
}

private static IEnumerable<string> CombinationsWithRepetition(string symbols, int stringLength)
{
for (var i = 0; i < Math.Pow(symbols.Length, stringLength); i++)
yield return Combinations(symbols, i, stringLength);
}
}
}
1 change: 1 addition & 0 deletions Common/GuiHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public static class GuiHelpers
{
public delegate void BeatmapsEventArgs(object sender, Beatmaps args);
public delegate void CollectionBeatmapsEventArgs(object sender, Beatmaps args, string collectionName);
public delegate void CollectionReorderEventArgs(object sender, Collections collections, Collection targetCollection, bool placeBefore);
public delegate void BeatmapListingActionArgs(object sender, BeatmapListingAction args);
public delegate void SidePanelActionsHandlerArgs(object sender, MainSidePanelActions args, object data = null);
public delegate void LoadFileArgs(object sender, string[] filePaths);
Expand Down
1 change: 1 addition & 0 deletions Common/Interfaces/Controls/ICollectionListingView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace GuiComponents.Interfaces
public interface ICollectionListingView
{
event GuiHelpers.LoadFileArgs OnLoadFile;
event GuiHelpers.CollectionReorderEventArgs OnCollectionReorder;

string SearchText { get; }

Expand Down
1 change: 1 addition & 0 deletions GuiComponents/Controls/CollectionListingView.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a84c211

Please sign in to comment.