Skip to content

Commit

Permalink
Ad updates
Browse files Browse the repository at this point in the history
  • Loading branch information
dkrivoruchko committed Sep 28, 2021
1 parent bb7f5b3 commit 4ab6c5f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
6 changes: 4 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ android {
productFlavors {
create("firebase") {
manifestPlaceholders = ["adMobPubId": "${localProps.getProperty("ad.pubId")}"]
buildConfigField "String", "AD_UNIT_ID", localProps.getProperty("ad.unitId", "")
buildConfigField "String", "AD_UNIT_ID_A", localProps.getProperty("ad.unitIdA", "")
buildConfigField "String", "AD_UNIT_ID_B", localProps.getProperty("ad.unitIdB", "")
buildConfigField "String", "AD_UNIT_ID_C", localProps.getProperty("ad.unitIdC", "")
}
create("firebasefree") {
manifestPlaceholders = ["adMobPubId": ""]
Expand Down Expand Up @@ -94,7 +96,7 @@ dependencies {
implementation("androidx.activity:activity-ktx:1.3.1")
implementation("androidx.fragment:fragment-ktx:1.4.0-alpha09")
implementation("androidx.appcompat:appcompat:1.3.1")
implementation("androidx.constraintlayout:constraintlayout:2.1.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.1")
implementation("androidx.recyclerview:recyclerview:1.2.1")
implementation("com.google.android.material:material:1.4.0")
implementation("androidx.window:window:1.0.0-beta02")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ abstract class AdFragment(@LayoutRes contentLayoutId: Int) : Fragment(contentLay

private var adView: AdView? = null
private lateinit var adSize: AdSize
private val ads: MutableMap<String, Long> by lazy(LazyThreadSafetyMode.NONE) {
(requireActivity().application as BaseApp).lastAdLoadTimeMap.apply {
putIfAbsent(BuildConfig.AD_UNIT_ID_A, 0)
putIfAbsent(BuildConfig.AD_UNIT_ID_B, 0)
putIfAbsent(BuildConfig.AD_UNIT_ID_C, 0)
}
}

fun loadAdOnViewCreated(adViewContainer: FrameLayout) {
if (::adSize.isInitialized) loadAd(adViewContainer)
Expand All @@ -40,14 +47,16 @@ abstract class AdFragment(@LayoutRes contentLayoutId: Int) : Fragment(contentLay
private fun loadAd(adViewContainer: FrameLayout) {
adViewContainer.minimumHeight = adSize.getHeightInPixels(requireActivity())
viewLifecycleOwner.lifecycleScope.launchWhenStarted {
delay((requireActivity().application as BaseApp).lastAdLoadTime + 61_000 - System.currentTimeMillis())
val currentAd = ads.filter { it.value + 61_000 - System.currentTimeMillis() <= 0 }.entries.firstOrNull()
?: ads.minByOrNull { it.value }!!
while (currentAd.value + 61_000 - System.currentTimeMillis() > 0) delay(100)
MobileAds.initialize(requireActivity()) {}
adView = AdView(requireActivity()).also { adView ->
adViewContainer.addView(adView)
adView.adUnitId = BuildConfig.AD_UNIT_ID
adView.adUnitId = currentAd.key
adView.adSize = adSize
adView.loadAd(AdRequest.Builder().build())
(requireActivity().application as BaseApp).lastAdLoadTime = System.currentTimeMillis()
ads.replace(currentAd.key, System.currentTimeMillis())
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/kotlin/info/dvkr/screenstream/BaseApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ abstract class BaseApp : Application() {
.build()
}

var lastAdLoadTime: Long = 0
val lastAdLoadTimeMap: MutableMap<String, Long> = mutableMapOf()

abstract fun initLogger()

Expand Down

0 comments on commit 4ab6c5f

Please sign in to comment.