Skip to content

Commit

Permalink
Custom Fields in Collections
Browse files Browse the repository at this point in the history
  • Loading branch information
omkelderman committed May 14, 2023
1 parent 6af6b22 commit e49d4b4
Show file tree
Hide file tree
Showing 13 changed files with 329 additions and 89 deletions.
9 changes: 0 additions & 9 deletions App/Interfaces/Controls/IBeatmapListingPresenter.cs

This file was deleted.

13 changes: 7 additions & 6 deletions App/Models/Controls/BeatmapListingModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,17 @@ public void SetBeatmaps(Beatmaps beatmaps)

public void SetCollection(ICollection collection)
{
CurrentCollection = collection;
if (collection == null)
{
SetBeatmaps(null);
CurrentCollection = collection;
return;
}
CurrentCollection = collection;
var maps = new Beatmaps();
maps.AddRange(collection.AllBeatmaps());
SetBeatmaps(maps);
else
{
var maps = new Beatmaps();
maps.AddRange(collection.AllBeatmaps());
SetBeatmaps(maps);
}
}

public void FilterBeatmaps(string text)
Expand Down
30 changes: 11 additions & 19 deletions App/Presenters/Controls/BeatmapListingPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,11 @@

namespace App.Presenters.Controls
{
public class BeatmapListingPresenter: IBeatmapListingPresenter
public class BeatmapListingPresenter
{
readonly IBeatmapListingView _view;
readonly IBeatmapListingModel _model;

private Beatmaps _beatmaps;

public Beatmaps Beatmaps
{
get
{
return _beatmaps;
}
set
{
_beatmaps = value;
_view.SetBeatmaps(value);
}
}
public BeatmapListingPresenter(IBeatmapListingView view, IBeatmapListingModel model)
{
_view = view;
Expand All @@ -36,11 +22,11 @@ public BeatmapListingPresenter(IBeatmapListingView view, IBeatmapListingModel mo
_view.BeatmapOperation += (s, a) => _model.EmitBeatmapOperation(a);

_model = model;
_model.BeatmapsChanged += _model_BeatmapsChanged;
_model.BeatmapsChanged += (_, _) => RefreshBeatmapsInViewFromModel();
_model.FilteringStarted+=ModelOnFilteringStarted;
_model.FilteringFinished += _model_FilteringFinished;
_view.SetFilter(_model.GetFilter());
Beatmaps = _model.GetBeatmaps();
RefreshBeatmapsInViewFromModel();
}

private void _model_FilteringFinished(object sender, EventArgs e)
Expand All @@ -63,9 +49,15 @@ private void ViewOnSearchTextChanged(object sender, EventArgs eventArgs)
_model.FilterBeatmaps(_view.SearchText);
}

private void _model_BeatmapsChanged(object sender, System.EventArgs e)
private void RefreshBeatmapsInViewFromModel()
{
Beatmaps = _model.GetBeatmaps();
_view.SetBeatmaps(_model.GetBeatmaps());
_view.ClearCustomFieldDefinitions();
var curCol = _model.CurrentCollection;
if(curCol != null && curCol.CustomFieldDefinitions != null)
{
_view.SetCustomFieldDefinitions(curCol.CustomFieldDefinitions);
}
}


Expand Down
39 changes: 39 additions & 0 deletions CollectionManagerDll/DataTypes/BeatmapExtension.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace CollectionManager.DataTypes
{
Expand All @@ -13,5 +15,42 @@ public class BeatmapExtension : Beatmap
public string UserComment { get; set; } = "";

#endregion

#region Custom Field Stuff

private Dictionary<string, object> _customFields;

public void SetCustomFieldValues(BeatmapExtension other)
{
_customFields = other._customFields == null ? null : new Dictionary<string, object>(other._customFields);
}

public void SetCustomFieldValue(string key, object value)
{
_customFields ??= new Dictionary<string, object>();
_customFields[key] = value;
}

public object GetCustomFieldValue(string key)
{
if(_customFields == null ) return null;
return _customFields.TryGetValue(key, out var value) ? value : null;
}

public IEnumerable<string> GetStringCustomFieldValues()
{
if (_customFields == null) yield break;
foreach(var customField in _customFields)
{
if(customField.Value is string stringValue) yield return stringValue;
}
}

public IEnumerable<KeyValuePair<string, object>> GetCustomFields()
{
return _customFields ?? Enumerable.Empty<KeyValuePair<string, object>>();
}

#endregion
}
}
4 changes: 3 additions & 1 deletion CollectionManagerDll/DataTypes/Collection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public virtual int NumberOfBeatmaps

public int Id { get; set; }

public IReadOnlyCollection<CustomFieldDefinition> CustomFieldDefinitions { get; set; }

public void SetLoadedMaps(MapCacher instance)
{
if (instance == null)
Expand Down Expand Up @@ -161,6 +163,7 @@ public void AddBeatmapByMapId(int mapId)
private void ProcessAdditionalProps(BeatmapExtension src, BeatmapExtension dest)
{
dest.UserComment = src.UserComment;
dest.SetCustomFieldValues(src);
}
protected virtual void ProcessNewlyAddedMap(BeatmapExtension map)
{
Expand Down Expand Up @@ -236,6 +239,5 @@ public IEnumerator GetEnumerator()
{
return this.AllBeatmaps().GetEnumerator();
}

}
}
11 changes: 11 additions & 0 deletions CollectionManagerDll/DataTypes/CustomFieldDefinition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using CollectionManager.Enums;

namespace CollectionManager.DataTypes
{
public class CustomFieldDefinition
{
public string Key { get; set; }
public CustomFieldType Type { get; set; }
public string DisplayText { get; set; }
}
}
2 changes: 2 additions & 0 deletions CollectionManagerDll/DataTypes/ICollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public interface ICollection

int Id { get; set; }

IReadOnlyCollection<CustomFieldDefinition> CustomFieldDefinitions { get; }

void SetLoadedMaps(MapCacher instance);
IEnumerable<BeatmapExtension> AllBeatmaps();
IEnumerable<BeatmapExtension> NotKnownBeatmaps();
Expand Down
21 changes: 21 additions & 0 deletions CollectionManagerDll/Enums/CustomFieldTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace CollectionManager.Enums
{
public enum CustomFieldType
{
String,
Boolean,
GameMode,
Grade,
UInt8,
UInt16,
UInt32,
UInt64,
Int8,
Int16,
Int32,
Int64,
DateTime,
Single,
Double
}
}
Loading

0 comments on commit e49d4b4

Please sign in to comment.