summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Android/Cells/TextCellRenderer.cs
blob: b678ee1c15595824e5e1483b0b10522a0435062b (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
using System.ComponentModel;
using Android.Content;
using Android.Views;
using AView = Android.Views.View;
using AColor = Android.Graphics.Color;
using Xamarin.Forms.Internals;

namespace Xamarin.Forms.Platform.Android
{
	public class TextCellRenderer : CellRenderer
	{
		internal TextCellView View { get; private set; }

		protected override AView GetCellCore(Cell item, AView convertView, ViewGroup parent, Context context)
		{
			if ((View = convertView as TextCellView) == null)
				View = new TextCellView(context, item);

			UpdateMainText();
			UpdateDetailText();
			UpdateHeight();
			UpdateIsEnabled();
			View.SetImageVisible(false);

			return View;
		}

		protected override void OnCellPropertyChanged(object sender, PropertyChangedEventArgs args)
		{
			if (args.PropertyName == TextCell.TextProperty.PropertyName || args.PropertyName == TextCell.TextColorProperty.PropertyName)
				UpdateMainText();
			else if (args.PropertyName == TextCell.DetailProperty.PropertyName || args.PropertyName == TextCell.DetailColorProperty.PropertyName)
				UpdateDetailText();
			else if (args.PropertyName == Cell.IsEnabledProperty.PropertyName)
				UpdateIsEnabled();
			else if (args.PropertyName == "RenderHeight")
				UpdateHeight();
		}

		void UpdateDetailText()
		{
			var cell = (TextCell)Cell;
			View.DetailText = cell.Detail;
			View.SetDetailTextColor(cell.DetailColor);
		}

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

		void UpdateIsEnabled()
		{
			var cell = (TextCell)Cell;
			View.SetIsEnabled(cell.IsEnabled);
		}

		void UpdateMainText()
		{
			var cell = (TextCell)Cell;
			View.MainText = cell.Text;

			if (!cell.GetIsGroupHeader<ItemsView<Cell>, Cell>())
				View.SetDefaultMainTextColor(Color.Accent);
			else
				View.SetDefaultMainTextColor(Color.Default);

			View.SetMainTextColor(cell.TextColor);
		}

		// ensure we don't get other people's BaseCellView's
		internal class TextCellView : BaseCellView
		{
			public TextCellView(Context context, Cell cell) : base(context, cell)
			{
			}
		}
	}
}