Skip to content

Commit

Permalink
frontpage-recommendations-externalAd-layout (#1371)
Browse files Browse the repository at this point in the history
* Revert "frontpage-recommendations-layout-fix (#1324)"

This reverts commit 2b786a4.

* add missing constraint

* snapshot tests

* wip

* snapshot tests wip

---------

Co-authored-by: Adrian Sergheev <[email protected]>
  • Loading branch information
adriansergheev and Adrian Sergheev authored Aug 15, 2024
1 parent bc9a446 commit 9005270
Show file tree
Hide file tree
Showing 12 changed files with 16 additions and 6 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ struct AdDataSource {
ads.insert(.ad(AdFactory.googleDemoAd), at: 4)
ads.insert(.ad(AdFactory.nativeDemoAd), at: 8)
var externals: [AdRecommendation] = ExternalAdFactory.create(numberOfModels: 4)
.map { .external($0) }
ads.insert(externals[0], at: 0)
ads.append(contentsOf: externals.dropFirst())
.map { .external($0) }
ads.insert(externals[0], at: 0)
ads.append(contentsOf: externals.dropFirst())
return ads
}()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public class ExternalAdRecommendationCell: UICollectionViewCell, AdRecommendatio
private static let accessoryHeight: CGFloat = 14.0
private static let margin: CGFloat = 8.0
private static let cornerRadius: CGFloat = 8.0
private static let heightMultiplier: CGFloat = 1
private static let minImageAspectRatio: CGFloat = 0.75
private static let maxImageAspectRatio: CGFloat = 1.5

private let loadingColor: UIColor = .backgroundSubtle

Expand Down Expand Up @@ -151,13 +152,19 @@ public class ExternalAdRecommendationCell: UICollectionViewCell, AdRecommendatio

backgroundColor = .clear

let imageHeightMinimumConstraint = imageContentView.heightAnchor.constraint(equalTo: imageContentView.widthAnchor, multiplier: ExternalAdRecommendationCell.minImageAspectRatio)
let imageHeightMaximumConstraint = imageContentView.heightAnchor.constraint(lessThanOrEqualTo: imageContentView.widthAnchor, multiplier: ExternalAdRecommendationCell.maxImageAspectRatio)

imageHeightMinimumConstraint.priority = .defaultHigh

containerView.fillInSuperview()

NSLayoutConstraint.activate([
imageContentView.topAnchor.constraint(equalTo: contentView.topAnchor),
imageContentView.widthAnchor.constraint(equalTo: contentView.widthAnchor),
imageContentView.centerXAnchor.constraint(equalTo: contentView.centerXAnchor),
imageContentView.heightAnchor.constraint(equalTo: imageContentView.widthAnchor, multiplier: Self.heightMultiplier),
imageHeightMinimumConstraint,
imageHeightMaximumConstraint,

ribbonView.topAnchor.constraint(equalTo: imageContentView.bottomAnchor, constant: ExternalAdRecommendationCell.ribbonTopMargin),
ribbonView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
Expand All @@ -176,6 +183,7 @@ public class ExternalAdRecommendationCell: UICollectionViewCell, AdRecommendatio
titleLabel.topAnchor.constraint(equalTo: subtitleLabel.bottomAnchor, constant: ExternalAdRecommendationCell.titleTopMargin),
titleLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
titleLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
titleLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
])
}

Expand Down Expand Up @@ -239,7 +247,9 @@ public class ExternalAdRecommendationCell: UICollectionViewCell, AdRecommendatio

public static func height(for model: ExternalAdRecommendationViewModel, width: CGFloat) -> CGFloat {
let titleHeight = model.title.height(withConstrainedWidth: width, font: .body)
let imageHeight = width * Self.heightMultiplier
let imageRatio = model.imageSize.height / model.imageSize.width
let clippedImageRatio = min(max(imageRatio, ExternalAdRecommendationCell.minImageAspectRatio), ExternalAdRecommendationCell.maxImageAspectRatio)
let imageHeight = width * clippedImageRatio
let contentHeight = subtitleTopMargin + subtitleHeight + titleTopMargin + titleHeight + bottomMargin

return imageHeight + contentHeight
Expand Down

0 comments on commit 9005270

Please sign in to comment.