< Summary

Information
Class: Chronicis.Client.ViewModels.ViewModelBase
Assembly: Chronicis.Client
File(s): /home/runner/work/chronicis/chronicis/src/Chronicis.Client/ViewModels/ViewModelBase.cs
Line coverage
100%
Covered lines: 9
Uncovered lines: 0
Coverable lines: 9
Total lines: 62
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
SetField(...)100%22100%
RaisePropertyChanged(...)100%22100%
OnPropertyChanged(...)100%11100%

File(s)

/home/runner/work/chronicis/chronicis/src/Chronicis.Client/ViewModels/ViewModelBase.cs

#LineLine coverage
 1using System.ComponentModel;
 2using System.Runtime.CompilerServices;
 3
 4namespace Chronicis.Client.ViewModels;
 5
 6/// <summary>
 7/// Base class for all Chronicis ViewModels.
 8/// Implements <see cref="INotifyPropertyChanged"/> with a SetField helper
 9/// so that Blazor components can subscribe to property changes and call
 10/// <see cref="Microsoft.AspNetCore.Components.ComponentBase.StateHasChanged"/>
 11/// without pulling MudBlazor or rendering infrastructure into ViewModels.
 12/// </summary>
 13public abstract class ViewModelBase : INotifyPropertyChanged
 14{
 15    /// <inheritdoc />
 16    public event PropertyChangedEventHandler? PropertyChanged;
 17
 18    /// <summary>
 19    /// Sets <paramref name="field"/> to <paramref name="value"/> and raises
 20    /// <see cref="PropertyChanged"/> if the value has changed.
 21    /// </summary>
 22    /// <typeparam name="T">The type of the property.</typeparam>
 23    /// <param name="field">Reference to the backing field.</param>
 24    /// <param name="value">The new value.</param>
 25    /// <param name="propertyName">
 26    /// The property name, automatically provided by the compiler via
 27    /// <see cref="CallerMemberNameAttribute"/>.
 28    /// </param>
 29    /// <returns>
 30    /// <c>true</c> if the value changed and <see cref="PropertyChanged"/> was raised;
 31    /// <c>false</c> if the value was already equal.
 32    /// </returns>
 33    protected bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = "")
 34    {
 288735        if (EqualityComparer<T>.Default.Equals(field, value))
 117536            return false;
 37
 171238        field = value;
 171239        RaisePropertyChanged(propertyName);
 171240        return true;
 41    }
 42
 43    /// <summary>
 44    /// Raises <see cref="PropertyChanged"/> for the specified property name.
 45    /// Use this when a computed/dependent property needs to notify separately.
 46    /// </summary>
 47    /// <param name="propertyName">The name of the property that changed.</param>
 48    protected void RaisePropertyChanged(string propertyName)
 49    {
 172250        OnPropertyChanged(propertyName);
 172251        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
 37352    }
 53
 54    /// <summary>
 55    /// Called before <see cref="PropertyChanged"/> is raised.
 56    /// Override in subclasses to react to specific property changes.
 57    /// </summary>
 58    /// <param name="propertyName">The name of the property that changed.</param>
 59    protected virtual void OnPropertyChanged(string propertyName)
 60    {
 170961    }
 62}