-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bugfix - OnChanged method refactored to allow BindingPropertyChangedD…
…elegate
- Loading branch information
Showing
5 changed files
with
137 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/Maui.BindableProperty.Generator/Core/BindableProperty/Implementation/IImplementation.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace Maui.BindableProperty.Generator.Core.BindableProperty.Implementation | ||
{ | ||
public interface IImplementation | ||
{ | ||
void Initialize(TypedConstant nameProperty, IFieldSymbol fieldSymbol, ISymbol attributeSymbol, INamedTypeSymbol classSymbol); | ||
bool Implemented(); | ||
string ProcessBindableParameters(); | ||
void ProcessBodyStter(CodeWriter w); | ||
void ProcessImplementationLogic(CodeWriter w); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/Maui.BindableProperty.Generator/Core/BindableProperty/Implementation/PropertyChanged.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using Maui.BindableProperty.Generator.Helpers; | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace Maui.BindableProperty.Generator.Core.BindableProperty.Implementation | ||
{ | ||
public class PropertyChanged : IImplementation | ||
{ | ||
private TypedConstant NameProperty { get; set; } | ||
private TypedConstant OnChangedProperty { get; set; } | ||
private IFieldSymbol FieldSymbol { get; set; } | ||
private ISymbol AttributeSymbol { get; set; } | ||
private INamedTypeSymbol ClassSymbol { get; set; } | ||
|
||
public void Initialize(TypedConstant nameProperty, IFieldSymbol fieldSymbol, ISymbol attributeSymbol, INamedTypeSymbol classSymbol) | ||
{ | ||
this.NameProperty = nameProperty; | ||
this.OnChangedProperty = fieldSymbol.GetTypedConstant(attributeSymbol, "OnChanged"); | ||
this.FieldSymbol = fieldSymbol; | ||
this.AttributeSymbol = attributeSymbol; | ||
this.ClassSymbol = classSymbol; | ||
} | ||
|
||
public bool Implemented() | ||
{ | ||
return !this.OnChangedProperty.IsNull; | ||
} | ||
|
||
public string ProcessBindableParameters() | ||
{ | ||
if (!this.OnChangedProperty.IsNull) | ||
{ | ||
var method = this.OnChangedProperty.Value?.ToString(); | ||
return $@"propertyChanged: __{method}"; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public void ProcessBodyStter(CodeWriter w) | ||
{ | ||
if (!this.OnChangedProperty.IsNull) | ||
{ | ||
var method = this.OnChangedProperty.Value?.ToString(); | ||
w._($@"this.{method}(value);"); | ||
} | ||
} | ||
|
||
public void ProcessImplementationLogic(CodeWriter w) | ||
{ | ||
if (!this.OnChangedProperty.IsNull) | ||
{ | ||
var method = this.OnChangedProperty.Value?.ToString(); | ||
w._(AutoBindableConstants.AttributeGeneratedCodeString); | ||
using (w.B(@$"public static void __{method}(Microsoft.Maui.Controls.BindableObject bindable, object oldValue, object newValue)")) | ||
{ | ||
w._($@"(({this.ClassSymbol.Name})bindable).{method}(({this.FieldSymbol.Type})newValue);"); | ||
} | ||
} | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/Maui.BindableProperty.Generator/Helpers/SymbolExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace Maui.BindableProperty.Generator.Helpers | ||
{ | ||
internal static class SymbolExtensions | ||
{ | ||
public static TypedConstant GetTypedConstant( | ||
this IFieldSymbol fieldSymbol, | ||
ISymbol attributeSymbol, | ||
string key) | ||
{ | ||
// Get the AutoNotify attribute from the field, and any associated data | ||
var attributeData = fieldSymbol.GetAttributes().Single(ad => ad.AttributeClass.Equals(attributeSymbol, SymbolEqualityComparer.Default)); | ||
return attributeData.NamedArguments.SingleOrDefault(kvp => kvp.Key == key).Value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters