Skip to content

Commit

Permalink
prep for command binding fix
Browse files Browse the repository at this point in the history
  • Loading branch information
dpvreony committed Jun 8, 2024
1 parent dc79350 commit b97cf71
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 75 deletions.
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
62 changes: 4 additions & 58 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,72 +14,19 @@ namespace Vetuviem.Core
/// Represents a command binding between a control and a viewmodel.
/// </summary>
/// <typeparam name="TViewModel">The type for the viewmodel.</typeparam>
public class CommandBinding<TViewModel> : CommandBinding<TViewModel, Unit, Unit>
public class CommandBinding<TViewModel> : ICommandBinding<TViewModel, ICommand>
where TViewModel : class
{
/// <summary>
/// Initializes a new instance of the <see cref="CommandBinding{TViewModel}"/> class.
/// </summary>
/// <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,
string? toEvent = null)
: base(
viewModelBinding,
toEvent)
{
}
}

/// <summary>
/// Represents a command binding between a control and a viewmodel.
/// </summary>
/// <typeparam name="TViewModel">The type for the viewmodel.</typeparam>
/// <typeparam name="TResult">
/// The type of the values that are the result of command execution.
/// </typeparam>
public class CommandBinding<TViewModel, TResult> : CommandBinding<TViewModel, Unit, TResult>
where TViewModel : class
{
/// <summary>
/// Initializes a new instance of the <see cref="CommandBinding{TViewModel, TResult}"/> class.
/// </summary>
/// <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, TResult>?>> viewModelBinding,
string? toEvent = null)
: base(
viewModelBinding,
toEvent)
{
}
}

/// <summary>
/// Represents a command binding between a control and a viewmodel.
/// </summary>
/// <typeparam name="TViewModel">The type for the viewmodel.</typeparam>
/// <typeparam name="TParam">
/// The type of parameter values passed in during command execution.
/// </typeparam>
/// <typeparam name="TResult">
/// The type of the values that are the result of command execution.
/// </typeparam>
public class CommandBinding<TViewModel, TParam, TResult> : ICommandBinding<TViewModel, ICommand>
where TViewModel : class
{
private readonly Expression<Func<TViewModel, ReactiveCommand<TParam, TResult>?>> _viewModelBinding;
private readonly Expression<Func<TViewModel, ICommand?>> _viewModelBinding;
private readonly string? _toEvent;

/// <summary>
/// Initializes a new instance of the <see cref="CommandBinding{TViewModel, TParam, TResult}"/> class.
/// Initializes a new instance of the <see cref="CommandBinding{TViewModel}"/> class.
/// </summary>
/// <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<TParam, TResult>?>> 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 @@ private static TypeSyntax GetBindingTypeSyntax(
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

0 comments on commit b97cf71

Please sign in to comment.