| | | 1 | | using Chronicler; |
| | | 2 | | using MemoryPack; |
| | | 3 | | using System; |
| | | 4 | | using System.ComponentModel; |
| | | 5 | | using System.Text.Json.Serialization; |
| | | 6 | | |
| | | 7 | | namespace SwiftCollections.Observable; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Represents an array of observable properties, raising events whenever an element is updated. |
| | | 11 | | /// Designed for performance-critical scenarios in Unity game development. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <typeparam name="TValue">The type of elements in the array.</typeparam> |
| | | 14 | | [Serializable] |
| | | 15 | | [JsonConverter(typeof(StateJsonConverterFactory))] |
| | | 16 | | [MemoryPackable] |
| | | 17 | | public partial class SwiftObservableArray<TValue> : IStateBacked<SwiftArrayState<TValue>>, INotifyPropertyChanged |
| | | 18 | | { |
| | | 19 | | #region Fields |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Represents the collection of observable properties managed by this instance. |
| | | 23 | | /// </summary> |
| | | 24 | | /// <remarks>Each element in the array corresponds to an individual observable property. |
| | | 25 | | /// The array may be null or empty if no properties are currently managed. |
| | | 26 | | /// </remarks> |
| | | 27 | | protected SwiftObservableProperty<TValue>[] _items; |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Represents the event handler that is invoked when a property value changes on an item. |
| | | 31 | | /// </summary> |
| | | 32 | | /// <remarks> |
| | | 33 | | /// This handler is typically used to subscribe to property change notifications for items within a collection or co |
| | | 34 | | /// Derived classes can use this field to manage event subscriptions for item property changes. |
| | | 35 | | /// </remarks> |
| | | 36 | | protected PropertyChangedEventHandler _itemChangedHandler; |
| | | 37 | | |
| | | 38 | | #endregion |
| | | 39 | | |
| | | 40 | | #region Events |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Raised when any element in the array changes, providing the index and the new value. |
| | | 44 | | /// </summary> |
| | | 45 | | public event EventHandler<ElementChangedEventArgs<TValue>>? ElementChanged; |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Raised when the array's state changes. |
| | | 49 | | /// </summary> |
| | | 50 | | public event PropertyChangedEventHandler? PropertyChanged; |
| | | 51 | | |
| | | 52 | | #endregion |
| | | 53 | | |
| | | 54 | | #region Nested Types |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Provides details about a changed element, including its index and new value. |
| | | 58 | | /// </summary> |
| | | 59 | | public class ElementChangedEventArgs<T> : EventArgs |
| | | 60 | | { |
| | | 61 | | /// <summary> |
| | | 62 | | /// The index of the changed element. |
| | | 63 | | /// </summary> |
| | 7 | 64 | | public int Index { get; } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// The new value of the changed element. |
| | | 68 | | /// </summary> |
| | 3 | 69 | | public T NewValue { get; } |
| | | 70 | | |
| | 1008 | 71 | | internal ElementChangedEventArgs(int index, T newValue) |
| | | 72 | | { |
| | 1008 | 73 | | Index = index; |
| | 1008 | 74 | | NewValue = newValue; |
| | 1008 | 75 | | } |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | #endregion |
| | | 79 | | |
| | | 80 | | #region Constructors |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Initializes a new instance of the SwiftObservableArray class with the specified capacity. |
| | | 84 | | /// </summary> |
| | | 85 | | /// <remarks> |
| | | 86 | | /// Each element in the array is initialized with a new <see cref="SwiftObservableProperty{TValue}"/> instance. |
| | | 87 | | /// The capacity determines the fixed size of the array and cannot be changed after construction. |
| | | 88 | | /// </remarks> |
| | | 89 | | /// <param name="capacity">The number of elements the array can contain. Must be a positive integer.</param> |
| | 20 | 90 | | public SwiftObservableArray(int capacity) |
| | | 91 | | { |
| | 20 | 92 | | SwiftThrowHelper.ThrowIfNegativeOrZero(capacity, nameof(capacity)); |
| | | 93 | | |
| | 20 | 94 | | _items = new SwiftObservableProperty<TValue>[capacity]; |
| | 1045 | 95 | | _itemChangedHandler = (sender, e) => OnItemChanged(sender); |
| | | 96 | | |
| | 202146 | 97 | | for (int i = 0; i < capacity; i++) |
| | | 98 | | { |
| | 101053 | 99 | | _items[i] = new SwiftObservableProperty<TValue> |
| | 101053 | 100 | | { |
| | 101053 | 101 | | Index = i |
| | 101053 | 102 | | }; |
| | 101053 | 103 | | _items[i].PropertyChanged += _itemChangedHandler; |
| | | 104 | | } |
| | 20 | 105 | | } |
| | | 106 | | |
| | | 107 | | /// <summary> |
| | | 108 | | /// Initializes a new instance of the SwiftObservableArray class with the specified observable properties. |
| | | 109 | | /// </summary> |
| | | 110 | | /// <remarks> |
| | | 111 | | /// Each property in the array is assigned its index and subscribed to change notifications. |
| | | 112 | | /// Changes to any property will be observed by the array. |
| | | 113 | | /// </remarks> |
| | | 114 | | /// <param name="observableProperties">An array of <see cref="SwiftObservableProperty{TValue}"/> instances to be man |
| | 2 | 115 | | public SwiftObservableArray(SwiftObservableProperty<TValue>[] observableProperties) |
| | | 116 | | { |
| | 2 | 117 | | SwiftThrowHelper.ThrowIfNull(observableProperties, nameof(observableProperties)); |
| | | 118 | | |
| | 1 | 119 | | _items = observableProperties; |
| | 2 | 120 | | _itemChangedHandler = (sender, e) => OnItemChanged(sender); |
| | | 121 | | |
| | 6 | 122 | | for (int i = 0; i < _items.Length; i++) |
| | | 123 | | { |
| | 2 | 124 | | _items[i].Index = i; |
| | 2 | 125 | | _items[i].PropertyChanged += _itemChangedHandler; |
| | | 126 | | } |
| | 1 | 127 | | } |
| | | 128 | | |
| | | 129 | | /// <summary> |
| | | 130 | | /// Initializes a new instance of the <see cref="SwiftObservableArray{TValue}"/> class with the specified <see cref |
| | | 131 | | /// </summary> |
| | | 132 | | /// <param name="state">The state containing the internal array, count, offset, and version for initialization.</pa |
| | | 133 | | [MemoryPackConstructor] |
| | 7 | 134 | | public SwiftObservableArray(SwiftArrayState<TValue> state) |
| | | 135 | | { |
| | 7 | 136 | | State = state; |
| | | 137 | | |
| | 7 | 138 | | SwiftThrowHelper.ThrowIfNull(_items, nameof(_items)); |
| | 7 | 139 | | SwiftThrowHelper.ThrowIfNull(_itemChangedHandler, nameof(_itemChangedHandler)); |
| | 7 | 140 | | } |
| | | 141 | | |
| | | 142 | | #endregion |
| | | 143 | | |
| | | 144 | | #region Properties |
| | | 145 | | |
| | | 146 | | /// <summary> |
| | | 147 | | /// Gets the total number of elements that the internal data structure can hold without resizing. |
| | | 148 | | /// </summary> |
| | | 149 | | [JsonIgnore] |
| | | 150 | | [MemoryPackIgnore] |
| | 2 | 151 | | public int Capacity => _items.Length; |
| | | 152 | | |
| | | 153 | | /// <summary> |
| | | 154 | | /// Gets or sets the value at the specified index. |
| | | 155 | | /// </summary> |
| | | 156 | | /// <param name="index">The zero-based index of the element to get or set. Must be within the valid range of the col |
| | | 157 | | [JsonIgnore] |
| | | 158 | | [MemoryPackIgnore] |
| | | 159 | | public TValue this[int index] |
| | | 160 | | { |
| | | 161 | | get |
| | | 162 | | { |
| | 10 | 163 | | ValidateIndex(index); |
| | 8 | 164 | | return _items[index].Value; |
| | | 165 | | } |
| | | 166 | | set |
| | | 167 | | { |
| | 1029 | 168 | | ValidateIndex(index); |
| | 1029 | 169 | | if (!Equals(_items[index].Value, value)) |
| | 1027 | 170 | | _items[index].Value = value; |
| | 1029 | 171 | | } |
| | | 172 | | } |
| | | 173 | | |
| | | 174 | | /// <summary> |
| | | 175 | | /// Gets or sets the current state of the array, including the values of all items. |
| | | 176 | | /// </summary> |
| | | 177 | | /// <remarks> |
| | | 178 | | /// Setting this property replaces the entire array state, including all item values and their order. |
| | | 179 | | /// Any existing item change handlers are reset when the state is set. |
| | | 180 | | /// </remarks> |
| | | 181 | | [JsonInclude] |
| | | 182 | | [MemoryPackInclude] |
| | | 183 | | public SwiftArrayState<TValue> State |
| | | 184 | | { |
| | | 185 | | get |
| | | 186 | | { |
| | 8 | 187 | | var values = new TValue[_items.Length]; |
| | | 188 | | |
| | 54 | 189 | | for (int i = 0; i < _items.Length; i++) |
| | 19 | 190 | | values[i] = _items[i].Value; |
| | | 191 | | |
| | 8 | 192 | | return new SwiftArrayState<TValue>(values); |
| | | 193 | | } |
| | | 194 | | internal set |
| | | 195 | | { |
| | 7 | 196 | | SwiftThrowHelper.ThrowIfNull(value.Items, nameof(value.Items)); |
| | | 197 | | |
| | 7 | 198 | | var values = value.Items; |
| | | 199 | | |
| | 7 | 200 | | int capacity = values.Length; |
| | | 201 | | |
| | 7 | 202 | | _items = new SwiftObservableProperty<TValue>[capacity]; |
| | 9 | 203 | | _itemChangedHandler = (sender, e) => OnItemChanged(sender); |
| | | 204 | | |
| | 48 | 205 | | for (int i = 0; i < capacity; i++) |
| | | 206 | | { |
| | 17 | 207 | | _items[i] = new SwiftObservableProperty<TValue>(values[i]) |
| | 17 | 208 | | { |
| | 17 | 209 | | Index = i |
| | 17 | 210 | | }; |
| | 17 | 211 | | _items[i].PropertyChanged += _itemChangedHandler; |
| | | 212 | | } |
| | 7 | 213 | | } |
| | | 214 | | } |
| | | 215 | | |
| | | 216 | | #endregion |
| | | 217 | | |
| | | 218 | | #region Methods |
| | | 219 | | |
| | | 220 | | /// <summary> |
| | | 221 | | /// Returns an array containing all elements in the collection. |
| | | 222 | | /// </summary> |
| | | 223 | | /// <returns> |
| | | 224 | | /// An array of type TValue that contains the values of the collection in order. |
| | | 225 | | /// The array will be empty if the collection contains no elements. |
| | | 226 | | /// </returns> |
| | | 227 | | public TValue[] ToArray() |
| | | 228 | | { |
| | 7 | 229 | | var array = new TValue[_items.Length]; |
| | 202046 | 230 | | for (int i = 0; i < _items.Length; i++) |
| | 101016 | 231 | | array[i] = _items[i].Value; |
| | 7 | 232 | | return array; |
| | | 233 | | } |
| | | 234 | | |
| | | 235 | | private void ValidateIndex(int index) |
| | | 236 | | { |
| | 1039 | 237 | | if (index < 0 || index >= _items.Length) |
| | 2 | 238 | | throw new IndexOutOfRangeException($"Index {index} is out of bounds for this array."); |
| | 1037 | 239 | | } |
| | | 240 | | |
| | | 241 | | private void OnItemChanged(object? sender) |
| | | 242 | | { |
| | 1028 | 243 | | if (sender is SwiftObservableProperty<TValue> property) |
| | | 244 | | { |
| | 1028 | 245 | | int index = property.Index; |
| | | 246 | | |
| | 1028 | 247 | | ElementChanged?.Invoke( |
| | 1028 | 248 | | this, |
| | 1028 | 249 | | new ElementChangedEventArgs<TValue>(index, property.Value) |
| | 1028 | 250 | | ); |
| | | 251 | | |
| | 1028 | 252 | | OnPropertyChanged("Items[]"); |
| | | 253 | | } |
| | 1028 | 254 | | } |
| | | 255 | | |
| | | 256 | | private void OnPropertyChanged(string propertyName) |
| | | 257 | | { |
| | 1028 | 258 | | PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); |
| | 1 | 259 | | } |
| | | 260 | | |
| | | 261 | | #endregion |
| | | 262 | | } |