summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.MacOS/Renderers/TableViewDataSource.cs
blob: 90335ee268e3ac1368187c00e32d4180228d2d51 (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
´╗┐using System;
using AppKit;
using Foundation;

namespace Xamarin.Forms.Platform.MacOS
{
	internal class TableViewDataSource : NSTableViewSource
	{
		static int s_sectionCount;

		const string HeaderIdentifier = nameof(TextCell);
		const string ItemIdentifier = nameof(ViewCell);

		protected ITableViewController Controller => _tableView;

		readonly NSTableView _nsTableView;
		readonly TableView _tableView;

		public TableViewDataSource(TableViewRenderer tableViewRenderer)
		{
			_tableView = tableViewRenderer.Element;
			_nsTableView = tableViewRenderer.TableView;
			Controller.ModelChanged += (s, e) => { _nsTableView?.ReloadData(); };
			AutomaticallyDeselect = true;
		}

		public bool AutomaticallyDeselect { get; set; }

		public override void SelectionDidChange(NSNotification notification)
		{
			var row = _nsTableView.SelectedRow;
			if (row == -1)
				return;

			int sectionIndex;
			bool isHeader;
			int itemIndexInSection;

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

			var cell = Controller.Model.GetCell(sectionIndex, itemIndexInSection);
			Controller.Model.RowSelected(cell);
			if (AutomaticallyDeselect)
				_nsTableView.DeselectRow(row);
		}

		public override nint GetRowCount(NSTableView tableView)
		{
			nint count = 0;
			s_sectionCount = Controller.Model.GetSectionCount();
			for (int i = 0; i < s_sectionCount; i++)
			{
				count += Controller.Model.GetRowCount(i) + 1;
			}

			return count;
		}

		public override bool ShouldSelectRow(NSTableView tableView, nint row)
		{
			int sectionIndex;
			bool isHeader;
			int itemIndexInSection;

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

			return !isHeader;
		}

		public override NSView GetViewForItem(NSTableView tableView, NSTableColumn tableColumn, nint row)
		{
			int sectionIndex;
			bool isHeader;
			int itemIndexInSection;

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

			string id;
			Cell cell;
			if (isHeader)
			{
				id = HeaderIdentifier;
				cell = Controller.Model.GetHeaderCell(sectionIndex) ??
						new TextCell { Text = Controller.Model.GetSectionTitle(sectionIndex) };
			}
			else
			{
				id = ItemIdentifier;
				cell = Controller.Model.GetCell(sectionIndex, itemIndexInSection);
			}

			var nativeCell = CellNSView.GetNativeCell(tableView, cell, id, isHeader);
			return nativeCell;
		}

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

			for (int i = 0; i < s_sectionCount; i++)
			{
				var groupCount = Controller.Model.GetRowCount(i);
				var itemsInSection = groupCount + 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;
			}
		}
	}

	//TODO: Implement Uneven rows
	internal class UnEvenTableViewModelRenderer : TableViewDataSource
	{
		public UnEvenTableViewModelRenderer(TableViewRenderer model) : base(model)
		{
		}
	}
}