summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.ControlGallery.iOS/CustomRenderers.cs
blob: 6b2ab38f5c7ed2c8d22acac748baa9adbfe5d8c1 (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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
using System;
using System.Collections.Generic;
using CoreLocation;
using Foundation;
using MapKit;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.ControlGallery.iOS;
using Xamarin.Forms.Controls;
using Xamarin.Forms.Platform.iOS;
using RectangleF = CoreGraphics.CGRect;

[assembly: ExportRenderer(typeof(Bugzilla21177.CollectionView), typeof(CollectionViewRenderer))]
[assembly: ExportRenderer(typeof(Bugzilla31395.CustomContentView), typeof(CustomContentRenderer))]
[assembly: ExportRenderer(typeof(NativeCell), typeof(NativeiOSCellRenderer))]
[assembly: ExportRenderer(typeof(NativeListView2), typeof(NativeiOSListViewRenderer))]
[assembly: ExportRenderer(typeof(NativeListView), typeof(NativeListViewRenderer))]
[assembly: ExportRenderer(typeof(CustomMapView), typeof(CustomIOSMapRenderer))]
[assembly: ExportRenderer(typeof(TabbedPage), typeof(TabbedPageWithCustomBarColorRenderer))]
[assembly: ExportRenderer(typeof(Bugzilla43161.AccessoryViewCell), typeof(AccessoryViewCellRenderer))]
namespace Xamarin.Forms.ControlGallery.iOS
{
	public class CustomIOSMapRenderer : ViewRenderer<CustomMapView, MKMapView>
	{
		private MKMapView _mapView;

		protected override void OnElementChanged(ElementChangedEventArgs<CustomMapView> e)
		{
			base.OnElementChanged(e);

			if (e.NewElement != null)
			{
				if (Control == null)
				{
					_mapView = new MKMapView(UIScreen.MainScreen.Bounds);
					_mapView.MapType = MKMapType.Standard;
					_mapView.RotateEnabled = false;
					SetNativeControl(_mapView);
				}

			}

			CLLocationCoordinate2D coords = new CLLocationCoordinate2D(48.857, 2.351);
			MKCoordinateSpan span = new MKCoordinateSpan(MilesToLatitudeDegrees(20), MilesToLongitudeDegrees(20, coords.Latitude));
			_mapView.Region = new MKCoordinateRegion(coords, span);
		}

		public double MilesToLatitudeDegrees(double miles)
		{
			double earthRadius = 3960.0; // in miles
			double radiansToDegrees = 180.0 / Math.PI;
			return (miles / earthRadius) * radiansToDegrees;
		}

		public double MilesToLongitudeDegrees(double miles, double atLatitude)
		{
			double earthRadius = 3960.0; // in miles
			double degreesToRadians = Math.PI / 180.0;
			double radiansToDegrees = 180.0 / Math.PI;
			// derive the earth's radius at that point in latitude
			double radiusAtLatitude = earthRadius * Math.Cos(atLatitude * degreesToRadians);
			return (miles / radiusAtLatitude) * radiansToDegrees;
		}
	}


	public class NativeiOSCellRenderer : ViewCellRenderer
	{
		static NSString s_rid = new NSString("NativeCell");

		public NativeiOSCellRenderer()
		{
		}

		public override UITableViewCell GetCell(Xamarin.Forms.Cell item, UITableViewCell reusableCell, UITableView tv)
		{
			var x = (NativeCell)item;
			Console.WriteLine(x);

			NativeiOSCell c = reusableCell as NativeiOSCell;

			if (c == null)
			{
				c = new NativeiOSCell(s_rid);
			}

			UIImage i = null;
			if (!string.IsNullOrWhiteSpace(x.ImageFilename))
			{
				i = UIImage.FromFile("Images/" + x.ImageFilename + ".jpg");
			}

			base.WireUpForceUpdateSizeRequested(item, c, tv);

			c.UpdateCell(x.Name, x.Category, i);

			return c;
		}
	}


	/// <summary>
	/// Sample of a custom cell layout, taken from the iOS docs at
	/// http://developer.xamarin.com/guides/ios/user_interface/tables/part_3_-_customizing_a_table's_appearance/
	/// </summary>
	public class NativeiOSCell : UITableViewCell
	{
		UILabel _headingLabel;
		UILabel _subheadingLabel;
		UIImageView _imageView;

		public NativeiOSCell(NSString cellId) : base(UITableViewCellStyle.Default, cellId)
		{
			SelectionStyle = UITableViewCellSelectionStyle.Gray;

			ContentView.BackgroundColor = UIColor.FromRGB(255, 255, 224);

			_imageView = new UIImageView();

			_headingLabel = new UILabel()
			{
				Font = UIFont.FromName("Cochin-BoldItalic", 22f),
				TextColor = UIColor.FromRGB(127, 51, 0),
				BackgroundColor = UIColor.Clear
			};

			_subheadingLabel = new UILabel()
			{
				Font = UIFont.FromName("AmericanTypewriter", 12f),
				TextColor = UIColor.FromRGB(38, 127, 0),
				TextAlignment = UITextAlignment.Center,
				BackgroundColor = UIColor.Clear
			};

			ContentView.Add(_headingLabel);
			ContentView.Add(_subheadingLabel);
			ContentView.Add(_imageView);
		}

		public void UpdateCell(string caption, string subtitle, UIImage image)
		{
			_imageView.Image = image;
			_headingLabel.Text = caption;
			_subheadingLabel.Text = subtitle;
		}

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

			_imageView.Frame = new RectangleF(ContentView.Bounds.Width - 63, 5, 33, 33);
			_headingLabel.Frame = new RectangleF(5, 4, ContentView.Bounds.Width - 63, 25);
			_subheadingLabel.Frame = new RectangleF(100, 18, 100, 20);
		}
	}

	/// <summary>
	/// Sample of a custom cell layout, taken from the iOS docs at
	/// http://developer.xamarin.com/guides/ios/user_interface/tables/part_3_-_customizing_a_table's_appearance/
	/// </summary>
	public class NativeiOSListViewCell : UITableViewCell
	{
		UILabel _headingLabel;
		UILabel _subheadingLabel;
		UIImageView _imageView;

		public NativeiOSListViewCell(NSString cellId) : base(UITableViewCellStyle.Default, cellId)
		{
			SelectionStyle = UITableViewCellSelectionStyle.Gray;

			ContentView.BackgroundColor = UIColor.FromRGB(218, 255, 127);

			_imageView = new UIImageView();

			_headingLabel = new UILabel()
			{
				Font = UIFont.FromName("Cochin-BoldItalic", 22f),
				TextColor = UIColor.FromRGB(127, 51, 0),
				BackgroundColor = UIColor.Clear
			};

			_subheadingLabel = new UILabel()
			{
				Font = UIFont.FromName("AmericanTypewriter", 12f),
				TextColor = UIColor.FromRGB(38, 127, 0),
				TextAlignment = UITextAlignment.Center,
				BackgroundColor = UIColor.Clear
			};

			ContentView.Add(_headingLabel);
			ContentView.Add(_subheadingLabel);
			ContentView.Add(_imageView);
		}

		public void UpdateCell(string caption, string subtitle, UIImage image)
		{
			_imageView.Image = image;
			_headingLabel.Text = caption;
			_subheadingLabel.Text = subtitle;
		}

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

			_imageView.Frame = new RectangleF(ContentView.Bounds.Width - 63, 5, 33, 33);
			_headingLabel.Frame = new RectangleF(5, 4, ContentView.Bounds.Width - 63, 25);
			_subheadingLabel.Frame = new RectangleF(100, 18, 100, 20);
		}
	}

	public class NativeiOSListViewRenderer : ViewRenderer<NativeListView2, UITableView>
	{
		public NativeiOSListViewRenderer()
		{
		}

		protected override void OnElementChanged(ElementChangedEventArgs<NativeListView2> e)
		{
			base.OnElementChanged(e);

			if (Control == null)
			{
				SetNativeControl(new UITableView());
			}

			if (e.OldElement != null)
			{
				// unsubscribe
			}

			if (e.NewElement != null)
			{
				// subscribe

				var s = new NativeiOSListViewSource(e.NewElement);
				Control.Source = s;
			}
		}

		protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
		{
			base.OnElementPropertyChanged(sender, e);

			if (e.PropertyName == NativeListView.ItemsProperty.PropertyName)
			{
				// update the Items list in the UITableViewSource
				var s = new NativeiOSListViewSource(Element);

				Control.Source = s;
			}
		}

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

	public class NativeListViewRenderer : ViewRenderer<NativeListView, UITableView>
	{
		public NativeListViewRenderer()
		{
		}

		protected override void OnElementChanged(ElementChangedEventArgs<NativeListView> e)
		{
			base.OnElementChanged(e);

			if (Control == null)
			{
				SetNativeControl(new UITableView());
			}

			if (e.OldElement != null)
			{
				// unsubscribe
			}

			if (e.NewElement != null)
			{
				// subscribe

				var s = new NativeListViewSource(e.NewElement);
				Control.Source = s;
			}
		}

		protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
		{
			base.OnElementPropertyChanged(sender, e);

			if (e.PropertyName == NativeListView.ItemsProperty.PropertyName)
			{
				// update the Items list in the UITableViewSource
				var s = new NativeListViewSource(Element);
				Control.Source = s;
			}
		}

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

	public class NativeiOSListViewSource : UITableViewSource
	{
		// declare vars
		IList<DataSource> _tableItems;
		NativeListView2 _listView;
		readonly NSString _cellIdentifier = new NSString("TableCell");

		public IEnumerable<DataSource> Items
		{
			//get{ }
			set { _tableItems = new List<DataSource>(value); }
		}

		public NativeiOSListViewSource(NativeListView2 view)
		{
			_tableItems = new List<DataSource>(view.Items);
			_listView = view;
		}

		/// <summary>
		/// Called by the TableView to determine how many cells to create for that particular section.
		/// </summary>

		public override nint RowsInSection(UITableView tableview, nint section)
		{
			return _tableItems.Count;
		}

		#region user interaction methods

		public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
		{
			_listView.NotifyItemSelected(_tableItems[indexPath.Row]);
			Console.WriteLine("Row " + indexPath.Row.ToString() + " selected");
			tableView.DeselectRow(indexPath, true);
		}

		public override void RowDeselected(UITableView tableView, NSIndexPath indexPath)
		{
			Console.WriteLine("Row " + indexPath.Row.ToString() + " deselected");
		}

		#endregion

		/// <summary>
		/// Called by the TableView to get the actual UITableViewCell to render for the particular section and row
		/// </summary>
		public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
		{
			// request a recycled cell to save memory
			NativeiOSListViewCell cell = tableView.DequeueReusableCell(_cellIdentifier) as NativeiOSListViewCell;

			// if there are no cells to reuse, create a new one
			if (cell == null)
			{
				cell = new NativeiOSListViewCell(_cellIdentifier);
			}

			if (string.IsNullOrWhiteSpace(_tableItems[indexPath.Row].ImageFilename))
			{
				cell.UpdateCell(_tableItems[indexPath.Row].Name
					, _tableItems[indexPath.Row].Category
					, null);
			}
			else
			{
				cell.UpdateCell(_tableItems[indexPath.Row].Name
					, _tableItems[indexPath.Row].Category
					, UIImage.FromFile("Images/" + _tableItems[indexPath.Row].ImageFilename + ".jpg"));
			}

			return cell;
		}
	}

	public class NativeListViewSource : UITableViewSource
	{
		// declare vars
		IList<string> _tableItems;
		string _cellIdentifier = "TableCell";
		NativeListView _listView;

		public IEnumerable<string> Items
		{
			set { _tableItems = new List<string>(value); 
			}
		}

		public NativeListViewSource(NativeListView view)
		{
			_tableItems = new List<string>(view.Items);
			_listView = view;
		}

		public override nint RowsInSection(UITableView tableview, nint section)
		{
			return _tableItems.Count;
		}

		#region user interaction methods

		public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
		{
			_listView.NotifyItemSelected(_tableItems[indexPath.Row]);

			Console.WriteLine("Row " + indexPath.Row.ToString() + " selected");

			tableView.DeselectRow(indexPath, true);
		}

		public override void RowDeselected(UITableView tableView, NSIndexPath indexPath)
		{
			Console.WriteLine("Row " + indexPath.Row.ToString() + " deselected");
		}

		#endregion

		/// <summary>
		/// Called by the TableView to get the actual UITableViewCell to render for the particular section and row
		/// </summary>
		public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
		{
			// declare vars
			UITableViewCell cell = tableView.DequeueReusableCell(_cellIdentifier);
			//string item = tableItems [indexPath.Row]; //.Items[indexPath.Row];

			// if there are no cells to reuse, create a new one
			if (cell == null)
				cell = new UITableViewCell(UITableViewCellStyle.Subtitle, _cellIdentifier);

			// set the item text
			cell.TextLabel.Text = _tableItems[indexPath.Row]; //.Items[indexPath.Row].Heading;

			// if it's a cell style that supports a subheading, set it
			//			if(item.CellStyle == UITableViewCellStyle.Subtitle 
			//				|| item.CellStyle == UITableViewCellStyle.Value1
			//				|| item.CellStyle == UITableViewCellStyle.Value2)
			//			{ cell.DetailTextLabel.Text = item.SubHeading; }

			// if the item has a valid image, and it's not the contact style (doesn't support images)
			//			if(!string.IsNullOrEmpty(item.ImageName) && item.CellStyle != UITableViewCellStyle.Value2)
			//			{
			//				if(File.Exists(item.ImageName))
			//					cell.ImageView.Image = UIImage.FromBundle(item.ImageName);
			//			}

			// set the accessory
			cell.Accessory = UITableViewCellAccessory.DisclosureIndicator;

			return cell;
		}

	}

	public class CustomContentRenderer : ViewRenderer
	{
	}

	public class CollectionViewRenderer : ViewRenderer<Bugzilla21177.CollectionView, UICollectionView>
	{
		public void ItemSelected(UICollectionView collectionViewView, NSIndexPath indexPath)
		{
			Element.InvokeItemSelected(indexPath.Row);
		}

		protected override void OnElementChanged(ElementChangedEventArgs<Bugzilla21177.CollectionView> e)
		{
			if (e.NewElement != null)
			{
				var flowLayout = new UICollectionViewFlowLayout
				{
					SectionInset = new UIEdgeInsets(20, 20, 20, 20),
					ScrollDirection = UICollectionViewScrollDirection.Vertical,
					MinimumInteritemSpacing = 5, // minimum spacing between cells 
					MinimumLineSpacing = 5 // minimum spacing between rows if ScrollDirection is Vertical or between columns if Horizontal 
				};
				var collectionView = new CollectionViewController(flowLayout, ItemSelected);
				SetNativeControl(collectionView.CollectionView);
			}

			base.OnElementChanged(e);
		}
	}

	public class CollectionViewController : UICollectionViewController
	{
		readonly OnItemSelected _onItemSelected;
		static NSString cellId = new NSString("CollectionViewCell");
		List<string> items;

		public delegate void OnItemSelected(UICollectionView collectionView, NSIndexPath indexPath);

		public CollectionViewController(UICollectionViewLayout layout, OnItemSelected onItemSelected) : base(layout)
		{
			items = new List<string>();
			for (int i = 0; i < 20; i++) {
				items.Add($"#{i}");
			}
			_onItemSelected = onItemSelected;
		}

		public override void ViewDidLoad()
		{
			base.ViewDidLoad();
			CollectionView.RegisterClassForCell(typeof(CollectionViewCell), cellId);
		}

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

		public override nint GetItemsCount(UICollectionView collectionView, nint section)
		{
			return items.Count;
		}

		public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
		{
			var cell = (CollectionViewCell)collectionView.DequeueReusableCell(cellId, indexPath);
			cell.Label.Text = items[indexPath.Row];
			return cell;
		}

		public override void ItemSelected(UICollectionView collectionView, NSIndexPath indexPath)
		{
			_onItemSelected(collectionView, indexPath);
		}
	}

	public class CollectionViewCell : UICollectionViewCell
	{
		public UILabel Label { get; private set; }

		[Export("initWithFrame:")]
		public CollectionViewCell(RectangleF frame) : base(frame)
		{
			var rand = new Random();
			BackgroundView = new UIView { BackgroundColor = UIColor.FromRGB(rand.Next(0, 256), rand.Next(0, 256), rand.Next(0, 256)) };
			SelectedBackgroundView = new UIView { BackgroundColor = UIColor.Green };
			ContentView.Layer.BorderColor = UIColor.LightGray.CGColor;
			ContentView.Layer.BorderWidth = 2.0f;
			Label = new UILabel(frame);
			Label.Center = ContentView.Center;
			ContentView.AddSubview(Label);
		}
	}

	public class TabbedPageWithCustomBarColorRenderer : TabbedRenderer
	{
		public TabbedPageWithCustomBarColorRenderer()
		{
			TabBar.TintColor = UIColor.White;
			TabBar.BarTintColor = UIColor.Purple;

			//UITabBar.Appearance.TintColor = UIColor.White;
			//UITabBar.Appearance.BarTintColor = UIColor.Purple;
		}
	}

	public class AccessoryViewCellRenderer : ViewCellRenderer
	{
		public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
		{
			var cell = base.GetCell(item, reusableCell, tv);

			// remove highlight on selected cell
			cell.SelectionStyle = UITableViewCellSelectionStyle.None;

			// iOS right arrow
			cell.Accessory = UITableViewCellAccessory.DisclosureIndicator;

			return cell;
		}
	}
}