diff --git a/Ooui.Forms/Exports.cs b/Ooui.Forms/Exports.cs index b093983..f245db7 100644 --- a/Ooui.Forms/Exports.cs +++ b/Ooui.Forms/Exports.cs @@ -2,23 +2,23 @@ using Ooui.Forms; using Ooui.Forms.Renderers; using Xamarin.Forms; -using Xamarin.Forms.Internals; -[assembly: Dependency (typeof (ResourcesProvider))] -[assembly: ExportRenderer (typeof (ActivityIndicator), typeof (ActivityIndicatorRenderer))] -[assembly: ExportRenderer (typeof (BoxView), typeof (BoxRenderer))] -[assembly: ExportRenderer (typeof (Button), typeof (ButtonRenderer))] -[assembly: ExportRenderer (typeof (DatePicker), typeof (DatePickerRenderer))] -[assembly: ExportRenderer (typeof (Editor), typeof (EditorRenderer))] -[assembly: ExportRenderer (typeof (Entry), typeof (EntryRenderer))] -[assembly: ExportRenderer (typeof (Frame), typeof (FrameRenderer))] -[assembly: ExportRenderer (typeof (Image), typeof (ImageRenderer))] -[assembly: ExportRenderer (typeof (Label), typeof (LabelRenderer))] -[assembly: ExportRenderer (typeof (ProgressBar), typeof (ProgressBarRenderer))] -[assembly: ExportRenderer (typeof (Switch), typeof (SwitchRenderer))] -[assembly: ExportImageSourceHandler (typeof (FileImageSource), typeof (FileImageSourceHandler))] -[assembly: ExportImageSourceHandler (typeof (StreamImageSource), typeof (StreamImagesourceHandler))] -[assembly: ExportImageSourceHandler (typeof (UriImageSource), typeof (ImageLoaderSourceHandler))] +[assembly: Dependency(typeof(ResourcesProvider))] +[assembly: ExportRenderer(typeof(ActivityIndicator), typeof(ActivityIndicatorRenderer))] +[assembly: ExportRenderer(typeof(BoxView), typeof(BoxRenderer))] +[assembly: ExportRenderer(typeof(Button), typeof(ButtonRenderer))] +[assembly: ExportRenderer(typeof(DatePicker), typeof(DatePickerRenderer))] +[assembly: ExportRenderer(typeof(Editor), typeof(EditorRenderer))] +[assembly: ExportRenderer(typeof(Entry), typeof(EntryRenderer))] +[assembly: ExportRenderer(typeof(Frame), typeof(FrameRenderer))] +[assembly: ExportRenderer(typeof(Image), typeof(ImageRenderer))] +[assembly: ExportRenderer(typeof(Label), typeof(LabelRenderer))] +[assembly: ExportRenderer(typeof(ProgressBar), typeof(ProgressBarRenderer))] +[assembly: ExportRenderer(typeof(TimePicker), typeof(TimePickerRenderer))] +[assembly: ExportRenderer(typeof(Switch), typeof(SwitchRenderer))] +[assembly: ExportImageSourceHandler(typeof(FileImageSource), typeof(FileImageSourceHandler))] +[assembly: ExportImageSourceHandler(typeof(StreamImageSource), typeof(StreamImagesourceHandler))] +[assembly: ExportImageSourceHandler(typeof(UriImageSource), typeof(ImageLoaderSourceHandler))] namespace Ooui.Forms { diff --git a/Ooui.Forms/Renderers/TimePickerRenderer.cs b/Ooui.Forms/Renderers/TimePickerRenderer.cs new file mode 100644 index 0000000..b282e23 --- /dev/null +++ b/Ooui.Forms/Renderers/TimePickerRenderer.cs @@ -0,0 +1,92 @@ +using System; +using System.ComponentModel; +using Xamarin.Forms; +using Ooui.Forms.Extensions; + +namespace Ooui.Forms.Renderers +{ + public class TimePickerRenderer : ViewRenderer + { + 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 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 == TimePicker.FormatProperty.PropertyName) + UpdateTime(true); + else if (e.PropertyName == TimePicker.TextColorProperty.PropertyName) + UpdateTextColor(); + } + + void OnEnded(object sender, EventArgs eventArgs) + { + TimeSpan.TryParse(Control.Value, out var time); + ElementController?.SetValueFromRenderer(TimePicker.TimeProperty, time); + 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); + } + } +} diff --git a/Samples/Program.cs b/Samples/Program.cs index 6e1152a..d4b4175 100644 --- a/Samples/Program.cs +++ b/Samples/Program.cs @@ -32,6 +32,7 @@ namespace Samples new DisplayAlertSample ().Publish (); new DotMatrixClockSample().Publish(); new EditorSample().Publish(); + new TimePickerSample().Publish(); new TipCalcSample().Publish(); new WeatherAppSample().Publish(); new XuzzleSample().Publish(); diff --git a/Samples/TimePickerSample.cs b/Samples/TimePickerSample.cs new file mode 100644 index 0000000..e4b487b --- /dev/null +++ b/Samples/TimePickerSample.cs @@ -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); + } + } +}