< Summary

Information
Class: MyNet.UI.ViewModels.Crud.ItemViewModel<T>
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/Crud/ItemViewModel.cs
Tag: 323_28699572109
Line coverage
64%
Covered lines: 16
Uncovered lines: 9
Coverable lines: 25
Total lines: 101
Line coverage: 64%
Branch coverage
50%
Covered branches: 4
Total branches: 8
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
set_Item(...)100%11100%
SetItem(...)50%2283.33%
HandleItemChanged()100%11100%
OnItemChanged(...)50%2280%
SubscribeToItem(...)50%2266.66%
UnsubscribeFromItem(...)50%2266.66%
HandleItemPropertyChanged(...)100%210%
HandleCurrentItemPropertyChanged(...)100%210%
DisposeManagedResources()100%210%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/Crud/ItemViewModel.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="ItemViewModel.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Collections.Generic;
 9using System.ComponentModel;
 10using MyNet.Globalization.Culture;
 11using MyNet.UI.Commands;
 12using MyNet.UI.ViewModels.Workspace;
 13
 14namespace MyNet.UI.ViewModels.Crud;
 15
 16/// <summary>
 17/// Provides a reusable base implementation for item view models.
 18/// </summary>
 19/// <typeparam name="T">The item type.</typeparam>
 20/// <param name="commandFactory">Optional command factory used to create commands.</param>
 21/// <param name="cultureService">Optional culture service used to manage culture changes.</param>
 22public abstract class ItemViewModel<T>(
 23    ICommandFactory? commandFactory = null,
 24    ICultureService? cultureService = null)
 925    : WorkspaceViewModel(commandFactory, cultureService), IItemViewModel<T>
 26{
 27    /// <summary>
 28    /// Gets or sets the current item.
 29    /// </summary>
 2130    public T? Item { get; protected set => SetProperty(ref field, value); }
 31
 32    /// <summary>
 33    /// Event raised when the current item changes, providing the old and new values of the item.
 34    /// </summary>
 35    public event EventHandler<ItemChangedEventArgs<T>>? ItemChanged;
 36
 37    /// <summary>
 38    /// Sets the current item and raises <see cref="ItemChanged"/> when the value changes.
 39    /// </summary>
 40    /// <param name="item">The item to expose.</param>
 41    public virtual void SetItem(T? item)
 42    {
 2143        if (EqualityComparer<T?>.Default.Equals(Item, item))
 044            return;
 45
 2146        var previous = Item;
 2147        Item = item;
 2148        OnItemChanged(previous, item);
 2149    }
 50
 51    /// <summary>
 52    /// Called when the current item changes.
 53    /// </summary>
 54    protected virtual void HandleItemChanged()
 55    {
 2156    }
 57
 58    /// <summary>
 59    /// Handles Fody property change notifications for <see cref="Item"/>.
 60    /// </summary>
 61    /// <param name="oldValue">The previous item value.</param>
 62    /// <param name="newValue">The new item value.</param>
 63    protected virtual void OnItemChanged(T? oldValue, T? newValue)
 64    {
 2165        UnsubscribeFromItem(oldValue);
 2166        SubscribeToItem(newValue);
 2167        HandleItemChanged();
 68
 2169        ItemChanged?.Invoke(this, new(oldValue, newValue));
 070    }
 71
 72    private void SubscribeToItem(T? item)
 73    {
 2174        if (item is INotifyPropertyChanged notifyPropertyChanged)
 075            notifyPropertyChanged.PropertyChanged += HandleItemPropertyChanged;
 2176    }
 77
 78    private void UnsubscribeFromItem(T? item)
 79    {
 2180        if (item is INotifyPropertyChanged notifyPropertyChanged)
 081            notifyPropertyChanged.PropertyChanged -= HandleItemPropertyChanged;
 2182    }
 83
 084    private void HandleItemPropertyChanged(object? sender, PropertyChangedEventArgs e) => HandleCurrentItemPropertyChang
 85
 86    /// <summary>
 87    /// Called when a property of the current item changes.
 88    /// </summary>
 89    /// <param name="e">The property change arguments.</param>
 90    protected virtual void HandleCurrentItemPropertyChanged(PropertyChangedEventArgs e)
 91    {
 092    }
 93
 94    /// <inheritdoc />
 95    protected override void DisposeManagedResources()
 96    {
 097        UnsubscribeFromItem(Item);
 098        base.DisposeManagedResources();
 099    }
 100}
 101