// -----------------------------------------------------------------------
//
// Copyright 2014 MIT Licence. See licence.md for more information.
//
// -----------------------------------------------------------------------
namespace Perspex.Collections
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
public class PerspexReadOnlyListView : IPerspexReadOnlyList, IDisposable
{
private IPerspexReadOnlyList source;
public PerspexReadOnlyListView()
: this(null)
{
}
public PerspexReadOnlyListView(IPerspexReadOnlyList source)
{
this.source = source;
if (source != null)
{
this.source.CollectionChanged += this.SourceCollectionChanged;
}
}
public event NotifyCollectionChangedEventHandler CollectionChanged;
public event PropertyChangedEventHandler PropertyChanged;
public int Count
{
get { return this.source?.Count ?? 0; }
}
public IPerspexReadOnlyList Source
{
get
{
return this.source;
}
set
{
if (this.source != null)
{
this.source.CollectionChanged -= this.SourceCollectionChanged;
if (this.CollectionChanged != null)
{
var ev = new NotifyCollectionChangedEventArgs(
NotifyCollectionChangedAction.Remove,
this.source,
0);
this.CollectionChanged(this, ev);
}
}
this.source = value;
if (this.source != null)
{
this.source.CollectionChanged += this.SourceCollectionChanged;
if (this.CollectionChanged != null)
{
var ev = new NotifyCollectionChangedEventArgs(
NotifyCollectionChangedAction.Add,
this.source.ToList(),
0);
this.CollectionChanged(this, ev);
}
}
}
}
public T this[int index]
{
get { return this.source[index]; }
}
public void Dispose()
{
this.source.CollectionChanged -= this.SourceCollectionChanged;
}
public IEnumerator GetEnumerator()
{
return (this.source != null) ?
this.source.GetEnumerator() :
Enumerable.Empty().GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
private void SourceCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (this.CollectionChanged != null)
{
NotifyCollectionChangedEventArgs ev;
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
ev = new NotifyCollectionChangedEventArgs(
NotifyCollectionChangedAction.Add,
e.NewItems.Cast