summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Tizen/Cells/ViewCellRenderer.cs
blob: bb17ec82fabb9589ebf65dfeb65fb7007a2992c0 (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
using System.Collections.Generic;
using ElmSharp;

namespace Xamarin.Forms.Platform.Tizen
{
	public class ViewCellRenderer : CellRenderer
	{
		readonly Dictionary<EvasObject, ViewCell> _cacheCandidate = new Dictionary<EvasObject, ViewCell>();
		public ViewCellRenderer() : base("full")
		{
			MainContentPart = "elm.swallow.content";
		}

		protected string MainContentPart { get; set; }

		protected override EvasObject OnReusableContent(Cell cell, string part, EvasObject old)
		{
			if (_cacheCandidate.ContainsKey(old))
			{
				var viewCell = _cacheCandidate[old];
				var widget = (old as Widget);
				if (widget != null)
					widget.IsEnabled = true;
				viewCell.BindingContext = cell.BindingContext;
				return old;
			}
			return null;
		}

		protected override EvasObject OnGetContent(Cell cell, string part)
		{
			if (part == MainContentPart)
			{
				var viewCell = (ViewCell)cell;

				var listView = viewCell?.RealParent as ListView;

				// It is a condition for reusable the cell
				if (listView != null &&
					listView.HasUnevenRows == false &&
					!(listView.ItemTemplate is DataTemplateSelector) && !GetCurrentItem().IsGroupItem)
				{
					return CreateReusableContent(viewCell);
				}

				Platform.GetRenderer(viewCell.View)?.Dispose();
				var renderer = Platform.GetOrCreateRenderer(viewCell.View);
				int height = (int)viewCell.RenderHeight;
				height = height > 0 ? height : FindCellContentHeight(viewCell);

				renderer.NativeView.MinimumHeight = height;
				return renderer.NativeView;
			}
			return null;
		}

		protected override bool OnCellPropertyChanged(Cell cell, string property, Dictionary<string, EvasObject> realizedView)
		{
			if (property == "View")
			{
				return true;
			}
			return base.OnCellPropertyChanged(cell, property, realizedView);
		}

		EvasObject CreateReusableContent(ViewCell viewCell)
		{
			var listView = viewCell.RealParent as ListView;
			ViewCell duplicatedCell = (ViewCell)listView.ItemTemplate.CreateContent();
			duplicatedCell.BindingContext = viewCell.BindingContext;
			duplicatedCell.Parent = listView;

			var renderer = Platform.GetOrCreateRenderer(duplicatedCell.View);
			int height = (int)duplicatedCell.RenderHeight;
			height = height > 0 ? height : FindCellContentHeight(duplicatedCell);
			renderer.NativeView.MinimumHeight = height;

			_cacheCandidate[renderer.NativeView] = duplicatedCell;
			renderer.NativeView.Deleted += (sender, e) =>
			{
				_cacheCandidate.Remove((EvasObject)sender);
			};

			return renderer.NativeView;
		}
	}
}