-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Fixed [iOS] Fix CarouselViewRemoveAt so that it's passing on both CV1 and CV2 sets of handlers #25919
Changes from all commits
1276b51
4fff4fe
fdd2757
3916ba3
6ee5ba6
3162808
c1eff46
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -104,7 +106,7 @@ public override void ViewWillLayoutSubviews() | |
UpdateVisualStates(); | ||
} | ||
|
||
public override void ViewDidLayoutSubviews() | ||
public override async void ViewDidLayoutSubviews() | ||
{ | ||
base.ViewDidLayoutSubviews(); | ||
|
||
|
@@ -122,7 +124,7 @@ public override void ViewDidLayoutSubviews() | |
} | ||
else | ||
{ | ||
UpdateInitialPosition(); | ||
await UpdateInitialPosition(); | ||
} | ||
_isRotating = false; | ||
} | ||
|
@@ -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() | ||
|
@@ -220,7 +223,7 @@ private protected override void DetachingFromWindow() | |
void TearDown(CarouselView carouselView) | ||
{ | ||
_oldViews = null; | ||
|
||
InitialPositionSet = false; | ||
carouselView.Scrolled -= CarouselViewScrolled; | ||
DeviceDisplay.MainDisplayInfoChanged -= OnDisplayInfoChanged; | ||
|
||
|
@@ -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; | ||
} | ||
|
@@ -478,6 +481,9 @@ void SetCurrentItem(int carouselPosition) | |
|
||
internal void UpdateFromCurrentItem() | ||
{ | ||
if (!InitialPositionSet) | ||
return; | ||
|
||
if (ItemsView is not CarouselView carousel) | ||
{ | ||
return; | ||
|
@@ -497,6 +503,11 @@ internal void UpdateFromCurrentItem() | |
|
||
internal void UpdateFromPosition() | ||
{ | ||
if (!InitialPositionSet) | ||
{ | ||
return; | ||
} | ||
|
||
if (ItemsView is not CarouselView carousel) | ||
{ | ||
return; | ||
|
@@ -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) | ||
|
@@ -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) => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
|
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.
Could include an UITest?
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.
@jsuarezruiz The test case for this scenario is already present. I have updated it to use the default CarouselView instead of CarouselView2.