summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Tizen/Renderers/DatePickerRenderer.cs
blob: f7aef30f9ab3dbf9afe9245b9ed4e14fc1ac73aa (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
using System;
using EColor = ElmSharp.Color;

namespace Xamarin.Forms.Platform.Tizen
{
	public class DatePickerRenderer : ViewRenderer<DatePicker, Native.Button>
	{
		//TODO need to add internationalization support
		const string DialogTitle = "Choose Date";

		public DatePickerRenderer()
		{
			RegisterPropertyHandler(DatePicker.DateProperty, UpdateDate);
			RegisterPropertyHandler(DatePicker.FormatProperty, UpdateDate);
			RegisterPropertyHandler(DatePicker.TextColorProperty, UpdateTextColor);
		}

		protected override void OnElementChanged(ElementChangedEventArgs<DatePicker> e)
		{
			if (Control == null)
			{
				var button = new Native.Button(Forms.Context.MainWindow);
				SetNativeControl(button);
			}

			if (e.OldElement != null)
			{
				Control.Clicked -= ButtonClickedHandler;
			}

			if (e.NewElement != null)
			{
				Control.Clicked += ButtonClickedHandler;
			}

			base.OnElementChanged(e);
		}

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

			dialog.InitializeDatePicker(Element.Date, Element.MinimumDate, Element.MaximumDate);
			dialog.DateTimeChanged += DialogDateTimeChangedHandler;
			dialog.Dismissed += DialogDismissedHandler;
			dialog.Show();
		}

		void DialogDateTimeChangedHandler(object sender, Native.DateChangedEventArgs dcea)
		{
			Element.Date = dcea.NewDate;
			Control.Text = dcea.NewDate.ToString(Element.Format);
		}

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

		void UpdateDate()
		{
			Control.Text = Element.Date.ToString(Element.Format);
		}

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

	}
}