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

Fix iOS] Fix CarouselViewLoopNoFreeze so that it's passing on both CV1 and CV2 sets of handlers #26053

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ void CarouselViewScrolled(object sender, ItemsViewScrolledEventArgs e)
[UnconditionalSuppressMessage("Memory", "MEM0003", Justification = "Proven safe in test: MemoryTests.HandlerDoesNotLeak")]
void CollectionViewUpdating(object sender, NotifyCollectionChangedEventArgs e)
{
if (ItemsView is not CarouselView carousel)
if (ItemsSource.ItemCount == 0 || ItemsView is not CarouselView carousel)
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you 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.

Could you include an UITest?

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

{
return;
}
Expand Down Expand Up @@ -343,6 +343,18 @@ void CollectionViewUpdated(object sender, NotifyCollectionChangedEventArgs e)

SetPosition(targetPosition);
SetCurrentItem(targetPosition);

if (e.Action == NotifyCollectionChangedAction.Remove)
{
//Since we can be removing the item that is already created next to the current item we need to update the visible cells
if (ItemsView.Loop)
{
CollectionView.ReloadItems(new NSIndexPath[] { GetScrollToIndexPath(targetPosition) });
}
}

ScrollToPosition(targetPosition,targetPosition,false,true);

}

int GetPositionWhenAddingItems(int carouselPosition, int currentItemPosition)
Expand Down Expand Up @@ -437,7 +449,7 @@ void ScrollToPosition(int goToPosition, int carouselPosition, bool animate, bool
return;
}

if (_gotoPosition == -1 && (goToPosition != carouselPosition || forceScroll))
if (goToPosition != carouselPosition || forceScroll)
{
_gotoPosition = goToPosition;
carousel.ScrollTo(goToPosition, position: Microsoft.Maui.Controls.ScrollToPosition.Center, animate: animate);
Expand Down Expand Up @@ -729,7 +741,7 @@ public NSIndexPath GetGoToIndex(UICollectionView collectionView, int newPosition
var incrementAbs = Math.Abs(increment);

int goToPosition;
if (diffToStart < incrementAbs)
if (diffToStart <= incrementAbs)
{
goToPosition = centerIndexPath.Row - diffToStart;
}
Expand Down Expand Up @@ -830,7 +842,12 @@ void FinishCenterIfNeeded(UICollectionView collectionView, int shiftCells)
int GetCorrectedIndex(int indexToCorrect)
{
var itemsCount = GetItemsSourceCount();
if ((indexToCorrect < itemsCount && indexToCorrect >= 0) || itemsCount == 0)
if ( itemsCount == 0)
{
return 0;
}

if (indexToCorrect < itemsCount && indexToCorrect >= 0)
{
return indexToCorrect;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class CarouselViewLoopNoFreeze : ContentPage
readonly string _btnRemoveAllAutomationId = "btnRemoveAll";

readonly ViewModelIssue12574 _viewModel;
readonly CarouselView2 _carouselView;
readonly CarouselView _carouselView;
readonly Button _btn;
readonly Button _btn2;

Expand All @@ -33,7 +33,7 @@ public CarouselViewLoopNoFreeze()
};
_btn2.SetBinding(Button.CommandProperty, "RemoveAllItemsCommand");

_carouselView = new CarouselView2
_carouselView = new CarouselView
{
AutomationId = _carouselAutomationId,
Margin = new Thickness(30),
Expand Down Expand Up @@ -178,4 +178,4 @@ protected void OnPropertyChanged(string propertyName)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}