Skip to content

Commit 7b53ae2

Browse files
authored
Merge pull request #722 from CommunityToolkit/dev/notify-for-changing
Fix generation for OnPropertyChanging for [NotifyPropertyChangedFor]
2 parents ecd1711 + 8857555 commit 7b53ae2

File tree

4 files changed

+465
-22
lines changed

4 files changed

+465
-22
lines changed

src/CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.Execute.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ public static bool TryGetInfo(
109109
token.ThrowIfCancellationRequested();
110110

111111
using ImmutableArrayBuilder<string> propertyChangedNames = ImmutableArrayBuilder<string>.Rent();
112-
using ImmutableArrayBuilder<string> propertyChangingNames = ImmutableArrayBuilder<string>.Rent();
113112
using ImmutableArrayBuilder<string> notifiedCommandNames = ImmutableArrayBuilder<string>.Rent();
114113
using ImmutableArrayBuilder<AttributeInfo> forwardedAttributes = ImmutableArrayBuilder<AttributeInfo>.Rent();
115114

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

132131
token.ThrowIfCancellationRequested();
133132

134-
// Track the property changing event for the property, if the type supports it
135-
if (shouldInvokeOnPropertyChanging)
136-
{
137-
propertyChangingNames.Add(propertyName);
138-
}
139-
140133
// The current property is always notified
141134
propertyChangedNames.Add(propertyName);
142135

@@ -296,12 +289,24 @@ public static bool TryGetInfo(
296289

297290
token.ThrowIfCancellationRequested();
298291

292+
// Prepare the effective property changing/changed names. For the property changing names,
293+
// there are two possible cases: if the mode is disabled, then there are no names to report
294+
// at all. If the mode is enabled, then the list is just the same as for property changed.
295+
ImmutableArray<string> effectivePropertyChangedNames = propertyChangedNames.ToImmutable();
296+
ImmutableArray<string> effectivePropertyChangingNames = shouldInvokeOnPropertyChanging switch
297+
{
298+
true => effectivePropertyChangedNames,
299+
false => ImmutableArray<string>.Empty
300+
};
301+
302+
token.ThrowIfCancellationRequested();
303+
299304
propertyInfo = new PropertyInfo(
300305
typeNameWithNullabilityAnnotations,
301306
fieldName,
302307
propertyName,
303-
propertyChangingNames.ToImmutable(),
304-
propertyChangedNames.ToImmutable(),
308+
effectivePropertyChangingNames,
309+
effectivePropertyChangedNames,
305310
notifiedCommandNames.ToImmutable(),
306311
notifyRecipients,
307312
notifyDataErrorInfo,

src/CommunityToolkit.Mvvm/ComponentModel/Attributes/NotifyPropertyChangedForAttribute.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,14 @@ namespace CommunityToolkit.Mvvm.ComponentModel;
4646
/// get => name;
4747
/// set
4848
/// {
49-
/// if (SetProperty(ref name, value))
49+
/// if (!EqualityComparer&lt;string&gt;.Default.Equals(name, value))
5050
/// {
51+
/// OnPropertyChanging(nameof(Name));
52+
/// OnPropertyChanged(nameof(FullName));
53+
///
54+
/// name = value;
55+
///
56+
/// OnPropertyChanged(nameof(Name));
5157
/// OnPropertyChanged(nameof(FullName));
5258
/// }
5359
/// }
@@ -58,8 +64,14 @@ namespace CommunityToolkit.Mvvm.ComponentModel;
5864
/// get => surname;
5965
/// set
6066
/// {
61-
/// if (SetProperty(ref surname, value))
67+
/// if (!EqualityComparer&lt;string&gt;.Default.Equals(name, value))
6268
/// {
69+
/// OnPropertyChanging(nameof(Surname));
70+
/// OnPropertyChanged(nameof(FullName));
71+
///
72+
/// surname = value;
73+
///
74+
/// OnPropertyChanged(nameof(Surname));
6375
/// OnPropertyChanged(nameof(FullName));
6476
/// }
6577
/// }

0 commit comments

Comments
 (0)