Skip to content

Commit

Permalink
Fixed [iOS] Fix CarouselViewRemoveAt so that it's passing on both CV1…
Browse files Browse the repository at this point in the history
… and CV2 sets of handlers (#25919)

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

* modified code changes

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

* modified code changes

* Fixed test case failure
  • Loading branch information
NirmalKumarYuvaraj authored Dec 11, 2024
1 parent 07f41ae commit eeaf57b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 24 deletions.
75 changes: 53 additions & 22 deletions src/Controls/src/Core/Handlers/Items/iOS/CarouselViewController.cs
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)
{
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) =>
{
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

0 comments on commit eeaf57b

Please sign in to comment.