summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core/TableView.cs
blob: b6c668002866eaed97976a7e459f4f27bf1728b0 (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
using System;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using Xamarin.Forms.Platform;
using Xamarin.Forms.Internals;

namespace Xamarin.Forms
{
	[ContentProperty("Root")]
	[RenderWith(typeof(_TableViewRenderer))]
	public class TableView : View, ITableViewController, IElementConfiguration<TableView>
	{
		public static readonly BindableProperty RowHeightProperty = BindableProperty.Create("RowHeight", typeof(int), typeof(TableView), -1);

		public static readonly BindableProperty HasUnevenRowsProperty = BindableProperty.Create("HasUnevenRows", typeof(bool), typeof(TableView), false);

		readonly Lazy<PlatformConfigurationRegistry<TableView>> _platformConfigurationRegistry;

		readonly TableSectionModel _tableModel;

		TableIntent _intent = TableIntent.Data;

		TableModel _model;

		public TableView() : this(null)
		{
		}

		public TableView(TableRoot root)
		{
			VerticalOptions = HorizontalOptions = LayoutOptions.FillAndExpand;
			Model = _tableModel = new TableSectionModel(this, root);
			_platformConfigurationRegistry = new Lazy<PlatformConfigurationRegistry<TableView>>(() => new PlatformConfigurationRegistry<TableView>(this));
		}

		public bool HasUnevenRows
		{
			get { return (bool)GetValue(HasUnevenRowsProperty); }
			set { SetValue(HasUnevenRowsProperty, value); }
		}

		public TableIntent Intent
		{
			get { return _intent; }
			set
			{
				if (_intent == value)
					return;

				OnPropertyChanging();
				_intent = value;
				OnPropertyChanged();
			}
		}

		public TableRoot Root
		{
			get { return _tableModel.Root; }
			set
			{
				if (_tableModel.Root != null)
				{
					_tableModel.Root.SectionCollectionChanged -= OnSectionCollectionChanged;
					_tableModel.Root.PropertyChanged -= OnTableModelRootPropertyChanged;
				}
				_tableModel.Root = value ?? new TableRoot();
				SetInheritedBindingContext(_tableModel.Root, BindingContext);

				Root.SelectMany(r => r).ForEach(cell => cell.Parent = this);
				_tableModel.Root.SectionCollectionChanged += OnSectionCollectionChanged;
				_tableModel.Root.PropertyChanged += OnTableModelRootPropertyChanged;
				OnModelChanged();
			}
		}

		public int RowHeight
		{
			get { return (int)GetValue(RowHeightProperty); }
			set { SetValue(RowHeightProperty, value); }
		}

		internal TableModel Model
		{
			get { return _model; }
			set
			{
				_model = value;
				OnModelChanged();
			}
		}
		ITableModel ITableViewController.Model
		{
			get
			{
				return Model;
			}
		}

		protected override void OnBindingContextChanged()
		{
			base.OnBindingContextChanged();
			if (Root != null)
				SetInheritedBindingContext(Root, BindingContext);
		}

		protected virtual void OnModelChanged()
		{
			foreach (Cell cell in Root.SelectMany(r => r))
				cell.Parent = this;

			if (ModelChanged != null)
				ModelChanged(this, EventArgs.Empty);
		}

		[Obsolete("Use OnMeasure")]
		protected override SizeRequest OnSizeRequest(double widthConstraint, double heightConstraint)
		{
			var minimumSize = new Size(40, 40);
			double width = Math.Min(Device.Info.ScaledScreenSize.Width, Device.Info.ScaledScreenSize.Height);
			var request = new Size(width, Math.Max(Device.Info.ScaledScreenSize.Width, Device.Info.ScaledScreenSize.Height));

			return new SizeRequest(request, minimumSize);
		}

		internal event EventHandler ModelChanged;
		event EventHandler ITableViewController.ModelChanged
		{
			add { ModelChanged += value; }
			remove { ModelChanged -= value; }
		}

		public IPlatformElementConfiguration<T, TableView> On<T>() where T : IConfigPlatform
		{
			return _platformConfigurationRegistry.Value.On<T>();
		}

		void CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
		{
			OnModelChanged();
		}

		void OnSectionCollectionChanged(object sender, ChildCollectionChangedEventArgs childCollectionChangedEventArgs)
		{
			if (childCollectionChangedEventArgs.Args.NewItems != null)
				childCollectionChangedEventArgs.Args.NewItems.Cast<Cell>().ForEach(cell => cell.Parent = this);
			OnModelChanged();
		}

		void OnTableModelRootPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			if (e.PropertyName == TableSectionBase.TitleProperty.PropertyName)
				OnModelChanged();
		}

		internal class TableSectionModel : TableModel
		{
			static readonly BindableProperty PathProperty = BindableProperty.Create("Path", typeof(Tuple<int, int>), typeof(Cell), null);

			readonly TableView _parent;
			TableRoot _root;

			public TableSectionModel(TableView tableParent, TableRoot tableRoot)
			{
				_parent = tableParent;
				Root = tableRoot ?? new TableRoot();
			}

			public TableRoot Root
			{
				get { return _root; }
				set
				{
					if (_root == value)
						return;

					RemoveEvents(_root);
					_root = value;
					ApplyEvents(_root);
				}
			}

			public override Cell GetCell(int section, int row)
			{
				var cell = (Cell)GetItem(section, row);
				SetPath(cell, new Tuple<int, int>(section, row));
				return cell;
			}

			public override object GetItem(int section, int row)
			{
				return _root[section][row];
			}

			public override int GetRowCount(int section)
			{
				return _root[section].Count;
			}

			public override int GetSectionCount()
			{
				return _root.Count;
			}

			public override string GetSectionTitle(int section)
			{
				return _root[section].Title;
			}

			protected override void OnRowSelected(object item)
			{
				base.OnRowSelected(item);

				((Cell)item).OnTapped();
			}

			internal static Tuple<int, int> GetPath(Cell item)
			{
				if (item == null)
					throw new ArgumentNullException("item");

				return (Tuple<int, int>)item.GetValue(PathProperty);
			}

			void ApplyEvents(TableRoot tableRoot)
			{
				tableRoot.CollectionChanged += _parent.CollectionChanged;
				tableRoot.SectionCollectionChanged += _parent.OnSectionCollectionChanged;
			}

			void RemoveEvents(TableRoot tableRoot)
			{
				if (tableRoot == null)
					return;

				tableRoot.CollectionChanged -= _parent.CollectionChanged;
				tableRoot.SectionCollectionChanged -= _parent.OnSectionCollectionChanged;
			}

			static void SetPath(Cell item, Tuple<int, int> index)
			{
				if (item == null)
					return;

				item.SetValue(PathProperty, index);
			}
		}
	}
}