Skip to content

Commit

Permalink
Empty doesn't raise events.
Browse files Browse the repository at this point in the history
  • Loading branch information
Berrysoft committed Apr 26, 2019
1 parent 0fc604c commit 3426744
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 16 deletions.
35 changes: 31 additions & 4 deletions MvvmHelpers.UnitTests/ObservableRangeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ public void AddRange()
{
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 };

collection.CollectionChanged += (s, e) =>
{
Assert.AreEqual(e.Action,
NotifyCollectionChangedAction.Add,
"AddRange didn't use Add like requested.");

Assert.IsNull(e.OldItems, "OldItems should be null.");
Assert.AreEqual(toAdd.Length,
e.NewItems.Count,

Assert.AreEqual(toAdd.Length,
e.NewItems.Count,
"Expected and actual OldItems don't match.");

for (int i = 0; i < toAdd.Length; i++)
Expand All @@ -36,6 +36,19 @@ public void AddRange()
collection.AddRange(toAdd);
}

[TestMethod]
public void AddRangeEmpty()
{
ObservableRangeCollection<int> collection = new ObservableRangeCollection<int>();
int[] toAdd = new int[0];

collection.CollectionChanged += (s, e) =>
{
Assert.Fail("The event is raised.");
};
collection.AddRange(toAdd);
}

[TestMethod]
public void ReplaceRange()
{
Expand Down Expand Up @@ -89,6 +102,20 @@ public void RemoveRangeRemoveTestMethod()
collection.RemoveRange(toRemove, NotifyCollectionChangedAction.Remove);

}

[TestMethod]
public void RemoveRangeEmpty()
{
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 int[0];
collection.AddRange(toAdd);
collection.CollectionChanged += (s, e) =>
{
Assert.Fail("The event is raised.");
};
collection.RemoveRange(toRemove, NotifyCollectionChangedAction.Remove);
}
}
}

41 changes: 29 additions & 12 deletions MvvmHelpers/ObservableRangeCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,19 @@ public void AddRange(IEnumerable<T> collection, NotifyCollectionChangedAction no

if (notificationMode == NotifyCollectionChangedAction.Reset)
{
bool raiseEvents = false;
foreach (var i in collection)
{
Items.Add(i);
raiseEvents = true;
}

OnPropertyChanged(new PropertyChangedEventArgs("Count"));
OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
if (raiseEvents)
{
OnPropertyChanged(new PropertyChangedEventArgs("Count"));
OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}

return;
}
Expand All @@ -61,9 +68,12 @@ public void AddRange(IEnumerable<T> collection, NotifyCollectionChangedAction no
foreach (var i in changedItems)
Items.Add(i);

OnPropertyChanged(new PropertyChangedEventArgs("Count"));
OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, changedItems, startIndex));
if (changedItems.Count > 0)
{
OnPropertyChanged(new PropertyChangedEventArgs("Count"));
OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, changedItems, startIndex));
}
}

/// <summary>
Expand All @@ -80,15 +90,19 @@ public void RemoveRange(IEnumerable<T> collection, NotifyCollectionChangedAction

if (notificationMode == NotifyCollectionChangedAction.Reset)
{

bool raiseEvents = false;
foreach (var i in collection)
{
Items.Remove(i);
raiseEvents = true;
}

OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
if (raiseEvents)
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++)
{
Expand All @@ -99,9 +113,12 @@ public void RemoveRange(IEnumerable<T> collection, NotifyCollectionChangedAction
}
}

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

/// <summary>
Expand Down

0 comments on commit 3426744

Please sign in to comment.