Skip to content

Commit

Permalink
Added RaiseChangeNotificationEvents constructor for Replace notificat…
Browse files Browse the repository at this point in the history
…ions. Updated tests.
  • Loading branch information
simusr2 committed Mar 18, 2021
1 parent 70529f5 commit 6567ab5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
11 changes: 6 additions & 5 deletions MvvmHelpers.UnitTests/ObservableRangeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,8 @@ public void ReplaceRange()
collection.CollectionChanged += (s, e) =>
{
Assert.AreEqual(e.Action,
NotifyCollectionChangedAction.Reset,
"ReplaceRange didn't use Remove like requested.");
Assert.IsNull(e.OldItems, "OldItems should be null.");
Assert.IsNull(e.NewItems, "NewItems should be null.");
NotifyCollectionChangedAction.Replace,
"ReplaceRange didn't raised Replace like requested.");
Assert.AreEqual(collection.Count, toAdd.Length, "Lengths are not the same");
Expand All @@ -83,6 +80,10 @@ public void ReplaceRange_on_non_empty_collection_should_always_raise_collection_

collection.CollectionChanged += (s, e) =>
{
Assert.AreEqual(e.Action,
NotifyCollectionChangedAction.Reset,
"ReplaceRange didn't raised Reset like requested.");
eventRaised = true;
};

Expand Down
19 changes: 16 additions & 3 deletions MvvmHelpers/ObservableRangeCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;

using System.Linq;

namespace MvvmHelpers
{
Expand Down Expand Up @@ -128,6 +128,8 @@ public void ReplaceRange(IEnumerable<T> collection)

var previouslyEmpty = Items.Count == 0;

var oldItems = new List<T>(Items);

Items.Clear();

AddArrangeCore(collection);
Expand All @@ -137,7 +139,10 @@ public void ReplaceRange(IEnumerable<T> collection)
if (previouslyEmpty && currentlyEmpty)
return;

RaiseChangeNotificationEvents(action: currentlyEmpty ? NotifyCollectionChangedAction.Reset : NotifyCollectionChangedAction.Replace);
if (currentlyEmpty)
RaiseChangeNotificationEvents(action: NotifyCollectionChangedAction.Reset);
else
RaiseChangeNotificationEvents(action: NotifyCollectionChangedAction.Replace, oldItems.ToList(), collection.ToList());
}

private bool AddArrangeCore(IEnumerable<T> collection)
Expand All @@ -151,7 +156,7 @@ private bool AddArrangeCore(IEnumerable<T> collection)
return itemAdded;
}

private void RaiseChangeNotificationEvents(NotifyCollectionChangedAction action, List<T>? changedItems = null, int startingIndex = -1)
private void RaiseChangeNotificationEvents(NotifyCollectionChangedAction action, List<T>? changedItems = null, int startingIndex = -1)
{
OnPropertyChanged(new PropertyChangedEventArgs(nameof(Count)));
OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
Expand All @@ -161,5 +166,13 @@ private void RaiseChangeNotificationEvents(NotifyCollectionChangedAction action,
else
OnCollectionChanged(new NotifyCollectionChangedEventArgs(action, changedItems: changedItems, startingIndex: startingIndex));
}

private void RaiseChangeNotificationEvents(NotifyCollectionChangedAction action, List<T>? newItems, List<T>? oldItems)
{
OnPropertyChanged(new PropertyChangedEventArgs(nameof(Count)));
OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));

OnCollectionChanged(new NotifyCollectionChangedEventArgs(action, newItems, oldItems));
}
}
}

0 comments on commit 6567ab5

Please sign in to comment.