| | | 1 | | using MemoryPack; |
| | | 2 | | using System; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Collections.Specialized; |
| | | 5 | | using System.ComponentModel; |
| | | 6 | | using System.Text.Json.Serialization; |
| | | 7 | | |
| | | 8 | | namespace SwiftCollections.Observable; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Represents an observable extension of the high-performance <see cref="SwiftList{T}"/>. |
| | | 12 | | /// Notifies listeners of changes to its items and structure for reactive programming scenarios. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <typeparam name="T">The type of elements in the list.</typeparam> |
| | | 15 | | [Serializable] |
| | | 16 | | [JsonConverter(typeof(SwiftStateJsonConverterFactory))] |
| | | 17 | | [MemoryPackable] |
| | | 18 | | public partial class SwiftObservableList<T> : SwiftList<T>, INotifyPropertyChanged, INotifyCollectionChanged |
| | | 19 | | { |
| | | 20 | | #region Events |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Raised when a property on the list changes. |
| | | 24 | | /// </summary> |
| | | 25 | | public event PropertyChangedEventHandler PropertyChanged; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Raised when the list's collection is modified. |
| | | 29 | | /// </summary> |
| | | 30 | | public event NotifyCollectionChangedEventHandler CollectionChanged; |
| | | 31 | | |
| | | 32 | | #endregion |
| | | 33 | | |
| | | 34 | | #region Constructors |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Initializes a new instance of the <see cref="SwiftObservableList{T}"/> class. |
| | | 38 | | /// </summary> |
| | 96 | 39 | | public SwiftObservableList() : base() { } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Initializes a new instance of the <see cref="SwiftObservableList{T}"/> class with the specified initial capacity |
| | | 43 | | /// </summary> |
| | 3 | 44 | | public SwiftObservableList(int capacity) : base(capacity) { } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Initializes a new instance of the <see cref="SwiftObservableList{T}"/> class that contains elements copied from |
| | | 48 | | /// </summary> |
| | 18 | 49 | | public SwiftObservableList(IEnumerable<T> collection) : base(collection) { } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Initializes a new instance of the <see cref="SwiftObservableList{T}"/> class with the specified <see cref="Swif |
| | | 53 | | /// </summary> |
| | | 54 | | /// <param name="state">The state containing the internal array, count, offset, and version for initialization.</pa |
| | | 55 | | [MemoryPackConstructor] |
| | 18 | 56 | | public SwiftObservableList(SwiftArrayState<T> state) : base(state) { } |
| | | 57 | | |
| | | 58 | | #endregion |
| | | 59 | | |
| | | 60 | | #region Properties |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Sets or gets the element at the specified index. |
| | | 64 | | /// Raises collection change notifications on modification. |
| | | 65 | | /// </summary> |
| | | 66 | | [JsonIgnore] |
| | | 67 | | [MemoryPackIgnore] |
| | | 68 | | public new T this[int index] |
| | | 69 | | { |
| | 506 | 70 | | get => base[index]; |
| | | 71 | | set |
| | 1506 | 72 | | { |
| | 1506 | 73 | | T oldValue = base[index]; |
| | 1504 | 74 | | if (!Equals(oldValue, value)) |
| | 1501 | 75 | | { |
| | 1501 | 76 | | base[index] = value; |
| | 1501 | 77 | | OnCollectionChanged(NotifyCollectionChangedAction.Replace, oldValue, value, index); |
| | 1501 | 78 | | OnPropertyChanged("InnerArray[]"); |
| | 1501 | 79 | | } |
| | 1504 | 80 | | } |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | #endregion |
| | | 84 | | |
| | | 85 | | #region Methods |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Adds an element to the end of the list. |
| | | 89 | | /// Raises collection and property change notifications. |
| | | 90 | | /// </summary> |
| | | 91 | | public override void Add(T item) |
| | 11051 | 92 | | { |
| | 11051 | 93 | | base.Add(item); |
| | 11051 | 94 | | OnCollectionChanged(NotifyCollectionChangedAction.Add, item, _count - 1); |
| | 11051 | 95 | | OnPropertyChanged(nameof(Count)); |
| | 11051 | 96 | | } |
| | | 97 | | |
| | | 98 | | public override void AddRange(IEnumerable<T> items) |
| | 1 | 99 | | { |
| | 1 | 100 | | SwiftThrowHelper.ThrowIfNull(items, nameof(items)); |
| | | 101 | | |
| | 9 | 102 | | foreach (T item in items) |
| | 3 | 103 | | Add(item); |
| | 1 | 104 | | } |
| | | 105 | | |
| | | 106 | | /// <summary> |
| | | 107 | | /// Adds the elements of the specified array to the end of the list. |
| | | 108 | | /// Raises collection and property change notifications for each added item. |
| | | 109 | | /// </summary> |
| | | 110 | | public override void AddRange(T[] items) |
| | 2 | 111 | | { |
| | 2 | 112 | | SwiftThrowHelper.ThrowIfNull(items, nameof(items)); |
| | 1 | 113 | | AddRange(items.AsSpan()); |
| | 1 | 114 | | } |
| | | 115 | | |
| | | 116 | | /// <summary> |
| | | 117 | | /// Adds the elements of the specified span to the end of the list. |
| | | 118 | | /// Raises collection and property change notifications for each added item. |
| | | 119 | | /// </summary> |
| | | 120 | | public override void AddRange(ReadOnlySpan<T> items) |
| | 2 | 121 | | { |
| | 16 | 122 | | for (int i = 0; i < items.Length; i++) |
| | 6 | 123 | | Add(items[i]); |
| | 2 | 124 | | } |
| | | 125 | | |
| | | 126 | | /// <summary> |
| | | 127 | | /// Removes the first occurrence of a specific object from the list. |
| | | 128 | | /// Raises collection and property change notifications. |
| | | 129 | | /// </summary> |
| | | 130 | | public override bool Remove(T item) |
| | 503 | 131 | | { |
| | 503 | 132 | | int index = IndexOf(item); |
| | 503 | 133 | | if (index >= 0) |
| | 502 | 134 | | { |
| | 502 | 135 | | var removedItem = this[index]; |
| | 502 | 136 | | base.RemoveAt(index); |
| | 502 | 137 | | OnCollectionChanged(NotifyCollectionChangedAction.Remove, removedItem, index); |
| | 502 | 138 | | OnPropertyChanged(nameof(Count)); |
| | 502 | 139 | | return true; |
| | | 140 | | } |
| | | 141 | | |
| | 1 | 142 | | return false; |
| | 503 | 143 | | } |
| | | 144 | | |
| | | 145 | | /// <summary> |
| | | 146 | | /// Removes the element at the specified index. |
| | | 147 | | /// Raises collection and property change notifications. |
| | | 148 | | /// </summary> |
| | | 149 | | public override void RemoveAt(int index) |
| | 3 | 150 | | { |
| | 3 | 151 | | var removedItem = this[index]; |
| | 1 | 152 | | base.RemoveAt(index); |
| | 1 | 153 | | OnCollectionChanged(NotifyCollectionChangedAction.Remove, removedItem, index); |
| | 1 | 154 | | OnPropertyChanged(nameof(Count)); |
| | 1 | 155 | | } |
| | | 156 | | |
| | | 157 | | public override int RemoveAll(Predicate<T> match) |
| | 6 | 158 | | { |
| | 6 | 159 | | int removedCount = base.RemoveAll(match); |
| | | 160 | | |
| | 5 | 161 | | if (removedCount > 0) |
| | 3 | 162 | | { |
| | 3 | 163 | | OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); |
| | 3 | 164 | | OnPropertyChanged(nameof(Count)); |
| | 3 | 165 | | } |
| | | 166 | | |
| | 5 | 167 | | return removedCount; |
| | 5 | 168 | | } |
| | | 169 | | |
| | | 170 | | /// <summary> |
| | | 171 | | /// Inserts an element into the list at the specified index. |
| | | 172 | | /// Raises collection and property change notifications. |
| | | 173 | | /// </summary> |
| | | 174 | | public override void Insert(int index, T item) |
| | 1 | 175 | | { |
| | 1 | 176 | | base.Insert(index, item); |
| | 1 | 177 | | OnCollectionChanged(NotifyCollectionChangedAction.Add, item, index); |
| | 1 | 178 | | OnPropertyChanged(nameof(Count)); |
| | 1 | 179 | | } |
| | | 180 | | |
| | | 181 | | /// <summary> |
| | | 182 | | /// Clears all elements from the list. |
| | | 183 | | /// Raises a reset collection change notification. |
| | | 184 | | /// </summary> |
| | | 185 | | public override void Clear() |
| | 1 | 186 | | { |
| | 1 | 187 | | if (_count > 0) |
| | 1 | 188 | | { |
| | 1 | 189 | | base.Clear(); |
| | 1 | 190 | | OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); |
| | 1 | 191 | | OnPropertyChanged(nameof(Count)); |
| | 1 | 192 | | } |
| | 1 | 193 | | } |
| | | 194 | | |
| | | 195 | | #endregion |
| | | 196 | | |
| | | 197 | | #region Notifications |
| | | 198 | | |
| | | 199 | | /// <summary> |
| | | 200 | | /// Raises the <see cref="PropertyChanged"/> event. |
| | | 201 | | /// </summary> |
| | | 202 | | /// <param name="propertyName">The name of the property that changed.</param> |
| | | 203 | | protected virtual void OnPropertyChanged(string propertyName) |
| | 13060 | 204 | | { |
| | 13060 | 205 | | var handler = PropertyChanged; |
| | 13060 | 206 | | handler?.Invoke(this, new PropertyChangedEventArgs(propertyName)); |
| | 13060 | 207 | | } |
| | | 208 | | |
| | | 209 | | /// <summary> |
| | | 210 | | /// Raises the <see cref="CollectionChanged"/> event for the specified action and items. |
| | | 211 | | /// </summary> |
| | | 212 | | protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs args) |
| | 13060 | 213 | | { |
| | 13060 | 214 | | var handler = CollectionChanged; |
| | 13060 | 215 | | handler?.Invoke(this, args); |
| | 13060 | 216 | | } |
| | | 217 | | |
| | | 218 | | private void OnCollectionChanged(NotifyCollectionChangedAction action, T item, int index) |
| | 11555 | 219 | | { |
| | 11555 | 220 | | var args = new NotifyCollectionChangedEventArgs(action, item, index); |
| | 11555 | 221 | | OnCollectionChanged(args); |
| | 11555 | 222 | | } |
| | | 223 | | |
| | | 224 | | private void OnCollectionChanged(NotifyCollectionChangedAction action, T oldItem, T newItem, int index) |
| | 1501 | 225 | | { |
| | 1501 | 226 | | var args = new NotifyCollectionChangedEventArgs(action, newItem, oldItem, index); |
| | 1501 | 227 | | OnCollectionChanged(args); |
| | 1501 | 228 | | } |
| | | 229 | | |
| | | 230 | | #endregion |
| | | 231 | | } |