summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.iOS/Renderers/CarouselViewRenderer.cs
blob: 26ef16cc5dd43bffe3777028adb2e2597f30a299 (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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
#if __UNIFIED__
using UIKit;
using Foundation;
#else
using MonoTouch.UIKit;
using MonoTouch.Foundation;
#endif
#if __UNIFIED__
using RectangleF = CoreGraphics.CGRect;
using SizeF = CoreGraphics.CGSize;
using PointF = CoreGraphics.CGPoint;

#else
using nfloat=System.Single;
using nint=System.Int32;
using nuint=System.UInt32;
#endif

namespace Xamarin.Forms.Platform.iOS
{
	/// <summary>
	///     UICollectionView visualizes a collection of data. UICollectionViews are created indirectly by first creating a
	///     CarouselViewController from which the CollectionView is accessed via the CollectionView property.
	///     The CarouselViewController functionality is exposed through a set of interfaces (aka "conforms to" in the Apple
	///     docs).
	///     When Xamarin exposed CarouselViewRenderer the following interfaces where implemented as virtual methods:
	///     UICollectionViewSource
	///     UIScrollViewDelegate
	///     UICollectionViewDelegate		Allow you to manage the selection and highlighting of items in a collection view
	///     UICollectionViewDataSource		Creation and configuration of cells and supplementary views used to display data
	///     The interfaces only implement required method while the UICollectionView exposes optional methods via
	///     ExportAttribute.
	///     The C# method name may be aliased. For example, C# "GetCell" maps to obj-C "CellForItemAtIndexPath".
	///     <seealso cref="https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionView_class/" />
	/// </summary>
	public class CarouselViewRenderer : ViewRenderer<CarouselView, UICollectionView>
	{
		const int DefaultMinimumDimension = 44;
		static readonly UIColor DefaultBackgroundColor = UIColor.White;

		CarouselViewController.Layout _layout;
		int _position;

		CarouselViewController CarouselViewController { get; set; }

		new UIScrollView Control
		{
			get
			{
				Initialize();
				return base.Control;
			}
		}

		ICarouselViewController Controller
		{
			get { return Element; }
		}

		public override SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint)
		{
			return Control.GetSizeRequest(widthConstraint, heightConstraint, DefaultMinimumDimension, DefaultMinimumDimension);
		}

		protected override void OnElementChanged(ElementChangedEventArgs<CarouselView> e)
		{
			base.OnElementChanged(e);
			Initialize();
		}

		protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			if (e.PropertyName == "Position")
				// not ideal; the event is raised before the animation to move completes (or even starts)
				ScrollToPosition(Element.Position);

			base.OnElementPropertyChanged(sender, e);
		}

		void Initialize()
		{
			// cache hit? 
			var carouselView = base.Control;
			if (carouselView != null)
				return;

			CarouselViewController = new CarouselViewController(this, _layout = new CarouselViewController.Layout(UICollectionViewScrollDirection.Horizontal), Element.Position);

			// hook up on position changed event
			// not ideal; the event is raised upon releasing the swipe instead of animation completion
			_layout.OnSwipeOffsetChosen += o => OnPositionChange(o);

			// hook up crud events
			Element.CollectionChanged += OnCollectionChanged;

			// populate cache
			SetNativeControl(CarouselViewController.CollectionView);
		}

		void OnCollectionChanged(object source, NotifyCollectionChangedEventArgs e)
		{
			switch (e.Action)
			{
				case NotifyCollectionChangedAction.Add:
					CarouselViewController.ReloadData();

					if (e.NewStartingIndex <= _position)
						ShiftPosition(e.NewItems.Count);

					break;

				case NotifyCollectionChangedAction.Move:
					for (var i = 0; i < e.NewItems.Count; i++)
					{
						CarouselViewController.MoveItem(e.OldStartingIndex + i, e.NewStartingIndex + i);
					}
					break;

				case NotifyCollectionChangedAction.Remove:
					if (Element.Count == 0)
						throw new InvalidOperationException("CarouselView must retain a least one item.");

					if (e.OldStartingIndex == _position)
					{
						CarouselViewController.DeleteItems(Enumerable.Range(e.OldStartingIndex, e.OldItems.Count));
						if (_position == Element.Count)
							_position--;
						OnItemChange(_position);
					}

					else
					{
						CarouselViewController.ReloadData();

						if (e.OldStartingIndex < _position)
							ShiftPosition(-e.OldItems.Count);
					}

					break;

				case NotifyCollectionChangedAction.Replace:
					CarouselViewController.ReloadItems(Enumerable.Range(e.OldStartingIndex, e.OldItems.Count));
					break;

				case NotifyCollectionChangedAction.Reset:
					CarouselViewController.ReloadData();
					break;

				default:
					throw new Exception();
			}
		}

		void OnItemChange(int position)
		{
			var item = Controller.GetItem(position);
			Controller.SendSelectedItemChanged(item);
		}

		bool OnPositionChange(int position)
		{
			if (position == _position)
				return false;

			_position = position;

			Controller.SendSelectedPositionChanged(position);
			OnItemChange(position);
			return true;
		}

		void ScrollToPosition(int position, bool animated = true)
		{
			if (!OnPositionChange(position))
				return;

			CarouselViewController.ScrollToPosition(position, animated);
		}

		void ShiftPosition(int offset)
		{
			// By default the position remains the same which causes an animation in the case
			// of the added/removed position preceding the current position. I prefer the constructed
			// Android behavior whereby the item remains the same and the position changes.
			ScrollToPosition(_position + offset, false);
		}
	}

	internal sealed class CarouselViewController : UICollectionViewController
	{
		readonly Dictionary<object, int> _typeIdByType;
		UICollectionViewLayout _layout;
		int _nextItemTypeId;
		int _originPosition;

		public Action<int> OnBind;
		public Action<int> OnSwipeTargetChosen;

		public CarouselViewController(CarouselViewRenderer renderer, UICollectionViewLayout layout, int originPosition) : base(layout)
		{
			Renderer = renderer;
			_typeIdByType = new Dictionary<object, int>();
			_nextItemTypeId = 0;
			_layout = layout;
			_originPosition = originPosition;
		}

		ICarouselViewController Controller
		{
			get { return Element; }
		}

		CarouselView Element
		{
			get { return Renderer.Element; }
		}

		CarouselViewRenderer Renderer { get; }

		public void DeleteItems(IEnumerable<int> positions)
		{
			var indices = positions.Select(o => NSIndexPath.FromRowSection(o, 0)).ToArray();
			CollectionView.DeleteItems(indices);
		}

		public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
		{
			var index = indexPath.Row;

			if (_originPosition != 0)
				index = _originPosition;

			var item = Controller.GetItem(index);
			var itemType = Controller.GetItemType(item);

			var itemTypeId = default(int);
			if (!_typeIdByType.TryGetValue(itemType, out itemTypeId))
			{
				_typeIdByType[itemType] = itemTypeId = _nextItemTypeId++;
				CollectionView.RegisterClassForCell(typeof(Cell), itemTypeId.ToString());
			}

			var cell = (Cell)CollectionView.DequeueReusableCell(itemTypeId.ToString(), indexPath);
			cell.Initialize(Element, itemType, item, index);

			// a semantically weak approach to OnAppearing; decided not to expose as such
			if (cell.OnBind == null)
				cell.OnBind += o => OnBind?.Invoke(o);

			return cell;
		}

		public override nint GetItemsCount(UICollectionView collectionView, nint section)
		{
			var result = Element.Count;
			return result;
		}

		public void MoveItem(int oldPosition, int newPosition)
		{
			base.MoveItem(CollectionView, NSIndexPath.FromRowSection(oldPosition, 0), NSIndexPath.FromRowSection(newPosition, 0));
		}

		public override nint NumberOfSections(UICollectionView collectionView)
		{
			return 1;
		}

		public void ReloadData() => CollectionView.ReloadData();

		public void ReloadItems(IEnumerable<int> positions)
		{
			var indices = positions.Select(o => NSIndexPath.FromRowSection(o, 0)).ToArray();
			CollectionView.ReloadItems(indices);
		}

		public void ScrollToPosition(int position, bool animated = true)
		{
			CollectionView.ScrollToItem(NSIndexPath.FromRowSection(position, 0), UICollectionViewScrollPosition.CenteredHorizontally, animated);
		}

		public override void ViewDidLoad()
		{
			base.ViewDidLoad();

			CollectionView.PagingEnabled = true;
			CollectionView.BackgroundColor = UIColor.Clear;
		}

		public override void WillDisplayCell(UICollectionView collectionView, UICollectionViewCell cell, NSIndexPath indexPath)
		{
			if (_originPosition == 0)
				return;

			// Ideally position zero would not be rendered in memory however it is.
			// Thankfully, position zero is not displyed; position originPosition is rendered and displayed.
			ScrollToPosition(_originPosition, false);
			_originPosition = 0;
		}

		[Export("collectionView:layout:sizeForItemAtIndexPath:")]
		SizeF GetSizeForItem(UICollectionView collectionView, UICollectionViewLayout layout, NSIndexPath indexPath)
		{
			return collectionView.Frame.Size;
		}

		internal new sealed class Layout : UICollectionViewFlowLayout
		{
			static readonly nfloat ZeroMinimumInteritemSpacing = 0;
			static readonly nfloat ZeroMinimumLineSpacing = 0;

			int _width;

			public Action<int> OnSwipeOffsetChosen;

			public Layout(UICollectionViewScrollDirection scrollDirection)
			{
				ScrollDirection = scrollDirection;
				MinimumInteritemSpacing = ZeroMinimumInteritemSpacing;
				MinimumLineSpacing = ZeroMinimumLineSpacing;
			}

			public override SizeF CollectionViewContentSize
			{
				get
				{
					var result = base.CollectionViewContentSize;
					return result;
				}
			}

			public override UICollectionViewLayoutAttributes FinalLayoutAttributesForDisappearingItem(NSIndexPath itemIndexPath)
			{
				return base.FinalLayoutAttributesForDisappearingItem(itemIndexPath);
			}

			public override NSIndexPath GetTargetIndexPathForInteractivelyMovingItem(NSIndexPath previousIndexPath, PointF position)
			{
				var result = base.GetTargetIndexPathForInteractivelyMovingItem(previousIndexPath, position);
				return result;
			}

			public override UICollectionViewLayoutAttributes InitialLayoutAttributesForAppearingItem(NSIndexPath itemIndexPath)
			{
				return base.InitialLayoutAttributesForAppearingItem(itemIndexPath);
			}

			public override UICollectionViewLayoutAttributes[] LayoutAttributesForElementsInRect(RectangleF rect)
			{
				// couldn't figure a way to use these values to compute when an element appeared to disappeared. YMMV
				var result = base.LayoutAttributesForElementsInRect(rect);
				foreach (var item in result)
				{
					var index = item.IndexPath;
					var category = item.RepresentedElementCategory;
					var kind = item.RepresentedElementKind;

					var hidden = item.Hidden;
					var bounds = item.Bounds;
					var frame = item.Frame;
					var center = item.Center;

					_width = (int)item.Bounds.Width;
				}
				return result;
			}

			public override bool ShouldInvalidateLayoutForBoundsChange(RectangleF newBounds)
			{
				return true;
			}

			public override PointF TargetContentOffset(PointF proposedContentOffset, PointF scrollingVelocity)
			{
				var result = base.TargetContentOffset(proposedContentOffset, scrollingVelocity);
				OnSwipeOffsetChosen?.Invoke((int)result.X / _width);
				return result;
			}

			public override PointF TargetContentOffsetForProposedContentOffset(PointF proposedContentOffset)
			{
				var result = base.TargetContentOffsetForProposedContentOffset(proposedContentOffset);
				return result;
			}
		}

		sealed class Cell : UICollectionViewCell
		{
			IItemViewController _controller;
			int _position;
			IVisualElementRenderer _renderer;
			View _view;

			public Action<int> OnBind;

			[Export("initWithFrame:")]
			internal Cell(RectangleF frame) : base(frame)
			{
				_position = -1;
			}

			public override void LayoutSubviews()
			{
				base.LayoutSubviews();

				_renderer.Element.Layout(new Rectangle(0, 0, ContentView.Frame.Width, ContentView.Frame.Height));
			}

			internal void Initialize(IItemViewController controller, object itemType, object item, int position)
			{
				_position = position;

				if (_controller == null)
				{
					_controller = controller;

					// create view
					_view = controller.CreateView(itemType);

					// bind view
					Bind(item, position);

					// render view
					_renderer = Platform.CreateRenderer(_view);
					Platform.SetRenderer(_view, _renderer);

					// attach view
					var uiView = _renderer.NativeView;
					ContentView.AddSubview(uiView);
				}
				else
					Bind(item, position);
			}

			void Bind(object item, int position)
			{
				//if (position != this.position)
				//	controller.SendPositionDisappearing (this.position);

				_position = position;
				OnBind?.Invoke(position);

				_controller.BindView(_view, item);
			}
		}
	}
}