Skip to content

Commit

Permalink
Feature: add support for composite disposable (#408)
Browse files Browse the repository at this point in the history
* add composite disposable binding handlers

* composite command bindings
  • Loading branch information
dpvreony authored May 29, 2024
1 parent 052d9d5 commit c0eeb73
Show file tree
Hide file tree
Showing 16 changed files with 388 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/Vetuviem.Core/AbstractControlBindingModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for full license information.

using System;
using System.Reactive.Disposables;

namespace Vetuviem.Core
{
Expand Down Expand Up @@ -36,5 +37,11 @@ public abstract void ApplyBindings(
TView view,
TViewModel viewModel,
Action<IDisposable> disposeAction);

/// <inheritdoc/>
public abstract void ApplyBindings(
TView view,
TViewModel viewModel,
CompositeDisposable disposeAction);
}
}
17 changes: 17 additions & 0 deletions src/Vetuviem.Core/AbstractEnableViewToViewModelBindings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Collections.Generic;
using System.Reactive.Disposables;
using ReactiveUI;

namespace Vetuviem.Core
Expand Down Expand Up @@ -34,6 +35,22 @@ public void ApplyBindings(
}
}

/// <inheritdoc />
public void ApplyBindings(
CompositeDisposable compositeDisposable,
TView view,
TViewModel viewModel)
{
var bindings = GetBindings();
foreach (var viewBindingModel in bindings)
{
viewBindingModel.ApplyBindings(
view,
viewModel,
compositeDisposable);
}
}

/// <summary>
/// Gets the controls to be bound on the view.
/// </summary>
Expand Down
36 changes: 36 additions & 0 deletions src/Vetuviem.Core/CommandBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Linq.Expressions;
using System.Reactive;
using System.Reactive.Disposables;
using System.Windows.Input;
using ReactiveUI;

Expand Down Expand Up @@ -67,5 +68,40 @@ public void ApplyBinding<TView>(
viewBinding,
_toEvent));
}

/// <inheritdoc/>
public void ApplyBinding<TView>(
CompositeDisposable compositeDisposable,
TView view,
TViewModel viewModel,
Expression<Func<TView, ICommand>> viewBinding)
where TView : class, IViewFor<TViewModel>
{
if (compositeDisposable == null)
{
throw new ArgumentNullException(nameof(compositeDisposable));
}

if (view == null)
{
throw new ArgumentNullException(nameof(view));
}

if (viewModel == null)
{
throw new ArgumentNullException(nameof(viewModel));
}

if (viewBinding == null)
{
throw new ArgumentNullException(nameof(viewBinding));
}

view.BindCommand(
viewModel,
_viewModelBinding,
viewBinding,
_toEvent).DisposeWith(compositeDisposable);
}
}
}
16 changes: 16 additions & 0 deletions src/Vetuviem.Core/ICommandBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Linq.Expressions;
using System.Reactive.Disposables;
using ReactiveUI;

namespace Vetuviem.Core
Expand All @@ -30,5 +31,20 @@ void ApplyBinding<TView>(
TViewModel viewModel,
Expression<Func<TView, TViewProp>> viewBinding)
where TView : class, IViewFor<TViewModel>;

/// <summary>
/// Applies a View to View Model Binding.
/// </summary>
/// <typeparam name="TView">The type for the view.</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>(
CompositeDisposable compositeDisposable,
TView view,
TViewModel viewModel,
Expression<Func<TView, TViewProp>> viewBinding)
where TView : class, IViewFor<TViewModel>;
}
}
9 changes: 9 additions & 0 deletions src/Vetuviem.Core/IControlBindingModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for full license information.

using System;
using System.Reactive.Disposables;
using ReactiveUI;

namespace Vetuviem.Core
Expand All @@ -23,5 +24,13 @@ public interface IControlBindingModel<in TView, in TViewModel>
/// <param name="viewModel">The viewmodel.</param>
/// <param name="disposeAction">The action to register disposals against.</param>
void ApplyBindings(TView view, TViewModel viewModel, Action<IDisposable> disposeAction);

/// <summary>
/// Applies the binding between the view and the view model.
/// </summary>
/// <param name="view">The view.</param>
/// <param name="viewModel">The viewmodel.</param>
/// <param name="compositeDisposable">The disposable container to register disposals against.</param>
void ApplyBindings(TView view, TViewModel viewModel, CompositeDisposable compositeDisposable);
}
}
14 changes: 13 additions & 1 deletion src/Vetuviem.Core/IEnableViewToViewModelBindings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for full license information.

using System;
using System.Reactive.Disposables;
using ReactiveUI;

namespace Vetuviem.Core
Expand All @@ -19,12 +20,23 @@ public interface IEnableViewToViewModelBindings<in TView, in TViewModel>
/// <summary>
/// Apply control bindings between a View and ViewModel.
/// </summary>
/// <param name="disposeWithAction">The ReactiveUI Disposal Tracker.</param>
/// <param name="disposeWithAction">The ReactiveUI Disposal Tracker. Used to discard binding registrations when the view is finished with them.</param>
/// <param name="view">Instance of the view.</param>
/// <param name="viewModel">Instance of the viewmodel.</param>
void ApplyBindings(
Action<IDisposable> disposeWithAction,
TView view,
TViewModel viewModel);

/// <summary>
/// Apply control bindings between a View and ViewModel.
/// </summary>
/// <param name="compositeDisposable">The Composite Disposable Tracker. Used to discard binding registrations when the view is finished with them.</param>
/// <param name="view">Instance of the view.</param>
/// <param name="viewModel">Instance of the viewmodel.</param>
void ApplyBindings(
CompositeDisposable compositeDisposable,
TView view,
TViewModel viewModel);
}
}
16 changes: 16 additions & 0 deletions src/Vetuviem.Core/IOneOrTwoWayBind{TViewModel,TViewProp}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Linq.Expressions;
using System.Reactive.Disposables;
using ReactiveUI;

namespace Vetuviem.Core
Expand All @@ -30,5 +31,20 @@ void ApplyBinding<TView>(
TViewModel viewModel,
Expression<Func<TView, TViewProp>> viewBinding)
where TView : class, IViewFor<TViewModel>;

/// <summary>
/// Applies a View to View Model Binding.
/// </summary>
/// <typeparam name="TView">The type for the view.</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>(
CompositeDisposable compositeDisposable,
TView view,
TViewModel viewModel,
Expression<Func<TView, TViewProp>> viewBinding)
where TView : class, IViewFor<TViewModel>;
}
}
16 changes: 16 additions & 0 deletions src/Vetuviem.Core/IOneWayBind{TViewModel,TViewProp}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Linq.Expressions;
using System.Reactive.Disposables;
using ReactiveUI;

namespace Vetuviem.Core
Expand Down Expand Up @@ -35,5 +36,20 @@ void ApplyBinding<TView>(
TViewModel viewModel,
Expression<Func<TView, TViewProp>> viewBinding)
where TView : class, IViewFor<TViewModel>;

/// <summary>
/// Applies a View to View Model Binding.
/// </summary>
/// <typeparam name="TView">The type for the view.</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>(
CompositeDisposable compositeDisposable,
TView view,
TViewModel viewModel,
Expression<Func<TView, TViewProp>> viewBinding)
where TView : class, IViewFor<TViewModel>;
}
}
20 changes: 20 additions & 0 deletions src/Vetuviem.Core/OneWayBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Linq.Expressions;
using System.Reactive.Disposables;
using ReactiveUI;

namespace Vetuviem.Core
Expand Down Expand Up @@ -49,5 +50,24 @@ public void ApplyBinding<TView>(
ViewModelBinding,
viewBinding));
}

/// <inheritdoc/>
public void ApplyBinding<TView>(
CompositeDisposable compositeDisposable,
TView view,
TViewModel viewModel,
Expression<Func<TView, TViewProp>> viewBinding)
where TView : class, IViewFor<TViewModel>
{
if (compositeDisposable == null)
{
throw new ArgumentNullException(nameof(compositeDisposable));
}

view.OneWayBind(
viewModel,
ViewModelBinding,
viewBinding).DisposeWith(compositeDisposable);
}
}
}
21 changes: 21 additions & 0 deletions src/Vetuviem.Core/OneWayBindingOnOneOrTwoWayBind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Linq.Expressions;
using System.Reactive.Disposables;
using ReactiveUI;

namespace Vetuviem.Core
Expand Down Expand Up @@ -51,5 +52,25 @@ public void ApplyBinding<TView>(
viewBinding,
_vmToViewConverter));
}

/// <inheritdoc/>
public void ApplyBinding<TView>(
CompositeDisposable compositeDisposable,
TView view,
TViewModel viewModel,
Expression<Func<TView, TViewProp>> viewBinding)
where TView : class, IViewFor<TViewModel>
{
if (compositeDisposable == null)
{
throw new ArgumentNullException(nameof(compositeDisposable));
}

view.OneWayBind(
viewModel,
_viewModelBinding,
viewBinding,
_vmToViewConverter).DisposeWith(compositeDisposable);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Linq.Expressions;
using System.Reactive.Disposables;
using ReactiveUI;

namespace Vetuviem.Core
Expand Down Expand Up @@ -53,5 +54,25 @@ public void ApplyBinding<TView>(
viewBinding,
_converterBinding));
}

/// <inheritdoc/>
public void ApplyBinding<TView>(
CompositeDisposable compositeDisposable,
TView view,
TViewModel viewModel,
Expression<Func<TView, TOut>> viewBinding)
where TView : class, IViewFor<TViewModel>
{
if (compositeDisposable == null)
{
throw new ArgumentNullException(nameof(compositeDisposable));
}

view.OneWayBind(
viewModel,
_viewModelBinding,
viewBinding,
_converterBinding).DisposeWith(compositeDisposable);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Linq.Expressions;
using System.Reactive.Disposables;
using ReactiveUI;

namespace Vetuviem.Core
Expand Down Expand Up @@ -60,5 +61,25 @@ public void ApplyBinding<TView>(
viewBinding,
Selector));
}

/// <inheritdoc />
public void ApplyBinding<TView>(
CompositeDisposable compositeDisposable,
TView view,
TViewModel viewModel,
Expression<Func<TView, TViewProp>> viewBinding)
where TView : class, IViewFor<TViewModel>
{
if (compositeDisposable == null)
{
throw new ArgumentNullException(nameof(compositeDisposable));
}

view.OneWayBind(
viewModel,
ViewModelBinding,
viewBinding,
Selector).DisposeWith(compositeDisposable);
}
}
}
Loading

0 comments on commit c0eeb73

Please sign in to comment.