Skip to content

Commit

Permalink
Merge pull request #49 from ahmedalejo/patch-1
Browse files Browse the repository at this point in the history
Refactored ObservableRangeCollection change notifications
  • Loading branch information
jamesmontemagno authored Sep 26, 2019
2 parents 5dcb87d + 2cf2ef6 commit 6862d6b
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions MvvmHelpers/ObservableRangeCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ public void AddRange(IEnumerable<T> collection, NotifyCollectionChangedAction no
}

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

return;
}
Expand All @@ -70,10 +66,11 @@ public void AddRange(IEnumerable<T> collection, NotifyCollectionChangedAction no

if (changedItems.Count == 0)
return;

OnPropertyChanged(new PropertyChangedEventArgs("Count"));
OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, changedItems, startIndex));

RaiseChangeNotificationEvents(
action: NotifyCollectionChangedAction.Add,
changedItems: changedItems,
startingIndex: startIndex);
}

/// <summary>
Expand All @@ -98,7 +95,7 @@ public void RemoveRange(IEnumerable<T> collection, NotifyCollectionChangedAction
}

if (raiseEvents)
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
RaiseChangeNotificationEvents(action: NotifyCollectionChangedAction.Reset);

return;
}
Expand All @@ -116,9 +113,9 @@ public void RemoveRange(IEnumerable<T> collection, NotifyCollectionChangedAction
if (changedItems.Count == 0)
return;

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

/// <summary>
Expand All @@ -138,6 +135,15 @@ public void ReplaceRange(IEnumerable<T> collection)
AddRange(collection, NotifyCollectionChangedAction.Reset);
}

private void RaiseChangeNotificationEvents(NotifyCollectionChangedAction action, List<T> changedItems = null, int startingIndex = -1)
{
OnPropertyChanged(new PropertyChangedEventArgs(nameof(Count)));
OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));

if (changedItems is null)
OnCollectionChanged(new NotifyCollectionChangedEventArgs(action));
else
OnCollectionChanged(new NotifyCollectionChangedEventArgs(action, changedItems: changedItems, startingIndex: startingIndex));
}
}
}

0 comments on commit 6862d6b

Please sign in to comment.