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