< Summary

Information
Class: MyNet.UI.ViewModels.ViewModelExtensions
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/Extensions/ViewModelExtensions.cs
Tag: 323_28699572109
Line coverage
100%
Covered lines: 2
Uncovered lines: 0
Coverable lines: 2
Total lines: 36
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ObserveItemChanged(...)100%11100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/Extensions/ViewModelExtensions.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="ViewModelExtensions.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Reactive.Disposables;
 9using MyNet.UI.ViewModels.Crud;
 10
 11#pragma warning disable IDE0130
 12namespace MyNet.UI.ViewModels;
 13#pragma warning restore IDE0130
 14
 15/// <summary>
 16/// Provides extension methods for view models, particularly for observing changes to items in IItemViewModel instances.
 17/// </summary>
 18public static class ViewModelExtensions
 19{
 20    /// <summary>
 21    /// Observes changes to the item of an IItemViewModel and invokes the provided handler with the new item value whene
 22    /// </summary>
 23    /// <param name="vm">The IItemViewModel to observe.</param>
 24    /// <param name="handler">The action to invoke when the item changes.</param>
 25    /// <typeparam name="T">The type of the item.</typeparam>
 26    /// <returns>An IDisposable that can be used to unsubscribe from the item changes.</returns>
 27    public static IDisposable ObserveItemChanged<T>(this IItemViewModel<T> vm, Action<T?> handler)
 28    {
 329        vm.ItemChanged += localHandler;
 30
 331        return Disposable.Create(() => vm.ItemChanged -= localHandler);
 32
 33        void localHandler(object? sender, ItemChangedEventArgs<T> e) => handler(e.NewItem);
 34    }
 35}
 36