fix ObservableCollection watcher not handling list reset (#685)
This commit is contained in:
parent
3818d53752
commit
d9a9cef172
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue