[Core] Ignore empty groups
Lookup groups are removed once empty, we shouldn't be adding them if they're empty. This happens when all of a category's properties are removed to form a grouped category, the original category lingered.
This commit is contained in:
Родитель
08cd44b728
Коммит
78d9d43477
|
@ -1,4 +1,5 @@
|
|||
using System.Collections.Specialized;
|
||||
using System;
|
||||
using System.Collections.Specialized;
|
||||
using System.Linq;
|
||||
using NUnit.Framework;
|
||||
|
||||
|
@ -67,5 +68,53 @@ namespace Xamarin.PropertyEditing.Tests
|
|||
Assert.That (grouping, Does.Not.Contains (value));
|
||||
Assert.That (lookup, Does.Not.Contain (grouping));
|
||||
}
|
||||
|
||||
[TestCase ("group")]
|
||||
[TestCase (null)]
|
||||
[Description ("If removing the last item from a group removes the group, adding a group with no items shouldn't add it")]
|
||||
public void AddingEmptyElementsIsIgnored (string groupName)
|
||||
{
|
||||
var lookup = new ObservableLookup<string, string> ();
|
||||
bool changed = false;
|
||||
lookup.CollectionChanged += (sender, args) => {
|
||||
changed = true;
|
||||
};
|
||||
lookup.Add (groupName, Enumerable.Empty<string> ());
|
||||
|
||||
Assert.That (lookup, Is.Empty);
|
||||
Assert.That (changed, Is.False);
|
||||
}
|
||||
|
||||
[TestCase ("group")]
|
||||
[TestCase (null)]
|
||||
[Description ("If removing the last item from a group removes the group, adding a group with no items shouldn't add it")]
|
||||
public void AddingEmptyGroupingIsIgnored (string groupName)
|
||||
{
|
||||
var lookup = new ObservableLookup<string, string> ();
|
||||
bool changed = false;
|
||||
lookup.CollectionChanged += (sender, args) => {
|
||||
changed = true;
|
||||
};
|
||||
lookup.Add (new ObservableGrouping<string, string> (groupName));
|
||||
|
||||
Assert.That (lookup, Is.Empty);
|
||||
Assert.That (changed, Is.False);
|
||||
}
|
||||
|
||||
[TestCase ("group")]
|
||||
[TestCase (null)]
|
||||
[Description ("If removing the last item from a group removes the group, adding a group with no items shouldn't add it")]
|
||||
public void InsertingEmptyIsIgnored (string groupName)
|
||||
{
|
||||
var lookup = new ObservableLookup<string, string> ();
|
||||
bool changed = false;
|
||||
lookup.CollectionChanged += (sender, args) => {
|
||||
changed = true;
|
||||
};
|
||||
lookup.Insert (0, new ObservableGrouping<string, string> (groupName));
|
||||
|
||||
Assert.That (lookup, Is.Empty);
|
||||
Assert.That (changed, Is.False);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -102,20 +102,26 @@ namespace Xamarin.PropertyEditing
|
|||
public void Add (TKey key, IEnumerable<TElement> elements)
|
||||
{
|
||||
if (elements == null)
|
||||
throw new ArgumentNullException ("elements");
|
||||
throw new ArgumentNullException (nameof(elements));
|
||||
|
||||
ObservableGrouping<TKey, TElement> grouping;
|
||||
if (key == null) {
|
||||
bool wasEmpty = this.nullGrouping.Count == 0;
|
||||
grouping = nullGrouping;
|
||||
grouping.AddRange (elements);
|
||||
if (wasEmpty)
|
||||
if (wasEmpty && this.nullGrouping.Count > 0)
|
||||
OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Add, (object)grouping, this.groupings.Count));
|
||||
} else if (!this.groupings.TryGetValue (key, out grouping)) {
|
||||
if (!ReuseGroups || !this.oldGroups.TryRemove (key, out grouping))
|
||||
grouping = new ObservableGrouping<TKey, TElement> (key);
|
||||
|
||||
grouping.AddRange (elements);
|
||||
if (grouping.Count == 0) {
|
||||
if (ReuseGroups)
|
||||
this.oldGroups.Add (grouping.Key, grouping);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
this.groupings.Add (key, grouping);
|
||||
OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Add, (object)grouping, this.groupings.Count - 1));
|
||||
|
@ -124,10 +130,13 @@ namespace Xamarin.PropertyEditing
|
|||
|
||||
public void Add (IGrouping<TKey, TElement> grouping)
|
||||
{
|
||||
if (grouping == null)
|
||||
throw new ArgumentNullException (nameof(grouping));
|
||||
|
||||
if (grouping.Key == null) {
|
||||
bool wasEmpty = this.nullGrouping.Count == 0;
|
||||
this.nullGrouping.AddRange (grouping);
|
||||
if (wasEmpty)
|
||||
if (wasEmpty && this.nullGrouping.Count > 0)
|
||||
OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Add, (object)grouping, this.groupings.Count));
|
||||
|
||||
return;
|
||||
|
@ -139,6 +148,12 @@ namespace Xamarin.PropertyEditing
|
|||
}
|
||||
|
||||
og.AddRange (grouping);
|
||||
if (og.Count == 0) {
|
||||
if (ReuseGroups)
|
||||
this.oldGroups.Add (grouping.Key, og);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
this.groupings.Add (grouping.Key, og);
|
||||
OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Add, (object)og, this.groupings.Count - 1));
|
||||
|
@ -152,6 +167,13 @@ namespace Xamarin.PropertyEditing
|
|||
}
|
||||
|
||||
og.AddRange (grouping);
|
||||
if (og.Count == 0) {
|
||||
if (ReuseGroups)
|
||||
this.oldGroups.Add (grouping.Key, og);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
this.groupings.Insert (index, og.Key, og);
|
||||
OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Add, (object)og, index));
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче