// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Linq;
using CommunityToolkit.Mvvm.Input;
namespace CommunityToolkit.Mvvm.ComponentModel;
///
/// An attribute that can be used to support properties in generated properties. When this attribute is
/// used, the generated property setter will also call for the properties specified
/// in the attribute data, causing the validation logic for the command to be executed again. This can be useful to keep the code compact
/// when there are one or more dependent commands that should also be notified when a property is updated. If this attribute is used in
/// a field without , it is ignored (just like ).
///
/// In order to use this attribute, the target property has to implement the interface.
///
///
/// This attribute can be used as follows:
///
/// partial class MyViewModel : ObservableObject
/// {
/// [ObservableProperty]
/// [AlsoNotifyCanExecuteFor(nameof(GreetUserCommand))]
/// private string name;
///
/// public IRelayCommand GreetUserCommand { get; }
/// }
///
///
/// And with this, code analogous to this will be generated:
///
/// partial class MyViewModel
/// {
/// public string Name
/// {
/// get => name;
/// set
/// {
/// if (SetProperty(ref name, value))
/// {
/// GreetUserCommand.NotifyCanExecuteChanged();
/// }
/// }
/// }
/// }
///
///
[AttributeUsage(AttributeTargets.Field, AllowMultiple = true, Inherited = false)]
public sealed class AlsoNotifyCanExecuteForAttribute : Attribute
{
///
/// Initializes a new instance of the class.
///
/// The name of the command to also notify when the annotated property changes.
public AlsoNotifyCanExecuteForAttribute(string commandName)
{
CommandNames = new[] { commandName };
}
///
/// Initializes a new instance of the class.
///
/// The name of the property to also notify when the annotated property changes.
///
/// The other command names to also notify when the annotated property changes. This parameter can optionally
/// be used to indicate a series of dependent commands from the same attribute, to keep the code more compact.
///
public AlsoNotifyCanExecuteForAttribute(string commandName, params string[] otherCommandNames)
{
CommandNames = new[] { commandName }.Concat(otherCommandNames).ToArray();
}
///
/// Gets the command names to also notify when the annotated property changes.
///
public string[] CommandNames { get; }
}