Skip to content

Commit

Permalink
Merge pull request #722 from CommunityToolkit/dev/notify-for-changing
Browse files Browse the repository at this point in the history
Fix generation for OnPropertyChanging for [NotifyPropertyChangedFor]
  • Loading branch information
Sergio0694 authored Oct 24, 2023
2 parents ecd1711 + 8857555 commit 7b53ae2
Show file tree
Hide file tree
Showing 4 changed files with 465 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ public static bool TryGetInfo(
token.ThrowIfCancellationRequested();

using ImmutableArrayBuilder<string> propertyChangedNames = ImmutableArrayBuilder<string>.Rent();
using ImmutableArrayBuilder<string> propertyChangingNames = ImmutableArrayBuilder<string>.Rent();
using ImmutableArrayBuilder<string> notifiedCommandNames = ImmutableArrayBuilder<string>.Rent();
using ImmutableArrayBuilder<AttributeInfo> forwardedAttributes = ImmutableArrayBuilder<AttributeInfo>.Rent();

Expand All @@ -131,12 +130,6 @@ public static bool TryGetInfo(

token.ThrowIfCancellationRequested();

// Track the property changing event for the property, if the type supports it
if (shouldInvokeOnPropertyChanging)
{
propertyChangingNames.Add(propertyName);
}

// The current property is always notified
propertyChangedNames.Add(propertyName);

Expand Down Expand Up @@ -296,12 +289,24 @@ public static bool TryGetInfo(

token.ThrowIfCancellationRequested();

// Prepare the effective property changing/changed names. For the property changing names,
// there are two possible cases: if the mode is disabled, then there are no names to report
// at all. If the mode is enabled, then the list is just the same as for property changed.
ImmutableArray<string> effectivePropertyChangedNames = propertyChangedNames.ToImmutable();
ImmutableArray<string> effectivePropertyChangingNames = shouldInvokeOnPropertyChanging switch
{
true => effectivePropertyChangedNames,
false => ImmutableArray<string>.Empty
};

token.ThrowIfCancellationRequested();

propertyInfo = new PropertyInfo(
typeNameWithNullabilityAnnotations,
fieldName,
propertyName,
propertyChangingNames.ToImmutable(),
propertyChangedNames.ToImmutable(),
effectivePropertyChangingNames,
effectivePropertyChangedNames,
notifiedCommandNames.ToImmutable(),
notifyRecipients,
notifyDataErrorInfo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,14 @@ namespace CommunityToolkit.Mvvm.ComponentModel;
/// get => name;
/// set
/// {
/// if (SetProperty(ref name, value))
/// if (!EqualityComparer&lt;string&gt;.Default.Equals(name, value))
/// {
/// OnPropertyChanging(nameof(Name));
/// OnPropertyChanged(nameof(FullName));
///
/// name = value;
///
/// OnPropertyChanged(nameof(Name));
/// OnPropertyChanged(nameof(FullName));
/// }
/// }
Expand All @@ -58,8 +64,14 @@ namespace CommunityToolkit.Mvvm.ComponentModel;
/// get => surname;
/// set
/// {
/// if (SetProperty(ref surname, value))
/// if (!EqualityComparer&lt;string&gt;.Default.Equals(name, value))
/// {
/// OnPropertyChanging(nameof(Surname));
/// OnPropertyChanged(nameof(FullName));
///
/// surname = value;
///
/// OnPropertyChanged(nameof(Surname));
/// OnPropertyChanged(nameof(FullName));
/// }
/// }
Expand Down
Loading

0 comments on commit 7b53ae2

Please sign in to comment.