diff options
Diffstat (limited to 'src/SMAPI/Framework/StateTracking/FieldWatchers/ObservableCollectionWatcher.cs')
-rw-r--r-- | src/SMAPI/Framework/StateTracking/FieldWatchers/ObservableCollectionWatcher.cs | 28 |
1 files changed, 24 insertions, 4 deletions
diff --git a/src/SMAPI/Framework/StateTracking/FieldWatchers/ObservableCollectionWatcher.cs b/src/SMAPI/Framework/StateTracking/FieldWatchers/ObservableCollectionWatcher.cs index 883b1023..c29d2783 100644 --- a/src/SMAPI/Framework/StateTracking/FieldWatchers/ObservableCollectionWatcher.cs +++ b/src/SMAPI/Framework/StateTracking/FieldWatchers/ObservableCollectionWatcher.cs @@ -21,6 +21,9 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers /// <summary>The pairs removed since the last reset.</summary> private readonly List<TValue> RemovedImpl = new List<TValue>(); + /// <summary>The previous values as of the last update.</summary> + private readonly List<TValue> PreviousValues = new List<TValue>(); + /********* ** Accessors @@ -78,10 +81,27 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers /// <param name="e">The event arguments.</param> private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { - if (e.NewItems != null) - this.AddedImpl.AddRange(e.NewItems.Cast<TValue>()); - if (e.OldItems != null) - this.RemovedImpl.AddRange(e.OldItems.Cast<TValue>()); + if (e.Action == NotifyCollectionChangedAction.Reset) + { + this.RemovedImpl.AddRange(this.PreviousValues); + this.PreviousValues.Clear(); + } + else + { + TValue[] added = e.NewItems?.Cast<TValue>().ToArray(); + TValue[] removed = e.OldItems?.Cast<TValue>().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); + } + } } } } |