summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Tizen/Renderers/PickerRenderer.cs
blob: 65f95ff3a8a2c0d5e1b2fec5ac75be9a161716ba (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
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>
	{
		internal List _list;
		internal Native.Dialog _dialog;
		Dictionary<ListItem, int> _itemToItemNumber = new Dictionary<ListItem, int>();

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

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

			UpdateSelectedIndex();
			UpdateTextColor();
			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.ToNative();
		}

		void OnClicked(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 += OnDialogDismissed;
			_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 += OnItemSelected;
			_dialog.Content = _list;

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

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

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

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