summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.iOS/Cells/EntryCellRenderer.cs
blob: b00ff9dd8f5fa78cd6b6fa63d023a38e58661ed4 (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
166
167
168
169
170
171
172
using System;
using System.ComponentModel;
using UIKit;
using RectangleF = CoreGraphics.CGRect;

namespace Xamarin.Forms.Platform.iOS
{
	public class EntryCellRenderer : CellRenderer
	{
		static readonly Color DefaultTextColor = Color.Black;

		public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
		{
			var entryCell = (EntryCell)item;

			var tvc = reusableCell as EntryCellTableViewCell;
			if (tvc == null)
				tvc = new EntryCellTableViewCell(item.GetType().FullName);
			else
			{
				tvc.Cell.PropertyChanged -= OnCellPropertyChanged;
				tvc.TextFieldTextChanged -= OnTextFieldTextChanged;
				tvc.KeyboardDoneButtonPressed -= OnKeyBoardDoneButtonPressed;
			}

			SetRealCell(item, tvc);

			tvc.Cell = item;
			tvc.Cell.PropertyChanged += OnCellPropertyChanged;
			tvc.TextFieldTextChanged += OnTextFieldTextChanged;
			tvc.KeyboardDoneButtonPressed += OnKeyBoardDoneButtonPressed;

			WireUpForceUpdateSizeRequested(item, tvc, tv);

			UpdateBackground(tvc, entryCell);
			UpdateLabel(tvc, entryCell);
			UpdateText(tvc, entryCell);
			UpdateKeyboard(tvc, entryCell);
			UpdatePlaceholder(tvc, entryCell);
			UpdateLabelColor(tvc, entryCell);
			UpdateHorizontalTextAlignment(tvc, entryCell);
			UpdateIsEnabled(tvc, entryCell);

			return tvc;
		}

		static void OnCellPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			var entryCell = (EntryCell)sender;
			var realCell = (EntryCellTableViewCell)GetRealCell(entryCell);

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

		static void OnKeyBoardDoneButtonPressed(object sender, EventArgs e)
		{
			var cell = (EntryCellTableViewCell)sender;
			var model = (IEntryCellController)cell.Cell;

			model.SendCompleted();
		}

		static void OnTextFieldTextChanged(object sender, EventArgs eventArgs)
		{
			var cell = (EntryCellTableViewCell)sender;
			var model = (EntryCell)cell.Cell;

			model.Text = cell.TextField.Text;
		}

		static void UpdateHorizontalTextAlignment(EntryCellTableViewCell cell, EntryCell entryCell)
		{
			cell.TextField.TextAlignment = entryCell.HorizontalTextAlignment.ToNativeTextAlignment();
		}

		static void UpdateIsEnabled(EntryCellTableViewCell cell, EntryCell entryCell)
		{
			cell.UserInteractionEnabled = entryCell.IsEnabled;
			cell.TextLabel.Enabled = entryCell.IsEnabled;
			cell.DetailTextLabel.Enabled = entryCell.IsEnabled;
			cell.TextField.Enabled = entryCell.IsEnabled;
		}

		static void UpdateKeyboard(EntryCellTableViewCell cell, EntryCell entryCell)
		{
			cell.TextField.ApplyKeyboard(entryCell.Keyboard);
		}

		static void UpdateLabel(EntryCellTableViewCell cell, EntryCell entryCell)
		{
			cell.TextLabel.Text = entryCell.Label;
		}

		static void UpdateLabelColor(EntryCellTableViewCell cell, EntryCell entryCell)
		{
			cell.TextLabel.TextColor = entryCell.LabelColor.ToUIColor(DefaultTextColor);
		}

		static void UpdatePlaceholder(EntryCellTableViewCell cell, EntryCell entryCell)
		{
			cell.TextField.Placeholder = entryCell.Placeholder;
		}

		static void UpdateText(EntryCellTableViewCell cell, EntryCell entryCell)
		{
			if (cell.TextField.Text == entryCell.Text)
				return;
			// double sets side effect on iOS, YAY
			cell.TextField.Text = entryCell.Text;
		}

		class EntryCellTableViewCell : CellTableViewCell
		{
			public EntryCellTableViewCell(string cellName) : base(UITableViewCellStyle.Value1, cellName)
			{
				TextField = new UITextField(new RectangleF(0, 0, 100, 30)) { BorderStyle = UITextBorderStyle.None };

				TextField.EditingChanged += TextFieldOnEditingChanged;
				TextField.ShouldReturn = OnShouldReturn;

				ContentView.AddSubview(TextField);
			}

			public UITextField TextField { get; }

			public event EventHandler KeyboardDoneButtonPressed;

			public override void LayoutSubviews()
			{
				base.LayoutSubviews();

				// simple algorithm to generally line up entries
				var start = (nfloat)Math.Round(Math.Max(Frame.Width * 0.3, TextLabel.Frame.Right + 10));
				TextField.Frame = new RectangleF(start, (Frame.Height - 30) / 2, Frame.Width - TextLabel.Frame.Left - start, 30);
				// Centers TextField Content  (iOS6)
				TextField.VerticalAlignment = UIControlContentVerticalAlignment.Center;
			}

			public event EventHandler TextFieldTextChanged;

			bool OnShouldReturn(UITextField view)
			{
				var handler = KeyboardDoneButtonPressed;
				if (handler != null)
					handler(this, EventArgs.Empty);

				TextField.ResignFirstResponder();
				return true;
			}

			void TextFieldOnEditingChanged(object sender, EventArgs eventArgs)
			{
				var handler = TextFieldTextChanged;
				if (handler != null)
					handler(this, EventArgs.Empty);
			}
		}
	}
}