-
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() |
src/Controls/src/Core/Handlers/Items/iOS/CarouselViewController.cs
Outdated
Show resolved
Hide resolved
@@ -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
src/Controls/src/Core/PublicAPI/net-maccatalyst/PublicAPI.Unshipped.txt
Outdated
Show resolved
Hide resolved
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.
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 ?
Seems is breaking this test
|
@@ -17,48 +17,146 @@ public CarouselViewUITests(TestDevice device) | |||
protected override void FixtureSetup() | |||
{ | |||
base.FixtureSetup(); | |||
App.NavigateToGallery(CarouselViewGallery); | |||
//App.NavigateToGallery(CarouselViewGallery); |
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 was maybe commented testing and has it escaped?
base.LoadView(); | ||
var collectionView = new MauiCollectionView(CGRect.Empty, ItemsViewLayout); | ||
CollectionView = collectionView; | ||
(collectionView as IUIViewLifeCycleEvents).MovedToWindow += CollectionViewMovedToWindow; |
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.
Unsubscribe when doing dispose
@@ -14,4 +16,18 @@ public override void ScrollRectToVisible(CGRect rect, bool animated) | |||
if (!KeyboardAutoManagerScroll.IsKeyboardAutoScrollHandling) | |||
base.ScrollRectToVisible(rect, animated); | |||
} | |||
|
|||
[UnconditionalSuppressMessage("Memory", "MEM0002", Justification = IUIViewLifeCycleEvents.UnconditionalSuppressMessage)] |
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.
Also the CarouselView is included in the HandlerDoesNotLeak. Looks fine pasing the test.
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 an 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. This changed the Scrolled event to be fired when the scroll animation 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 includes changes to the
CarouselViewHandler.iOS.cs
andCarouselViewController.cs
files in thesrc/Controls/src/Core/Handlers/Items/
directory. The changes primarily involve refactoring the code, adding new methods, and improving the existing logic.Here are the most significant changes:
Changes in
CarouselViewHandler.iOS.cs
:Foundation
andUIKit
namespaces.ScrollToRequested
method to include a newNSIndexPath
instance and added a newscrollToItemAction
method.MapPosition
method to handle situations where the initial position hasn't been set.AnimationDuration
property in theMapLoop
method.Changes in
CarouselViewController.cs
:Microsoft.Maui.Devices
namespace.CarouselViewController
class to include new fields and methods, and modified the constructor.GetCell
method to include new conditions forDefaultCell
andTemplatedCell
. [1] [2]ViewDidLayoutSubviews
method to use_isCenteringItem
instead of_updatingScrollOffset
.UpdateItemsSource
method to useInitialPositionSet
instead of_initialPositionSet
.AttachingToWindow
andDetachingFromWindow
methods, and added new methodsInitialPositionSet
,TearDown
, andSetup
.CarouselViewScrolled
method to include new conditions.CollectionViewUpdating
andCollectionViewUpdated
methods to include new conditions.UpdateLoop
andUpdateFromCurrentItem
methods to include new conditions. [1] [2]UpdateFromPosition
method to include new conditions and logic.UpdateInitialPosition
andUpdateVisualStates
methods to include new conditions and logic. [1] [2]CarouselViewLoopManager
class to include new conditions. [1] [2] [3]