using DTS.Common.Utilities.Logging;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
namespace DTS.Common.Classes
{
///
/// this is a helper class to avoid performance issues
/// adding or removing a bulk sequence of items from the collection
/// by firing just one notification rather than n
///
///
public class BulkObservableCollection : ObservableCollection
{
public void AddRange(IEnumerable items)
{
try
{
// Turn off notifications (check if anyone is listening first)
CheckReentrancy();
foreach (var item in items) { Items.Add(item); }
// Fire a single notification that the entire "Reset" happened
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
OnPropertyChanged(new PropertyChangedEventArgs(nameof(Count)));
}
catch( Exception ex)
{
APILogger.Log(ex);
}
}
public void RemoveRange(IEnumerable itemsToRemove)
{
try
{
CheckReentrancy();
foreach (var item in itemsToRemove)
{
Items.Remove(item);
}
// Fire one single notification for the entire operation
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
OnPropertyChanged(new PropertyChangedEventArgs(nameof(Count)));
}
catch( Exception ex)
{
APILogger.Log(ex);
}
}
}
}