Remove ItemSizingStrategy from CarouselView (#8588)
authorE.Z. Hart <hartez@users.noreply.github.com>
Thu, 21 Nov 2019 15:53:39 +0000 (08:53 -0700)
committerRui Marinho <me@ruimarinho.net>
Thu, 21 Nov 2019 15:53:39 +0000 (15:53 +0000)
* Move ItemSizingStrategy

* Fix tests

* Fix Android

* Fix Tizen

* Fix iOS

16 files changed:
Xamarin.Forms.Core.UnitTests/CarouselViewTests.cs
Xamarin.Forms.Core/Items/CarouselView.cs
Xamarin.Forms.Core/Items/ItemSizingStrategy.cs
Xamarin.Forms.Core/Items/ItemsView.cs
Xamarin.Forms.Core/Items/StructuredItemsView.cs
Xamarin.Forms.Platform.Android/CollectionView/CarouselViewRenderer.cs
Xamarin.Forms.Platform.Android/CollectionView/ItemsViewAdapter.cs
Xamarin.Forms.Platform.Android/CollectionView/ItemsViewRenderer.cs
Xamarin.Forms.Platform.Android/CollectionView/StructuredItemsViewAdapter.cs
Xamarin.Forms.Platform.Android/CollectionView/StructuredItemsViewRenderer.cs
Xamarin.Forms.Platform.Tizen/Renderers/ItemsViewRenderer.cs
Xamarin.Forms.Platform.iOS/CollectionView/CarouselViewLayout.cs
Xamarin.Forms.Platform.iOS/CollectionView/CarouselViewRenderer.cs
Xamarin.Forms.Platform.iOS/CollectionView/ItemsViewLayout.cs
Xamarin.Forms.Platform.iOS/CollectionView/ItemsViewRenderer.cs
Xamarin.Forms.Platform.iOS/CollectionView/StructuredItemsViewRenderer.cs

index 417dfab..e42ca5e 100644 (file)
@@ -33,7 +33,6 @@ namespace Xamarin.Forms.Core.UnitTests
                        Assert.IsNull(carouselView.ItemTemplate);
                        Assert.IsNotNull(carouselView.ItemsLayout);
                        Assert.IsTrue(carouselView.Position == 0);
-                       Assert.IsTrue(carouselView.ItemSizingStrategy == ItemSizingStrategy.None);
                }
 
                [Test]
index 6b23e0a..d68c652 100644 (file)
@@ -180,7 +180,6 @@ namespace Xamarin.Forms
                                SnapPointsType = SnapPointsType.MandatorySingle,
                                SnapPointsAlignment = SnapPointsAlignment.Center
                        };
-                       ItemSizingStrategy = ItemSizingStrategy.None;
                }
 
                [EditorBrowsable(EditorBrowsableState.Never)]
index e237689..c7cb166 100644 (file)
@@ -3,7 +3,6 @@
        public enum ItemSizingStrategy
        {
                MeasureAllItems,
-               MeasureFirstItem,
-               None
+               MeasureFirstItem
        }
 }
\ No newline at end of file
index 37f25ff..a5824c3 100644 (file)
@@ -131,15 +131,6 @@ namespace Xamarin.Forms
                        set => SetValue(InternalItemsLayoutProperty, value);
                }
 
-               public static readonly BindableProperty ItemSizingStrategyProperty =
-                       BindableProperty.Create(nameof(ItemSizingStrategy), typeof(ItemSizingStrategy), typeof(ItemsView));
-
-               public ItemSizingStrategy ItemSizingStrategy
-               {
-                       get => (ItemSizingStrategy)GetValue(ItemSizingStrategyProperty);
-                       set => SetValue(ItemSizingStrategyProperty, value);
-               }
-
                public static readonly BindableProperty ItemTemplateProperty =
                        BindableProperty.Create(nameof(ItemTemplate), typeof(DataTemplate), typeof(ItemsView));
 
index 4b09ff0..9e9b77c 100644 (file)
                        get => InternalItemsLayout;
                        set => InternalItemsLayout = value;
                }
+
+               public static readonly BindableProperty ItemSizingStrategyProperty =
+                       BindableProperty.Create(nameof(ItemSizingStrategy), typeof(ItemSizingStrategy), typeof(ItemsView));
+
+               public ItemSizingStrategy ItemSizingStrategy
+               {
+                       get => (ItemSizingStrategy)GetValue(ItemSizingStrategyProperty);
+                       set => SetValue(ItemSizingStrategyProperty, value);
+               }
        }
 }
\ No newline at end of file
index 3879519..171a019 100644 (file)
@@ -167,7 +167,7 @@ namespace Xamarin.Forms.Platform.Android
                        UpdateAdapter();
                }
 
-               void UpdateAdapter()
+               protected override void UpdateAdapter()
                {
                        // By default the CollectionViewAdapter creates the items at whatever size the template calls for
                        // But for the Carousel, we want it to create the items to fit the width/height of the viewport
index 400da14..d323e70 100644 (file)
@@ -16,8 +16,6 @@ namespace Xamarin.Forms.Platform.Android
                internal TItemsViewSource ItemsSource;
 
                bool _disposed;
-               Size? _size;
-
                bool _usingItemTemplate = false;
 
                internal ItemsViewAdapter(TItemsView itemsView, Func<View, Context, ItemContentView> createItemContentView = null)
@@ -126,16 +124,9 @@ namespace Xamarin.Forms.Platform.Android
                        return ItemsSource.GetPosition(item);
                }
 
-               protected void BindTemplatedItemViewHolder(TemplatedItemViewHolder templatedItemViewHolder, object context)
+               protected virtual void BindTemplatedItemViewHolder(TemplatedItemViewHolder templatedItemViewHolder, object context)
                {
-                       if (ItemsView.ItemSizingStrategy == ItemSizingStrategy.MeasureFirstItem)
-                       {
-                               templatedItemViewHolder.Bind(context, ItemsView, SetStaticSize, _size);
-                       }
-                       else
-                       {
-                               templatedItemViewHolder.Bind(context, ItemsView);
-                       }
+                       templatedItemViewHolder.Bind(context, ItemsView);
                }
 
                void UpdateItemsSource()
@@ -145,11 +136,6 @@ namespace Xamarin.Forms.Platform.Android
                        ItemsSource = CreateItemsSource();
                }
 
-               void SetStaticSize(Size size)
-               {
-                       _size = size;
-               }
-
                void UpdateUsingItemTemplate()
                {
                        _usingItemTemplate = ItemsView.ItemTemplate != null;
index 89a432b..d1f086d 100644 (file)
@@ -228,10 +228,6 @@ namespace Xamarin.Forms.Platform.Android
                        {
                                UpdateEmptyView();
                        }
-                       else if (changedProperty.Is(Xamarin.Forms.ItemsView.ItemSizingStrategyProperty))
-                       {
-                               UpdateAdapter();
-                       }
                        else if (changedProperty.Is(Xamarin.Forms.ItemsView.HorizontalScrollBarVisibilityProperty))
                        {
                                UpdateHorizontalScrollBarVisibility();
@@ -272,7 +268,7 @@ namespace Xamarin.Forms.Platform.Android
                        return (TAdapter)new ItemsViewAdapter<TItemsView, TItemsViewSource>(ItemsView);
                }
 
-               void UpdateAdapter()
+               protected virtual void UpdateAdapter()
                {
                        var oldItemViewAdapter = ItemsViewAdapter;
 
index 7239c5c..b274ba1 100644 (file)
@@ -10,6 +10,8 @@ namespace Xamarin.Forms.Platform.Android
                where TItemsView : StructuredItemsView
                where TItemsViewSource : IItemsViewSource
        {
+               Size? _size;
+
                internal StructuredItemsViewAdapter(TItemsView itemsView, 
                        Func<View, Context, ItemContentView> createItemContentView = null) : base(itemsView, createItemContentView)
                {
@@ -90,6 +92,18 @@ namespace Xamarin.Forms.Platform.Android
                        base.OnBindViewHolder(holder, position);
                }
 
+               protected override void BindTemplatedItemViewHolder(TemplatedItemViewHolder templatedItemViewHolder, object context)
+               {
+                       if (ItemsView.ItemSizingStrategy == ItemSizingStrategy.MeasureFirstItem)
+                       {
+                               templatedItemViewHolder.Bind(context, ItemsView, SetStaticSize, _size);
+                       }
+                       else
+                       {
+                               base.BindTemplatedItemViewHolder(templatedItemViewHolder, context);
+                       }
+               }
+
                void UpdateHasHeader()
                {
                        ItemsSource.HasHeader = ItemsView.Header != null;
@@ -126,5 +140,10 @@ namespace Xamarin.Forms.Platform.Android
                        // No template, Footer is not a Forms View, so just display Footer.ToString
                        return SimpleViewHolder.FromText(content?.ToString(), context, fill: false);
                }
+
+               void SetStaticSize(Size size)
+               {
+                       _size = size;
+               }
        }
 }
\ No newline at end of file
index bdfdae0..e8dcf1c 100644 (file)
@@ -22,6 +22,10 @@ namespace Xamarin.Forms.Platform.Android
                        {
                                UpdateLayoutManager();
                        }
+                       else if (changedProperty.Is(StructuredItemsView.ItemSizingStrategyProperty))
+                       {
+                               UpdateAdapter();
+                       }
                }
 
                protected override TAdapter CreateAdapter()
index 5aab9dd..f23c7ef 100644 (file)
@@ -14,7 +14,7 @@ namespace Xamarin.Forms.Platform.Tizen
                        RegisterPropertyHandler(ItemsView.ItemsSourceProperty, UpdateItemsSource);
                        RegisterPropertyHandler(ItemsView.ItemTemplateProperty, UpdateAdaptor);
                        RegisterPropertyHandler(StructuredItemsView.ItemsLayoutProperty, UpdateItemsLayout);
-                       RegisterPropertyHandler(ItemsView.ItemSizingStrategyProperty, UpdateSizingStrategy);
+                       RegisterPropertyHandler(StructuredItemsView.ItemSizingStrategyProperty, UpdateSizingStrategy);
                        RegisterPropertyHandler(SelectableItemsView.SelectedItemProperty, UpdateSelectedItem);
                        RegisterPropertyHandler(SelectableItemsView.SelectionModeProperty, UpdateSelectionMode);
                }
index 905e1fc..751e1a6 100644 (file)
@@ -9,7 +9,7 @@ namespace Xamarin.Forms.Platform.iOS
                readonly CarouselView _carouselView;
                readonly ItemsLayout _itemsLayout;
 
-               public CarouselViewLayout(ItemsLayout itemsLayout, ItemSizingStrategy itemSizingStrategy, CarouselView carouselView) : base(itemsLayout, itemSizingStrategy)
+               public CarouselViewLayout(ItemsLayout itemsLayout, CarouselView carouselView) : base(itemsLayout)
                {
                        _carouselView = carouselView;
                        _itemsLayout = itemsLayout;
@@ -23,7 +23,6 @@ namespace Xamarin.Forms.Platform.iOS
                public override void ConstrainTo(CGSize size)
                {
                        //TODO: Should we scale the items 
-                       var aspectRatio = size.Width / size.Height;
                        var numberOfVisibleItems = _carouselView.NumberOfSideItems * 2 + 1;
                        var width = (size.Width - _carouselView.PeekAreaInsets.Left - _carouselView.PeekAreaInsets.Right) / numberOfVisibleItems;
                        var height = (size.Height - _carouselView.PeekAreaInsets.Top - _carouselView.PeekAreaInsets.Bottom) / numberOfVisibleItems;
index eb26796..62aafb9 100644 (file)
@@ -33,7 +33,7 @@ namespace Xamarin.Forms.Platform.iOS
 
                protected override ItemsViewLayout SelectLayout()
                {
-                       return new CarouselViewLayout(CarouselView.ItemsLayout, CarouselView.ItemSizingStrategy, CarouselView);
+                       return new CarouselViewLayout(CarouselView.ItemsLayout, CarouselView);
                }
 
                protected override void SetUpNewElement(CarouselView newElement)
index 728bbee..ef5fb0a 100644 (file)
@@ -18,7 +18,13 @@ namespace Xamarin.Forms.Platform.iOS
 
                public ItemsUpdatingScrollMode ItemsUpdatingScrollMode { get; set; }
 
-               protected ItemsViewLayout(ItemsLayout itemsLayout, ItemSizingStrategy itemSizingStrategy)
+               public nfloat ConstrainedDimension { get; set; }
+
+               public Func<UICollectionViewCell> GetPrototype { get; set; }
+
+               internal ItemSizingStrategy ItemSizingStrategy { get; private set; }
+
+               protected ItemsViewLayout(ItemsLayout itemsLayout, ItemSizingStrategy itemSizingStrategy = ItemSizingStrategy.MeasureFirstItem)
                {
                        ItemSizingStrategy = itemSizingStrategy;
 
@@ -75,12 +81,6 @@ namespace Xamarin.Forms.Platform.iOS
                        }
                }
 
-               public nfloat ConstrainedDimension { get; set; }
-
-               public Func<UICollectionViewCell> GetPrototype { get; set; }
-
-               internal ItemSizingStrategy ItemSizingStrategy { get; private set; }
-
                public abstract void ConstrainTo(CGSize size);
 
                public virtual UIEdgeInsets GetInsetForSection(UICollectionView collectionView, UICollectionViewLayout layout,
index 522bf43..a4b5427 100644 (file)
@@ -15,8 +15,6 @@ namespace Xamarin.Forms.Platform.iOS
 
                protected TItemsView ItemsView => Element;
 
-
-
                public override UIViewController ViewController => Controller;
 
                protected TViewController Controller { get; private set; }
@@ -56,10 +54,6 @@ namespace Xamarin.Forms.Platform.iOS
                        {
                                Controller.UpdateEmptyView();
                        }
-                       else if (changedProperty.Is(Xamarin.Forms.ItemsView.ItemSizingStrategyProperty))
-                       {
-                               UpdateItemSizingStrategy();
-                       }
                        else if (changedProperty.Is(Xamarin.Forms.ItemsView.HorizontalScrollBarVisibilityProperty))
                        {
                                UpdateHorizontalScrollBarVisibility();
index 8dcb855..f6f761f 100644 (file)
@@ -27,6 +27,10 @@ namespace Xamarin.Forms.Platform.iOS
                        {
                                UpdateLayout();
                        }
+                       else if (changedProperty.Is(StructuredItemsView.ItemSizingStrategyProperty))
+                       {
+                               UpdateItemSizingStrategy();
+                       }
                }
 
                protected override void SetUpNewElement(TItemsView newElement)