| | | 1 | | using Chronicler; |
| | | 2 | | using MemoryPack; |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Collections.Specialized; |
| | | 6 | | using System.ComponentModel; |
| | | 7 | | using System.Text.Json.Serialization; |
| | | 8 | | |
| | | 9 | | namespace SwiftCollections.Observable; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Represents a dictionary that notifies listeners of changes to its items and structure. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <typeparam name="TKey">The type of keys in the dictionary.</typeparam> |
| | | 15 | | /// <typeparam name="TValue">The type of values in the dictionary.</typeparam> |
| | | 16 | | [Serializable] |
| | | 17 | | [JsonConverter(typeof(StateJsonConverterFactory))] |
| | | 18 | | [MemoryPackable] |
| | | 19 | | public partial class SwiftObservableDictionary<TKey, TValue> : SwiftDictionary<TKey, TValue>, IStateBacked<SwiftDictiona |
| | | 20 | | where TKey : notnull |
| | | 21 | | { |
| | | 22 | | #region Events |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Raised when a property on the dictionary changes. |
| | | 26 | | /// </summary> |
| | | 27 | | public event PropertyChangedEventHandler? PropertyChanged; |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Raised when the dictionary's collection is modified. |
| | | 31 | | /// </summary> |
| | | 32 | | public event NotifyCollectionChangedEventHandler? CollectionChanged; |
| | | 33 | | |
| | | 34 | | #endregion |
| | | 35 | | |
| | | 36 | | #region Constructor |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Initializes a new instance of the SwiftObservableDictionary class. |
| | | 40 | | /// </summary> |
| | 34 | 41 | | public SwiftObservableDictionary() : base() { } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Initializes a new instance of the <see cref="SwiftObservableDictionary{TKey, TValue}"/> class with the specifie |
| | | 45 | | /// </summary> |
| | | 46 | | /// <param name="state">The state containing the internal array, count, offset, and version for initialization.</pa |
| | | 47 | | [MemoryPackConstructor] |
| | 10 | 48 | | public SwiftObservableDictionary(SwiftDictionaryState<TKey, TValue> state) : base(state) { } |
| | | 49 | | |
| | | 50 | | #endregion |
| | | 51 | | |
| | | 52 | | #region Properties |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Gets or sets the value associated with the specified key. |
| | | 56 | | /// Raises property and collection change notifications. |
| | | 57 | | /// </summary> |
| | | 58 | | [JsonIgnore] |
| | | 59 | | [MemoryPackIgnore] |
| | | 60 | | public new TValue this[TKey key] |
| | | 61 | | { |
| | 9 | 62 | | get => base[key]; |
| | | 63 | | set |
| | | 64 | | { |
| | 5 | 65 | | if (TryGetValue(key, out TValue oldValue)) |
| | | 66 | | { |
| | 5 | 67 | | if (!Equals(oldValue, value)) |
| | | 68 | | { |
| | 4 | 69 | | base[key] = value; |
| | 4 | 70 | | OnCollectionChanged(NotifyCollectionChangedAction.Replace, key, oldValue, value); |
| | 4 | 71 | | OnPropertyChanged("Entries[]"); |
| | | 72 | | } |
| | | 73 | | } |
| | 5 | 74 | | } |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | #endregion |
| | | 78 | | |
| | | 79 | | #region Methods |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Adds an element with the provided key and value to the dictionary. |
| | | 83 | | /// Raises a collection change notification. |
| | | 84 | | /// </summary> |
| | | 85 | | public override bool Add(TKey key, TValue value) |
| | | 86 | | { |
| | 20 | 87 | | bool result = base.Add(key, value); |
| | 20 | 88 | | OnCollectionChanged(NotifyCollectionChangedAction.Add, key, value); |
| | 20 | 89 | | OnPropertyChanged("Entries[]"); // Notify that the dictionary contents have changed |
| | 20 | 90 | | return result; |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Removes the element with the specified key from the dictionary. |
| | | 95 | | /// Raises a collection change notification if the key existed. |
| | | 96 | | /// </summary> |
| | | 97 | | public override bool Remove(TKey key) |
| | | 98 | | { |
| | 3 | 99 | | if (TryGetValue(key, out TValue value)) |
| | | 100 | | { |
| | 3 | 101 | | bool removed = base.Remove(key); |
| | 3 | 102 | | if (removed) |
| | | 103 | | { |
| | 3 | 104 | | OnCollectionChanged(NotifyCollectionChangedAction.Remove, key, value); |
| | 3 | 105 | | OnPropertyChanged("Entries[]"); // Notify that the dictionary contents have changed |
| | 3 | 106 | | return true; |
| | | 107 | | } |
| | | 108 | | } |
| | 0 | 109 | | return false; |
| | | 110 | | } |
| | | 111 | | |
| | | 112 | | /// <summary> |
| | | 113 | | /// Clears all elements from the dictionary. |
| | | 114 | | /// Raises a reset collection change notification. |
| | | 115 | | /// </summary> |
| | | 116 | | public override void Clear() |
| | | 117 | | { |
| | 1 | 118 | | if (Count > 0) |
| | | 119 | | { |
| | 1 | 120 | | base.Clear(); |
| | 1 | 121 | | OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); |
| | 1 | 122 | | OnPropertyChanged(nameof(Count)); |
| | | 123 | | } |
| | 1 | 124 | | } |
| | | 125 | | |
| | | 126 | | #endregion |
| | | 127 | | |
| | | 128 | | #region Notifications |
| | | 129 | | |
| | | 130 | | /// <summary> |
| | | 131 | | /// Raises the <see cref="PropertyChanged"/> event. |
| | | 132 | | /// </summary> |
| | | 133 | | /// <param name="propertyName">The name of the property that changed.</param> |
| | | 134 | | protected virtual void OnPropertyChanged(string propertyName) |
| | | 135 | | { |
| | 28 | 136 | | var handler = PropertyChanged; |
| | 28 | 137 | | handler?.Invoke(this, new PropertyChangedEventArgs(propertyName)); |
| | 2 | 138 | | } |
| | | 139 | | |
| | | 140 | | /// <summary> |
| | | 141 | | /// Raises the <see cref="CollectionChanged"/> event for the specified action and items. |
| | | 142 | | /// </summary> |
| | | 143 | | protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs args) |
| | | 144 | | { |
| | 28 | 145 | | var handler = CollectionChanged; |
| | 28 | 146 | | handler?.Invoke(this, args); |
| | 7 | 147 | | } |
| | | 148 | | |
| | | 149 | | private void OnCollectionChanged(NotifyCollectionChangedAction action, TKey key, TValue value) |
| | | 150 | | { |
| | 23 | 151 | | var entry = new KeyValuePair<TKey, TValue>(key, value); |
| | 23 | 152 | | var args = new NotifyCollectionChangedEventArgs(action, entry); |
| | 23 | 153 | | OnCollectionChanged(args); |
| | 23 | 154 | | } |
| | | 155 | | |
| | | 156 | | private void OnCollectionChanged(NotifyCollectionChangedAction action, TKey key, TValue oldValue, TValue newValue) |
| | | 157 | | { |
| | 4 | 158 | | var oldEntry = new KeyValuePair<TKey, TValue>(key, oldValue); |
| | 4 | 159 | | var newEntry = new KeyValuePair<TKey, TValue>(key, newValue); |
| | 4 | 160 | | var args = new NotifyCollectionChangedEventArgs(action, newEntry, oldEntry); |
| | 4 | 161 | | OnCollectionChanged(args); |
| | 4 | 162 | | } |
| | | 163 | | |
| | | 164 | | #endregion |
| | | 165 | | } |