Skip to content

Commit

Permalink
New implementation for TrackableCollection, based on CollectionChange…
Browse files Browse the repository at this point in the history
…d events of ObservableCollection
  • Loading branch information
morpheusxx committed Feb 17, 2013
1 parent e5f533b commit 8ee73a5
Showing 1 changed file with 37 additions and 60 deletions.
97 changes: 37 additions & 60 deletions TvEngine3/Mediaportal/TV/Server/TVDatabase/Entities/Model.cs
Expand Up @@ -360,94 +360,71 @@ public class TrackableCollection<T> : ObservableCollection<T>
{
private readonly HashSet<T> _lookupList = new HashSet<T>();

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<T> collection):
base(collection)
{
if (_lookupList.Contains(oldValue))
{
_lookupList.Remove(oldValue);
}
_lookupList.Add(newValue);
CollectionChanged += OnCollectionChanged;
}

public new bool Remove(T item)
public TrackableCollection(List<T> 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<T>)this).IsReadOnly; }
return _lookupList.Contains(item);
}


}

// An interface that provides an event that fires when complex properties change.
Expand Down

0 comments on commit 8ee73a5

Please sign in to comment.