Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce fields for commands to get rid of null warnings #88

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,16 @@ namespace MyCode
{{
partial class EmployeeViewModel : global::MvvmGen.ViewModels.ViewModelBase
{{
private IDelegateCommand? _saveAllCommand;

public EmployeeViewModel()
{{
this.InitializeCommands();
this.OnInitialize();
}}

partial void OnInitialize();

private void InitializeCommands()
{{
SaveAllCommand = new DelegateCommand(_ => SaveAll());
}}

public DelegateCommand SaveAllCommand {{ get; private set; }}
public IDelegateCommand SaveAllCommand => _saveAllCommand ??= new DelegateCommand(_ => SaveAll());
}}
}}
");
Expand Down Expand Up @@ -76,20 +72,16 @@ namespace MyCode
{{
partial class EmployeeViewModel : global::MvvmGen.ViewModels.ViewModelBase
{{
private IDelegateCommand? _saveAllCommand;

public EmployeeViewModel()
{{
this.InitializeCommands();
this.OnInitialize();
}}

partial void OnInitialize();

private void InitializeCommands()
{{
SaveAllCommand = new DelegateCommand(_ => SaveAll(), _ => CanSaveAll());
}}

public DelegateCommand SaveAllCommand {{ get; private set; }}
public IDelegateCommand SaveAllCommand => _saveAllCommand ??= new DelegateCommand(_ => SaveAll(), _ => CanSaveAll());
}}
}}
");
Expand Down Expand Up @@ -122,20 +114,16 @@ namespace MyCode
{{
partial class EmployeeViewModel : global::MvvmGen.ViewModels.ViewModelBase
{{
private IDelegateCommand? _superCommand;

public EmployeeViewModel()
{{
this.InitializeCommands();
this.OnInitialize();
}}

partial void OnInitialize();

private void InitializeCommands()
{{
SuperCommand = new DelegateCommand(_ => SaveAll(), _ => CanSaveAll());
}}

public DelegateCommand SuperCommand {{ get; private set; }}
public IDelegateCommand SuperCommand => _superCommand ??= new DelegateCommand(_ => SaveAll(), _ => CanSaveAll());
}}
}}
");
Expand Down Expand Up @@ -172,20 +160,16 @@ namespace MyCode
{{
partial class EmployeeViewModel : global::MvvmGen.ViewModels.ViewModelBase
{{
private IDelegateCommand? _saveCommand;

public EmployeeViewModel()
{{
this.InitializeCommands();
this.OnInitialize();
}}

partial void OnInitialize();

private void InitializeCommands()
{{
SaveCommand = new DelegateCommand({expectedConstructorArguments});
}}

public DelegateCommand SaveCommand {{ get; private set; }}
public IDelegateCommand SaveCommand => _saveCommand ??= new DelegateCommand({expectedConstructorArguments});
}}
}}
");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,16 @@ namespace MyCode
{{
partial class EmployeeViewModel : global::MvvmGen.ViewModels.ViewModelBase
{{
private IDelegateCommand? _saveCommand;

public EmployeeViewModel()
{{
this.InitializeCommands();
this.OnInitialize();
}}

partial void OnInitialize();

private void InitializeCommands()
{{
SaveCommand = new DelegateCommand(_ => Save(), _ => CanSave());
}}

public DelegateCommand SaveCommand {{ get; private set; }}
public IDelegateCommand SaveCommand => _saveCommand ??= new DelegateCommand(_ => Save(), _ => CanSave());

public string FirstName
{{
Expand Down Expand Up @@ -170,23 +166,19 @@ namespace MyCode
{{
partial class EmployeeViewModel : global::MvvmGen.ViewModels.ViewModelBase
{{
private IDelegateCommand? _saveCommand;
private IDelegateCommand? _deleteCommand;

public EmployeeViewModel()
{{
this.InitializeCommands();
this.OnInitialize();
}}

partial void OnInitialize();

private void InitializeCommands()
{{
SaveCommand = new DelegateCommand(_ => Save(), _ => CanSave());
DeleteCommand = new DelegateCommand(_ => Delete(), _ => CanDelete());
}}

public DelegateCommand SaveCommand {{ get; private set; }}
public IDelegateCommand SaveCommand => _saveCommand ??= new DelegateCommand(_ => Save(), _ => CanSave());

public DelegateCommand DeleteCommand {{ get; private set; }}
public IDelegateCommand DeleteCommand => _deleteCommand ??= new DelegateCommand(_ => Delete(), _ => CanDelete());

public string FirstName
{{
Expand Down Expand Up @@ -252,7 +244,7 @@ public partial class EmployeeViewModel
[Command(nameof(CanSave))]
public void Save() {{ }}

[CommandInvalidate(nameof(FirstName);
[CommandInvalidate(nameof(FirstName))]
public bool CanSave() => true;
}}
}}",
Expand All @@ -262,20 +254,16 @@ namespace MyCode
{{
partial class EmployeeViewModel : global::MvvmGen.ViewModels.ViewModelBase
{{
private IDelegateCommand? _saveCommand;

public EmployeeViewModel()
{{
this.InitializeCommands();
this.OnInitialize();
}}

partial void OnInitialize();

private void InitializeCommands()
{{
SaveCommand = new DelegateCommand(_ => Save(), _ => CanSave());
}}

public DelegateCommand SaveCommand {{ get; private set; }}
public IDelegateCommand SaveCommand => _saveCommand ??= new DelegateCommand(_ => Save(), _ => CanSave());

protected override void InvalidateCommands(string? propertyName)
{{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,25 +165,21 @@ namespace MyCode
{{
partial class EmployeeViewModel : global::MvvmGen.ViewModels.ViewModelBase, IEmployeeViewModel
{{
private IDelegateCommand? _saveCommand;

public EmployeeViewModel()
{{
this.InitializeCommands();
this.OnInitialize();
}}

partial void OnInitialize();

private void InitializeCommands()
{{
SaveCommand = new DelegateCommand(_ => Save());
}}

public DelegateCommand SaveCommand {{ get; private set; }}
public IDelegateCommand SaveCommand => _saveCommand ??= new DelegateCommand(_ => Save());
}}

public interface IEmployeeViewModel
{{
DelegateCommand SaveCommand {{ get; }}
IDelegateCommand SaveCommand {{ get; }}
void Save();
}}
}}
Expand Down
27 changes: 27 additions & 0 deletions src/MvvmGen.SourceGenerators/Generators/CommandFieldGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// ***********************************************************************
// ⚡ MvvmGen => https://github.com/thomasclaudiushuber/mvvmgen
// Copyright © by Thomas Claudius Huber
// Licensed under the MIT license => See LICENSE file in repository root
// ***********************************************************************

using System.Collections.Generic;
using System.Linq;
using MvvmGen.Model;

namespace MvvmGen.Generators
{
internal static class CommandFieldGenerator
{
internal static void GenerateCommandFields(this ViewModelBuilder vmBuilder, IEnumerable<CommandToGenerate>? commandsToGenerate)
{
if (commandsToGenerate is not null && commandsToGenerate.Any())
{
vmBuilder.AppendLineBeforeMember();
foreach (var commandToGenerate in commandsToGenerate)
{
vmBuilder.AppendLine($"private IDelegateCommand? {commandToGenerate.FieldName};");
}
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,26 @@ internal static void GenerateCommandProperties(this ViewModelBuilder vmBuilder,
foreach (var commandToGenerate in commandsToGenerate)
{
vmBuilder.AppendLineBeforeMember();
vmBuilder.AppendLine($"public DelegateCommand {commandToGenerate.PropertyName} {{ get; private set; }}");
vmBuilder.Append($"public IDelegateCommand {commandToGenerate.PropertyName} => {commandToGenerate.FieldName} ??= new DelegateCommand({GetMethodCall(commandToGenerate.ExecuteMethod)}");
if (commandToGenerate.CanExecuteMethod is not null)
{
vmBuilder.Append($", {GetMethodCall(commandToGenerate.CanExecuteMethod)}");
}

vmBuilder.AppendLine(");");
}
}
}

private static object GetMethodCall(CommandMethod methodInfo)
{
return methodInfo switch
{
{ IsAwaitable: true, HasParameter: true } => $"async x => await {methodInfo.Name}(x)",
{ IsAwaitable: true, HasParameter: false } => $"async _ => await {methodInfo.Name}()",
{ IsAwaitable: false, HasParameter: true } => $"{methodInfo.Name}",
{ IsAwaitable: false, HasParameter: false } => $"_ => {methodInfo.Name}()",
};
}
}
}
11 changes: 1 addition & 10 deletions src/MvvmGen.SourceGenerators/Generators/ConstructorGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@ internal static void GenerateConstructor(this ViewModelBuilder vmBuilder, ViewMo
{
Generate(vmBuilder, viewModelToGenerate.ClassName,
viewModelToGenerate.InjectionsToGenerate,
viewModelToGenerate.CommandsToGenerate?.Any() == true,
viewModelToGenerate.IsEventSubscriber);
}
}

private static void Generate(ViewModelBuilder vmBuilder, string viewModelClassName,
IEnumerable<InjectionToGenerate>? injectionsToGenerate,
bool hasCommands, bool isEventSubscriber)
IEnumerable<InjectionToGenerate>? injectionsToGenerate, bool isEventSubscriber)
{
vmBuilder.AppendLineBeforeMember();
vmBuilder.Append($"public {viewModelClassName}(");
Expand Down Expand Up @@ -72,18 +70,11 @@ private static void Generate(ViewModelBuilder vmBuilder, string viewModelClassNa
vmBuilder.AppendLine($"{eventAggregatorAccessForSubscription}.RegisterSubscriber(this);");
}

if (hasCommands)
{
vmBuilder.AppendLine($"this.InitializeCommands();");
}

vmBuilder.AppendLine($"this.OnInitialize();");
vmBuilder.DecreaseIndent();
vmBuilder.AppendLine("}");
vmBuilder.AppendLine();
vmBuilder.AppendLine($"partial void OnInitialize();");


}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ internal static class ViewModelGenerateInterfaceAttributeInspector
foreach (var commandToGenerate in commandsToGenerate)
{
properties ??= new();
properties.Add(new InterfaceProperty(commandToGenerate.PropertyName, "DelegateCommand", true));
properties.Add(new InterfaceProperty(commandToGenerate.PropertyName, "IDelegateCommand", true));
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/MvvmGen.SourceGenerators/Model/CommandToGenerate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ public CommandToGenerate(CommandMethod executeMethod, string propertyName)
{
ExecuteMethod = executeMethod;
PropertyName = propertyName;
FieldName = $"_{PropertyName.Substring(0,1).ToLower()}{PropertyName.Substring(1)}";
}

public CommandMethod ExecuteMethod { get; }

public string PropertyName { get; }

public string FieldName { get; }

public CommandMethod? CanExecuteMethod { get; set; }

public override bool Equals(object? obj)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<EmbeddedResource Include="..\MvvmGen\Attributes\ViewModelGenerateFactoryAttribute.cs" Link="MvvmGenLib\Attributes\ViewModelGenerateFactoryAttribute.cs" />
<EmbeddedResource Include="..\MvvmGen\Attributes\ViewModelGenerateInterfaceAttribute.cs" Link="MvvmGenLib\Attributes\ViewModelGenerateInterfaceAttribute.cs" />
<EmbeddedResource Include="..\MvvmGen\Commands\DelegateCommand.cs" Link="MvvmGenLib\Commands\DelegateCommand.cs" />
<EmbeddedResource Include="..\MvvmGen\Commands\IDelegateCommand.cs" Link="MvvmGenLib\Commands\IDelegateCommand.cs" />
<EmbeddedResource Include="..\MvvmGen\Events\EventAggregator.cs" Link="MvvmGenLib\Events\EventAggregator.cs" />
<EmbeddedResource Include="..\MvvmGen\Events\IEventAggregator.cs" Link="MvvmGenLib\Events\IEventAggregator.cs" />
<EmbeddedResource Include="..\MvvmGen\Events\IEventSubscriber.cs" Link="MvvmGenLib\Events\IEventSubscriber.cs" />
Expand Down
4 changes: 2 additions & 2 deletions src/MvvmGen.SourceGenerators/ViewModelGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/// Generates ViewModels for classes that are decorated with the MvvmGen.ViewModelAttribute.
/// </summary>
[Generator]
public class ViewModelGenerator : IIncrementalGenerator

Check warning on line 24 in src/MvvmGen.SourceGenerators/ViewModelGenerator.cs

View workflow job for this annotation

GitHub Actions / build-mvvmgen

'MvvmGen.ViewModelGenerator': A project containing analyzers or source generators should specify the property '<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>'

Check warning on line 24 in src/MvvmGen.SourceGenerators/ViewModelGenerator.cs

View workflow job for this annotation

GitHub Actions / build-mvvmgen

'MvvmGen.ViewModelGenerator': A project containing analyzers or source generators should specify the property '<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>'

Check warning on line 24 in src/MvvmGen.SourceGenerators/ViewModelGenerator.cs

View workflow job for this annotation

GitHub Actions / build-mvvmgen-purecodegen

'MvvmGen.ViewModelGenerator': A project containing analyzers or source generators should specify the property '<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>'

Check warning on line 24 in src/MvvmGen.SourceGenerators/ViewModelGenerator.cs

View workflow job for this annotation

GitHub Actions / build-mvvmgen-purecodegen

'MvvmGen.ViewModelGenerator': A project containing analyzers or source generators should specify the property '<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>'
{
private static readonly string _versionString;

Expand Down Expand Up @@ -137,9 +137,9 @@

vmBuilder.GenerateClass(viewModelToGenerate);

vmBuilder.GenerateConstructor(viewModelToGenerate);
vmBuilder.GenerateCommandFields(viewModelToGenerate.CommandsToGenerate);

vmBuilder.GenerateCommandInitializeMethod(viewModelToGenerate.CommandsToGenerate);
vmBuilder.GenerateConstructor(viewModelToGenerate);

vmBuilder.GenerateCommandProperties(viewModelToGenerate.CommandsToGenerate);

Expand Down
Loading