summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.iOS/Renderers/TableViewModelRenderer.cs
blob: 060b9d2051e91e1366b5b9b59fa1d33b4dc98f70 (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
using System;
using System.Collections.Generic;
using Foundation;
using UIKit;

namespace Xamarin.Forms.Platform.iOS
{
	public class TableViewModelRenderer : UITableViewSource
	{
		readonly Dictionary<nint, Cell> _headerCells = new Dictionary<nint, Cell>();

		protected ITableViewController Controller => View;

		protected bool HasBoundGestures;
		protected UITableView Table;

		protected TableView View;

		public TableViewModelRenderer(TableView model)
		{
			View = model;
			Controller.ModelChanged += (s, e) =>
			{
				if (Table != null)
					Table.ReloadData();
			};
			AutomaticallyDeselect = true;
		}

		public bool AutomaticallyDeselect { get; set; }

		public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
		{
			var cell = Controller.Model.GetCell(indexPath.Section, indexPath.Row);

			var nativeCell = CellTableViewCell.GetNativeCell(tableView, cell);
			return nativeCell;
		}

		public override nfloat GetHeightForHeader(UITableView tableView, nint section)
		{
			if (!_headerCells.ContainsKey((int)section))
				_headerCells[section] = Controller.Model.GetHeaderCell((int)section);

			var result = _headerCells[section];

			return result == null ? UITableView.AutomaticDimension : (nfloat)result.Height;
		}

		public override UIView GetViewForHeader(UITableView tableView, nint section)
		{
			if (!_headerCells.ContainsKey((int)section))
				_headerCells[section] = Controller.Model.GetHeaderCell((int)section);

			var result = _headerCells[section];

			if (result != null)
			{
				var reusable = tableView.DequeueReusableCell(result.GetType().FullName);

				var cellRenderer = Internals.Registrar.Registered.GetHandler<CellRenderer>(result.GetType());
				return cellRenderer.GetCell(result, reusable, Table);
			}
			return null;
		}

		public void LongPress(UILongPressGestureRecognizer gesture)
		{
			var point = gesture.LocationInView(Table);
			var indexPath = Table.IndexPathForRowAtPoint(point);
			if (indexPath == null)
				return;

			Controller.Model.RowLongPressed(indexPath.Section, indexPath.Row);
		}

		public override nint NumberOfSections(UITableView tableView)
		{
			BindGestures(tableView);
			return Controller.Model.GetSectionCount();
		}

		public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
		{
			Controller.Model.RowSelected(indexPath.Section, indexPath.Row);
			if (AutomaticallyDeselect)
				tableView.DeselectRow(indexPath, true);
		}

		public override nint RowsInSection(UITableView tableview, nint section)
		{
			return Controller.Model.GetRowCount((int)section);
		}

		public override string[] SectionIndexTitles(UITableView tableView)
		{
			return Controller.Model.GetSectionIndexTitles();
		}

		public override string TitleForHeader(UITableView tableView, nint section)
		{
			return Controller.Model.GetSectionTitle((int)section);
		}

		void BindGestures(UITableView tableview)
		{
			if (HasBoundGestures)
				return;

			HasBoundGestures = true;

			var gesture = new UILongPressGestureRecognizer(LongPress);
			gesture.MinimumPressDuration = 2;
			tableview.AddGestureRecognizer(gesture);

			var dismissGesture = new UITapGestureRecognizer(Tap);
			dismissGesture.CancelsTouchesInView = false;
			tableview.AddGestureRecognizer(dismissGesture);

			Table = tableview;
		}

		void Tap(UITapGestureRecognizer gesture)
		{
			gesture.View.EndEditing(true);
		}
	}

	public class UnEvenTableViewModelRenderer : TableViewModelRenderer
	{
		public UnEvenTableViewModelRenderer(TableView model) : base(model)
		{
		}

		public override nfloat GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
		{
			var cell = View.Model.GetCell(indexPath.Section, indexPath.Row);
			var h = cell.Height;

			if (View.RowHeight == -1 && h == -1 && cell is ViewCell) {
				return UITableView.AutomaticDimension;
			} else if (h == -1)
				return tableView.RowHeight;
			return (nfloat)h;
		}
	}
}