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

feature: add support for input, output args on commands #416

Closed
wants to merge 2 commits into from
Closed
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 @@ -71,7 +71,7 @@ public QuestionnaireViewModel()
vm => vm.AnswerFive,
vm => vm.AnswerFiveLengthRemaining);

LaunchInteraction = ReactiveCommand.CreateFromTask(() => OnLaunchInteraction());
LaunchInteraction = ReactiveCommand.CreateFromTask(OnLaunchInteraction);
}

/// <summary>
Expand Down
3 changes: 3 additions & 0 deletions src/ReactiveUI.WPF.SampleApp/Views/QuestionnaireView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public QuestionnaireView()

private void OnWhenActivated(Action<IDisposable> disposeWithAction)
{
#if TBC
disposeWithAction(this.BindCommand(ViewModel, vw => vw.LaunchInteraction, vm => vm.LaunchInteraction));
#endif
new QuestionnaireViewBindingModels().ApplyBindings(
disposeWithAction,
this,
Expand Down
7 changes: 3 additions & 4 deletions src/Vetuviem.Core/CommandBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

using System;
using System.Linq.Expressions;
using System.Reactive;
using System.Reactive.Disposables;
using System.Windows.Input;
using ReactiveUI;
Expand All @@ -15,10 +14,10 @@ namespace Vetuviem.Core
/// Represents a command binding between a control and a viewmodel.
/// </summary>
/// <typeparam name="TViewModel">The type for the viewmodel.</typeparam>
public sealed class CommandBinding<TViewModel> : ICommandBinding<TViewModel, ICommand>
public class CommandBinding<TViewModel> : ICommandBinding<TViewModel, ICommand>
where TViewModel : class
{
private readonly Expression<Func<TViewModel, ReactiveCommand<Unit, Unit>?>> _viewModelBinding;
private readonly Expression<Func<TViewModel, ICommand?>> _viewModelBinding;
private readonly string? _toEvent;

/// <summary>
Expand All @@ -27,7 +26,7 @@ public sealed class CommandBinding<TViewModel> : ICommandBinding<TViewModel, ICo
/// <param name="viewModelBinding">Expression for the View Model binding.</param>
/// <param name="toEvent">If specified, bind to the specific event instead of the default.</param>
public CommandBinding(
Expression<Func<TViewModel, ReactiveCommand<Unit, Unit>?>> viewModelBinding,
Expression<Func<TViewModel, ICommand?>> viewModelBinding,
string? toEvent = null)
{
_viewModelBinding = viewModelBinding;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@
prop,
desiredCommandInterface);

#error we need to change command to handle the control type instead of the property type. This is because BindCommand binds to the control, not the property.

Check failure on line 159 in src/Vetuviem.SourceGenerator/Features/ControlBindingModels/ControlBindingModelPropertyGenerator.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

#error: 'we need to change command to handle the control type instead of the property type. This is because BindCommand binds to the control, not the property.'

Check failure on line 159 in src/Vetuviem.SourceGenerator/Features/ControlBindingModels/ControlBindingModelPropertyGenerator.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

#error: 'we need to change command to handle the control type instead of the property type. This is because BindCommand binds to the control, not the property.'

Check failure on line 159 in src/Vetuviem.SourceGenerator/Features/ControlBindingModels/ControlBindingModelPropertyGenerator.cs

View workflow job for this annotation

GitHub Actions / build

#error: 'we need to change command to handle the control type instead of the property type. This is because BindCommand binds to the control, not the property.'

Check failure on line 159 in src/Vetuviem.SourceGenerator/Features/ControlBindingModels/ControlBindingModelPropertyGenerator.cs

View workflow job for this annotation

GitHub Actions / build

#error: 'we need to change command to handle the control type instead of the property type. This is because BindCommand binds to the control, not the property.'

var returnType = prop.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
var type = SyntaxFactory.ParseTypeName($"global::Vetuviem.Core.{bindingName}<TViewModel, {returnType}>?");
return type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,14 +388,23 @@ private static StatementSyntax[] GetApplyBindingMethodBody(
continue;
}

var propType = propertySymbol.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
var propType = propertySymbol.Type;
var propTypeDisplayString = propType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);

var isCommand = !string.IsNullOrWhiteSpace(desiredCommandInterface) &&
(propType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat).Equals(desiredCommandInterface, StringComparison.Ordinal)
|| propType.AllInterfaces.Any(interfaceName => interfaceName.GetFullName().Equals(desiredCommandInterface, StringComparison.Ordinal)));

var expressionArg = isCommand
? propertySymbol.Name
: $"global::Vetuviem.Core.ExpressionHelpers.GetControlPropertyExpressionFromViewExpression<TView, TControl, {propTypeDisplayString}>(VetuviemControlBindingExpression, \"{propertySymbol.Name}\")";

var invokeArgs = new[]
{
"registerForDisposalAction",
"view",
"viewModel",
$"global::Vetuviem.Core.ExpressionHelpers.GetControlPropertyExpressionFromViewExpression<TView, TControl, {propType}>(VetuviemControlBindingExpression, \"{propertySymbol.Name}\")",
expressionArg,
};

var invocationStatement = RoslynGenerationHelpers.GetMethodOnPropertyOfVariableInvocationSyntax(
Expand All @@ -406,7 +415,7 @@ private static StatementSyntax[] GetApplyBindingMethodBody(

AddInvocationStatementToRelevantCollection(
propertySymbol,
desiredCommandInterface,
isCommand,
invocationStatement,
commandBindingStatements,
oneWayBindingStatements,
Expand Down Expand Up @@ -471,14 +480,23 @@ private static StatementSyntax[] GetApplyBindingCompositeDisposableMethodBody(IN
continue;
}

var propType = propertySymbol.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
var propType = propertySymbol.Type;
var propTypeDisplayString = propType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);

var isCommand = !string.IsNullOrWhiteSpace(desiredCommandInterface) &&
(propType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat).Equals(desiredCommandInterface, StringComparison.Ordinal)
|| propType.AllInterfaces.Any(interfaceName => interfaceName.GetFullName().Equals(desiredCommandInterface, StringComparison.Ordinal)));

var expressionArg = isCommand
? propertySymbol.Name
: $"global::Vetuviem.Core.ExpressionHelpers.GetControlPropertyExpressionFromViewExpression<TView, TControl, {propTypeDisplayString}>(VetuviemControlBindingExpression, \"{propertySymbol.Name}\")";

var invokeArgs = new[]
{
"compositeDisposable",
"view",
"viewModel",
$"global::Vetuviem.Core.ExpressionHelpers.GetControlPropertyExpressionFromViewExpression<TView, TControl, {propType}>(VetuviemControlBindingExpression, \"{propertySymbol.Name}\")",
expressionArg,
};

var invocationStatement = RoslynGenerationHelpers.GetMethodOnPropertyOfVariableInvocationSyntax(
Expand All @@ -489,7 +507,7 @@ private static StatementSyntax[] GetApplyBindingCompositeDisposableMethodBody(IN

AddInvocationStatementToRelevantCollection(
propertySymbol,
desiredCommandInterface,
isCommand,
invocationStatement,
commandBindingStatements,
oneWayBindingStatements,
Expand All @@ -505,22 +523,16 @@ private static StatementSyntax[] GetApplyBindingCompositeDisposableMethodBody(IN

private static void AddInvocationStatementToRelevantCollection(
IPropertySymbol prop,
string? desiredCommandInterface,
bool isCommand,
StatementSyntax invocation,
ICollection<StatementSyntax> commandBindingStatements,
ICollection<StatementSyntax> oneWayBindingStatements,
ICollection<StatementSyntax> twoWayBindingStatements)
{
if (!string.IsNullOrWhiteSpace(desiredCommandInterface))
if (isCommand)
{
var propType = prop.Type;
var isCommand = propType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat).Equals(desiredCommandInterface, StringComparison.Ordinal)
|| propType.AllInterfaces.Any(interfaceName => interfaceName.GetFullName().Equals(desiredCommandInterface, StringComparison.Ordinal));
if (isCommand)
{
commandBindingStatements.Add(invocation);
return;
}
commandBindingStatements.Add(invocation);
return;
}

if (prop.IsReadOnly)
Expand Down
Loading