using System.Collections.Generic; using System.Collections.ObjectModel; using System.Collections.Specialized; using System.Linq; namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers { /// A watcher which detects changes to an observable collection. /// The value type within the collection. internal class ObservableCollectionWatcher : BaseDisposableWatcher, ICollectionWatcher { /********* ** Fields *********/ /// The field being watched. private readonly ObservableCollection Field; /// The pairs added since the last reset. private readonly List AddedImpl = new(); /// The pairs removed since the last reset. private readonly List RemovedImpl = new(); /// The previous values as of the last update. private readonly List PreviousValues = new(); /********* ** Accessors *********/ /// public string Name { get; } /// public bool IsChanged => this.AddedImpl.Count > 0 || this.RemovedImpl.Count > 0; /// public IEnumerable Added => this.AddedImpl; /// public IEnumerable Removed => this.RemovedImpl; /********* ** Public methods *********/ /// Construct an instance. /// A name which identifies what the watcher is watching, used for troubleshooting. /// The field to watch. public ObservableCollectionWatcher(string name, ObservableCollection field) { this.Name = name; this.Field = field; field.CollectionChanged += this.OnCollectionChanged; } /// public void Update() { this.AssertNotDisposed(); } /// public void Reset() { this.AssertNotDisposed(); this.AddedImpl.Clear(); this.RemovedImpl.Clear(); } /// public override void Dispose() { if (!this.IsDisposed) this.Field.CollectionChanged -= this.OnCollectionChanged; base.Dispose(); } /********* ** Private methods *********/ /// A callback invoked when an entry is added or removed from the collection. /// The event sender. /// The event arguments. private void OnCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) { if (e.Action == NotifyCollectionChangedAction.Reset) { this.RemovedImpl.AddRange(this.PreviousValues); this.PreviousValues.Clear(); } else { TValue[]? added = e.NewItems?.Cast().ToArray(); TValue[]? removed = e.OldItems?.Cast().ToArray(); if (removed != null) { this.RemovedImpl.AddRange(removed); this.PreviousValues.RemoveRange(e.OldStartingIndex, removed.Length); } if (added != null) { this.AddedImpl.AddRange(added); this.PreviousValues.InsertRange(e.NewStartingIndex, added); } } } } }