Skip to content

Commit

Permalink
simplify command binding generics
Browse files Browse the repository at this point in the history
  • Loading branch information
dpvreony committed Jun 9, 2024
1 parent f027414 commit bbfdb6d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using ReactiveUI.WPF.SampleApp.ViewModels;
using ReactiveUI.WPF.SampleApp.Views;
Expand Down
11 changes: 5 additions & 6 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,7 +14,7 @@ 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 sealed class CommandBinding<TViewModel> : ICommandBinding<TViewModel>
where TViewModel : class
{
private readonly Expression<Func<TViewModel, ICommand?>> _viewModelBinding;
Expand All @@ -35,11 +34,11 @@ public CommandBinding(
}

/// <inheritdoc/>
public void ApplyBinding<TView>(
public void ApplyBinding<TView, TViewProp>(
Action<IDisposable> disposeAction,
TView view,
TViewModel viewModel,
Expression<Func<TView, ICommand>> viewBinding)
Expression<Func<TView, TViewProp>> viewBinding)
where TView : class, IViewFor<TViewModel>
{
if (disposeAction == null)
Expand Down Expand Up @@ -70,11 +69,11 @@ public void ApplyBinding<TView>(
}

/// <inheritdoc/>
public void ApplyBinding<TView>(
public void ApplyBinding<TView, TViewProp>(
CompositeDisposable compositeDisposable,
TView view,
TViewModel viewModel,
Expression<Func<TView, ICommand>> viewBinding)
Expression<Func<TView, TViewProp>> viewBinding)
where TView : class, IViewFor<TViewModel>
{
if (compositeDisposable == null)
Expand Down
9 changes: 5 additions & 4 deletions src/Vetuviem.Core/ICommandBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ namespace Vetuviem.Core
/// Represents a View to View Model Binding for Commands.
/// </summary>
/// <typeparam name="TViewModel">The type for the ViewModel.</typeparam>
/// <typeparam name="TViewProp">The type for the View.</typeparam>
public interface ICommandBinding<TViewModel, TViewProp>
public interface ICommandBinding<TViewModel>
where TViewModel : class
{
/// <summary>
/// Applies a View to View Model Binding.
/// </summary>
/// <typeparam name="TView">The type for the view.</typeparam>
/// <typeparam name="TViewProp">The type for the View Property.</typeparam>
/// <param name="d">The disposable action registration. Used to clean up when bindings fall out of scope.</param>
/// <param name="view">The instance of the View to bind.</param>
/// <param name="viewModel">The instance of the ViewModel to Bind.</param>
/// <param name="viewBinding">Expression of the View Property to Bind to.</param>
void ApplyBinding<TView>(
void ApplyBinding<TView, TViewProp>(
Action<IDisposable> d,
TView view,
TViewModel viewModel,
Expand All @@ -36,11 +36,12 @@ void ApplyBinding<TView>(
/// Applies a View to View Model Binding.
/// </summary>
/// <typeparam name="TView">The type for the view.</typeparam>
/// <typeparam name="TViewProp">The type for the View Property.</typeparam>
/// <param name="compositeDisposable">The disposable action registration. Used to clean up when bindings fall out of scope.</param>
/// <param name="view">The instance of the View to bind.</param>
/// <param name="viewModel">The instance of the ViewModel to Bind.</param>
/// <param name="viewBinding">Expression of the View Property to Bind to.</param>
void ApplyBinding<TView>(
void ApplyBinding<TView, TViewProp>(
CompositeDisposable compositeDisposable,
TView view,
TViewModel viewModel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ private static PropertyDeclarationSyntax GetPropertyDeclaration(

private static TypeSyntax GetCommandBindingTypeSyntax(string platformCommandType)
{
var type = SyntaxFactory.ParseTypeName($"global::Vetuviem.Core.ICommandBinding<TViewModel, {platformCommandType}>?");
var type = SyntaxFactory.ParseTypeName($"global::Vetuviem.Core.ICommandBinding<TViewModel>?");
return type;
}

Expand Down

0 comments on commit bbfdb6d

Please sign in to comment.