summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Tizen/Renderers/PickerRenderer.cs
blob: 6080d647df9a58a91a77dcb8cf3260c2f577414b (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using System.ComponentModel;
using System.Collections.Generic;
using ElmSharp;
using EColor = ElmSharp.Color;

namespace Xamarin.Forms.Platform.Tizen
{
	public class PickerRenderer : ViewRenderer<Picker, Native.Button>
	{
		static readonly EColor s_defaultTextColor = EColor.White;
		internal List _list;
		internal Native.Dialog _dialog;
		Dictionary<ListItem, int> _itemToItemNumber = new Dictionary<ListItem, int>();

		public PickerRenderer()
		{
		}

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

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

			if (e.NewElement != null)
			{
				UpdateSelectedIndex();
				UpdateTextColor();
				Control.Clicked += OnClick;
			}

			base.OnElementChanged(e);
		}

		protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			base.OnElementPropertyChanged(sender, e);

			if (e.PropertyName == Picker.SelectedIndexProperty.PropertyName)
			{
				UpdateSelectedIndex();
			}
			else if (e.PropertyName == Picker.TextColorProperty.PropertyName)
			{
				UpdateTextColor();
			}
		}

		void UpdateSelectedIndex()
		{
			Control.Text = (Element.SelectedIndex == -1 || Element.Items == null ?
				"" : Element.Items[Element.SelectedIndex]);
		}

		void UpdateTextColor()
		{
			Control.TextColor = Element.TextColor.IsDefault ? s_defaultTextColor : Element.TextColor.ToNative();
		}

		void OnClick(object sender, EventArgs e)
		{
			int i = 0;
			_dialog = new Native.Dialog(Forms.Context.MainWindow);
			_list = new List(_dialog);
			_dialog.AlignmentX = -1;
			_dialog.AlignmentY = -1;

			_dialog.Title = Element.Title;
			_dialog.Dismissed += DialogDismissed;
			_dialog.BackButtonPressed += (object senders, EventArgs es) =>
			{
				_dialog.Dismiss();
			};

			foreach (var s in Element.Items)
			{
				ListItem item = _list.Append(s);
				_itemToItemNumber[item] = i;
				i++;
			}
			_list.ItemSelected += ItemSelected;
			_dialog.Content = _list;

			_dialog.Show();
			_list.Show();
		}

		void ItemSelected(object senderObject, EventArgs ev)
		{
			Element.SelectedIndex = _itemToItemNumber[(senderObject as List).SelectedItem];
			_dialog.Dismiss();
		}

		void DialogDismissed(object sender, EventArgs e)
		{
			CleanView();
		}

		void CleanView()
		{
			if (null != _list)
			{
				_list.Unrealize();
				_itemToItemNumber.Clear();
				_list = null;
			}
			if (null != _dialog)
			{
				_dialog.Unrealize();
				_dialog = null;
			}
		}
	}
}