| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ViewModelExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Reactive.Disposables; |
| | | 9 | | using MyNet.UI.ViewModels.Crud; |
| | | 10 | | |
| | | 11 | | #pragma warning disable IDE0130 |
| | | 12 | | namespace 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> |
| | | 18 | | public 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 | | { |
| | 3 | 29 | | vm.ItemChanged += localHandler; |
| | | 30 | | |
| | 3 | 31 | | return Disposable.Create(() => vm.ItemChanged -= localHandler); |
| | | 32 | | |
| | | 33 | | void localHandler(object? sender, ItemChangedEventArgs<T> e) => handler(e.NewItem); |
| | | 34 | | } |
| | | 35 | | } |
| | | 36 | | |