fix ObservableCollection watcher not handling list reset (#685)

This commit is contained in:
Jesse Plamondon-Willard 2019-12-30 11:27:25 -05:00
parent 3818d53752
commit d9a9cef172
No known key found for this signature in database
GPG Key ID: CF8B1456B3E29F49
1 changed files with 24 additions and 4 deletions

View File

@ -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);
}
}
}
}
}