Skip to content

Commit

Permalink
Refactored code.
Browse files Browse the repository at this point in the history
  • Loading branch information
mahara committed Jun 29, 2024
1 parent ab3d9bd commit b85e862
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -299,4 +299,42 @@ public void CanRemoveRangeAllByIndexAndCount_NonEmptyObservableCollection()

Assert.That(collection, Has.Count.EqualTo(itemsCount));
}

[Test]
public void CanReplaceRange_NonEmptyObservableCollection()
{
NotifyCollectionChangedEventArgs args = null!;
var notificationCount = 0;

var collection = new ObservableCollection<int>(_items);
var itemsCount = collection.Count;

Assert.That(collection, Has.Count.EqualTo(itemsCount));

using var _ = new CollectionChangedEventSubscription(
collection,
(o, e) =>
{
args = e;
notificationCount++;
});

var replaceStartingIndex = 4;
var replacedItemsCount = 4;

collection.ReplaceRange(replaceStartingIndex, replacedItemsCount, _items);

Assert.That(args, Is.Not.Null);
Assert.That(notificationCount, Is.GreaterThanOrEqualTo(1));

var addedItems = args.NewItems! as IEnumerable;

Assert.That(addedItems, Is.Not.Null);
Assert.That(addedItems.Cast<object>().Count(), Is.EqualTo(itemsCount));
Assert.That(args.NewStartingIndex, Is.EqualTo(replaceStartingIndex));

var newItemsCount = _items.Count - 4 + _items.Count;

Assert.That(collection, Has.Count.EqualTo(newItemsCount));
}
}
22 changes: 21 additions & 1 deletion src/NHibernate.ObservableCollections/ObservableCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace Iesi.Collections.Generic;
/// - <see href="https://github.com/dotnet/runtime/issues/18087" />
/// - <see href="https://gist.github.com/weitzhandler/65ac9113e31d12e697cb58cd92601091" />
/// - <see href="https://stackoverflow.com/questions/670577/observablecollection-doesnt-support-addrange-method-so-i-get-notified-for-each" />
/// - <see href="https://github.com/CodingOctocat/WpfObservableRangeCollection" />
/// - <see href="https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Collections/ObjectModel/Collection.cs" />
/// - <see href="https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/List.cs" />
/// - <see href="https://github.com/dotnet/runtime/blob/main/src/libraries/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs" />
Expand Down Expand Up @@ -175,6 +176,9 @@ protected virtual void MoveItem(int oldIndex, int newIndex)
/// </summary>
protected override void ClearItems()
{
using var _ = BlockReentrancy();
using var __ = DeferEvents();

CheckReentrancy();

base.ClearItems();
Expand All @@ -191,6 +195,9 @@ protected override void ClearItems()
/// <param name="collection"></param>
public void AddRange(IEnumerable<T> collection)
{
using var _ = BlockReentrancy();
using var __ = DeferEvents();

InsertItemsRange(Count, collection);
}

Expand All @@ -202,6 +209,9 @@ public void AddRange(IEnumerable<T> collection)
/// <param name="collection"></param>
public void InsertRange(int index, IEnumerable<T> collection)
{
using var _ = BlockReentrancy();
using var __ = DeferEvents();

InsertItemsRange(index, collection);
}

Expand All @@ -212,6 +222,9 @@ public void InsertRange(int index, IEnumerable<T> collection)
/// <param name="collection"></param>
public void RemoveRange(IEnumerable<T> collection)
{
using var _ = BlockReentrancy();
using var __ = DeferEvents();

RemoveItemsRange(collection);
}

Expand All @@ -223,6 +236,9 @@ public void RemoveRange(IEnumerable<T> collection)
/// <param name="count"></param>
public void RemoveRange(int index, int count)
{
using var _ = BlockReentrancy();
using var __ = DeferEvents();

RemoveItemsRange(index, count);
}

Expand All @@ -235,6 +251,9 @@ public void RemoveRange(int index, int count)
/// <param name="collection"></param>
public void ReplaceRange(int index, int count, IEnumerable<T> collection)
{
using var _ = BlockReentrancy();
using var __ = DeferEvents();

RemoveItemsRange(index, count);
InsertItemsRange(index, collection);
}
Expand Down Expand Up @@ -293,6 +312,7 @@ protected virtual void RemoveItemsRange(IEnumerable<T> collection)
else if (countable.Count == 1)
{
using var enumerator = countable.GetEnumerator();

enumerator.MoveNext();

Remove(enumerator.Current);
Expand Down Expand Up @@ -546,7 +566,7 @@ private sealed class SimpleMonitor : IDisposable
internal int _busyCount; // Only used during (de)serialization to maintain compatibility with desktop. Do not rename (binary serialization)

[NonSerialized]
internal ObservableCollection<T> _collection = null!;
internal ObservableCollection<T> _collection;

public SimpleMonitor(ObservableCollection<T> collection)
{
Expand Down

0 comments on commit b85e862

Please sign in to comment.