summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Android/Cells/EntryCellRenderer.cs
blob: e46422a0d0906953fcbd8614b90b7458b5cfc00f (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
using System.ComponentModel;
using Android.Content;
using Android.Views;

namespace Xamarin.Forms.Platform.Android
{
	public class EntryCellRenderer : CellRenderer
	{
		EntryCellView _view;

		protected override global::Android.Views.View GetCellCore(Cell item, global::Android.Views.View convertView, ViewGroup parent, Context context)
		{
			if ((_view = convertView as EntryCellView) == null)
				_view = new EntryCellView(context, item);
			else
			{
				_view.TextChanged = null;
				_view.FocusChanged = null;
				_view.EditingCompleted = null;
			}

			UpdateLabel();
			UpdateLabelColor();
			UpdatePlaceholder();
			UpdateKeyboard();
			UpdateHorizontalTextAlignment();
			UpdateText();
			UpdateIsEnabled();
			UpdateHeight();

			_view.TextChanged = OnTextChanged;
			_view.EditingCompleted = OnEditingCompleted;

			return _view;
		}

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

			if (e.PropertyName == EntryCell.LabelProperty.PropertyName)
				UpdateLabel();
			else if (e.PropertyName == EntryCell.TextProperty.PropertyName)
				UpdateText();
			else if (e.PropertyName == EntryCell.PlaceholderProperty.PropertyName)
				UpdatePlaceholder();
			else if (e.PropertyName == EntryCell.KeyboardProperty.PropertyName)
				UpdateKeyboard();
			else if (e.PropertyName == EntryCell.LabelColorProperty.PropertyName)
				UpdateLabelColor();
			else if (e.PropertyName == EntryCell.HorizontalTextAlignmentProperty.PropertyName)
				UpdateHorizontalTextAlignment();
			else if (e.PropertyName == Cell.IsEnabledProperty.PropertyName)
				UpdateIsEnabled();
			else if (e.PropertyName == "RenderHeight")
				UpdateHeight();
		}

		void OnEditingCompleted()
		{
			var entryCell = (IEntryCellController)Cell;
			entryCell.SendCompleted();
		}

		void OnTextChanged(string text)
		{
			var entryCell = (EntryCell)Cell;
			entryCell.Text = text;
		}

		void UpdateHeight()
		{
			_view.SetRenderHeight(Cell.RenderHeight);
		}

		void UpdateHorizontalTextAlignment()
		{
			var entryCell = (EntryCell)Cell;
			_view.EditText.Gravity = entryCell.HorizontalTextAlignment.ToHorizontalGravityFlags();
		}

		void UpdateIsEnabled()
		{
			var entryCell = (EntryCell)Cell;
			_view.EditText.Enabled = entryCell.IsEnabled;
		}

		void UpdateKeyboard()
		{
			var entryCell = (EntryCell)Cell;
			_view.EditText.InputType = entryCell.Keyboard.ToInputType();
		}

		void UpdateLabel()
		{
			_view.LabelText = ((EntryCell)Cell).Label;
		}

		void UpdateLabelColor()
		{
			_view.SetLabelTextColor(((EntryCell)Cell).LabelColor, global::Android.Resource.Color.PrimaryTextDark);
		}

		void UpdatePlaceholder()
		{
			var entryCell = (EntryCell)Cell;
			_view.EditText.Hint = entryCell.Placeholder;
		}

		void UpdateText()
		{
			var entryCell = (EntryCell)Cell;
			if (_view.EditText.Text == entryCell.Text)
				return;

			_view.EditText.Text = entryCell.Text;
		}
	}
}