Skip to content

Commit

Permalink
Merge pull request #1829 from tritter/feature/ios-control-events
Browse files Browse the repository at this point in the history
Add support for all UIControlEvents
  • Loading branch information
martijn00 committed May 22, 2017
2 parents c2e85eb + 4126f73 commit d54ac7d
Show file tree
Hide file tree
Showing 9 changed files with 314 additions and 214 deletions.
3 changes: 1 addition & 2 deletions MvvmCross/Binding/iOS/MvvmCross.Binding.iOS.csproj
Expand Up @@ -49,8 +49,6 @@
<Compile Include="Target\MvxBaseUIDatePickerTargetBinding.cs" />
<Compile Include="Target\MvxBaseUIViewVisibleTargetBinding.cs" />
<Compile Include="Target\MvxUIActivityIndicatorViewHiddenTargetBinding.cs" />
<Compile Include="Target\MvxUIControlTouchUpInsideTargetBinding.cs" />
<Compile Include="Target\MvxUIControlValueChangedTargetBinding.cs" />
<Compile Include="Target\MvxUIDatePickerDateTargetBinding.cs" />
<Compile Include="Target\MvxUIDatePickerTimeTargetBinding.cs" />
<Compile Include="Target\MvxUILabelTextTargetBinding.cs" />
Expand Down Expand Up @@ -93,6 +91,7 @@
<Compile Include="Views\IMvxBindable.cs" />
<Compile Include="Views\MvxView.cs" />
<Compile Include="Target\MvxUISegmentedControlSelectedSegmentTargetBinding.cs" />
<Compile Include="Target\MvxUIControlTargetBinding.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Platform\iOS\MvvmCross.Platform.iOS.csproj">
Expand Down
50 changes: 47 additions & 3 deletions MvvmCross/Binding/iOS/MvxIosBindingBuilder.cs
Expand Up @@ -45,12 +45,56 @@ protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry reg
base.FillTargetFactories(registry);

registry.RegisterCustomBindingFactory<UIControl>(
MvxIosPropertyBinding.UIControl_TouchDown,
view => new MvxUIControlTargetBinding(view, MvxIosPropertyBinding.UIControl_TouchDown));

registry.RegisterCustomBindingFactory<UIControl>(
MvxIosPropertyBinding.UIControl_TouchDownRepeat,
view => new MvxUIControlTargetBinding(view, MvxIosPropertyBinding.UIControl_TouchDownRepeat));

registry.RegisterCustomBindingFactory<UIControl>(
MvxIosPropertyBinding.UIControl_TouchDragInside,
view => new MvxUIControlTargetBinding(view, MvxIosPropertyBinding.UIControl_TouchDragInside));

registry.RegisterCustomBindingFactory<UIControl>(
MvxIosPropertyBinding.UIControl_TouchUpInside,
view => new MvxUIControlTouchUpInsideTargetBinding(view));
view => new MvxUIControlTargetBinding(view, MvxIosPropertyBinding.UIControl_TouchUpInside));

registry.RegisterCustomBindingFactory<UIControl>(
registry.RegisterCustomBindingFactory<UIControl>(
MvxIosPropertyBinding.UIControl_ValueChanged,
view => new MvxUIControlValueChangedTargetBinding(view));
view => new MvxUIControlTargetBinding(view, MvxIosPropertyBinding.UIControl_ValueChanged));

registry.RegisterCustomBindingFactory<UIControl>(
MvxIosPropertyBinding.UIControl_PrimaryActionTriggered,
view => new MvxUIControlTargetBinding(view, MvxIosPropertyBinding.UIControl_PrimaryActionTriggered));

registry.RegisterCustomBindingFactory<UIControl>(
MvxIosPropertyBinding.UIControl_EditingDidBegin,
view => new MvxUIControlTargetBinding(view, MvxIosPropertyBinding.UIControl_EditingDidBegin));

registry.RegisterCustomBindingFactory<UIControl>(
MvxIosPropertyBinding.UIControl_EditingChanged,
view => new MvxUIControlTargetBinding(view, MvxIosPropertyBinding.UIControl_EditingChanged));

registry.RegisterCustomBindingFactory<UIControl>(
MvxIosPropertyBinding.UIControl_EditingDidEnd,
view => new MvxUIControlTargetBinding(view, MvxIosPropertyBinding.UIControl_EditingDidEnd));

registry.RegisterCustomBindingFactory<UIControl>(
MvxIosPropertyBinding.UIControl_EditingDidEndOnExit,
view => new MvxUIControlTargetBinding(view, MvxIosPropertyBinding.UIControl_EditingDidEndOnExit));

registry.RegisterCustomBindingFactory<UIControl>(
MvxIosPropertyBinding.UIControl_AllTouchEvents,
view => new MvxUIControlTargetBinding(view, MvxIosPropertyBinding.UIControl_AllTouchEvents));

registry.RegisterCustomBindingFactory<UIControl>(
MvxIosPropertyBinding.UIControl_AllEditingEvents,
view => new MvxUIControlTargetBinding(view, MvxIosPropertyBinding.UIControl_AllEditingEvents));

registry.RegisterCustomBindingFactory<UIControl>(
MvxIosPropertyBinding.UIControl_AllEvents,
view => new MvxUIControlTargetBinding(view, MvxIosPropertyBinding.UIControl_AllEvents));

registry.RegisterCustomBindingFactory<UIView>(
MvxIosPropertyBinding.UIView_Visibility,
Expand Down
13 changes: 12 additions & 1 deletion MvvmCross/Binding/iOS/MvxIosPropertyBinding.cs
Expand Up @@ -9,9 +9,20 @@ namespace MvvmCross.Binding.iOS
{
internal static class MvxIosPropertyBinding
{
public const string UIControl_TouchDown = "TouchDown";
public const string UIControl_TouchDownRepeat = "TouchDownRepeat";
public const string UIControl_TouchDragInside = "TouchDragInside";
public const string UIControl_TouchUpInside = "TouchUpInside";
public const string UIControl_ValueChanged = "ValueChanged";
public const string UIView_Visibility = "Visibility";
public const string UIControl_PrimaryActionTriggered = "PrimaryActionTriggered";
public const string UIControl_EditingDidBegin = "EditingDidBegin";
public const string UIControl_EditingChanged = "EditingChanged";
public const string UIControl_EditingDidEnd = "EditingDidEnd";
public const string UIControl_EditingDidEndOnExit = "EditingDidEndOnExit";
public const string UIControl_AllTouchEvents = "AllTouchEvents";
public const string UIControl_AllEditingEvents = "AllEditingEvents";
public const string UIControl_AllEvents = "AllEvents";
public const string UIView_Visibility = "Visibility";
public const string UIView_Visible = "Visible";
public const string UIActivityIndicatorView_Hidden = "Hidden";
public const string UIView_Hidden = "Hidden";
Expand Down
33 changes: 33 additions & 0 deletions MvvmCross/Binding/iOS/MvxIosPropertyBindingExtensions.cs
Expand Up @@ -17,6 +17,39 @@ public static string BindTouchUpInside(this UIControl uiControl)
public static string BindValueChanged(this UIControl uiControl)
=> MvxIosPropertyBinding.UIControl_ValueChanged;

public static string BindTouchDown(this UIControl uiControl)
=> MvxIosPropertyBinding.UIControl_TouchDown;

public static string BindTouchDownRepeat(this UIControl uiControl)
=> MvxIosPropertyBinding.UIControl_TouchDownRepeat;

public static string BindTouchDragInside(this UIControl uiControl)
=> MvxIosPropertyBinding.UIControl_TouchDragInside;

public static string BindPrimaryActionTriggered(this UIControl uiControl)
=> MvxIosPropertyBinding.UIControl_PrimaryActionTriggered;

public static string BindEditingDidBegin(this UIControl uiControl)
=> MvxIosPropertyBinding.UIControl_EditingDidBegin;

public static string BindEditingChanged(this UIControl uiControl)
=> MvxIosPropertyBinding.UIControl_EditingChanged;

public static string BindEditingDidEnd(this UIControl uiControl)
=> MvxIosPropertyBinding.UIControl_EditingDidEnd;

public static string BindEditingDidEndOnExit(this UIControl uiControl)
=> MvxIosPropertyBinding.UIControl_EditingDidEndOnExit;

public static string BindAllTouchEvents(this UIControl uiControl)
=> MvxIosPropertyBinding.UIControl_AllTouchEvents;

public static string BindAllEditingEvents(this UIControl uiControl)
=> MvxIosPropertyBinding.UIControl_AllEditingEvents;

public static string BindAllEvents(this UIControl uiControl)
=> MvxIosPropertyBinding.UIControl_AllEvents;

public static string BindVisibility(this UIView uiView)
=> MvxIosPropertyBinding.UIView_Visibility;

Expand Down
209 changes: 209 additions & 0 deletions MvvmCross/Binding/iOS/Target/MvxUIControlTargetBinding.cs
@@ -0,0 +1,209 @@
// MvxUIControlTargetBinding.cs

// MvvmCross is licensed using Microsoft Public License (Ms-PL)
// Contributions and inspirations noted in readme.md and license.txt
//
// Project Lead - Stuart Lodge, @slodge, me@slodge.com

namespace MvvmCross.Binding.iOS.Target
{
using System;
using System.Windows.Input;

using MvvmCross.Binding.Bindings.Target;
using MvvmCross.Platform.Platform;
using MvvmCross.Platform.WeakSubscription;
using UIKit;

public class MvxUIControlTargetBinding : MvxConvertingTargetBinding
{
private ICommand _command;
private IDisposable _canExecuteSubscription;
private readonly EventHandler<EventArgs> _canExecuteEventHandler;
private readonly string _controlEvent;
protected UIControl Control => base.Target as UIControl;

public MvxUIControlTargetBinding(UIControl control, string controlEvent)
: base(control)
{
_controlEvent = controlEvent;

if (control == null)
{
MvxBindingTrace.Trace(MvxTraceLevel.Error, "Error - UIControl is null in MvxUIControlTargetBinding");
}
else
{
AddHandler(control);
}

this._canExecuteEventHandler = new EventHandler<EventArgs>(this.OnCanExecuteChanged);
}

private void ControlEvent(object sender, EventArgs eventArgs)
{
if (this._command == null)
return;

if (!this._command.CanExecute(null))
return;

this._command.Execute(null);
}

public override MvxBindingMode DefaultMode => MvxBindingMode.OneWay;

public override System.Type TargetType => typeof(ICommand);

protected override void SetValueImpl(object target, object value)
{
if (this._canExecuteSubscription != null)
{
this._canExecuteSubscription.Dispose();
this._canExecuteSubscription = null;
}
this._command = value as ICommand;
if (this._command != null)
{
this._canExecuteSubscription = this._command.WeakSubscribe(this._canExecuteEventHandler);
}
this.RefreshEnabledState();
}

private void RefreshEnabledState()
{
var view = this.Control;
if (view == null)
return;

var shouldBeEnabled = false;
if (this._command != null)
{
shouldBeEnabled = this._command.CanExecute(null);
}
view.Enabled = shouldBeEnabled;
}

private void OnCanExecuteChanged(object sender, EventArgs e)
{
this.RefreshEnabledState();
}

protected override void Dispose(bool isDisposing)
{
if (isDisposing)
{
var view = this.Control;
if (view != null)
{
RemoveHandler(view);
}
if (this._canExecuteSubscription != null)
{
this._canExecuteSubscription.Dispose();
this._canExecuteSubscription = null;
}
}
base.Dispose(isDisposing);
}

private void AddHandler(UIControl control)
{
switch (_controlEvent)
{
case (MvxIosPropertyBinding.UIControl_TouchDown):
control.TouchDown += this.ControlEvent;
break;
case (MvxIosPropertyBinding.UIControl_TouchDownRepeat):
control.TouchDownRepeat += this.ControlEvent;
break;
case (MvxIosPropertyBinding.UIControl_TouchDragInside):
control.TouchDragInside += this.ControlEvent;
break;
case (MvxIosPropertyBinding.UIControl_TouchUpInside):
control.TouchUpInside += this.ControlEvent;
break;
case (MvxIosPropertyBinding.UIControl_ValueChanged):
control.ValueChanged += this.ControlEvent;
break;
case (MvxIosPropertyBinding.UIControl_PrimaryActionTriggered):
control.PrimaryActionTriggered += this.ControlEvent;
break;
case (MvxIosPropertyBinding.UIControl_EditingDidBegin):
control.EditingDidBegin += this.ControlEvent;
break;
case (MvxIosPropertyBinding.UIControl_EditingChanged):
control.EditingChanged += this.ControlEvent;
break;
case (MvxIosPropertyBinding.UIControl_EditingDidEnd):
control.EditingDidEnd += this.ControlEvent;
break;
case (MvxIosPropertyBinding.UIControl_EditingDidEndOnExit):
control.EditingDidEndOnExit += this.ControlEvent;
break;
case (MvxIosPropertyBinding.UIControl_AllTouchEvents):
control.AllTouchEvents += this.ControlEvent;
break;
case (MvxIosPropertyBinding.UIControl_AllEditingEvents):
control.AllEditingEvents += this.ControlEvent;
break;
case (MvxIosPropertyBinding.UIControl_AllEvents):
control.AllEvents += this.ControlEvent;
break;
default:
MvxBindingTrace.Trace(MvxTraceLevel.Error, "Error - Invalid controlEvent in MvxUIControlTargetBinding");
break;
}

}

private void RemoveHandler(UIControl control)
{
switch (_controlEvent)
{
case (MvxIosPropertyBinding.UIControl_TouchDown):
control.TouchDown -= this.ControlEvent;
break;
case (MvxIosPropertyBinding.UIControl_TouchDownRepeat):
control.TouchDownRepeat -= this.ControlEvent;
break;
case (MvxIosPropertyBinding.UIControl_TouchDragInside):
control.TouchDragInside -= this.ControlEvent;
break;
case (MvxIosPropertyBinding.UIControl_TouchUpInside):
control.TouchUpInside -= this.ControlEvent;
break;
case (MvxIosPropertyBinding.UIControl_ValueChanged):
control.ValueChanged -= this.ControlEvent;
break;
case (MvxIosPropertyBinding.UIControl_PrimaryActionTriggered):
control.PrimaryActionTriggered -= this.ControlEvent;
break;
case (MvxIosPropertyBinding.UIControl_EditingDidBegin):
control.EditingDidBegin -= this.ControlEvent;
break;
case (MvxIosPropertyBinding.UIControl_EditingChanged):
control.EditingChanged -= this.ControlEvent;
break;
case (MvxIosPropertyBinding.UIControl_EditingDidEnd):
control.EditingDidEnd -= this.ControlEvent;
break;
case (MvxIosPropertyBinding.UIControl_EditingDidEndOnExit):
control.EditingDidEndOnExit -= this.ControlEvent;
break;
case (MvxIosPropertyBinding.UIControl_AllTouchEvents):
control.AllTouchEvents -= this.ControlEvent;
break;
case (MvxIosPropertyBinding.UIControl_AllEditingEvents):
control.AllEditingEvents -= this.ControlEvent;
break;
case (MvxIosPropertyBinding.UIControl_AllEvents):
control.AllEvents -= this.ControlEvent;
break;
default:
MvxBindingTrace.Trace(MvxTraceLevel.Error, "Error - Invalid controlEvent in MvxUIControlTargetBinding");
break;
}
}
}
}

0 comments on commit d54ac7d

Please sign in to comment.