using System; using System.Collections.Generic; using System.Collections.ObjectModel; namespace UniRx { public struct CollectionAddEvent : IEquatable> { public int Index { get; private set; } public T Value { get; private set; } public CollectionAddEvent(int index, T value) :this() { Index = index; Value = value; } public override string ToString() { return string.Format("Index:{0} Value:{1}", Index, Value); } public override int GetHashCode() { return Index.GetHashCode() ^ EqualityComparer.Default.GetHashCode(Value) << 2; } public bool Equals(CollectionAddEvent other) { return Index.Equals(other.Index) && EqualityComparer.Default.Equals(Value, other.Value); } } public struct CollectionRemoveEvent : IEquatable> { public int Index { get; private set; } public T Value { get; private set; } public CollectionRemoveEvent(int index, T value) : this() { Index = index; Value = value; } public override string ToString() { return string.Format("Index:{0} Value:{1}", Index, Value); } public override int GetHashCode() { return Index.GetHashCode() ^ EqualityComparer.Default.GetHashCode(Value) << 2; } public bool Equals(CollectionRemoveEvent other) { return Index.Equals(other.Index) && EqualityComparer.Default.Equals(Value, other.Value); } } public struct CollectionMoveEvent : IEquatable> { public int OldIndex { get; private set; } public int NewIndex { get; private set; } public T Value { get; private set; } public CollectionMoveEvent(int oldIndex, int newIndex, T value) : this() { OldIndex = oldIndex; NewIndex = newIndex; Value = value; } public override string ToString() { return string.Format("OldIndex:{0} NewIndex:{1} Value:{2}", OldIndex, NewIndex, Value); } public override int GetHashCode() { return OldIndex.GetHashCode() ^ NewIndex.GetHashCode() << 2 ^ EqualityComparer.Default.GetHashCode(Value) >> 2; } public bool Equals(CollectionMoveEvent other) { return OldIndex.Equals(other.OldIndex) && NewIndex.Equals(other.NewIndex) && EqualityComparer.Default.Equals(Value, other.Value); } } public struct CollectionReplaceEvent : IEquatable> { public int Index { get; private set; } public T OldValue { get; private set; } public T NewValue { get; private set; } public CollectionReplaceEvent(int index, T oldValue, T newValue) : this() { Index = index; OldValue = oldValue; NewValue = newValue; } public override string ToString() { return string.Format("Index:{0} OldValue:{1} NewValue:{2}", Index, OldValue, NewValue); } public override int GetHashCode() { return Index.GetHashCode() ^ EqualityComparer.Default.GetHashCode(OldValue) << 2 ^ EqualityComparer.Default.GetHashCode(NewValue) >> 2; } public bool Equals(CollectionReplaceEvent other) { return Index.Equals(other.Index) && EqualityComparer.Default.Equals(OldValue, other.OldValue) && EqualityComparer.Default.Equals(NewValue, other.NewValue); } } // IReadOnlyList is from .NET 4.5 public interface IReadOnlyReactiveCollection : IEnumerable { int Count { get; } T this[int index] { get; } IObservable> ObserveAdd(); IObservable ObserveCountChanged(bool notifyCurrentCount = false); IObservable> ObserveMove(); IObservable> ObserveRemove(); IObservable> ObserveReplace(); IObservable ObserveReset(); } public interface IReactiveCollection : IList, IReadOnlyReactiveCollection { new int Count { get; } new T this[int index] { get; set; } void Move(int oldIndex, int newIndex); } [Serializable] public class ReactiveCollection : Collection, IReactiveCollection, IDisposable { [NonSerialized] bool isDisposed = false; public ReactiveCollection() { } public ReactiveCollection(IEnumerable collection) { if (collection == null) throw new ArgumentNullException("collection"); foreach (var item in collection) { Add(item); } } public ReactiveCollection(List list) : base(list != null ? new List(list) : null) { } protected override void ClearItems() { var beforeCount = Count; base.ClearItems(); if (collectionReset != null) collectionReset.OnNext(Unit.Default); if (beforeCount > 0) { if (countChanged != null) countChanged.OnNext(Count); } } protected override void InsertItem(int index, T item) { base.InsertItem(index, item); if (collectionAdd != null) collectionAdd.OnNext(new CollectionAddEvent(index, item)); if (countChanged != null) countChanged.OnNext(Count); } public void Move(int oldIndex, int newIndex) { MoveItem(oldIndex, newIndex); } protected virtual void MoveItem(int oldIndex, int newIndex) { T item = this[oldIndex]; base.RemoveItem(oldIndex); base.InsertItem(newIndex, item); if (collectionMove != null) collectionMove.OnNext(new CollectionMoveEvent(oldIndex, newIndex, item)); } protected override void RemoveItem(int index) { T item = this[index]; base.RemoveItem(index); if (collectionRemove != null) collectionRemove.OnNext(new CollectionRemoveEvent(index, item)); if (countChanged != null) countChanged.OnNext(Count); } protected override void SetItem(int index, T item) { T oldItem = this[index]; base.SetItem(index, item); if (collectionReplace != null) collectionReplace.OnNext(new CollectionReplaceEvent(index, oldItem, item)); } [NonSerialized] Subject countChanged = null; public IObservable ObserveCountChanged(bool notifyCurrentCount = false) { if (isDisposed) return Observable.Empty(); var subject = countChanged ?? (countChanged = new Subject()); if (notifyCurrentCount) { return subject.StartWith(() => this.Count); } else { return subject; } } [NonSerialized] Subject collectionReset = null; public IObservable ObserveReset() { if (isDisposed) return Observable.Empty(); return collectionReset ?? (collectionReset = new Subject()); } [NonSerialized] Subject> collectionAdd = null; public IObservable> ObserveAdd() { if (isDisposed) return Observable.Empty>(); return collectionAdd ?? (collectionAdd = new Subject>()); } [NonSerialized] Subject> collectionMove = null; public IObservable> ObserveMove() { if (isDisposed) return Observable.Empty>(); return collectionMove ?? (collectionMove = new Subject>()); } [NonSerialized] Subject> collectionRemove = null; public IObservable> ObserveRemove() { if (isDisposed) return Observable.Empty>(); return collectionRemove ?? (collectionRemove = new Subject>()); } [NonSerialized] Subject> collectionReplace = null; public IObservable> ObserveReplace() { if (isDisposed) return Observable.Empty>(); return collectionReplace ?? (collectionReplace = new Subject>()); } void DisposeSubject(ref Subject subject) { if (subject != null) { try { subject.OnCompleted(); } finally { subject.Dispose(); subject = null; } } } #region IDisposable Support private bool disposedValue = false; protected virtual void Dispose(bool disposing) { if (!disposedValue) { if (disposing) { DisposeSubject(ref collectionReset); DisposeSubject(ref collectionAdd); DisposeSubject(ref collectionMove); DisposeSubject(ref collectionRemove); DisposeSubject(ref collectionReplace); DisposeSubject(ref countChanged); } disposedValue = true; } } public void Dispose() { Dispose(true); } #endregion } public static partial class ReactiveCollectionExtensions { public static ReactiveCollection ToReactiveCollection(this IEnumerable source) { return new ReactiveCollection(source); } } }