-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[iOS] Fix setting the CurrentItem on CarouselView load #22279
base: main
Are you sure you want to change the base?
Conversation
src/Controls/src/Core/Handlers/Items/iOS/CarouselViewController.cs
Outdated
Show resolved
Hide resolved
@@ -23,9 +23,9 @@ protected override void FixtureSetup() | |||
[Category(UITestCategories.CarouselView)] | |||
public async Task CarouselViewSetPosition() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -17,20 +18,34 @@ public class CarouselViewController : ItemsViewController<CarouselView> | |||
protected readonly CarouselView Carousel; | |||
|
|||
CarouselViewLoopManager _carouselViewLoopManager; | |||
bool _initialPositionSet; | |||
bool _updatingScrollOffset; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just renamed this one to be more clear
@@ -44,10 +59,14 @@ public override UICollectionViewCell GetCell(UICollectionView collectionView, NS | |||
var correctedIndexPath = NSIndexPath.FromRowSection(cellAndCorrectedIndex.correctedIndex, 0); | |||
|
|||
if (cell is DefaultCell defaultCell) | |||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixing warnings and added {
src/Controls/src/Core/Handlers/Items/iOS/CarouselViewController.cs
Outdated
Show resolved
Hide resolved
@@ -249,6 +262,15 @@ void InvalidateMeasureIfContentSizeChanged() | |||
_previousContentSize = contentSize.Value; | |||
} | |||
|
|||
internal virtual void AttachingToWindow() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we make this private protected
instead?
internal override void DettachingFromWindow() | ||
{ | ||
base.DettachingFromWindow(); | ||
TearDown(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we can just outright call TearDown
here
Or we need to move some of the cod that's in the ctor into AttachingToWindow
for example this is unsubscrived from
carousel.Scrolled -= CarouselViewScrolled;
Inside of TearDown, but subscribed to in the ctor.
Just because something is detaching from the window, doesn't mean it won't get re-attached.
For example, when a modal is pushed on top of the current screen. The VC below that modal will get unattached.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah , you right, got eager that actually never gets called, let me move the subscribe to the correct place to.
} | ||
} | ||
|
||
internal override void DettachingFromWindow() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
internal override void DettachingFromWindow() | |
internal override void DetachingFromWindow() |
{ | ||
return; | ||
} | ||
|
||
SetPosition(e.CenterItemIndex); | ||
|
||
UpdateVisualStates(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to call UpdateVisualStates
after the rotation has finished?
@@ -139,13 +139,15 @@ public CarouselItem(int index, string image = null) | |||
Index = index; | |||
|
|||
if (string.IsNullOrEmpty(image)) | |||
Image = "https://picsum.photos/700/300/"; | |||
Image = "oasis.jpg"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just fix so we don't use a web image
@@ -82,27 +82,31 @@ | |||
CurrentItem="{Binding Selected}"> | |||
<CarouselView.ItemTemplate> | |||
<DataTemplate> | |||
<Frame | |||
<Border |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We like Border
@@ -134,6 +134,7 @@ static Microsoft.Maui.Controls.Region.operator !=(Microsoft.Maui.Controls.Region | |||
static Microsoft.Maui.Controls.Region.operator ==(Microsoft.Maui.Controls.Region left, Microsoft.Maui.Controls.Region right) -> bool | |||
static Microsoft.Maui.Controls.Shapes.Matrix.operator !=(Microsoft.Maui.Controls.Shapes.Matrix left, Microsoft.Maui.Controls.Shapes.Matrix right) -> bool | |||
static Microsoft.Maui.Controls.Shapes.Matrix.operator ==(Microsoft.Maui.Controls.Shapes.Matrix left, Microsoft.Maui.Controls.Shapes.Matrix right) -> bool | |||
virtual Microsoft.Maui.Controls.Handlers.Items.CarouselViewHandler.AnimationDuration.get -> double |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure we want to make this public or not,
using CoreGraphics; | ||
using UIKit; | ||
|
||
namespace Microsoft.Maui.Controls.Handlers.Items; | ||
|
||
internal class MauiCollectionView : UICollectionView | ||
internal class MauiCollectionView : UICollectionView, IUIViewLifeCycleEvents |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This allows the Controller to know how it can subscribe and cleanup better.
@@ -1,9 +1,11 @@ | |||
using System; | |||
using System.Diagnostics.CodeAnalysis; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Drop this line
this.GetDesiredSizeFromHandler(widthConstraint, heightConstraint); | ||
|
||
protected virtual double AnimationDuration => 0.5; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be private for now? open on net9 ?
Description of Change
Since the mappers run when the handler is started and in different order than forms, seems the initial position based on CurrentItem wasn't being set correctly since the mapper for the Position property runs first and overrides it.
We also fix a issue when rotating was not keeping the correct position, since the scroll event was triggered and we want to avoid updating the position till the orientation finishes.
Added a way to know better when we the CollectionViewController is attached or detached from the view hierarchy. So now we can proper call the
TearDown
andSetup
.Fixes some warnings
Issues Fixed
Fixes #18879
Copilot summary
This pull request contains changes to the
CarouselViewHandler
andCarouselViewController
in thesrc/Controls/src/Core/Handlers/Items/
directory. The changes primarily focus on improving the handling of carousel view items, including updating the position and visual states of items, managing the carousel loop, and handling device rotation. The changes also include additional null checks and the introduction of new private properties and methods.Key changes include:
CarouselViewHandler.iOS.cs
: TheMapPosition
method has been updated to handle the initial position of the carousel view. The changes ensure that theUpdateFromPosition
method is only called when the initial position has been set.CarouselViewController.cs
: Several changes have been made to this file, including:Microsoft.Maui.Devices
namespace._isCenteringItem
,_initialOrientation
,_isRotating
) and the renaming of existing properties (_initialPositionSet
toInitialPositionSet
,_updatingScrollOffset
to_isCenteringItem
). [1] [2] [3] [4] [5] [6]AttachingToWindow
,DetachingFromWindow
,OnDisplayInfoChanged
) and the updating of existing methods (UpdateItemsSource
,UpdateVisibility
,UpdateLoop
,UpdateFromCurrentItem
,UpdateFromPosition
,UpdateInitialPosition
). These changes improve the handling of carousel view items and device rotation. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15]CarouselViewLoopManager
: TheCenterIfNeeded
,Dispose
,GetCorrectPositionForCenterItem
,GetGoToIndex
,CenterVerticallyIfNeeded
andCenterHorizontalIfNeeded
methods have been updated to include additional null checks. [1] [2] [3] [4] [5]