Skip to content

Commit 033b3d7

Browse files
committed
Update generated files
1 parent f04b977 commit 033b3d7

File tree

2 files changed

+89
-29
lines changed

2 files changed

+89
-29
lines changed

sample/ImmutableEditableObjectAdapter.Samples/GeneratedFiles/ImmutableEditableObjectAdapter/ImmutableEditableObjectAdapter.ImmutableEditableObjectAdapterGenerator/EditableObjectAdapterDeclaration.g.cs

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,21 @@ namespace System.ComponentModel
55
using global::System.Runtime.CompilerServices;
66

77
/// <summary>
8-
/// Provides the old, and new value of the <see cref="EditedEventHandler{TContract}"/>.
8+
/// Provides the old, and new value of the <see cref="EditedEventHandler{TContract}"/>, and indicates whether the value has changed.
99
/// </summary>
1010
/// <typeparam name="TContract">The type of the contract <c>record</c>.</typeparam>
1111
public sealed class EditedEventArgs<TContract> : EventArgs
1212
{
13-
public EditedEventArgs(
14-
TContract oldValue,
15-
TContract newValue
16-
)
13+
public EditedEventArgs(TContract oldValue, TContract newValue, bool cancelledOrUnchanged)
1714
{
1815
OldValue = oldValue;
1916
NewValue = newValue;
17+
CancelledOrUnchanged = cancelledOrUnchanged;
2018
}
2119

2220
public TContract OldValue { get; }
2321
public TContract NewValue { get; }
22+
public bool CancelledOrUnchanged { get; }
2423
}
2524

2625
/// <summary>
@@ -30,29 +29,74 @@ TContract newValue
3029
public delegate void EditedEventHandler<TContract>(
3130
ImmutableEditableObjectAdapter<TContract> sender,
3231
EditedEventArgs<TContract> args
33-
) where TContract : notnull;
32+
)
33+
where TContract : notnull;
34+
35+
/// <summary>
36+
/// Non-generic interface implemented by <see cref="ImmutableEditableObjectAdapter{TContract}"/>.
37+
/// </summary>
38+
public interface IImmutableEditableObjectAdapter : IEditableObject, INotifyPropertyChanged, INotifyPropertyChanging
39+
{
40+
/// <summary>
41+
/// Occurs before <see cref="IEditableObject.EndEdit"/> replaces the immutable state <c>record</c>, or <see cref="IEditableObject.CancelEdit"/> discards changes.
42+
/// </summary>
43+
event EventHandler? Edited;
44+
}
3445

3546
/// <summary>
3647
/// Derive a <c>sealed partial class</c> to generate a <see cref="IEditableObject"/> from a immutable state <c>record</c> type.
3748
/// <br/>
3849
/// Update the immutable state when the <see cref="Edited"/> event indicates the state is replaced.
3950
/// </summary>
4051
/// <typeparam name="TContract">The type of the contract <c>record</c>.</typeparam>
41-
public abstract class ImmutableEditableObjectAdapter<TContract>
42-
: IEditableObject, INotifyPropertyChanged, INotifyPropertyChanging
52+
public abstract class ImmutableEditableObjectAdapter<TContract> : IImmutableEditableObjectAdapter
4353
where TContract : notnull
4454
{
55+
private ConditionalWeakTable<Delegate, Delegate>? _editedByGenericEdited;
56+
4557
/// <inheritdoc />
4658
public event PropertyChangedEventHandler? PropertyChanged;
4759

4860
/// <inheritdoc />
4961
public event PropertyChangingEventHandler? PropertyChanging;
5062

5163
/// <summary>
52-
/// Occurs before <see cref="EndEdit"/> replaces the immutable state <c>record</c>.
64+
/// Occurs before <see cref="EndEdit"/> replaces the immutable state <c>record</c>, or <see cref="CancelEdit"/> discards changes.
5365
/// </summary>
5466
public event EditedEventHandler<TContract>? Edited;
5567

68+
/// <inheritdoc />
69+
event EventHandler? IImmutableEditableObjectAdapter.Edited
70+
{
71+
add
72+
{
73+
if (value is null)
74+
{
75+
return;
76+
}
77+
78+
_editedByGenericEdited ??= new ConditionalWeakTable<Delegate, Delegate>();
79+
if (!_editedByGenericEdited.TryGetValue(value, out var edited))
80+
{
81+
edited = (EditedEventHandler<TContract>)value.Invoke;
82+
_editedByGenericEdited.Add(value, edited);
83+
}
84+
85+
Edited += (EditedEventHandler<TContract>)edited;
86+
}
87+
remove
88+
{
89+
if (value is not null
90+
&& _editedByGenericEdited is not null
91+
&& _editedByGenericEdited.TryGetValue(value, out var edited)
92+
&& _editedByGenericEdited.Remove(value)
93+
)
94+
{
95+
Edited -= (EditedEventHandler<TContract>)edited;
96+
}
97+
}
98+
}
99+
56100
/// <inheritdoc />
57101
public abstract void BeginEdit();
58102

@@ -63,12 +107,12 @@ public abstract class ImmutableEditableObjectAdapter<TContract>
63107
public abstract void EndEdit();
64108

65109
/// <summary>
66-
/// Enumerate names of all changed properties during edit, and <see cref="Edited"/>.
110+
/// Enumerate names of all changed properties during edit.
67111
/// </summary>
68112
public abstract IEnumerable<string> ChangedProperties();
69113

70114
/// <summary>
71-
/// Indicates whether the property with the name name has changed during edit, and <see cref="Edited"/>.
115+
/// Indicates whether the property with the name name has changed during edit.
72116
/// </summary>
73117
public abstract bool IsPropertyChanged(string propertyName);
74118

@@ -82,14 +126,16 @@ protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName
82126
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
83127
}
84128

85-
protected virtual void OnEdited(TContract oldValue, TContract newValue)
129+
protected virtual void OnEdited(TContract oldValue, TContract newValue, bool cancelledOrUnchanged)
86130
{
87-
Edited?.Invoke(this, new EditedEventArgs<TContract>(oldValue, newValue));
131+
EditedEventArgs<TContract> args = new EditedEventArgs<TContract>(oldValue, newValue, cancelledOrUnchanged);
132+
Edited?.Invoke(this, args);
88133
}
89134

90135
protected bool SetField<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
91136
{
92-
if (EqualityComparer<T>.Default.Equals(field, value)) return false;
137+
if (EqualityComparer<T>.Default.Equals(field, value))
138+
return false;
93139
OnPropertyChanging(propertyName);
94140
field = value;
95141
OnPropertyChanged(propertyName);

sample/ImmutableEditableObjectAdapter.Samples/GeneratedFiles/ImmutableEditableObjectAdapter/ImmutableEditableObjectAdapter.ImmutableEditableObjectAdapterGenerator/EditablePerson.g.cs

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,35 @@ public override void CancelEdit()
203203
{
204204
ThrowIfNotEditing();
205205
SetEditing(false);
206+
OnEdited(Unedited, Unedited, true);
207+
DiscardChanges();
208+
}
209+
210+
public override void EndEdit()
211+
{
212+
ThrowIfNotEditing();
213+
SetEditing(false);
214+
global::Person unedited = _unedited;
215+
bool changed = ((_changedFlags1 | 0ul) != 0ul);
216+
if (!changed)
217+
{
218+
OnEdited(unedited, unedited, true);
219+
return;
220+
}
221+
222+
global::Person edited = unedited with {
223+
Name = NamePropertyChanged ? _changedName : Unedited.Name,
224+
FavouriteColor = FavouriteColorPropertyChanged ? _changedFavouriteColor : Unedited.FavouriteColor,
225+
BirthDay = BirthDayPropertyChanged ? _changedBirthDay : Unedited.BirthDay,
226+
DeceasedAt = DeceasedAtPropertyChanged ? _changedDeceasedAt : Unedited.DeceasedAt,
227+
};
228+
OnEdited(unedited, edited, false);
229+
DiscardChanges();
230+
SetField(ref _unedited, edited, nameof(Unedited));
231+
}
232+
233+
private void DiscardChanges()
234+
{
206235
bool isNameChanged = NamePropertyChanged;
207236
NamePropertyChanged = false;
208237
if (isNameChanged) OnPropertyChanging(nameof(Name));
@@ -225,21 +254,6 @@ public override void CancelEdit()
225254
if (isDeceasedAtChanged) OnPropertyChanged(nameof(DeceasedAt));
226255
}
227256

228-
public override void EndEdit()
229-
{
230-
ThrowIfNotEditing();
231-
global::Person unedited = _unedited;
232-
global::Person edited = unedited with {
233-
Name = NamePropertyChanged ? _changedName : Unedited.Name,
234-
FavouriteColor = FavouriteColorPropertyChanged ? _changedFavouriteColor : Unedited.FavouriteColor,
235-
BirthDay = BirthDayPropertyChanged ? _changedBirthDay : Unedited.BirthDay,
236-
DeceasedAt = DeceasedAtPropertyChanged ? _changedDeceasedAt : Unedited.DeceasedAt,
237-
};
238-
OnEdited(unedited, edited);
239-
CancelEdit();
240-
SetField(ref _unedited, edited, nameof(Unedited));
241-
}
242-
243257
public override IEnumerable<string> ChangedProperties()
244258
{
245259
if (NamePropertyChanged)

0 commit comments

Comments
 (0)