Refactoring Map.ItemsSource to use NotifyCollectionChangedEventArgsExtensions (#5689)
authorAndrei Nitescu <nitescua@yahoo.com>
Wed, 24 Apr 2019 01:00:49 +0000 (04:00 +0300)
committerSamantha Houts <samhouts@users.noreply.github.com>
Wed, 24 Apr 2019 01:00:49 +0000 (18:00 -0700)
Xamarin.Forms.Maps/Map.cs

index 23c0fd2..92ddcd5 100644 (file)
@@ -5,6 +5,7 @@ using System.Collections.ObjectModel;
 using System.Collections.Specialized;
 using System.ComponentModel;
 using System.Linq;
+using Xamarin.Forms.Internals;
 
 namespace Xamarin.Forms.Maps
 {
@@ -175,37 +176,10 @@ namespace Xamarin.Forms.Maps
 
                void OnItemsSourceCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
                {
-                       switch (e.Action)
-                       {
-                               case NotifyCollectionChangedAction.Add:
-                                       if (e.NewStartingIndex == -1)
-                                               goto case NotifyCollectionChangedAction.Reset;
-                                       foreach (object item in e.NewItems)
-                                               CreatePin(item);
-                                       break;
-                               case NotifyCollectionChangedAction.Move:
-                                       if (e.OldStartingIndex == -1 || e.NewStartingIndex == -1)
-                                               goto case NotifyCollectionChangedAction.Reset;
-                                       // Not tracking order
-                                       break;
-                               case NotifyCollectionChangedAction.Remove:
-                                       if (e.OldStartingIndex == -1)
-                                               goto case NotifyCollectionChangedAction.Reset;
-                                       foreach (object item in e.OldItems)
-                                               RemovePin(item);
-                                       break;
-                               case NotifyCollectionChangedAction.Replace:
-                                       if (e.OldStartingIndex == -1)
-                                               goto case NotifyCollectionChangedAction.Reset;
-                                       foreach (object item in e.OldItems)
-                                               RemovePin(item);
-                                       foreach (object item in e.NewItems)
-                                               CreatePin(item);
-                                       break;
-                               case NotifyCollectionChangedAction.Reset:
-                                       _pins.Clear();
-                                       break;
-                       }
+                       e.Apply(
+                               insert: (item, _, __) => CreatePin(item),
+                               removeAt: (item, _) => RemovePin(item),
+                               reset: () => _pins.Clear());
                }
 
                void CreatePinItems()
@@ -237,10 +211,15 @@ namespace Xamarin.Forms.Maps
 
                void RemovePin(object itemToRemove)
                {
-                       Pin pinToRemove = _pins.FirstOrDefault(pin => pin.BindingContext?.Equals(itemToRemove) == true);
-                       if (pinToRemove != null)
+                       // Instead of just removing by item (i.e. _pins.Remove(pinToRemove))
+                       //  we need to remove by index because of how Pin.Equals() works
+                       for (int i = 0; i < _pins.Count; ++i)
                        {
-                               _pins.Remove(pinToRemove);
+                               Pin pin = _pins[i];
+                               if (pin.BindingContext?.Equals(itemToRemove) == true)
+                               {
+                                       _pins.RemoveAt(i);
+                               }
                        }
                }
        }