|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Collections.ObjectModel; |
| 4 | +using System.Collections.Specialized; |
| 5 | +using System.ComponentModel; |
| 6 | +using System.Linq; |
| 7 | + |
| 8 | +namespace CodingNinja.Wpf.ObjectModel; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed. |
| 12 | +/// <para> |
| 13 | +/// Forked from <see href="https://github.com/xamarin/XamarinCommunityToolkit/blob/main/src/CommunityToolkit/Xamarin.CommunityToolkit/ObjectModel/ObservableRangeCollection.shared.cs">XamarinCommunityToolkit/src/CommunityToolkit/Xamarin.CommunityToolkit/ObjectModel/ObservableRangeCollection.shared.cs</see> |
| 14 | +/// </para> |
| 15 | +/// <para> |
| 16 | +/// Instead, it is merged from <see href="https://github.com/jamesmontemagno/mvvm-helpers/blob/master/MvvmHelpers/ObservableRangeCollection.cs">mvvm-helpers/MvvmHelpers/ObservableRangeCollection.cs</see> |
| 17 | +/// </para> |
| 18 | +/// </summary> |
| 19 | +/// <typeparam name="T"></typeparam> |
| 20 | +public class ObservableRangeCollection<T> : ObservableCollection<T> |
| 21 | +{ |
| 22 | + /// <summary> |
| 23 | + /// Initializes a new instance of the <seealso cref="ObservableCollection{T}"/> class. |
| 24 | + /// </summary> |
| 25 | + public ObservableRangeCollection() : base() |
| 26 | + { } |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Initializes a new instance of the <seealso cref="ObservableCollection{T}"/> class that contains elements copied from the specified collection. |
| 30 | + /// </summary> |
| 31 | + /// <param name="collection">collection: The collection from which the elements are copied.</param> |
| 32 | + /// <exception cref="ArgumentNullException">The collection parameter cannot be null.</exception> |
| 33 | + public ObservableRangeCollection(IEnumerable<T> collection) : base(collection) |
| 34 | + { } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Adds the elements of the specified collection to the end of the <seealso cref="ObservableCollection{T}"/>. |
| 38 | + /// </summary> |
| 39 | + public void AddRange(IEnumerable<T> collection, NotifyCollectionChangedAction notificationMode = NotifyCollectionChangedAction.Add) |
| 40 | + { |
| 41 | + if (notificationMode is not NotifyCollectionChangedAction.Add and not NotifyCollectionChangedAction.Reset) |
| 42 | + { |
| 43 | + throw new ArgumentException("Mode must be either Add or Reset for AddRange.", nameof(notificationMode)); |
| 44 | + } |
| 45 | + |
| 46 | + if (collection.TryGetNonEnumeratedCount(out int count) && count == 1) |
| 47 | + { |
| 48 | + Add(collection.First()); |
| 49 | + |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + var changedItems = collection is List<T> list ? list : new List<T>(collection); |
| 54 | + |
| 55 | + if (changedItems.Count == 1) |
| 56 | + { |
| 57 | + Add(changedItems[0]); |
| 58 | + |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + CheckReentrancy(); |
| 63 | + |
| 64 | + int startIndex = Count; |
| 65 | + |
| 66 | + bool itemsAdded = AddArrangeCore(collection); |
| 67 | + |
| 68 | + if (!itemsAdded) |
| 69 | + { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + if (notificationMode == NotifyCollectionChangedAction.Reset) |
| 74 | + { |
| 75 | + RaiseChangeNotificationEvents(action: NotifyCollectionChangedAction.Reset); |
| 76 | + |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + RaiseChangeNotificationEvents( |
| 81 | + action: NotifyCollectionChangedAction.Add, |
| 82 | + changedItems: changedItems, |
| 83 | + startingIndex: startIndex); |
| 84 | + } |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// Removes the first occurence of each item in the specified collection from <seealso cref="ObservableCollection{T}"/>. |
| 88 | + /// <para> |
| 89 | + /// NOTE: with notificationMode = Remove, removed items starting index is not set because items are not guaranteed to be consecutive. |
| 90 | + /// </para> |
| 91 | + /// </summary> |
| 92 | + public void RemoveRange(IEnumerable<T> collection, NotifyCollectionChangedAction notificationMode = NotifyCollectionChangedAction.Reset) |
| 93 | + { |
| 94 | + if (notificationMode is not NotifyCollectionChangedAction.Remove and not NotifyCollectionChangedAction.Reset) |
| 95 | + { |
| 96 | + throw new ArgumentException("Mode must be either Remove or Reset for RemoveRange.", nameof(notificationMode)); |
| 97 | + } |
| 98 | + |
| 99 | + CheckReentrancy(); |
| 100 | + |
| 101 | + if (notificationMode == NotifyCollectionChangedAction.Reset) |
| 102 | + { |
| 103 | + bool raiseEvents = false; |
| 104 | + |
| 105 | + foreach (var item in collection) |
| 106 | + { |
| 107 | + Items.Remove(item); |
| 108 | + raiseEvents = true; |
| 109 | + } |
| 110 | + |
| 111 | + if (raiseEvents) |
| 112 | + { |
| 113 | + RaiseChangeNotificationEvents(action: NotifyCollectionChangedAction.Reset); |
| 114 | + } |
| 115 | + |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + var changedItems = new List<T>(collection); |
| 120 | + |
| 121 | + for (int i = 0; i < changedItems.Count; i++) |
| 122 | + { |
| 123 | + if (!Items.Remove(changedItems[i])) |
| 124 | + { |
| 125 | + changedItems.RemoveAt(i); // Can't use a foreach because changedItems is intended to be (carefully) modified |
| 126 | + i--; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + if (changedItems.Count == 0) |
| 131 | + { |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + RaiseChangeNotificationEvents( |
| 136 | + action: NotifyCollectionChangedAction.Remove, |
| 137 | + changedItems: changedItems); |
| 138 | + } |
| 139 | + |
| 140 | + /// <summary> |
| 141 | + /// Clears the current collection and replaces it with the specified item. |
| 142 | + /// </summary> |
| 143 | + public void Replace(T item) |
| 144 | + { |
| 145 | + ReplaceRange(new T[] { item }); |
| 146 | + } |
| 147 | + |
| 148 | + /// <summary> |
| 149 | + /// Clears the current collection and replaces it with the specified collection. |
| 150 | + /// </summary> |
| 151 | + public void ReplaceRange(IEnumerable<T> collection) |
| 152 | + { |
| 153 | + CheckReentrancy(); |
| 154 | + |
| 155 | + bool previouslyEmpty = Items.Count == 0; |
| 156 | + |
| 157 | + Items.Clear(); |
| 158 | + |
| 159 | + AddArrangeCore(collection); |
| 160 | + |
| 161 | + bool currentlyEmpty = Items.Count == 0; |
| 162 | + |
| 163 | + if (previouslyEmpty && currentlyEmpty) |
| 164 | + { |
| 165 | + return; |
| 166 | + } |
| 167 | + |
| 168 | + RaiseChangeNotificationEvents(action: NotifyCollectionChangedAction.Reset); |
| 169 | + } |
| 170 | + |
| 171 | + private bool AddArrangeCore(IEnumerable<T> collection) |
| 172 | + { |
| 173 | + bool itemAdded = false; |
| 174 | + |
| 175 | + foreach (var item in collection) |
| 176 | + { |
| 177 | + Items.Add(item); |
| 178 | + itemAdded = true; |
| 179 | + } |
| 180 | + |
| 181 | + return itemAdded; |
| 182 | + } |
| 183 | + |
| 184 | + private void RaiseChangeNotificationEvents(NotifyCollectionChangedAction action, List<T>? changedItems = null, int startingIndex = -1) |
| 185 | + { |
| 186 | + OnPropertyChanged(new PropertyChangedEventArgs(nameof(Count))); |
| 187 | + OnPropertyChanged(new PropertyChangedEventArgs("Item[]")); |
| 188 | + |
| 189 | + if (changedItems == null) |
| 190 | + { |
| 191 | + OnCollectionChanged(new NotifyCollectionChangedEventArgs(action)); |
| 192 | + } |
| 193 | + else |
| 194 | + { |
| 195 | + OnCollectionChanged(new NotifyCollectionChangedEventArgs(action, changedItems: changedItems, startingIndex: startingIndex)); |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments