Skip to content

Commit

Permalink
Add support for generics
Browse files Browse the repository at this point in the history
  • Loading branch information
rrmanzano committed Nov 15, 2022
1 parent ae201aa commit 3ac5cfd
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<VerticalStackLayout xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.BindableProperty.Generator.Demo.CustomControls.HeaderGenericControl"
x:Name="this">

<VerticalStackLayout.Resources>

<Style TargetType="{x:Type Label}">
<Setter Property="Margin" Value="0,10,0,0" />
</Style>

</VerticalStackLayout.Resources>

</VerticalStackLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Maui.BindableProperty.Generator.Core;

namespace Maui.BindableProperty.Generator.Demo.CustomControls
{
public partial class HeaderGenericControl<T> : VerticalStackLayout
{
#pragma warning disable CS0169

[AutoBindable]
private bool _isGenerated;

[AutoBindable(OnChanged = nameof(UpdateGenericSample))]
private readonly T _genericSample = default!;

#pragma warning restore CS0169

public HeaderGenericControl()
{
this.IsGenerated = false;
}

private void UpdateGenericSample()
{
// Do stuff here
}

private void UpdateGenericSample(T oldValue, T newValue)
{
// Do stuff here
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0-android;net6.0-ios;net6.0-maccatalyst</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net6.0-windows10.0.19041.0</TargetFrameworks>
<TargetFrameworks>net7.0-android;net7.0-ios;net7.0-maccatalyst</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net7.0-windows10.0.19041.0</TargetFrameworks>
<!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
<!-- <TargetFrameworks>$(TargetFrameworks);net6.0-tizen</TargetFrameworks> -->
<!-- <TargetFrameworks>$(TargetFrameworks);net7.0-tizen</TargetFrameworks> -->
<OutputType>Exe</OutputType>
<RootNamespace>Maui.BindableProperty.Generator.Demo</RootNamespace>
<UseMaui>true</UseMaui>
Expand Down Expand Up @@ -51,12 +51,24 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommunityToolkit.Maui" Version="1.0.0" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.0.0-preview4" />
<PackageReference Include="CommunityToolkit.Maui" Version="3.0.0" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MAUI.BindableProperty.Generator\Maui.BindableProperty.Generator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>

<ItemGroup>
<Compile Update="CustomControls\HeaderGenericControl.xaml.cs">
<DependentUpon>HeaderGenericControl.xaml</DependentUpon>
</Compile>
</ItemGroup>

<ItemGroup>
<MauiXaml Update="CustomControls\HeaderGenericControl.xaml">
<Generator>MSBuild:Compile</Generator>
</MauiXaml>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="31" />
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="33" />
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private string ProcessClass(INamedTypeSymbol classSymbol, List<IFieldSymbol> fie
w._("#nullable enable");
using (w.B(@$"namespace {namespaceName}"))
{
using (w.B(@$"public partial class {classSymbol.Name}"))
using (w.B(@$"public partial class {classSymbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat)}"))
{
// Create properties for each field
foreach (IFieldSymbol fieldSymbol in fields)
Expand Down Expand Up @@ -101,7 +101,7 @@ private void ProcessBindableProperty(CodeWriter w, IFieldSymbol fieldSymbol, ISy
var applyHidesUnderlying = fieldSymbol.GetValue<bool>(attributeSymbol, AutoBindableConstants.AttrHidesUnderlyingProperty);
var hidesUnderlying = applyHidesUnderlying ? " new" : string.Empty;
var declaringType = fieldType.WithNullableAnnotation(NullableAnnotation.None);
var parameters = $"nameof({propertyName}),typeof({declaringType}),typeof({classSymbol.Name}){customParameters}".Split(',');
var parameters = $"nameof({propertyName}),typeof({declaringType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}),typeof({classSymbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat)}){customParameters}".Split(',');

w._(AttributeBuilder.GetAttrGeneratedCodeString());
w._($@"public static{hidesUnderlying} readonly {AutoBindableConstants.FullNameMauiControls}.BindableProperty {bindablePropertyName} =");
Expand All @@ -116,9 +116,9 @@ private void ProcessBindableProperty(CodeWriter w, IFieldSymbol fieldSymbol, ISy

w._();
AttributeBuilder.WriteAllAttrGeneratedCodeStrings(w);
using (w.B(@$"public{hidesUnderlying} {fieldType} {propertyName}"))
using (w.B(@$"public{hidesUnderlying} {fieldType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)} {propertyName}"))
{
w._($@"get => ({fieldType})GetValue({bindablePropertyName});");
w._($@"get => ({fieldType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)})GetValue({bindablePropertyName});");
if (this.ExistsBodySetter())
{
using (w.B(@$"set"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public string ProcessBindableParameters()
return default;
});

return defaultValue != null ? $"defaultValue: {defaultValue}" : $"defaultValue: default({fieldType})";
return defaultValue != null ? $"defaultValue: {defaultValue}" : $"defaultValue: default({fieldType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)})";
}

public void ProcessBodySetter(CodeWriter w)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void ProcessImplementationLogic(CodeWriter w)
var methods = this.GetMethodsToCall(methodName);
if (methods.Any())
{
w._($@"var ctrl = ({this.ClassSymbol.Name})bindable;");
w._($@"var ctrl = ({this.ClassSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)})bindable;");
methods.ToList().ForEach(m => {
var count = m.Parameters.Count();
if (count == 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void ProcessImplementationLogic(CodeWriter w)
AttributeBuilder.WriteAllAttrGeneratedCodeStrings(w);
using (w.B(methodDefinition))
{
w._($@"var ctrl = ({this.ClassSymbol.Name})bindable;");
w._($@"var ctrl = ({this.ClassSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)})bindable;");
w._($@"return ctrl.{methodName}(ctrl, ({this.FieldSymbol.Type})value);");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
<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.9.1</VersionPrefix>
<VersionPrefix>0.9.2</VersionPrefix>
<PackageProjectUrl>https://github.com/rrmanzano/maui-bindableproperty-generator</PackageProjectUrl>
<RepositoryUrl>https://github.com/rrmanzano/maui-bindableproperty-generator</RepositoryUrl>
<PackageId>M.BindableProperty.Generator</PackageId>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.1.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.3.1" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 3ac5cfd

Please sign in to comment.