From fae68e21b903d2a771bce21eeff81bb519d4d0e5 Mon Sep 17 00:00:00 2001 From: kingces95 Date: Tue, 5 Apr 2016 16:34:42 -0700 Subject: Reformat methods to original order --- .../Renderers/CarouselViewRenderer.cs | 417 ++++++++++----------- 1 file changed, 207 insertions(+), 210 deletions(-) diff --git a/Xamarin.Forms.Platform.iOS/Renderers/CarouselViewRenderer.cs b/Xamarin.Forms.Platform.iOS/Renderers/CarouselViewRenderer.cs index 26ef16cc..d07e9d8f 100644 --- a/Xamarin.Forms.Platform.iOS/Renderers/CarouselViewRenderer.cs +++ b/Xamarin.Forms.Platform.iOS/Renderers/CarouselViewRenderer.cs @@ -42,13 +42,16 @@ namespace Xamarin.Forms.Platform.iOS /// public class CarouselViewRenderer : ViewRenderer { + #region Static Fields const int DefaultMinimumDimension = 44; static readonly UIColor DefaultBackgroundColor = UIColor.White; + #endregion + #region Fields CarouselViewController.Layout _layout; int _position; - - CarouselViewController CarouselViewController { get; set; } + CarouselViewController _controller; + #endregion new UIScrollView Control { @@ -58,32 +61,10 @@ namespace Xamarin.Forms.Platform.iOS 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 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? @@ -91,7 +72,13 @@ namespace Xamarin.Forms.Platform.iOS if (carouselView != null) return; - CarouselViewController = new CarouselViewController(this, _layout = new CarouselViewController.Layout(UICollectionViewScrollDirection.Horizontal), Element.Position); + _controller = new CarouselViewController( + renderer: this, + layout: _layout = new CarouselViewController.Layout( + UICollectionViewScrollDirection.Horizontal + ), + originPosition: Element.Position + ); // hook up on position changed event // not ideal; the event is raised upon releasing the swipe instead of animation completion @@ -101,15 +88,38 @@ namespace Xamarin.Forms.Platform.iOS Element.CollectionChanged += OnCollectionChanged; // populate cache - SetNativeControl(CarouselViewController.CollectionView); + SetNativeControl(_controller.CollectionView); } + 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; + + _controller.ScrollToPosition(position, animated); + } void OnCollectionChanged(object source, NotifyCollectionChangedEventArgs e) { switch (e.Action) { case NotifyCollectionChangedAction.Add: - CarouselViewController.ReloadData(); + _controller.ReloadData(); if (e.NewStartingIndex <= _position) ShiftPosition(e.NewItems.Count); @@ -119,7 +129,10 @@ namespace Xamarin.Forms.Platform.iOS case NotifyCollectionChangedAction.Move: for (var i = 0; i < e.NewItems.Count; i++) { - CarouselViewController.MoveItem(e.OldStartingIndex + i, e.NewStartingIndex + i); + _controller.MoveItem( + oldPosition: e.OldStartingIndex + i, + newPosition: e.NewStartingIndex + i + ); } break; @@ -129,7 +142,9 @@ namespace Xamarin.Forms.Platform.iOS if (e.OldStartingIndex == _position) { - CarouselViewController.DeleteItems(Enumerable.Range(e.OldStartingIndex, e.OldItems.Count)); + _controller.DeleteItems( + Enumerable.Range(e.OldStartingIndex, e.OldItems.Count) + ); if (_position == Element.Count) _position--; OnItemChange(_position); @@ -137,7 +152,7 @@ namespace Xamarin.Forms.Platform.iOS else { - CarouselViewController.ReloadData(); + _controller.ReloadData(); if (e.OldStartingIndex < _position) ShiftPosition(-e.OldItems.Count); @@ -146,44 +161,19 @@ namespace Xamarin.Forms.Platform.iOS break; case NotifyCollectionChangedAction.Replace: - CarouselViewController.ReloadItems(Enumerable.Range(e.OldStartingIndex, e.OldItems.Count)); + _controller.ReloadItems( + Enumerable.Range(e.OldStartingIndex, e.OldItems.Count) + ); break; case NotifyCollectionChangedAction.Reset: - CarouselViewController.ReloadData(); + _controller.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 @@ -191,126 +181,29 @@ namespace Xamarin.Forms.Platform.iOS // Android behavior whereby the item remains the same and the position changes. ScrollToPosition(_position + offset, false); } - } - - internal sealed class CarouselViewController : UICollectionViewController - { - readonly Dictionary _typeIdByType; - UICollectionViewLayout _layout; - int _nextItemTypeId; - int _originPosition; - - public Action OnBind; - public Action OnSwipeTargetChosen; - public CarouselViewController(CarouselViewRenderer renderer, UICollectionViewLayout layout, int originPosition) : base(layout) - { - Renderer = renderer; - _typeIdByType = new Dictionary(); - _nextItemTypeId = 0; - _layout = layout; - _originPosition = originPosition; - } - - ICarouselViewController Controller - { - get { return Element; } - } - - CarouselView Element - { - get { return Renderer.Element; } - } - - CarouselViewRenderer Renderer { get; } - - public void DeleteItems(IEnumerable 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 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() + protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) { - base.ViewDidLoad(); + if (e.PropertyName == "Position") + // not ideal; the event is raised before the animation to move completes (or even starts) + ScrollToPosition(Element.Position); - CollectionView.PagingEnabled = true; - CollectionView.BackgroundColor = UIColor.Clear; + base.OnElementPropertyChanged(sender, e); } - - public override void WillDisplayCell(UICollectionView collectionView, UICollectionViewCell cell, NSIndexPath indexPath) + protected override void OnElementChanged(ElementChangedEventArgs e) { - 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; + base.OnElementChanged(e); + Initialize(); } - [Export("collectionView:layout:sizeForItemAtIndexPath:")] - SizeF GetSizeForItem(UICollectionView collectionView, UICollectionViewLayout layout, NSIndexPath indexPath) + public override SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint) { - return collectionView.Frame.Size; + return Control.GetSizeRequest(widthConstraint, heightConstraint, DefaultMinimumDimension, DefaultMinimumDimension); } + } + internal sealed class CarouselViewController : UICollectionViewController + { internal new sealed class Layout : UICollectionViewFlowLayout { static readonly nfloat ZeroMinimumInteritemSpacing = 0; @@ -318,8 +211,6 @@ namespace Xamarin.Forms.Platform.iOS int _width; - public Action OnSwipeOffsetChosen; - public Layout(UICollectionViewScrollDirection scrollDirection) { ScrollDirection = scrollDirection; @@ -327,31 +218,16 @@ namespace Xamarin.Forms.Platform.iOS MinimumLineSpacing = ZeroMinimumLineSpacing; } - public override SizeF CollectionViewContentSize + public Action OnSwipeOffsetChosen; + + public override UICollectionViewLayoutAttributes InitialLayoutAttributesForAppearingItem(NSIndexPath itemIndexPath) { - get - { - var result = base.CollectionViewContentSize; - return result; - } + return base.InitialLayoutAttributesForAppearingItem(itemIndexPath); } - 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 @@ -371,26 +247,35 @@ namespace Xamarin.Forms.Platform.iOS } return result; } - - public override bool ShouldInvalidateLayoutForBoundsChange(RectangleF newBounds) + public override SizeF CollectionViewContentSize { - return true; + get + { + var result = base.CollectionViewContentSize; + return result; + } } - - public override PointF TargetContentOffset(PointF proposedContentOffset, PointF scrollingVelocity) + public override NSIndexPath GetTargetIndexPathForInteractivelyMovingItem(NSIndexPath previousIndexPath, PointF position) { - var result = base.TargetContentOffset(proposedContentOffset, scrollingVelocity); - OnSwipeOffsetChosen?.Invoke((int)result.X / _width); + var result = base.GetTargetIndexPathForInteractivelyMovingItem(previousIndexPath, position); return result; } - public override PointF TargetContentOffsetForProposedContentOffset(PointF proposedContentOffset) { var result = base.TargetContentOffsetForProposedContentOffset(proposedContentOffset); return result; } + public override PointF TargetContentOffset(PointF proposedContentOffset, PointF scrollingVelocity) + { + var result = base.TargetContentOffset(proposedContentOffset, scrollingVelocity); + OnSwipeOffsetChosen?.Invoke((int)result.X / _width); + return result; + } + public override bool ShouldInvalidateLayoutForBoundsChange(RectangleF newBounds) + { + return true; + } } - sealed class Cell : UICollectionViewCell { IItemViewController _controller; @@ -398,19 +283,21 @@ namespace Xamarin.Forms.Platform.iOS IVisualElementRenderer _renderer; View _view; - public Action OnBind; - [Export("initWithFrame:")] internal Cell(RectangleF frame) : base(frame) { _position = -1; } - public override void LayoutSubviews() + void Bind(object item, int position) { - base.LayoutSubviews(); + //if (position != this.position) + // controller.SendPositionDisappearing (this.position); - _renderer.Element.Layout(new Rectangle(0, 0, ContentView.Frame.Width, ContentView.Frame.Height)); + _position = position; + OnBind?.Invoke(position); + + _controller.BindView(_view, item); } internal void Initialize(IItemViewController controller, object itemType, object item, int position) @@ -439,16 +326,126 @@ namespace Xamarin.Forms.Platform.iOS Bind(item, position); } - void Bind(object item, int position) + public Action OnBind; + + public override void LayoutSubviews() { - //if (position != this.position) - // controller.SendPositionDisappearing (this.position); + base.LayoutSubviews(); - _position = position; - OnBind?.Invoke(position); + _renderer.Element.Layout(new Rectangle(0, 0, ContentView.Frame.Width, ContentView.Frame.Height)); + } + } - _controller.BindView(_view, item); + readonly Dictionary _typeIdByType; + UICollectionViewLayout _layout; + CarouselViewRenderer _renderer; + int _nextItemTypeId; + int _originPosition; + + public Action OnBind; + public Action OnSwipeTargetChosen; + + public CarouselViewController(CarouselViewRenderer renderer, UICollectionViewLayout layout, int originPosition) : base(layout) + { + _renderer = renderer; + _typeIdByType = new Dictionary(); + _nextItemTypeId = 0; + _layout = layout; + _originPosition = originPosition; + } + + CarouselViewRenderer Renderer => _renderer; + CarouselView Element => _renderer.Element; + ICarouselViewController Controller => Element; + + [Export("collectionView:layout:sizeForItemAtIndexPath:")] + SizeF GetSizeForItem( + UICollectionView collectionView, + UICollectionViewLayout layout, + NSIndexPath indexPath) + { + return collectionView.Frame.Size; + } + + 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; + } + public override nint NumberOfSections(UICollectionView collectionView) + { + return 1; + } + public override void ViewDidLoad() + { + base.ViewDidLoad(); + + CollectionView.PagingEnabled = true; + CollectionView.BackgroundColor = UIColor.Clear; + } + public override nint GetItemsCount(UICollectionView collectionView, nint section) + { + var result = Element.Count; + return result; + } + 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 void ReloadData() => CollectionView.ReloadData(); + public void ReloadItems(IEnumerable positions) + { + var indices = positions.Select(o => NSIndexPath.FromRowSection(o, 0)).ToArray(); + CollectionView.ReloadItems(indices); + } + public void DeleteItems(IEnumerable positions) + { + var indices = positions.Select(o => NSIndexPath.FromRowSection(o, 0)).ToArray(); + CollectionView.DeleteItems(indices); + } + public void MoveItem(int oldPosition, int newPosition) + { + base.MoveItem( + CollectionView, + NSIndexPath.FromRowSection(oldPosition, 0), + NSIndexPath.FromRowSection(newPosition, 0) + ); + } + public void ScrollToPosition(int position, bool animated = true) + { + CollectionView.ScrollToItem( + indexPath: NSIndexPath.FromRowSection(position, 0), + scrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, + animated: animated + ); } } } \ No newline at end of file -- cgit v1.2.3