using PowerLauncher.ViewModel; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Collections.Specialized; using System.Diagnostics; using System.Windows; using System.Windows.Threading; namespace PowerLauncher.Helper { public class ResultCollection : ObservableCollection { /// /// This private variable holds the flag to /// turn on and off the collection changed notification. /// private bool suspendCollectionChangeNotification; /// /// Initializes a new instance of the FastObservableCollection class. /// public ResultCollection() : base() { this.suspendCollectionChangeNotification = false; } /// /// This event is overriden CollectionChanged event of the observable collection. /// //public override event NotifyCollectionChangedEventHandler CollectionChanged; /// /// Raises collection change event. /// public void NotifyChanges() { this.ResumeCollectionChangeNotification(); OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); } /// /// Resumes collection changed notification. /// public void ResumeCollectionChangeNotification() { this.suspendCollectionChangeNotification = false; } /// /// Suspends collection changed notification. /// public void SuspendCollectionChangeNotification() { this.suspendCollectionChangeNotification = true; } /// /// This method removes all items that match a predicate /// /// predicate public void RemovePredicate(Predicate predicate) { CheckReentrancy(); this.SuspendCollectionChangeNotification(); for (int i = Count - 1; i >= 0; i--) { if (predicate(this[i])) { RemoveAt(i); } } } /// /// Update the results collection with new results, try to keep identical results /// /// public void Update(List newItems) { if (newItems == null) { throw new ArgumentNullException(nameof(newItems)); } int newCount = newItems.Count; int oldCount = Items.Count; int location = newCount > oldCount ? oldCount : newCount; this.SuspendCollectionChangeNotification(); for (int i = 0; i < location; i++) { ResultViewModel oldResult = this[i]; ResultViewModel newResult = newItems[i]; if (!oldResult.Equals(newResult)) { // result is not the same update it in the current index this[i] = newResult; } else if (oldResult.Result.Score != newResult.Result.Score) { this[i].Result.Score = newResult.Result.Score; } } if (newCount >= oldCount) { for (int i = oldCount; i < newCount; i++) { Add(newItems[i]); } } else { for (int i = oldCount - 1; i >= newCount; i--) { RemoveAt(i); } } } /// /// This collection changed event performs thread safe event raising. /// /// The event argument. protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e) { // Recommended is to avoid reentry // in collection changed event while collection // is getting changed on other thread. if(!this.suspendCollectionChangeNotification) { base.OnCollectionChanged(e); } } } }