summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Tizen/Renderers/ListViewRenderer.cs
blob: fff217941a244c8c4e240bd0bd79c953988f7566 (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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
using System;
using System.Collections.Specialized;
using Xamarin.Forms.Internals;
using ElmSharp;
using EProgressBar = ElmSharp.ProgressBar;
using ERect = ElmSharp.Rect;

namespace Xamarin.Forms.Platform.Tizen
{
	/// <summary>
	/// Renderer class for Xamarin ListView class. This provides necessary logic translating
	/// Xamarin API to Tizen Native API. This is a derivate of a ViewRenderer base class.
	/// This is a template class with two template parameters. First one is restricted to
	/// Xamarin.Forms.View and can be accessed via property Element. This represent actual
	/// xamarin view which represents control logic. Second one is restricted to ElmSharp.Widget
	/// types, and can be accessed with Control property. This represents actual native control
	/// which is used to draw control and realize xamarin forms api.
	/// </summary>
	public class ListViewRenderer : ViewRenderer<ListView, Native.ListView>, IDisposable
	{
		IListViewController Controller => Element;
		ITemplatedItemsView<Cell> TemplatedItemsView => Element;

		/// <summary>
		/// Event handler for ScrollToRequested.
		/// </summary>
		readonly EventHandler<ScrollToRequestedEventArgs> _scrollToRequested;

		/// <summary>
		/// Event handler for collection changed.
		/// </summary>
		readonly NotifyCollectionChangedEventHandler _collectionChanged;

		/// <summary>
		/// Event handler for grouped collection changed.
		/// </summary>
		readonly NotifyCollectionChangedEventHandler _groupedCollectionChanged;

		/// <summary>
		/// The _lastSelectedItem and _selectedItemChanging are used for realizing ItemTapped event. Since Xamarin
		/// needs information only when an item has been taped, native handlers need to be agreagated
		/// and NotifyRowTapped has to be realized with this.
		/// </summary>

		GenListItem _lastSelectedItem = null;
		int _selectedItemChanging = 0;

		/// <summary>
		/// Initializes a new instance of the <see cref="Xamarin.Forms.Platform.Tizen.ListViewRenderer"/> class.
		/// Note that at this stage of construction renderer dose not have required native element. This should
		/// only be used with xamarin engine.
		/// </summary>
		public ListViewRenderer()
		{
			_scrollToRequested = OnScrollToRequested;
			_collectionChanged = OnCollectionChanged;
			_groupedCollectionChanged = OnGroupedCollectionChanged;

			RegisterPropertyHandler(ListView.IsGroupingEnabledProperty, UpdateIsGroupingEnabled);
			RegisterPropertyHandler(ListView.HasUnevenRowsProperty, UpdateHasUnevenRows);
			RegisterPropertyHandler(ListView.RowHeightProperty, UpdateRowHeight);
			RegisterPropertyHandler(ListView.SelectedItemProperty, UpdateSelectedItem);
			RegisterPropertyHandler(ListView.ItemsSourceProperty, UpdateSource);
			RegisterPropertyHandler("HeaderElement", UpdateHeader);
			RegisterPropertyHandler("FooterElement", UpdateFooter);
		}

		/// <summary>
		/// Invoked on creation of new ListView renderer. Handles the creation of a native
		/// element and initialization of the renderer.
		/// </summary>
		/// <param name="e"><see cref="Xamarin.Forms.Platform.Tizen.ElementChangedEventArgs"/>.</param>
		protected override void OnElementChanged(ElementChangedEventArgs<ListView> e)
		{
			if (Control == null)
			{
				SetNativeControl(new Native.ListView(Forms.Context.MainWindow));
			}

			if (e.OldElement != null)
			{
				e.OldElement.ScrollToRequested -= _scrollToRequested;
				if (Element.IsGroupingEnabled)
				{
					e.OldElement.TemplatedItems.GroupedCollectionChanged -= _groupedCollectionChanged;
				}
				e.OldElement.TemplatedItems.CollectionChanged -= _collectionChanged;
				Control.ItemSelected -= ListViewItemSelectedHandler;
				Control.ItemUnselected -= ListViewItemUnselectedHandler;
			}

			if (e.NewElement != null)
			{
				e.NewElement.ScrollToRequested += _scrollToRequested;
				Element.TemplatedItems.CollectionChanged += _collectionChanged;
				Control.ItemSelected += ListViewItemSelectedHandler;
				Control.ItemUnselected += ListViewItemUnselectedHandler;
			}

			base.OnElementChanged(e);
		}

		/// <summary>
		/// Handles the disposing of an existing renderer instance. Results in event handlers
		/// being detached and a Dispose() method from base class (VisualElementRenderer) being invoked.
		/// </summary>
		/// <param name="disposing">A boolean flag passed to the invocation of base class' Dispose() method.
		///  <c>True</c> if the memory release was requested on demand.</param>
		protected override void Dispose(bool disposing)
		{
			Element.ScrollToRequested -= _scrollToRequested;
			Element.TemplatedItems.CollectionChanged -= _collectionChanged;
			Element.TemplatedItems.GroupedCollectionChanged -= _groupedCollectionChanged;

			base.Dispose(disposing);
		}

		/// <summary>
		/// Handles item selected event. Note that it has to handle selection also for grouping mode as well.
		/// As a result of this method, ItemTapped event should be invoked in Xamarin.
		/// </summary>
		/// <param name="sender">A native list instance from which the event has originated.</param>
		/// <param name="e">Argument associated with handler, it holds native item for which event was raised</param>
		void ListViewItemSelectedHandler(object sender, GenListItemEventArgs e)
		{
			GenListItem item = e.Item;

			_lastSelectedItem = item;

			if (_selectedItemChanging == 0)
			{
				if (item != null)
				{
					int index = -1;
					if (Element.IsGroupingEnabled)
					{
						Native.ListView.ItemContext itemContext = item.Data as Native.ListView.ItemContext;
						if (itemContext.IsGroupItem)
						{
							return;
						}
						else
						{
							int groupIndex = (Element.TemplatedItems as System.Collections.IList).IndexOf(itemContext.ListOfSubItems);
							int inGroupIndex = itemContext.ListOfSubItems.IndexOf(itemContext.Cell);

							++_selectedItemChanging;
							Element.NotifyRowTapped(groupIndex, inGroupIndex);
							--_selectedItemChanging;
						}
					}
					else
					{
						index = Element.TemplatedItems.IndexOf((item.Data as Native.ListView.ItemContext).Cell);

						++_selectedItemChanging;
						Element.NotifyRowTapped(index);
						--_selectedItemChanging;
					}
				}
			}
		}

		/// <summary>
		/// Handles item unselected event.
		/// </summary>
		/// <param name="sender">A native list instance from which the event has originated.</param>
		/// <param name="e">Argument associated with handler, it holds native item for which event was raised</param>
		void ListViewItemUnselectedHandler(object sender, GenListItemEventArgs e)
		{
			if (_selectedItemChanging == 0)
			{
				_lastSelectedItem = null;
			}
		}

		/// <summary>
		/// This is method handles "scroll to" requests from xamarin events.
		/// It allows for scrolling to specified item on list view.
		/// </summary>
		/// <param name="sender">A native list instance from which the event has originated.</param>
		/// <param name="e">ScrollToRequestedEventArgs.</param>
		void OnScrollToRequested(object sender, ScrollToRequestedEventArgs e)
		{
			Cell cell;
			int position;
			var scrollArgs = (ITemplatedItemsListScrollToRequestedEventArgs)e;

			var templatedItems = TemplatedItemsView.TemplatedItems;
			if (Element.IsGroupingEnabled)
			{
				var results = templatedItems.GetGroupAndIndexOfItem(scrollArgs.Group, scrollArgs.Item);
				if (results.Item1 == -1 || results.Item2 == -1)
					return;

				var group = templatedItems.GetGroup(results.Item1);
				cell = group[results.Item2];
			}
			else
			{
				position = templatedItems.GetGlobalIndexOfItem(scrollArgs.Item);
				cell = templatedItems[position];
			}

			Control.ApplyScrollTo(cell, e.Position, e.ShouldAnimate);
		}

		/// <summary>
		/// Helper class for managing proper postion of Header and Footer element.
		/// Since both elements need to be implemented with ordinary list elements,
		/// both header and footer are removed at first, then the list is being modified
		/// and finally header and footer are prepended and appended to the list, respectively.
		/// </summary>
		class HeaderAndFooterHandler : IDisposable
		{
			VisualElement headerElement;
			VisualElement footerElement;

			Native.ListView Control;

			public HeaderAndFooterHandler(Widget control)
			{
				Control = control as Native.ListView;

				if (Control.HasHeader())
				{
					headerElement = Control.GetHeader();
					Control.RemoveHeader();
				}
				if (Control.HasFooter())
				{
					footerElement = Control.GetFooter();
					Control.RemoveFooter();
				}
			}

			public void Dispose()
			{
				if (headerElement != null)
				{
					Control.SetHeader(headerElement);
				}
				if (footerElement != null)
				{
					Control.SetFooter(footerElement);
				}
			}
		}

		/// <summary>
		/// This method is called whenever something changes in list view data model.
		/// Method will not be invoked for grouping mode, but for example event with
		/// action reset will be handled here when switching between group and no-group mode.
		/// </summary>
		/// <param name="sender">TemplatedItemsList<ItemsView<Cell>, Cell>.</param>
		/// <param name="e">NotifyCollectionChangedEventArgs.</param>
		void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
		{
			using (new HeaderAndFooterHandler(Control))
			{
				if (e.Action == NotifyCollectionChangedAction.Add)
				{
					Cell before = null;
					if (e.NewStartingIndex + e.NewItems.Count < Element.TemplatedItems.Count)
					{
						before = Element.TemplatedItems[e.NewStartingIndex + e.NewItems.Count];
					}
					Control.AddSource(e.NewItems, before);
				}
				else if (e.Action == NotifyCollectionChangedAction.Remove)
				{
					Control.Remove(e.OldItems);
				}
				else if (e.Action == NotifyCollectionChangedAction.Reset)
				{
					UpdateSource();
				}
			}
		}

		/// <summary>
		/// This method is called whenever something changes in list view data model.
		/// Method will be invoked for grouping mode, but some action can be also handled
		/// by OnCollectionChanged handler.
		/// </summary>
		/// <param name="sender">TemplatedItemsList<ItemsView<Cell>, Cell>.</param>
		/// <param name="e">NotifyCollectionChangedEventArgs.</param>
		void OnGroupedCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
		{
			using (new HeaderAndFooterHandler(Control))
			{
				if (e.Action == NotifyCollectionChangedAction.Add)
				{
					TemplatedItemsList<ItemsView<Cell>, Cell> itemsGroup = sender as TemplatedItemsList<ItemsView<Cell>, Cell>;
					Cell before = null;
					if (e.NewStartingIndex + e.NewItems.Count < itemsGroup.Count)
					{
						before = itemsGroup[e.NewStartingIndex + e.NewItems.Count];
					}
					Control.AddItemsToGroup(itemsGroup, e.NewItems, before);
				}
				else if (e.Action == NotifyCollectionChangedAction.Remove)
				{
					Control.Remove(e.OldItems);
				}
				else if (e.Action == NotifyCollectionChangedAction.Reset)
				{
					Control.ResetGroup(sender as TemplatedItemsList<ItemsView<Cell>, Cell>);
				}
			}
		}

		/// <summary>
		/// Updates the source.
		/// </summary>
		void UpdateSource()
		{
			Control.Clear();
			Control.AddSource(Element.TemplatedItems);
			UpdateSelectedItem();
		}

		/// <summary>
		/// Updates the header.
		/// </summary>
		void UpdateHeader()
		{
			Control.SetHeader(((IListViewController)Element).HeaderElement as VisualElement);
		}

		/// <summary>
		/// Updates the footer.
		/// </summary>
		void UpdateFooter()
		{
			Control.SetFooter(((IListViewController)Element).FooterElement as VisualElement);
		}

		/// <summary>
		/// Updates the has uneven rows.
		/// </summary>
		void UpdateHasUnevenRows()
		{
			Control.SetHasUnevenRows(Element.HasUnevenRows);
		}

		/// <summary>
		/// Updates the height of the row.
		/// </summary>
		void UpdateRowHeight()
		{
			Control.UpdateRealizedItems();
		}

		/// <summary>
		/// Updates the is grouping enabled.
		/// </summary>
		/// <param name="initialize">If set to <c>true</c>, this method is invoked during initialization
		/// (otherwise it will be invoked only after property changes).</param>
		void UpdateIsGroupingEnabled(bool initialize)
		{
			Control.IsGroupingEnabled = Element.IsGroupingEnabled;
			if (Element.IsGroupingEnabled)
			{
				Element.TemplatedItems.GroupedCollectionChanged += _groupedCollectionChanged;
			}
			else
			{
				Element.TemplatedItems.GroupedCollectionChanged -= _groupedCollectionChanged;
			}
		}

		/// <summary>
		/// Method is used for programaticaly selecting choosen item.
		/// </summary>
		void UpdateSelectedItem()
		{
			if (Element.SelectedItem == null)
			{
				if (_lastSelectedItem != null)
				{
					_lastSelectedItem.IsSelected = false;
					_lastSelectedItem = null;
				}
			}
			else
			{
				var templatedItems = TemplatedItemsView.TemplatedItems;
				var results = templatedItems.GetGroupAndIndexOfItem(Element.SelectedItem);
				if (results.Item1 != -1 && results.Item2 != -1)
				{
					var itemGroup = templatedItems.GetGroup(results.Item1);
					var cell = itemGroup[results.Item2];

					++_selectedItemChanging;
					Control.ApplySelectedItem(cell);
					--_selectedItemChanging;
				}
			}
		}
	}
}