diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2019-12-30 11:27:25 -0500 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2019-12-30 11:27:25 -0500 |
commit | d9a9cef172be8c34b57e345732bf4e582898bcbc (patch) | |
tree | 0aa2cebde0aad80e794587295d51f7e0ff301fe7 /src/SMAPI/Framework/StateTracking/FieldWatchers | |
parent | 3818d53752a5697f5e92c44cdbc976cfce17bad4 (diff) | |
download | SMAPI-d9a9cef172be8c34b57e345732bf4e582898bcbc.tar.gz SMAPI-d9a9cef172be8c34b57e345732bf4e582898bcbc.tar.bz2 SMAPI-d9a9cef172be8c34b57e345732bf4e582898bcbc.zip |
fix ObservableCollection watcher not handling list reset (#685)
Diffstat (limited to 'src/SMAPI/Framework/StateTracking/FieldWatchers')
-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); + } + } } } } |