summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Android/Renderers/PickerRenderer.cs
blob: b9114d223ad8bff2a1727495a46dcafeb6f8bd3e (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System;
using System.ComponentModel;
using System.Linq;
using Android.App;
using Android.Views;
using Android.Widget;
using ADatePicker = Android.Widget.DatePicker;
using ATimePicker = Android.Widget.TimePicker;
using Object = Java.Lang.Object;

namespace Xamarin.Forms.Platform.Android
{
	public class PickerRenderer : ViewRenderer<Picker, EditText>
	{
		AlertDialog _dialog;

		bool _isDisposed;

		public PickerRenderer()
		{
			AutoPackage = false;
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing && !_isDisposed)
			{
				_isDisposed = true;
				((ObservableList<string>)Element.Items).CollectionChanged -= RowsCollectionChanged;
			}

			base.Dispose(disposing);
		}

		protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
		{
			if (e.OldElement != null)
				((ObservableList<string>)e.OldElement.Items).CollectionChanged -= RowsCollectionChanged;

			if (e.NewElement != null)
			{
				((ObservableList<string>)e.NewElement.Items).CollectionChanged += RowsCollectionChanged;
				if (Control == null)
				{
					var textField = new EditText(Context) { Focusable = false, Clickable = true, Tag = this };
					textField.SetOnClickListener(PickerListener.Instance);
					SetNativeControl(textField);
				}
				UpdatePicker();
			}

			base.OnElementChanged(e);
		}

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

			if (e.PropertyName == Picker.TitleProperty.PropertyName)
				UpdatePicker();
			if (e.PropertyName == Picker.SelectedIndexProperty.PropertyName)
				UpdatePicker();
		}

		internal override void OnFocusChangeRequested(object sender, VisualElement.FocusRequestArgs e)
		{
			base.OnFocusChangeRequested(sender, e);

			if (e.Focus)
				OnClick();
			else if (_dialog != null)
			{
				_dialog.Hide();
				((IElementController)Element).SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, false);
				Control.ClearFocus();
				_dialog = null;
			}
		}

		void OnClick()
		{
			Picker model = Element;

			var picker = new NumberPicker(Context);
			if (model.Items != null && model.Items.Any())
			{
				picker.MaxValue = model.Items.Count - 1;
				picker.MinValue = 0;
				picker.SetDisplayedValues(model.Items.ToArray());
				picker.WrapSelectorWheel = false;
				picker.DescendantFocusability = DescendantFocusability.BlockDescendants;
				picker.Value = model.SelectedIndex;
			}

			var layout = new LinearLayout(Context) { Orientation = Orientation.Vertical };
			layout.AddView(picker);

			((IElementController)Element).SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, true);

			var builder = new AlertDialog.Builder(Context);
			builder.SetView(layout);
			builder.SetTitle(model.Title ?? "");
			builder.SetNegativeButton(global::Android.Resource.String.Cancel, (s, a) =>
			{
				((IElementController)Element).SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, false);
				// It is possible for the Content of the Page to be changed when Focus is changed.
				// In this case, we'll lose our Control.
				Control?.ClearFocus();
				_dialog = null;
			});
			builder.SetPositiveButton(global::Android.Resource.String.Ok, (s, a) =>
			{
				((IElementController)Element).SetValueFromRenderer(Picker.SelectedIndexProperty, picker.Value);
				// It is possible for the Content of the Page to be changed on SelectedIndexChanged. 
				// In this case, the Element & Control will no longer exist.
				if (Element != null)
				{
					if (model.Items.Count > 0 && Element.SelectedIndex >= 0)
						Control.Text = model.Items[Element.SelectedIndex];
					((IElementController)Element).SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, false);
					// It is also possible for the Content of the Page to be changed when Focus is changed.
					// In this case, we'll lose our Control.
					Control?.ClearFocus();
				}
				_dialog = null;
			});

			(_dialog = builder.Create()).Show();
		}

		void RowsCollectionChanged(object sender, EventArgs e)
		{
			UpdatePicker();
		}

		void UpdatePicker()
		{
			Control.Hint = Element.Title;

			string oldText = Control.Text;

			if (Element.SelectedIndex == -1 || Element.Items == null)
				Control.Text = null;
			else
				Control.Text = Element.Items[Element.SelectedIndex];

			if (oldText != Control.Text)
				((IVisualElementController)Element).NativeSizeChanged();
		}

		class PickerListener : Object, IOnClickListener
		{
			public static readonly PickerListener Instance = new PickerListener();

			public void OnClick(global::Android.Views.View v)
			{
				var renderer = v.Tag as PickerRenderer;
				if (renderer == null)
					return;

				renderer.OnClick();
			}
		}
	}
}