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

recommendations-iPad-fix #1369

Merged
merged 2 commits into from
Aug 13, 2024
Merged

recommendations-iPad-fix #1369

merged 2 commits into from
Aug 13, 2024

Conversation

adriansergheev
Copy link
Collaborator

@adriansergheev adriansergheev commented Aug 13, 2024

Why?

  • there's a crash on iPads when launching the app from background
Fatal Exception: NSInternalInconsistencyException
UICollectionView internal bug: Attempting to create a cell with invalid attributes. Collection View: <UICollectionView: 0x108dedc00; frame = (0 0; 1376 888); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x301442940>; backgroundColor = UIExtendedGrayColorSpace 0 0; layer = <CALayer: 0x301a57660>; contentOffset: {0, 30996}; contentSize: {1376, 33730.5}; adjustedContentInset: {0, 0, 0, 0}; layout: <FinniversKit.AdRecommendationsGridViewLayout: 0x11a2f92c0>; dataSource: <FinniversKit.AdRecommendationsGridView: 0x11a28ec00; frame = (0 0; 1376 888); layer = <CALayer: 0x301a57420>>>, Attributes: <UICollectionViewLayoutAttributes: 0x13b4fcc00; index path: (0-0); element kind: (UICollectionElementKindSectionHeader); frame = (0 0; 1376 252)>, Index Path: <NSIndexPath: 0x8c246f538daf20e4> {length = 2, path = 0 - 0}

What?

  • improve the lookup in the layout logic for AdRecommendationsGridViewLayout

Version Change

patch

UI Changes

  • none

Adrian Sergheev added 2 commits August 13, 2024 10:38
@@ -51,15 +47,14 @@ public class AdRecommendationsGridView: UIView {

private let imageCache = ImageMemoryCache()

private lazy var collectionViewLayout: AdRecommendationsGridViewLayout = {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

since collectionView is a mutable var, it's best to re-create the layout

return itemAttributes[indexPath.row]
guard itemAttributes.indices.contains(indexPath.row)
else { return nil }
if itemAttributes[indexPath.row].representedElementCategory == .cell {
Copy link
Collaborator Author

@adriansergheev adriansergheev Aug 13, 2024

Choose a reason for hiding this comment

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

fix:
the method layoutAttributesForItem retrieves layout info at the indexPath and it works for cells only
we have above a UICollectionViewLayoutAttributes for the header which is stored in the same array

for some reason, on iPads when launching the app from background this method is called with indexPath.row == 0 which would cause a crash, since at index 0 in the array we have a layout attribute for a header and not for a cell
on iPhones, the call is missing 🤷‍♂️

Comment on lines +121 to +127
guard itemAttributes.indices.contains(indexPath.row)
else { return nil }
if itemAttributes[indexPath.row].representedElementCategory == .cell {
return itemAttributes[indexPath.row]
} else {
return nil
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
guard itemAttributes.indices.contains(indexPath.row)
else { return nil }
if itemAttributes[indexPath.row].representedElementCategory == .cell {
return itemAttributes[indexPath.row]
} else {
return nil
}
if let itemAttribute = itemAttributes[safe: indexPath.row], itemAttribute.representedElementCategory == .cell {
return itemAttribute
} else {
return nil
}

Copy link
Contributor

@osanoj osanoj left a comment

Choose a reason for hiding this comment

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

🤞

Copy link
Contributor

@ninarg ninarg left a comment

Choose a reason for hiding this comment

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

Great work 🙌

@adriansergheev adriansergheev merged commit ee7f6c7 into master Aug 13, 2024
1 check passed
@adriansergheev adriansergheev deleted the frontpage-iPad-fix branch August 13, 2024 14:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants