2017-12-16 12:06:34 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using Xamarin.Forms;
|
|
|
|
|
using Ooui.Forms.Extensions;
|
|
|
|
|
|
|
|
|
|
namespace Ooui.Forms.Renderers
|
|
|
|
|
{
|
|
|
|
|
public class TimePickerRenderer : ViewRenderer<TimePicker, Input>
|
|
|
|
|
{
|
|
|
|
|
bool _disposed;
|
|
|
|
|
|
|
|
|
|
IElementController ElementController => Element as IElementController;
|
|
|
|
|
|
|
|
|
|
public override SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint)
|
|
|
|
|
{
|
2018-02-03 01:15:38 +00:00
|
|
|
|
var fontSize = 16.0;
|
|
|
|
|
var size = "00:00:00".MeasureSize(string.Empty, fontSize, FontAttributes.None, widthConstraint, heightConstraint);
|
2018-04-16 02:06:31 +00:00
|
|
|
|
size = new Size(size.Width + 32, size.Height + 16);
|
2017-12-16 12:06:34 +00:00
|
|
|
|
return new SizeRequest(size, size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnElementChanged(ElementChangedEventArgs<TimePicker> e)
|
|
|
|
|
{
|
|
|
|
|
base.OnElementChanged(e);
|
|
|
|
|
|
|
|
|
|
if (e.NewElement == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (Control == null)
|
|
|
|
|
{
|
|
|
|
|
var entry = new Input
|
|
|
|
|
{
|
|
|
|
|
ClassName = "form-control",
|
|
|
|
|
Type = InputType.Time
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
entry.Change += OnEnded;
|
|
|
|
|
|
|
|
|
|
SetNativeControl(entry);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UpdateTime(false);
|
|
|
|
|
UpdateTextColor();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
base.OnElementPropertyChanged(sender, e);
|
|
|
|
|
|
2017-12-16 16:01:06 +00:00
|
|
|
|
if (e.PropertyName == TimePicker.TimeProperty.PropertyName || e.PropertyName == TimePicker.FormatProperty.PropertyName)
|
2017-12-16 12:06:34 +00:00
|
|
|
|
UpdateTime(true);
|
2017-12-16 16:01:06 +00:00
|
|
|
|
else if (e.PropertyName == TimePicker.TextColorProperty.PropertyName)
|
2017-12-16 12:06:34 +00:00
|
|
|
|
UpdateTextColor();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OnEnded(object sender, EventArgs eventArgs)
|
|
|
|
|
{
|
2017-12-16 12:12:03 +00:00
|
|
|
|
TimeSpan.TryParse(Control.Value, out var time);
|
|
|
|
|
ElementController?.SetValueFromRenderer(TimePicker.TimeProperty, time);
|
2017-12-16 12:06:34 +00:00
|
|
|
|
ElementController?.SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UpdateTime(bool animate)
|
|
|
|
|
{
|
|
|
|
|
Control.Value = Element.Time.ToString(@"hh\:mm\:ss");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UpdateTextColor()
|
|
|
|
|
{
|
|
|
|
|
var textColor = (Xamarin.Forms.Color)Element.GetValue(TimePicker.TextColorProperty);
|
|
|
|
|
|
|
|
|
|
Control.Style.Color = textColor.ToOouiColor(Xamarin.Forms.Color.Black);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
if (_disposed)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
_disposed = true;
|
|
|
|
|
|
|
|
|
|
if (disposing)
|
|
|
|
|
{
|
|
|
|
|
if (Control != null)
|
|
|
|
|
{
|
|
|
|
|
Control.Change -= OnEnded;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
base.Dispose(disposing);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|