summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Tizen/Cells/CellRenderer.cs
blob: 1cda74c2c463d087c2de607474aed6047d736cab (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
using ElmSharp;
using System.Collections.Generic;
using EColor = ElmSharp.Color;

namespace Xamarin.Forms.Platform.Tizen
{
	public abstract class CellRenderer : IRegisterable
	{
		const string _heightProperty = "Height";
		protected static readonly EColor s_defaultTextColor = EColor.Black;
		protected static readonly EColor s_defaultBackgroundColor = EColor.White;
		readonly Dictionary<Cell, Dictionary<string, EvasObject>> _realizedNativeViews = new Dictionary<Cell, Dictionary<string, EvasObject>>();

		protected CellRenderer(string style)
		{
			Class = new GenItemClass(style)
			{
				GetTextHandler = GetText,
				GetContentHandler = GetContent,
				DeleteHandler = ItemDeleted,
			};
		}

		public GenItemClass Class
		{
			get;
			private set;
		}

		protected virtual bool OnCellPropertyChanged(Cell cell, string property, Dictionary<string, EvasObject> realizedView)
		{
			if (property == _heightProperty)
			{
				return true;
			}
			return false;
		}

		protected virtual Span OnGetText(Cell cell, string part)
		{
			return null;
		}
		protected virtual EvasObject OnGetContent(Cell cell, string part)
		{
			return null;
		}
		protected virtual void OnDeleted(Cell cell)
		{
		}

		protected virtual void OnUnrealizedCell(Cell cell)
		{
		}

		protected int FindCellContentHeight(Cell cell)
		{
			ViewCell viewCell = cell as ViewCell;
			if (viewCell != null)
			{
				var parentWidth = (cell.Parent as VisualElement).Width;
				var view = viewCell.View;
				return (int)view.Measure(parentWidth, double.PositiveInfinity).Request.Height;
			}
			else
				return -1;
		}

		static Native.Span ToNative(Span span)
		{
			var nativeSpan = new Native.Span();
			nativeSpan.Text = span.Text;
			nativeSpan.ForegroundColor = span.ForegroundColor.IsDefault ? s_defaultTextColor : span.ForegroundColor.ToNative();
			nativeSpan.FontAttributes = span.FontAttributes;
			nativeSpan.BackgroundColor = span.BackgroundColor.IsDefault ? s_defaultBackgroundColor : span.BackgroundColor.ToNative();
			nativeSpan.FontSize = span.FontSize;
			nativeSpan.FontFamily = span.FontFamily;
			return nativeSpan;
		}

		internal void SendCellPropertyChanged(Cell cell, GenListItem item, string property)
		{
			Dictionary<string, EvasObject> realizedView = null;
			_realizedNativeViews.TryGetValue(cell, out realizedView);

			// just to prevent null reference exception in OnCellPropertyChanged
			realizedView = realizedView ?? new Dictionary<string, EvasObject>();

			if (property == Cell.IsEnabledProperty.PropertyName)
			{
				item.IsEnabled = cell.IsEnabled;
			}
			// if true was returned, item was updated
			// if it's possible to update the cell property without Update(), return false
			else if (OnCellPropertyChanged(cell, property, realizedView))
			{
				item.Update();
			}
		}

		internal void SendUnrealizedCell(Cell cell)
		{
			_realizedNativeViews.Remove(cell);
			OnUnrealizedCell(cell);
		}

		string GetText(object data, string part)
		{
			var span = OnGetText((data as Native.ListView.ItemContext).Cell, part);
			return span != null ? ToNative(span).GetDecoratedText() : null;
		}

		EvasObject GetContent(object data, string part)
		{
			var cell = (data as Native.ListView.ItemContext).Cell;
			EvasObject nativeView = OnGetContent(cell, part);
			if (part != null && nativeView != null)
			{
				Dictionary<string, EvasObject> realizedView = null;
				_realizedNativeViews.TryGetValue(cell, out realizedView);
				if (realizedView == null)
				{
					realizedView = new Dictionary<string, EvasObject>();
					_realizedNativeViews[cell] = realizedView;
				}
				realizedView[part] = nativeView;

				nativeView.Deleted += (sender, e) =>
				{
					realizedView.Remove(part);
				};
			}
			return nativeView;
		}

		void ItemDeleted(object data)
		{
			var cell = (data as Native.ListView.ItemContext).Cell;
			_realizedNativeViews.Remove(cell);
			OnDeleted(cell);
		}
	}
}