Skip to content

Commit

Permalink
Added OnChanged functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
rrmanzano committed Apr 12, 2022
1 parent 2c5b8b9 commit ba73b7b
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 11 deletions.
42 changes: 38 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![NuGet](http://img.shields.io/nuget/vpre/M.BindableProperty.Generator.svg?label=NuGet)](https://www.nuget.org/packages/M.BindableProperty.Generator/)
[![NuGet](http://img.shields.io/nuget/vpre/M.BindableProperty.Generator.svg?label=NuGet)](https://www.nuget.org/packages/M.BindableProperty.Generator/) [![GitHub issues](https://img.shields.io/github/issues/rrmanzano/maui-bindableproperty-generator?style=flat-square)](https://github.com/rrmanzano/maui-bindableproperty-generator/) [![GitHub stars](https://img.shields.io/github/stars/rrmanzano/maui-bindableproperty-generator?style=flat-square)](https://github.com/rrmanzano/maui-bindableproperty-generator/stargazers) ![last commit](https://img.shields.io/github/last-commit/rrmanzano/maui-bindableproperty-generator?style=flat-square)

# Maui.BindableProperty.Generator

Expand Down Expand Up @@ -60,13 +60,47 @@ the prevoius code will generate this:
}
```

## Usage - OnChanged method
Just decorate field with the Bindable attribute. The 'OnChanged' method must have only one parameter (must match the type of the field)

```csharp
using Maui.BindableProperty.Generator.Core;

public partial class CustomEntry : ContentView
{
[AutoBindable(OnChanged = nameof(OnTextChanged))]
private string _text;

private void OnTextChanged(string newValue)
{
// Do stuff here
}
}
```
the prevoius code will generate this:
```csharp
public partial class CustomEntry
{
public static readonly Microsoft.Maui.Controls.BindableProperty TextProperty = Microsoft.Maui.Controls.BindableProperty.Create(nameof(Text), typeof(string), typeof(CustomEntry), default(string));
public string Text
{
get => (string)GetValue(TextProperty);
set =>
{
SetValue(TextProperty, value);
this.OnTextChanged(value);
}
}
}
```

## Project status

- ✅ Simple implementation - Done
- ✅ Custom property name - Done
- 🔲 Custom Parameters - In Progress
- 🔲 OnChanged method - Pending
- 🔲 Dependent properties - Pending
- OnChanged method - Done
- 🔲 Property Accessibility - Pending

## Extra info
This repo is using part of the code of this repo [CodeWriter](https://github.com/SaladLab/CodeWriter "CodeWriter") to generate the CSharp files, thanks to the author.
This repo is using part of the code of [CodeWriter](https://github.com/SaladLab/CodeWriter "CodeWriter") to generate the CSharp files, thanks to the author.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ public partial class CustomEntry: ContentView
private string _placeholder;


[AutoBindable(PropertyName = "Text")]
private string _t;
[AutoBindable(OnChanged = nameof(OnTextChanged))]
private string _text;

private void OnTextChanged(string newValue)
{
// Do stuff here
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public sealed class AutoBindableAttribute : Attribute
public AutoBindableAttribute(){}
public string PropertyName { get; set; }
public string OnChanged { get; set; }
}
}";

Expand Down Expand Up @@ -65,9 +67,15 @@ private void ProcessBindableProperty(CodeWriter w, IFieldSymbol fieldSymbol, ISy

// Get the AutoNotify attribute from the field, and any associated data
var attributeData = fieldSymbol.GetAttributes().Single(ad => ad.AttributeClass.Equals(attributeSymbol, SymbolEqualityComparer.Default));
var overridenNameOpt = attributeData.NamedArguments.SingleOrDefault(kvp => kvp.Key == "PropertyName").Value;
TypedConstant GetValue(string key)
{
return attributeData.NamedArguments.SingleOrDefault(kvp => kvp.Key == key).Value;
}

var propertyName = this.ChooseName(fieldName, overridenNameOpt);
var overridenNameProperty = GetValue("PropertyName");
var overridenOnChanged = GetValue("OnChanged");

var propertyName = this.ChooseName(fieldName, overridenNameProperty);
if (propertyName?.Length == 0 || propertyName == fieldName)
{
// TODO: issue a diagnostic that we can't process this field
Expand All @@ -80,8 +88,20 @@ private void ProcessBindableProperty(CodeWriter w, IFieldSymbol fieldSymbol, ISy
w._(AutoBindableConstants.AttributeGeneratedCodeString);
using (w.B(@$"public {fieldType} {propertyName}"))
{
w._($@"get => ({fieldType})GetValue({bindablePropertyName});",
$@"set => SetValue({bindablePropertyName}, value);");
w._($@"get => ({fieldType})GetValue({bindablePropertyName});");
if (!overridenOnChanged.IsNull)
{
var method = overridenOnChanged.Value?.ToString();
using (w.B(@$"set"))
{
w._($@"SetValue({bindablePropertyName}, value);",
$@"this.{method}(value);");
}
}
else
{
w._($@"set => SetValue({bindablePropertyName}, value);");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<Description>Source generator that automatically transforms fields into BindableProperties that can be used in MAUI</Description>
<PackageTags>MAUI;BindableProperty;Source Generator</PackageTags>
<PackageReadmeFile>README.md</PackageReadmeFile>
<VersionPrefix>0.3.3</VersionPrefix>
<VersionPrefix>0.4.0</VersionPrefix>
<PackageProjectUrl>https://github.com/rrmanzano/maui-bindableproperty-generator</PackageProjectUrl>
<RepositoryUrl>https://github.com/rrmanzano/maui-bindableproperty-generator</RepositoryUrl>
<PackageId>M.BindableProperty.Generator</PackageId>
Expand Down

0 comments on commit ba73b7b

Please sign in to comment.