< Summary

Information
Class: SwiftCollections.Observable.SwiftObservableDictionary<T1, T2>
Assembly: SwiftCollections
File(s): /home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/Observable/SwiftObservableDictionary.cs
Line coverage
97%
Covered lines: 39
Uncovered lines: 1
Coverable lines: 40
Total lines: 165
Line coverage: 97.5%
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%4485.71%
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 Chronicler;
 2using MemoryPack;
 3using System;
 4using System.Collections.Generic;
 5using System.Collections.Specialized;
 6using System.ComponentModel;
 7using System.Text.Json.Serialization;
 8
 9namespace 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]
 19public 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>
 3441    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]
 1048    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    {
 962        get => base[key];
 63        set
 64        {
 565            if (TryGetValue(key, out TValue oldValue))
 66            {
 567                if (!Equals(oldValue, value))
 68                {
 469                    base[key] = value;
 470                    OnCollectionChanged(NotifyCollectionChangedAction.Replace, key, oldValue, value);
 471                    OnPropertyChanged("Entries[]");
 72                }
 73            }
 574        }
 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    {
 2087        bool result = base.Add(key, value);
 2088        OnCollectionChanged(NotifyCollectionChangedAction.Add, key, value);
 2089        OnPropertyChanged("Entries[]"); // Notify that the dictionary contents have changed
 2090        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    {
 399        if (TryGetValue(key, out TValue value))
 100        {
 3101            bool removed = base.Remove(key);
 3102            if (removed)
 103            {
 3104                OnCollectionChanged(NotifyCollectionChangedAction.Remove, key, value);
 3105                OnPropertyChanged("Entries[]"); // Notify that the dictionary contents have changed
 3106                return true;
 107            }
 108        }
 0109        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    {
 1118        if (Count > 0)
 119        {
 1120            base.Clear();
 1121            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
 1122            OnPropertyChanged(nameof(Count));
 123        }
 1124    }
 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    {
 28136        var handler = PropertyChanged;
 28137        handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
 2138    }
 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    {
 28145        var handler = CollectionChanged;
 28146        handler?.Invoke(this, args);
 7147    }
 148
 149    private void OnCollectionChanged(NotifyCollectionChangedAction action, TKey key, TValue value)
 150    {
 23151        var entry = new KeyValuePair<TKey, TValue>(key, value);
 23152        var args = new NotifyCollectionChangedEventArgs(action, entry);
 23153        OnCollectionChanged(args);
 23154    }
 155
 156    private void OnCollectionChanged(NotifyCollectionChangedAction action, TKey key, TValue oldValue, TValue newValue)
 157    {
 4158        var oldEntry = new KeyValuePair<TKey, TValue>(key, oldValue);
 4159        var newEntry = new KeyValuePair<TKey, TValue>(key, newValue);
 4160        var args = new NotifyCollectionChangedEventArgs(action, newEntry, oldEntry);
 4161        OnCollectionChanged(args);
 4162    }
 163
 164    #endregion
 165}