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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
//
// Copyright © FINN.no AS, Inc. All rights reserved.
//

import UIKit

public protocol AdRecommendationsGridViewDelegate: AnyObject {
Expand Down Expand Up @@ -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

private func collectionViewLayout() -> AdRecommendationsGridViewLayout {
let layout = AdRecommendationsGridViewLayout()
layout.delegate = self
return layout
}()
}

// Have the collection view be private so nobody messes with it.
public private(set) lazy var collectionView: UICollectionView = {
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: collectionViewLayout)
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: collectionViewLayout())
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.delegate = self
collectionView.dataSource = self
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
//
// Copyright © FINN.no AS, Inc. All rights reserved.
//

import UIKit

protocol AdRecommendationsGridViewLayoutDelegate: AnyObject {
Expand Down Expand Up @@ -75,7 +71,7 @@ class AdRecommendationsGridViewLayout: UICollectionViewLayout {
override func prepare() {
super.prepare()

itemAttributes = [UICollectionViewLayoutAttributes]()
itemAttributes.removeAll()

guard let collectionView = collectionView else {
return
Expand Down Expand Up @@ -122,7 +118,13 @@ class AdRecommendationsGridViewLayout: UICollectionViewLayout {
}

override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
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 🤷‍♂️

return itemAttributes[indexPath.row]
} else {
return nil
}
Comment on lines +121 to +127
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
}

}

override var collectionViewContentSize: CGSize {
Expand Down