< Summary

Information
Class: SwiftCollections.Observable.SwiftObservableDictionary<T1, T2>
Assembly: SwiftCollections
File(s): /home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/Observable/SwiftObservableDictionary.cs
Line coverage
96%
Covered lines: 57
Uncovered lines: 2
Coverable lines: 59
Total lines: 160
Line coverage: 96.6%
Branch coverage
85%
Covered branches: 12
Total branches: 14
Branch coverage: 85.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
.ctor(...)100%11100%
get_Item(...)100%11100%
set_Item(...)100%44100%
Add(...)100%11100%
Remove(...)50%4483.33%
Clear()100%22100%
OnPropertyChanged(...)100%22100%
OnCollectionChanged(...)100%22100%
OnCollectionChanged(...)100%11100%
OnCollectionChanged(...)100%11100%

File(s)

/home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/Observable/SwiftObservableDictionary.cs

#LineLine coverage
 1using MemoryPack;
 2using System;
 3using System.Collections.Generic;
 4using System.Collections.Specialized;
 5using System.ComponentModel;
 6using System.Text.Json.Serialization;
 7
 8namespace 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]
 18public 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
 5136    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]
 1543    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    {
 957        get => base[key];
 58        set
 559        {
 560            if (TryGetValue(key, out TValue oldValue))
 561            {
 562                if (!Equals(oldValue, value))
 463                {
 464                    base[key] = value;
 465                    OnCollectionChanged(NotifyCollectionChangedAction.Replace, key, oldValue, value);
 466                    OnPropertyChanged("Entries[]");
 467                }
 568            }
 569        }
 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)
 2081    {
 2082        bool result = base.Add(key, value);
 2083        OnCollectionChanged(NotifyCollectionChangedAction.Add, key, value);
 2084        OnPropertyChanged("Entries[]"); // Notify that the dictionary contents have changed
 2085        return result;
 2086    }
 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)
 393    {
 394        if (TryGetValue(key, out TValue value))
 395        {
 396            bool removed = base.Remove(key);
 397            if (removed)
 398            {
 399                OnCollectionChanged(NotifyCollectionChangedAction.Remove, key, value);
 3100                OnPropertyChanged("Entries[]"); // Notify that the dictionary contents have changed
 3101                return true;
 102            }
 0103        }
 0104        return false;
 3105    }
 106
 107    /// <summary>
 108    /// Clears all elements from the dictionary.
 109    /// Raises a reset collection change notification.
 110    /// </summary>
 111    public override void Clear()
 1112    {
 1113        if (Count > 0)
 1114        {
 1115            base.Clear();
 1116            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
 1117            OnPropertyChanged(nameof(Count));
 1118        }
 1119    }
 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)
 28130    {
 28131        var handler = PropertyChanged;
 28132        handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
 28133    }
 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)
 28139    {
 28140        var handler = CollectionChanged;
 28141        handler?.Invoke(this, args);
 28142    }
 143
 144    private void OnCollectionChanged(NotifyCollectionChangedAction action, TKey key, TValue value)
 23145    {
 23146        var entry = new KeyValuePair<TKey, TValue>(key, value);
 23147        var args = new NotifyCollectionChangedEventArgs(action, entry);
 23148        OnCollectionChanged(args);
 23149    }
 150
 151    private void OnCollectionChanged(NotifyCollectionChangedAction action, TKey key, TValue oldValue, TValue newValue)
 4152    {
 4153        var oldEntry = new KeyValuePair<TKey, TValue>(key, oldValue);
 4154        var newEntry = new KeyValuePair<TKey, TValue>(key, newValue);
 4155        var args = new NotifyCollectionChangedEventArgs(action, newEntry, oldEntry);
 4156        OnCollectionChanged(args);
 4157    }
 158
 159    #endregion
 160}