| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ItemViewModel.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.ComponentModel; |
| | | 10 | | using MyNet.Globalization.Culture; |
| | | 11 | | using MyNet.UI.Commands; |
| | | 12 | | using MyNet.UI.ViewModels.Workspace; |
| | | 13 | | |
| | | 14 | | namespace 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> |
| | | 22 | | public abstract class ItemViewModel<T>( |
| | | 23 | | ICommandFactory? commandFactory = null, |
| | | 24 | | ICultureService? cultureService = null) |
| | 9 | 25 | | : WorkspaceViewModel(commandFactory, cultureService), IItemViewModel<T> |
| | | 26 | | { |
| | | 27 | | /// <summary> |
| | | 28 | | /// Gets or sets the current item. |
| | | 29 | | /// </summary> |
| | 21 | 30 | | 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 | | { |
| | 21 | 43 | | if (EqualityComparer<T?>.Default.Equals(Item, item)) |
| | 0 | 44 | | return; |
| | | 45 | | |
| | 21 | 46 | | var previous = Item; |
| | 21 | 47 | | Item = item; |
| | 21 | 48 | | OnItemChanged(previous, item); |
| | 21 | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Called when the current item changes. |
| | | 53 | | /// </summary> |
| | | 54 | | protected virtual void HandleItemChanged() |
| | | 55 | | { |
| | 21 | 56 | | } |
| | | 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 | | { |
| | 21 | 65 | | UnsubscribeFromItem(oldValue); |
| | 21 | 66 | | SubscribeToItem(newValue); |
| | 21 | 67 | | HandleItemChanged(); |
| | | 68 | | |
| | 21 | 69 | | ItemChanged?.Invoke(this, new(oldValue, newValue)); |
| | 0 | 70 | | } |
| | | 71 | | |
| | | 72 | | private void SubscribeToItem(T? item) |
| | | 73 | | { |
| | 21 | 74 | | if (item is INotifyPropertyChanged notifyPropertyChanged) |
| | 0 | 75 | | notifyPropertyChanged.PropertyChanged += HandleItemPropertyChanged; |
| | 21 | 76 | | } |
| | | 77 | | |
| | | 78 | | private void UnsubscribeFromItem(T? item) |
| | | 79 | | { |
| | 21 | 80 | | if (item is INotifyPropertyChanged notifyPropertyChanged) |
| | 0 | 81 | | notifyPropertyChanged.PropertyChanged -= HandleItemPropertyChanged; |
| | 21 | 82 | | } |
| | | 83 | | |
| | 0 | 84 | | 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 | | { |
| | 0 | 92 | | } |
| | | 93 | | |
| | | 94 | | /// <inheritdoc /> |
| | | 95 | | protected override void DisposeManagedResources() |
| | | 96 | | { |
| | 0 | 97 | | UnsubscribeFromItem(Item); |
| | 0 | 98 | | base.DisposeManagedResources(); |
| | 0 | 99 | | } |
| | | 100 | | } |
| | | 101 | | |