Skip to content

Commit

Permalink
[#4087] Move exact height calculation into AdManager (#4088)
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafaozhan authored Nov 24, 2024
1 parent 069cec0 commit 423611b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ internal class AdManagerImpl(context: Context) : AdManager {
override fun getBannerAd(
context: Context,
adId: String,
maxHeight: Int
maxHeightInDp: Float
): BannerAdView {
Logger.v { "AdManagerImpl getBannerAd" }

Expand All @@ -96,13 +96,15 @@ internal class AdManagerImpl(context: Context) : AdManager {
context.resources.displayMetrics.widthPixels
}

val adHeight = maxHeightInDp.toInt().toDp(context)

val adSize = AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(
context,
(adWidthPixels / context.resources.displayMetrics.density).toInt()
adWidthPixels.toDp(context)
)

val finalAdSize = if (adSize.height > maxHeight) {
AdSize(adSize.width, maxHeight)
val finalAdSize = if (adSize.height > adHeight) {
AdSize(adSize.width, adHeight)
} else {
adSize
}
Expand Down Expand Up @@ -177,6 +179,9 @@ internal class AdManagerImpl(context: Context) : AdManager {
)
}

private fun Int.toDp(context: Context) =
(this / context.resources.displayMetrics.density).toInt()

private fun Activity.initializeMobileAdsSdk() {
Logger.v { "AdManagerImpl initializeMobileAdsSdk" }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal class AdManagerImpl : AdManager {
override fun getBannerAd(
context: Context,
adId: String,
maxHeight: Int
maxHeightInDp: Float
): BannerAdView {
Logger.v { "AdManagerImpl getBannerAd" }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface AdManager {
fun getBannerAd(
context: Context,
adId: String,
maxHeight: Int
maxHeightInDp: Float
): BannerAdView

fun showInterstitialAd(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@ fun FrameLayout.buildBanner(
adId: String,
shouldShowAd: Boolean
) = if (shouldShowAd) {
val maxAdHeight =
(context.resources.getDimension(R.dimen.ads_banner_height) / resources.displayMetrics.density).toInt()
removeAllViews()
addView(adManager.getBannerAd(context, adId, maxAdHeight))
addView(
adManager.getBannerAd(
context,
adId,
context.resources.getDimension(R.dimen.ads_banner_height)
)
)
} else {
visibility = View.GONE
}
Expand Down

0 comments on commit 423611b

Please sign in to comment.