Skip to content
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

Fixed [iOS] Fix CarouselViewRemoveAt so that it's passing on both CV1 and CV2 sets of handlers #25919

Merged
merged 7 commits into from
Dec 11, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using CoreGraphics;
using Foundation;
using Microsoft.Maui.ApplicationModel;
using Microsoft.Maui.Devices;
using ObjCRuntime;
using UIKit;
Expand Down Expand Up @@ -104,7 +106,7 @@ public override void ViewWillLayoutSubviews()
UpdateVisualStates();
}

public override void ViewDidLayoutSubviews()
public override async void ViewDidLayoutSubviews()
{
base.ViewDidLayoutSubviews();

Expand All @@ -122,7 +124,7 @@ public override void ViewDidLayoutSubviews()
}
else
{
UpdateInitialPosition();
await UpdateInitialPosition();
}
_isRotating = false;
}
Expand Down Expand Up @@ -203,10 +205,11 @@ protected override void CacheCellAttributes(NSIndexPath indexPath, CGSize size)
base.CacheCellAttributes(NSIndexPath.FromItemSection(itemIndex, 0), size);
}

private protected override void AttachingToWindow()
private protected override async void AttachingToWindow()
{
base.AttachingToWindow();
Setup(ItemsView);
await UpdateInitialPosition();
}

private protected override void DetachingFromWindow()
Expand All @@ -220,7 +223,7 @@ private protected override void DetachingFromWindow()
void TearDown(CarouselView carouselView)
{
_oldViews = null;

InitialPositionSet = false;
carouselView.Scrolled -= CarouselViewScrolled;
DeviceDisplay.MainDisplayInfoChanged -= OnDisplayInfoChanged;

Expand Down Expand Up @@ -446,7 +449,7 @@ void ScrollToPosition(int goToPosition, int carouselPosition, bool animate, bool

void SetPosition(int position)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could include an UITest?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jsuarezruiz The test case for this scenario is already present. I have updated it to use the default CarouselView instead of CarouselView2.

{
if (position == -1 || ItemsView is not CarouselView carousel)
if (!InitialPositionSet || position == -1 || ItemsView is not CarouselView carousel)
{
return;
}
Expand Down Expand Up @@ -478,6 +481,9 @@ void SetCurrentItem(int carouselPosition)

internal void UpdateFromCurrentItem()
{
if (!InitialPositionSet)
return;

if (ItemsView is not CarouselView carousel)
{
return;
Expand All @@ -497,6 +503,11 @@ internal void UpdateFromCurrentItem()

internal void UpdateFromPosition()
{
if (!InitialPositionSet)
{
return;
}

if (ItemsView is not CarouselView carousel)
{
return;
Expand All @@ -520,8 +531,12 @@ internal void UpdateFromPosition()
SetCurrentItem(carouselPosition);
}

void UpdateInitialPosition()
async Task UpdateInitialPosition()
{
if (ItemsView is not CarouselView carousel)
{
return;
}
var itemsCount = ItemsSource?.ItemCount;

if (itemsCount == 0)
Expand All @@ -531,28 +546,44 @@ void UpdateInitialPosition()

if (!InitialPositionSet)
{
if (ItemsView is not CarouselView carousel)
{
return;
}

InitialPositionSet = true;

int position = carousel.Position;
var currentItem = carousel.CurrentItem;

if (currentItem != null)
{
position = ItemsSource.GetIndexForItem(currentItem).Row;
}
else
{
SetCurrentItem(position);
}
// Sometimes the item could be just being removed while we navigate back to the CarouselView
var positionCurrentItem = ItemsSource.GetIndexForItem(currentItem).Row;
if (positionCurrentItem != -1)
{
position = positionCurrentItem;
}
}

carousel.ScrollTo(position, -1, Microsoft.Maui.Controls.ScrollToPosition.Center, false);
await Task.Delay(100).ContinueWith((t) =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the best approach ? Why 100? can we try the Collection.PerformBatchUpdates see if it helps?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rmarinho , I have used the similar approach used in CarouselViewController2.cs. Using Collection.PerformBatchUpdates causes the page with carousel view freeze. Please verify and let me know if you have any concerns.

{
MainThread.BeginInvokeOnMainThread(() =>
{
if (!IsViewLoaded)
{
return;
}

InitialPositionSet = true;

if (ItemsSource is null || ItemsSource.ItemCount == 0)
{
return;
}


carousel.ScrollTo(position, -1, Microsoft.Maui.Controls.ScrollToPosition.Center, false);

SetCurrentItem(position);
UpdateVisualStates();
});

});
}

UpdateVisualStates();
}

void UpdateVisualStates()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Maui.Controls.Sample.Issues
[Issue(IssueTracker.None, 10300, "ObservableCollection.RemoveAt(index) with a valid index raises ArgumentOutOfRangeException", PlatformAffected.iOS)]
public class CarouselViewRemoveAt : ContentPage
{
readonly CarouselView2 _carousel;
readonly CarouselView _carousel;

public class ModalPage : ContentPage
{
Expand Down Expand Up @@ -47,7 +47,7 @@ public CarouselViewRemoveAt()
new ModelCarouselViewRemoveAt("8", Colors.IndianRed),
new ModelCarouselViewRemoveAt("9", Colors.Khaki),
});
_carousel = new CarouselView2
_carousel = new CarouselView
{
ItemTemplate = new DataTemplate(() =>
{
Expand Down
Loading