Added TimePickerRenderer (and sample)

This commit is contained in:
Javier Suárez Ruiz 2017-12-16 13:06:34 +01:00
parent 913247aedb
commit 9ddfc8f93d
4 changed files with 148 additions and 16 deletions

View File

@ -2,23 +2,23 @@
using Ooui.Forms; using Ooui.Forms;
using Ooui.Forms.Renderers; using Ooui.Forms.Renderers;
using Xamarin.Forms; using Xamarin.Forms;
using Xamarin.Forms.Internals;
[assembly: Dependency (typeof (ResourcesProvider))] [assembly: Dependency(typeof(ResourcesProvider))]
[assembly: ExportRenderer (typeof (ActivityIndicator), typeof (ActivityIndicatorRenderer))] [assembly: ExportRenderer(typeof(ActivityIndicator), typeof(ActivityIndicatorRenderer))]
[assembly: ExportRenderer (typeof (BoxView), typeof (BoxRenderer))] [assembly: ExportRenderer(typeof(BoxView), typeof(BoxRenderer))]
[assembly: ExportRenderer (typeof (Button), typeof (ButtonRenderer))] [assembly: ExportRenderer(typeof(Button), typeof(ButtonRenderer))]
[assembly: ExportRenderer (typeof (DatePicker), typeof (DatePickerRenderer))] [assembly: ExportRenderer(typeof(DatePicker), typeof(DatePickerRenderer))]
[assembly: ExportRenderer (typeof (Editor), typeof (EditorRenderer))] [assembly: ExportRenderer(typeof(Editor), typeof(EditorRenderer))]
[assembly: ExportRenderer (typeof (Entry), typeof (EntryRenderer))] [assembly: ExportRenderer(typeof(Entry), typeof(EntryRenderer))]
[assembly: ExportRenderer (typeof (Frame), typeof (FrameRenderer))] [assembly: ExportRenderer(typeof(Frame), typeof(FrameRenderer))]
[assembly: ExportRenderer (typeof (Image), typeof (ImageRenderer))] [assembly: ExportRenderer(typeof(Image), typeof(ImageRenderer))]
[assembly: ExportRenderer (typeof (Label), typeof (LabelRenderer))] [assembly: ExportRenderer(typeof(Label), typeof(LabelRenderer))]
[assembly: ExportRenderer (typeof (ProgressBar), typeof (ProgressBarRenderer))] [assembly: ExportRenderer(typeof(ProgressBar), typeof(ProgressBarRenderer))]
[assembly: ExportRenderer (typeof (Switch), typeof (SwitchRenderer))] [assembly: ExportRenderer(typeof(TimePicker), typeof(TimePickerRenderer))]
[assembly: ExportImageSourceHandler (typeof (FileImageSource), typeof (FileImageSourceHandler))] [assembly: ExportRenderer(typeof(Switch), typeof(SwitchRenderer))]
[assembly: ExportImageSourceHandler (typeof (StreamImageSource), typeof (StreamImagesourceHandler))] [assembly: ExportImageSourceHandler(typeof(FileImageSource), typeof(FileImageSourceHandler))]
[assembly: ExportImageSourceHandler (typeof (UriImageSource), typeof (ImageLoaderSourceHandler))] [assembly: ExportImageSourceHandler(typeof(StreamImageSource), typeof(StreamImagesourceHandler))]
[assembly: ExportImageSourceHandler(typeof(UriImageSource), typeof(ImageLoaderSourceHandler))]
namespace Ooui.Forms namespace Ooui.Forms
{ {

View File

@ -0,0 +1,92 @@
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)
{
var size = "00:00:00".MeasureSize(string.Empty, 16.0, FontAttributes.None);
size = new Size(size.Width, size.Height * 1.428 + 14);
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);
if (e.PropertyName == TimePicker.TimeProperty.PropertyName || e.PropertyName == DatePicker.FormatProperty.PropertyName)
UpdateTime(true);
else if (e.PropertyName == TimePicker.TextColorProperty.PropertyName || e.PropertyName == DatePicker.FormatProperty.PropertyName)
UpdateTextColor();
}
void OnEnded(object sender, EventArgs eventArgs)
{
DateTime.TryParseExact(Control.Value, @"hh\:mm\:ss", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AssumeUniversal, out var date);
ElementController?.SetValueFromRenderer(TimePicker.TimeProperty, date);
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);
}
}
}

View File

@ -32,6 +32,7 @@ namespace Samples
new DisplayAlertSample ().Publish (); new DisplayAlertSample ().Publish ();
new DotMatrixClockSample().Publish(); new DotMatrixClockSample().Publish();
new EditorSample().Publish(); new EditorSample().Publish();
new TimePickerSample().Publish();
new TipCalcSample().Publish(); new TipCalcSample().Publish();
new WeatherAppSample().Publish(); new WeatherAppSample().Publish();
new XuzzleSample().Publish(); new XuzzleSample().Publish();

View File

@ -0,0 +1,39 @@
using Ooui;
using Xamarin.Forms;
namespace Samples
{
public class TimePickerSample : ISample
{
public string Title => "Xamarin.Forms TimePicker Sample";
public Ooui.Element CreateElement()
{
var panel = new StackLayout();
var titleLabel = new Xamarin.Forms.Label
{
Text = "TimePicker",
FontSize = 24,
FontAttributes = FontAttributes.Bold
};
panel.Children.Add(titleLabel);
var timePicker = new TimePicker();
panel.Children.Add(timePicker);
var page = new ContentPage
{
Content = panel
};
return page.GetOouiElement();
}
public void Publish()
{
UI.Publish("/timepicker", CreateElement);
}
}
}