From 8ee73a5f6c8e41b61c3ae84aaa336ccd3facd639 Mon Sep 17 00:00:00 2001 From: morpheusxx Date: Sun, 17 Feb 2013 10:06:26 +0100 Subject: [PATCH] New implementation for TrackableCollection, based on CollectionChanged events of ObservableCollection --- .../TV/Server/TVDatabase/Entities/Model.cs | 97 +++++++------------ 1 file changed, 37 insertions(+), 60 deletions(-) diff --git a/TvEngine3/Mediaportal/TV/Server/TVDatabase/Entities/Model.cs b/TvEngine3/Mediaportal/TV/Server/TVDatabase/Entities/Model.cs index e7cb83ef01c..723e12f4442 100644 --- a/TvEngine3/Mediaportal/TV/Server/TVDatabase/Entities/Model.cs +++ b/TvEngine3/Mediaportal/TV/Server/TVDatabase/Entities/Model.cs @@ -360,94 +360,71 @@ public class TrackableCollection : ObservableCollection { private readonly HashSet _lookupList = new HashSet(); - private void InsertIntoLookUp(T item) + private void AddRange(IList list) { - _lookupList.Add(item); + if (list == null) + return; + foreach (T item in list) + _lookupList.Add(item); } - public new bool Contains(T item) - { - return _lookupList.Contains(item); - } - - public new void Insert(int index, T item) + private void RemoveRange(IList list) { - if (!Contains(item)) - { - base.Insert(index, item); - InsertIntoLookUp(item); - } + if (list == null) + return; + foreach (T item in list) + _lookupList.Remove(item); } - public new void RemoveAt(int index) - { - base.RemoveAt(index); - T item = this[index]; - RemoveFromLookUp(item); + private void Reset () + { + _lookupList.Clear(); + foreach (T item in this) + _lookupList.Add(item); } - private void RemoveFromLookUp(T item) + public TrackableCollection () { - if (_lookupList.Contains(item)) - { - _lookupList.Remove(item); - } + CollectionChanged += OnCollectionChanged; } - private void ReplaceIntoLookUp(T oldValue, T newValue) + public TrackableCollection(IEnumerable collection): + base(collection) { - if (_lookupList.Contains(oldValue)) - { - _lookupList.Remove(oldValue); - } - _lookupList.Add(newValue); + CollectionChanged += OnCollectionChanged; } - public new bool Remove(T item) + public TrackableCollection(List list) + : base(list) { - bool value = base.Remove(item); - RemoveFromLookUp(item); - return value; + CollectionChanged += OnCollectionChanged; } - public new T this[int index] + private void OnCollectionChanged (object sender, NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs) { - get { return base[index]; } - set + if (notifyCollectionChangedEventArgs.Action == NotifyCollectionChangedAction.Add) { - T oldValue = base[index]; - ReplaceIntoLookUp(oldValue, value); - base[index] = value; + AddRange(notifyCollectionChangedEventArgs.NewItems); } - } - - - public new void Add(T item) - { - if (!Contains(item)) + if (notifyCollectionChangedEventArgs.Action == NotifyCollectionChangedAction.Remove) { - base.Add(item); - InsertIntoLookUp(item); + RemoveRange(notifyCollectionChangedEventArgs.OldItems); } - } - - public new void Clear() - { - int count = Count; - for (int index = 0; index < count; index++) + if (notifyCollectionChangedEventArgs.Action == NotifyCollectionChangedAction.Replace) { - RemoveAt(0); + RemoveRange(notifyCollectionChangedEventArgs.OldItems); + AddRange(notifyCollectionChangedEventArgs.NewItems); + } + if (notifyCollectionChangedEventArgs.Action == NotifyCollectionChangedAction.Reset) + { + Reset(); } - - _lookupList.Clear(); } - bool IsReadOnly + public new bool Contains(T item) { - get { return ((ICollection)this).IsReadOnly; } + return _lookupList.Contains(item); } - - } // An interface that provides an event that fires when complex properties change.