summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Tizen/Renderers/TimePickerRenderer.cs
blob: 0e46ae98e456747578688f073a9378d3f5508452 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System;
using System.Globalization;

namespace Xamarin.Forms.Platform.Tizen
{
	public class TimePickerRenderer : ViewRenderer<TimePicker, Native.EditfieldEntry>
	{
		//TODO need to add internationalization support
		const string DialogTitle = "Choose Time";

		static readonly string s_defaultFormat = CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern;

		TimeSpan _time = DateTime.Now.TimeOfDay;

		static TimePickerRenderer()
		{
			RegisterPropertyHandler(TimePicker.FormatProperty, (r) => (r as TimePickerRenderer).UpdateFormat());
			RegisterPropertyHandler(TimePicker.TimeProperty, (r) => (r as TimePickerRenderer).UpdateTime());
			RegisterPropertyHandler(TimePicker.TextColorProperty, (r) => (r as TimePickerRenderer).UpdateTextColor());
		}

		protected override void OnElementChanged(ElementChangedEventArgs<TimePicker> e)
		{
			if (Control == null)
			{
				var entry = new Native.EditfieldEntry(Forms.Context.MainWindow)
				{
					IsSingleLine = true,
					HorizontalTextAlignment = Native.TextAlignment.Center,
				};
				entry.SetVerticalTextAlignment("elm.text", 0.5);
				entry.AllowFocus(false);
				SetNativeControl(entry);
				Control.Clicked += OnClicked;
			}
			base.OnElementChanged(e);
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				if (Control != null)
				{
					Control.Clicked -= OnClicked;
				}
			}

			base.Dispose(disposing);
		}

		protected override Size MinimumSize()
		{
			return Control.Measure(Control.MinimumWidth, Control.MinimumHeight).ToDP();
		}

		void OnClicked(object o, EventArgs e)
		{
			Native.DateTimePickerDialog dialog = new Native.DateTimePickerDialog(Forms.Context.MainWindow)
			{
				Title = DialogTitle
			};

			dialog.InitializeTimePicker(_time, null);
			dialog.DateTimeChanged += OnDialogTimeChanged;
			dialog.Dismissed += OnDialogDismissed;
			dialog.Show();
		}

		void OnDialogTimeChanged(object sender, Native.DateChangedEventArgs dcea)
		{
			Element.Time = dcea.NewDate.TimeOfDay;
			UpdateTime();
		}

		void OnDialogDismissed(object sender, EventArgs e)
		{
			var dialog = sender as Native.DateTimePickerDialog;
			dialog.DateTimeChanged -= OnDialogTimeChanged;
			dialog.Dismissed -= OnDialogDismissed;
		}

		void UpdateFormat()
		{
			UpdateTimeAndFormat();
		}

		void UpdateTextColor()
		{
			Control.TextColor = Element.TextColor.ToNative();
		}

		void UpdateTime()
		{
			_time = Element.Time;
			UpdateTimeAndFormat();
		}

		void UpdateTimeAndFormat()
		{
			// Xamarin using DateTime formatting (https://developer.xamarin.com/api/property/Xamarin.Forms.TimePicker.Format/)
			Control.Text = new DateTime(_time.Ticks).ToString(Element.Format ?? s_defaultFormat);
		}
	}
}