Skip to content

Commit

Permalink
Merge pull request #5 from AlexKven/master
Browse files Browse the repository at this point in the history
Added NotifyCollectionChangedAction.Remove to RemoveRange, unit tested
  • Loading branch information
jamesmontemagno authored Oct 30, 2016
2 parents 35f0ddd + 8ca0f6f commit 028bf23
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
5 changes: 4 additions & 1 deletion MvvmHelpers.Tests/MvvmHelpers.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand Down Expand Up @@ -46,4 +46,7 @@
<Name>MvvmHelpers</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
</Project>
27 changes: 26 additions & 1 deletion MvvmHelpers.Tests/ObservableRangeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,35 @@ public void GroupingTestCase()
orderby person.LastName
group person by person.SortName into personGroup
select new Grouping<string, Person>(personGroup.Key, personGroup);

grouped.AddRange(sorted);

}

[Test()]
public void RemoveRange_RemoveTest()
{
ObservableRangeCollection<int> collection = new ObservableRangeCollection<int>();
int[] toAdd = new[] { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3 };
int[] toRemove = new[] { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 0, 0 };
collection.AddRange(toAdd);
collection.CollectionChanged += (s, e) =>
{
if (e.Action != System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
Assert.Fail("RemoveRange didn't use Remove like requested.");
if (e.OldItems == null)
Assert.Fail("OldItems should not be null.");
int[] expected = new int[] { 1, 1, 2, 2, 3, 3, 4, 5, 5, 6, 7, 8, 9, 9 };
if (expected.Length != e.OldItems.Count)
Assert.Fail("Expected and actual OldItems don't match.");
for (int i = 0; i < expected.Length; i++)
{
if (expected[i] != (int)e.OldItems[i])
Assert.Fail("Expected and actual OldItems don't match.");
}
};
collection.RemoveRange(toRemove, System.Collections.Specialized.NotifyCollectionChangedAction.Remove);
}
}
}

36 changes: 31 additions & 5 deletions MvvmHelpers/ObservableRangeCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public ObservableRangeCollection(IEnumerable<T> collection)
/// </summary>
public void AddRange(IEnumerable<T> collection, NotifyCollectionChangedAction notificationMode = NotifyCollectionChangedAction.Add)
{
if (notificationMode != NotifyCollectionChangedAction.Add && notificationMode != NotifyCollectionChangedAction.Reset)
throw new ArgumentException("Mode must be either Add or Reset for AddRange.", "notificationMode");
if (collection == null)
throw new ArgumentNullException("collection");

Expand Down Expand Up @@ -69,16 +71,40 @@ public void AddRange(IEnumerable<T> collection, NotifyCollectionChangedAction no
}

/// <summary>
/// Removes the first occurence of each item in the specified collection from ObservableCollection(Of T).
/// Removes the first occurence of each item in the specified collection from ObservableCollection(Of T). NOTE: with notificationMode = Remove, removed items starting index is not set because items are not guaranteed to be consecutive.
/// </summary>
public void RemoveRange(IEnumerable<T> collection)
public void RemoveRange(IEnumerable<T> collection, NotifyCollectionChangedAction notificationMode = NotifyCollectionChangedAction.Reset)
{
if (notificationMode != NotifyCollectionChangedAction.Remove && notificationMode != NotifyCollectionChangedAction.Reset)
throw new ArgumentException("Mode must be either Remove or Reset for RemoveRange.", "notificationMode");
if (collection == null)
throw new ArgumentNullException("collection");

foreach (var i in collection)
Items.Remove(i);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
CheckReentrancy();

if (notificationMode == NotifyCollectionChangedAction.Reset)
{

foreach (var i in collection)
Items.Remove(i);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));

return;
}

var changedItems = collection is List<T> ? (List<T>)collection : new List<T>(collection);
for (int i = 0; i < changedItems.Count; i++)
{
if (!Items.Remove(changedItems[i]))
{
changedItems.RemoveAt(i); //Can't use a foreach because changedItems is intended to be (carefully) modified
i--;
}
}

OnPropertyChanged(new PropertyChangedEventArgs("Count"));
OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, changedItems, -1));
}

/// <summary>
Expand Down

0 comments on commit 028bf23

Please sign in to comment.