summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.MacOS/Renderers/ListViewDataSource.cs
blob: 6742053c9956145bb0ddaee35184918ef321e821 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
´╗┐using System;
using System.Collections;
using System.Collections.Generic;
using AppKit;
using Foundation;

namespace Xamarin.Forms.Platform.MacOS
{
	internal class ListViewDataSource : NSTableViewSource
	{
		IVisualElementRenderer _prototype;
		const int DefaultItemTemplateId = 1;
		static int s_dataTemplateIncrementer = 2; // lets start at not 0 because
		static int s_sectionCount;
		readonly nfloat _defaultSectionHeight;
		readonly Dictionary<DataTemplate, int> _templateToId = new Dictionary<DataTemplate, int>();
		readonly NSTableView _nsTableView;
		protected readonly ListView List;

		IListViewController Controller => List;

		ITemplatedItemsView<Cell> TemplatedItemsView => List;

		bool _selectionFromNative;

		public virtual bool IsGroupingEnabled => List.IsGroupingEnabled;

		public Dictionary<int, int> Counts { get; set; }

		public ListViewDataSource(ListViewDataSource source)
		{
			List = source.List;
			_nsTableView = source._nsTableView;
			_defaultSectionHeight = source._defaultSectionHeight;
			_selectionFromNative = source._selectionFromNative;
			Counts = new Dictionary<int, int>();
		}

		public ListViewDataSource(ListView list, NSTableView tableView)
		{
			List = list;
			List.ItemSelected += OnItemSelected;
			_nsTableView = tableView;
			Counts = new Dictionary<int, int>();
		}

		public void Update()
		{
			_nsTableView.ReloadData();
		}

		public void OnRowClicked()
		{
			var selectedRow = _nsTableView.SelectedRow;
			if (selectedRow == -1)
				return;

			Cell cell = null;
			NSIndexPath indexPath = GetPathFromRow(selectedRow, ref cell);

			if (cell == null)
				return;

			_selectionFromNative = true;
			Controller.NotifyRowTapped((int)indexPath.Section, (int)indexPath.Item, cell);
		}


		public void OnItemSelected(object sender, SelectedItemChangedEventArgs eventArg)
		{
			if (_selectionFromNative)
			{
				_selectionFromNative = false;
				return;
			}

			var location = TemplatedItemsView.TemplatedItems.GetGroupAndIndexOfItem(eventArg.SelectedItem);
			if (location.Item1 == -1 || location.Item2 == -1)
			{
				var row = _nsTableView.SelectedRow;
				int groupIndex = 1;
				var selectedIndexPath = NSIndexPath.FromItemSection(row, groupIndex);
				if (selectedIndexPath != null)
					_nsTableView.DeselectRow(selectedIndexPath.Item);
				return;
			}

			var rowId = location.Item2;

			_nsTableView.SelectRow(rowId, false);
		}

		public override bool IsGroupRow(NSTableView tableView, nint row)
		{
			if (!IsGroupingEnabled)
				return false;

			int sectionIndex;
			bool isGroupHeader;
			int itemIndexInSection;

			GetComputedIndexes(row, out sectionIndex, out itemIndexInSection, out isGroupHeader);
			return isGroupHeader;
		}

		public override bool ShouldSelectRow(NSTableView tableView, nint row)
		{
			return !IsGroupRow(tableView, row);
		}

		public override nfloat GetRowHeight(NSTableView tableView, nint row)
		{
			if (!List.HasUnevenRows)
				return List.RowHeight == -1 ? ListViewRenderer.DefaultRowHeight : List.RowHeight;

			Cell cell = null;
			GetPathFromRow(row, ref cell);

			return CalculateHeightForCell(tableView, cell);
		}

		public override nint GetRowCount(NSTableView tableView)
		{
			var templatedItems = TemplatedItemsView.TemplatedItems;
			nint count = 0;

			if (!IsGroupingEnabled)
			{
				count = templatedItems.Count;
			}
			else
			{
				var sections = templatedItems.Count;
				for (int i = 0; i < sections; i++)
				{
					var group = (IList)((IList)templatedItems)[i];
					count += group.Count + 1;
				}
				s_sectionCount = sections;
			}
			return count;
		}

		public override NSView GetViewForItem(NSTableView tableView, NSTableColumn tableColumn, nint row)
		{
			var sectionIndex = 0;
			var itemIndexInSection = (int)row;
			Cell cell;

			var isHeader = false;

			if (IsGroupingEnabled)
				GetComputedIndexes(row, out sectionIndex, out itemIndexInSection, out isHeader);

			var indexPath = NSIndexPath.FromItemSection(itemIndexInSection, sectionIndex);
			var templateId = isHeader ? "headerCell" : TemplateIdForPath(indexPath).ToString();

			NSView nativeCell;

			var cachingStrategy = Controller.CachingStrategy;
			if (cachingStrategy == ListViewCachingStrategy.RetainElement)
			{
				cell = GetCellForPath(indexPath, isHeader);
				nativeCell = CellNSView.GetNativeCell(tableView, cell, templateId, isHeader);
			}
			else if (cachingStrategy == ListViewCachingStrategy.RecycleElement)
			{
				nativeCell = tableView.MakeView(templateId, tableView);
				if (nativeCell == null)
				{
					cell = GetCellForPath(indexPath, isHeader);
					nativeCell = CellNSView.GetNativeCell(tableView, cell, templateId, isHeader, true);
				}
				else
				{
					var templatedList = TemplatedItemsView.TemplatedItems.GetGroup(sectionIndex);
					cell = (Cell)((INativeElementView)nativeCell).Element;
					ICellController controller = cell;
					controller.SendDisappearing();
					templatedList.UpdateContent(cell, itemIndexInSection);
					controller.SendAppearing();
				}
			}
			else
				throw new NotSupportedException();
			return nativeCell;
		}

		protected virtual Cell GetCellForPath(NSIndexPath indexPath, bool isGroupHeader)
		{
			var templatedItems = TemplatedItemsView.TemplatedItems;
			if (IsGroupingEnabled)
				templatedItems = (TemplatedItemsList<ItemsView<Cell>, Cell>)((IList)templatedItems)[(int)indexPath.Section];

			var cell = isGroupHeader ? templatedItems.HeaderContent : templatedItems[(int)indexPath.Item];
			return cell;
		}

		int TemplateIdForPath(NSIndexPath indexPath)
		{
			var itemTemplate = List.ItemTemplate;
			var selector = itemTemplate as DataTemplateSelector;
			if (selector == null)
				return DefaultItemTemplateId;

			var templatedList = TemplatedItemsView.TemplatedItems;
			if (List.IsGroupingEnabled)
				templatedList = (TemplatedItemsList<ItemsView<Cell>, Cell>)((IList)templatedList)[(int)indexPath.Section];

			var item = templatedList.ListProxy[(int)indexPath.Item];

			itemTemplate = selector.SelectTemplate(item, List);
			int key;
			if (!_templateToId.TryGetValue(itemTemplate, out key))
			{
				s_dataTemplateIncrementer++;
				key = s_dataTemplateIncrementer;
				_templateToId[itemTemplate] = key;
			}
			return key;
		}

		NSIndexPath GetPathFromRow(nint row, ref Cell cell)
		{
			var sectionIndex = 0;
			bool isGroupHeader = false;
			int itemIndexInSection;
			if (IsGroupingEnabled)
				GetComputedIndexes(row, out sectionIndex, out itemIndexInSection, out isGroupHeader);
			else
				itemIndexInSection = (int)row;
			NSIndexPath indexPath = NSIndexPath.FromItemSection(itemIndexInSection, sectionIndex);
			cell = GetCellForPath(indexPath, isGroupHeader);
			return indexPath;
		}

		nfloat CalculateHeightForCell(NSTableView tableView, Cell cell)
		{
			var viewCell = cell as ViewCell;
			double renderHeight;
			if (List.RowHeight == -1 && viewCell?.View != null)
			{
				var target = viewCell.View;
				if (_prototype == null)
				{
					_prototype = Platform.CreateRenderer(target);
					Platform.SetRenderer(target, _prototype);
				}
				else
				{
					_prototype.SetElement(target);
					Platform.SetRenderer(target, _prototype);
				}

				var req = target.Measure(tableView.Frame.Width, double.PositiveInfinity, MeasureFlags.IncludeMargins);

				target.ClearValue(Platform.RendererProperty);
				foreach (var descendant in target.Descendants())
					descendant.ClearValue(Platform.RendererProperty);

				renderHeight = req.Request.Height;
			}
			else
			{
				renderHeight = cell.RenderHeight;
			}

			return renderHeight > 0 ? (nfloat)renderHeight : ListViewRenderer.DefaultRowHeight;
		}

		void GetComputedIndexes(nint row, out int sectionIndex, out int itemIndexInSection, out bool isHeader)
		{
			var templatedItems = TemplatedItemsView.TemplatedItems;
			var totalItems = 0;
			isHeader = false;
			sectionIndex = 0;
			itemIndexInSection = 0;

			for (int i = 0; i < s_sectionCount; i++)
			{
				var group = (IList)((IList)templatedItems)[i];
				var itemsInSection = group.Count + 1;

				if (row < totalItems + itemsInSection)
				{
					sectionIndex = i;
					itemIndexInSection = (int)row - totalItems;
					isHeader = itemIndexInSection == 0;
					if (isHeader)
						itemIndexInSection = -1;
					else
						itemIndexInSection = itemIndexInSection - 1;
					break;
				}
				totalItems += itemsInSection;
			}
		}
	}
}