Skip to content

Commit 13dfd25

Browse files
committed
Replace should always trigger a reset
A reset occurs when the list is cleared, and a replace is just a clear and add.
1 parent a9a1362 commit 13dfd25

File tree

2 files changed

+39
-23
lines changed

2 files changed

+39
-23
lines changed

RangedObservableCollection.Tests/RangedObservableCollectionTest.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ public void ReplaceRaisesCorrectCollectionChangedEvent()
7171

7272
collection.CollectionChanged += (s, e) =>
7373
{
74-
Assert.Equal(NotifyCollectionChangedAction.Replace, e.Action);
75-
Assert.Equal(ItemsToAdd, e.OldItems);
76-
Assert.Equal(new[] { 11 }, e.NewItems);
74+
Assert.Equal(NotifyCollectionChangedAction.Reset, e.Action);
75+
Assert.Null(e.OldItems);
76+
Assert.Null(e.NewItems);
7777
};
7878

7979
collection.Replace(11);
@@ -107,9 +107,9 @@ public void ReplaceRangeRaisesCorrectCollectionChangedEvent()
107107

108108
collection.CollectionChanged += (s, e) =>
109109
{
110-
Assert.Equal(NotifyCollectionChangedAction.Replace, e.Action);
111-
Assert.Equal(ItemsToAdd, e.OldItems);
112-
Assert.Equal(AlternateItemsToAdd, e.NewItems);
110+
Assert.Equal(NotifyCollectionChangedAction.Reset, e.Action);
111+
Assert.Null(e.OldItems);
112+
Assert.Null(e.NewItems);
113113
};
114114

115115
collection.ReplaceRange(AlternateItemsToAdd);

RangedObservableCollection/RangedObservableCollection.cs

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ public void AddRange(IEnumerable<T> collection)
6060
foreach (var i in changedItems)
6161
Items.Add(i);
6262

63-
OnPropertyChanged(new PropertyChangedEventArgs(CountName));
64-
OnPropertyChanged(new PropertyChangedEventArgs(IndexerName));
63+
OnCountPropertyChanged();
64+
OnIndexerPropertyChanged();
6565
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, changedItems, startIndex));
6666
}
6767

@@ -81,16 +81,13 @@ public void ReplaceRange(IEnumerable<T> collection)
8181

8282
CheckReentrancy();
8383

84-
var oldItems = new List<T>(Items);
85-
var changedItems = new List<T>(collection);
86-
8784
Items.Clear();
88-
foreach (var i in changedItems)
85+
foreach (var i in collection)
8986
Items.Add(i);
9087

91-
OnPropertyChanged(new PropertyChangedEventArgs(CountName));
92-
OnPropertyChanged(new PropertyChangedEventArgs(IndexerName));
93-
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, changedItems, oldItems));
88+
OnCountPropertyChanged();
89+
OnIndexerPropertyChanged();
90+
OnCollectionReset();
9491
}
9592

9693
/// <summary>
@@ -121,8 +118,8 @@ public void RemoveRange(IEnumerable<T> collection)
121118

122119
if (changedItems.Count > 0)
123120
{
124-
OnPropertyChanged(new PropertyChangedEventArgs(CountName));
125-
OnPropertyChanged(new PropertyChangedEventArgs(IndexerName));
121+
OnCountPropertyChanged();
122+
OnIndexerPropertyChanged();
126123
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, changedItems));
127124
}
128125
}
@@ -135,15 +132,34 @@ public void Replace(T item)
135132
{
136133
CheckReentrancy();
137134

138-
var oldItems = new List<T>(Items);
139-
var changedItems = new List<T> { item };
140-
141135
Items.Clear();
142136
Items.Add(item);
143137

144-
OnPropertyChanged(new PropertyChangedEventArgs(CountName));
145-
OnPropertyChanged(new PropertyChangedEventArgs(IndexerName));
146-
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, changedItems, oldItems));
138+
OnCountPropertyChanged();
139+
OnIndexerPropertyChanged();
140+
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
141+
}
142+
143+
private void OnCountPropertyChanged()
144+
{
145+
OnPropertyChanged(EventArgsCache.CountPropertyChanged);
146+
}
147+
148+
private void OnIndexerPropertyChanged()
149+
{
150+
OnPropertyChanged(EventArgsCache.IndexerPropertyChanged);
151+
}
152+
153+
private void OnCollectionReset()
154+
{
155+
OnCollectionChanged(EventArgsCache.ResetCollectionChanged);
156+
}
157+
158+
private static class EventArgsCache
159+
{
160+
internal static readonly PropertyChangedEventArgs CountPropertyChanged = new PropertyChangedEventArgs(CountName);
161+
internal static readonly PropertyChangedEventArgs IndexerPropertyChanged = new PropertyChangedEventArgs(IndexerName);
162+
internal static readonly NotifyCollectionChangedEventArgs ResetCollectionChanged = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
147163
}
148164
}
149165
}

0 commit comments

Comments
 (0)