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

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

		Native.ListView.ItemContext _currentItem;

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

		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 virtual EvasObject OnReusableContent(Cell cell, string part, EvasObject old)
		{
			return null;
		}

		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.ToNative();
			nativeSpan.FontAttributes = span.FontAttributes;
			nativeSpan.BackgroundColor = 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)
		{
			Dictionary<string, EvasObject> realizedView = null;
			_realizedNativeViews.TryGetValue(cell, out realizedView);
			realizedView?.Clear();
			OnUnrealizedCell(cell);
		}

		internal Native.ListView.ItemContext GetCurrentItem()
		{
			return _currentItem;
		}

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

		EvasObject GetContent(object data, string part)
		{
			_currentItem = data as Native.ListView.ItemContext;
			var cell = _currentItem.Cell;
			EvasObject nativeView = OnGetContent(cell, part);
			UpdateRealizedView(cell, part, nativeView);
			return nativeView;
		}

		EvasObject ReusableContent(object data, string part, EvasObject old)
		{
			_currentItem = data as Native.ListView.ItemContext;
			var cell = _currentItem.Cell;
			EvasObject nativeView = OnReusableContent(cell, part, old);
			UpdateRealizedView(cell, part, nativeView);
			return nativeView;
		}

		void UpdateRealizedView(Cell cell, string part, EvasObject nativeView)
		{
			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;
			}
		}

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