Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored StepperHandler
Browse files Browse the repository at this point in the history
jsuarezruiz committed Oct 13, 2020
1 parent 554128d commit 1bb998f
Showing 9 changed files with 156 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Android.Widget;
using Android.Views;
using AButton = Android.Widget.Button;
@@ -7,14 +8,14 @@ namespace Xamarin.Platform.Handlers
{
public partial class StepperHandler : AbstractViewHandler<IStepper, LinearLayout> , IStepperHandler
{
AButton _downButton;
AButton _upButton;
AButton? _downButton;
AButton? _upButton;

IStepper IStepperHandler.Element => VirtualView;
IStepper? IStepperHandler.Element => VirtualView;

AButton IStepperHandler.UpButton => _upButton;
AButton? IStepperHandler.UpButton => _upButton;

AButton IStepperHandler.DownButton => _downButton;
AButton? IStepperHandler.DownButton => _downButton;

protected override LinearLayout CreateView()
{
@@ -26,24 +27,14 @@ protected override LinearLayout CreateView()
};

StepperHandlerManager.CreateStepperButtons(this, out _downButton, out _upButton);
stepperLayout.AddView(_downButton, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.MatchParent));
stepperLayout.AddView(_upButton, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.MatchParent));

return stepperLayout;
}

public static void MapMinimum(IViewHandler handler, IStepper slider) => (handler as StepperHandler)?.UpdateButtons();

public static void MapMaximum(IViewHandler handler, IStepper slider) => (handler as StepperHandler)?.UpdateButtons();
if (_downButton != null)
stepperLayout.AddView(_downButton, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.MatchParent));

public static void MapIncrement(IViewHandler handler, IStepper slider) => (handler as StepperHandler)?.UpdateButtons();
if (_upButton != null)
stepperLayout.AddView(_upButton, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.MatchParent));

public static void MapValue(IViewHandler handler, IStepper slider) => (handler as StepperHandler)?.UpdateButtons();

public static void MapIsEnabled(IViewHandler handler, IStepper slider)
{
ViewHandler.MapPropertyIsEnabled(handler, slider);
(handler as StepperHandler)?.UpdateButtons();
return stepperLayout;
}

public virtual void UpdateButtons()
@@ -53,6 +44,9 @@ public virtual void UpdateButtons()

AButton IStepperHandler.CreateButton()
{
if (Context == null)
throw new ArgumentException("Context is null or empty", nameof(Context));

var button = new AButton(Context);
button.SetHeight((int)Context.ToPixels(10.0));
return button;
Original file line number Diff line number Diff line change
@@ -5,10 +5,5 @@ namespace Xamarin.Platform.Handlers
public partial class StepperHandler : AbstractViewHandler<IStepper, object>
{
protected override object CreateView() => throw new NotImplementedException();

public static void MapMinimum(IViewHandler handler, IStepper slider) { }
public static void MapMaximum(IViewHandler handler, IStepper slider) { }
public static void MapIncrement(IViewHandler handler, IStepper slider) { }
public static void MapValue(IViewHandler handler, IStepper slider) { }
}
}
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
{
public partial class StepperHandler
{
public static PropertyMapper<IStepper> StepperMapper = new PropertyMapper<IStepper>(ViewHandler.ViewMapper)
public static PropertyMapper<IStepper, StepperHandler> StepperMapper = new PropertyMapper<IStepper, StepperHandler>(ViewHandler.ViewMapper)
{
[nameof(IStepper.Minimum)] = MapMinimum,
[nameof(IStepper.Maximum)] = MapMaximum,
@@ -13,6 +13,37 @@ public partial class StepperHandler
#endif
};

public static void MapMinimum(StepperHandler handler, IStepper stepper)
{
ViewHandler.CheckParameters(handler, stepper);
handler.TypedNativeView?.UpdateMinimum(handler, stepper);
}

public static void MapMaximum(StepperHandler handler, IStepper stepper)
{
ViewHandler.CheckParameters(handler, stepper);
handler.TypedNativeView?.UpdateMaximum(handler, stepper);
}

public static void MapIncrement(StepperHandler handler, IStepper stepper)
{
ViewHandler.CheckParameters(handler, stepper);
handler.TypedNativeView?.UpdateIncrement(handler, stepper);
}

public static void MapValue(StepperHandler handler, IStepper stepper)
{
ViewHandler.CheckParameters(handler, stepper);
handler.TypedNativeView?.UpdateValue(handler, stepper);
}

#if __ANDROID__ || NETCOREAPP
public static void MapIsEnabled(StepperHandler handler, IStepper stepper)
{
ViewHandler.CheckParameters(handler, stepper);
handler.TypedNativeView?.UpdateIsEnabled(handler, stepper);
}
#endif
public StepperHandler() : base(StepperMapper)
{

Original file line number Diff line number Diff line change
@@ -13,43 +13,11 @@ protected override UIStepper CreateView()
return uIStepper;
}

protected override void DisposeView(UIStepper uIStepper)
{
uIStepper.ValueChanged -= OnValueChanged;
base.DisposeView(uIStepper);
}

public static void MapMinimum(IViewHandler handler, IStepper slider) => (handler as StepperHandler)?.UpdateMinimum();

public static void MapMaximum(IViewHandler handler, IStepper slider) => (handler as StepperHandler)?.UpdateMaximum();

public static void MapIncrement(IViewHandler handler, IStepper slider) => (handler as StepperHandler)?.UpdateIncrement();

public static void MapValue(IViewHandler handler, IStepper slider) => (handler as StepperHandler)?.UpdateValue();

void UpdateIncrement()
{
TypedNativeView.StepValue = VirtualView.Increment;
}

void UpdateMaximum()
{
TypedNativeView.MaximumValue = VirtualView.Maximum;
}

void UpdateMinimum()
{
TypedNativeView.MinimumValue = VirtualView.Minimum;
}

void UpdateValue()
{
if (TypedNativeView.Value != VirtualView.Value)
TypedNativeView.Value = VirtualView.Value;
}

void OnValueChanged(object sender, EventArgs e)
{
if (TypedNativeView == null || VirtualView == null)
return;

VirtualView.Value = TypedNativeView.Value;
VirtualView.ValueChanged();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Android.Widget;
using Xamarin.Platform.Handlers;

namespace Xamarin.Platform
{
public static class StepperExtensions
{
public static void UpdateMinimum(this LinearLayout linearLayout, StepperHandler handler, IStepper stepper)
{
handler.UpdateButtons();
}

public static void UpdateMaximum(this LinearLayout linearLayout, StepperHandler handler, IStepper stepper)
{
handler.UpdateButtons();
}

public static void UpdateIncrement(this LinearLayout linearLayout, StepperHandler handler, IStepper stepper)
{
handler.UpdateButtons();
}

public static void UpdateValue(this LinearLayout linearLayout, StepperHandler handler, IStepper stepper)
{
handler.UpdateButtons();
}

public static void UpdateIsEnabled(this LinearLayout linearLayout, StepperHandler handler, IStepper stepper)
{
ViewHandler.MapIsEnabled(handler, stepper);
handler.UpdateButtons();
}
}
}
Original file line number Diff line number Diff line change
@@ -7,13 +7,13 @@ namespace Xamarin.Platform
{
public interface IStepperHandler
{
AButton UpButton { get; }
AButton? UpButton { get; }

AButton DownButton { get; }
AButton? DownButton { get; }

AButton CreateButton();

IStepper Element { get; }
IStepper? Element { get; }
}

class StepperHandlerHolder : Java.Lang.Object
@@ -28,7 +28,7 @@ public StepperHandlerHolder(IStepperHandler handler)

public static class StepperHandlerManager
{
public static void CreateStepperButtons<TButton>(IStepperHandler handler, out TButton downButton, out TButton upButton)
public static void CreateStepperButtons<TButton>(IStepperHandler handler, out TButton? downButton, out TButton? upButton)
where TButton : AButton
{
downButton = (TButton)handler.CreateButton();
@@ -61,32 +61,35 @@ public static void CreateStepperButtons<TButton>(IStepperHandler handler, out TB
downButton.NextFocusForwardId = upButton.Id;
}

public static void UpdateButtons<TButton>(IStepperHandler handler, TButton downButton, TButton upButton, PropertyChangedEventArgs e = null)
public static void UpdateButtons<TButton>(IStepperHandler handler, TButton? downButton, TButton? upButton, PropertyChangedEventArgs? e = null)
where TButton : AButton
{
if (!(handler?.Element is IStepper stepper))
return;

// NOTE: a value of `null` means that we are forcing an update
downButton.Enabled = stepper.IsEnabled && stepper.Value > stepper.Minimum;
upButton.Enabled = stepper.IsEnabled && stepper.Value < stepper.Maximum;
if (downButton != null)
downButton.Enabled = stepper.IsEnabled && stepper.Value > stepper.Minimum;

if (upButton != null)
upButton.Enabled = stepper.IsEnabled && stepper.Value < stepper.Maximum;
}

class StepperListener : Java.Lang.Object, AView.IOnClickListener
{
public static readonly StepperListener Instance = new StepperListener();

public void OnClick(AView v)
public void OnClick(AView? view)
{
if (!(v?.Tag is StepperHandlerHolder HandlerHolder))
if (!(view?.Tag is StepperHandlerHolder HandlerHolder))
return;

if (!(HandlerHolder.StepperHandler?.Element is IStepper stepper))
return;

var increment = stepper.Increment;

if (v == HandlerHolder.StepperHandler.DownButton)
if (view == HandlerHolder.StepperHandler.DownButton)
increment = -increment;

HandlerHolder.StepperHandler.Element.Value = stepper.Value + increment;
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace Xamarin.Platform
{
public static class StepperExtensions
{
public static void UpdateMinimum(this object nothing, object handler, IStepper stepper)
{

}

public static void UpdateMaximum(this object nothing, object handler, IStepper stepper)
{

}

public static void UpdateIncrement(this object nothing, object handler, IStepper stepper)
{

}

public static void UpdateValue(this object nothing, object handler, IStepper stepper)
{

}

public static void UpdateIsEnabled(this object nothing, object handler, IStepper stepper)
{

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using UIKit;
using Xamarin.Platform.Handlers;

namespace Xamarin.Platform
{
public static class StepperExtensions
{
public static void UpdateMinimum(this UIStepper nativeStepper, StepperHandler handler, IStepper stepper)
{
nativeStepper.MinimumValue = stepper.Minimum;
}

public static void UpdateMaximum(this UIStepper nativeStepper, StepperHandler handler, IStepper stepper)
{
nativeStepper.MaximumValue = stepper.Maximum;
}

public static void UpdateIncrement(this UIStepper nativeStepper, StepperHandler handler, IStepper stepper)
{
nativeStepper.StepValue = stepper.Increment;
}

public static void UpdateValue(this UIStepper nativeStepper, StepperHandler handler, IStepper stepper)
{
if (nativeStepper.Value != stepper.Value)
nativeStepper.Value = stepper.Value;
}
}
}
Original file line number Diff line number Diff line change
@@ -15,6 +15,6 @@ public PortHandlerAttribute(string description)
Description = description;
}

public string Description { get; set; }
public string? Description { get; set; }
}
}

0 comments on commit 1bb998f

Please sign in to comment.