Skip to content

Commit

Permalink
Rename CacheAdModel to CachedAd
Browse files Browse the repository at this point in the history
  • Loading branch information
teogor committed Oct 27, 2023
1 parent b712bcc commit b0cabcb
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 44 deletions.
36 changes: 18 additions & 18 deletions monetisation/admob/api/admob.api
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ public abstract class dev/teogor/ceres/monetisation/admob/formats/Ad : dev/teogo
public final class dev/teogor/ceres/monetisation/admob/formats/AdCache {
public static final field $stable I
public static final field INSTANCE Ldev/teogor/ceres/monetisation/admob/formats/AdCache;
public final fun cacheAd (Ljava/lang/String;Ldev/teogor/ceres/monetisation/admob/formats/CacheAdModel;)V
public final fun getAd (Ljava/lang/String;)Ldev/teogor/ceres/monetisation/admob/formats/CacheAdModel;
public final fun cacheAd (Ljava/lang/String;Ldev/teogor/ceres/monetisation/admob/formats/CachedAd;)V
public final fun getAd (Ljava/lang/String;)Ldev/teogor/ceres/monetisation/admob/formats/CachedAd;
public final fun getAdCount (Ljava/lang/String;)I
public final fun getAds (Ljava/lang/String;I)Ljava/util/List;
public final fun removeAd (Ljava/lang/String;)V
Expand Down Expand Up @@ -240,75 +240,75 @@ public abstract class dev/teogor/ceres/monetisation/admob/formats/BannerAd : dev
public fun useCache ()Z
}

public abstract class dev/teogor/ceres/monetisation/admob/formats/CacheAdModel {
public abstract class dev/teogor/ceres/monetisation/admob/formats/CachedAd {
public static final field $stable I
public synthetic fun <init> (JLkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun getLoadTime ()J
}

public final class dev/teogor/ceres/monetisation/admob/formats/CacheAdModel$AppOpen : dev/teogor/ceres/monetisation/admob/formats/CacheAdModel {
public final class dev/teogor/ceres/monetisation/admob/formats/CachedAd$AppOpen : dev/teogor/ceres/monetisation/admob/formats/CachedAd {
public static final field $stable I
public fun <init> (Lcom/google/android/gms/ads/appopen/AppOpenAd;J)V
public final fun component1 ()Lcom/google/android/gms/ads/appopen/AppOpenAd;
public final fun component2 ()J
public final fun copy (Lcom/google/android/gms/ads/appopen/AppOpenAd;J)Ldev/teogor/ceres/monetisation/admob/formats/CacheAdModel$AppOpen;
public static synthetic fun copy$default (Ldev/teogor/ceres/monetisation/admob/formats/CacheAdModel$AppOpen;Lcom/google/android/gms/ads/appopen/AppOpenAd;JILjava/lang/Object;)Ldev/teogor/ceres/monetisation/admob/formats/CacheAdModel$AppOpen;
public final fun copy (Lcom/google/android/gms/ads/appopen/AppOpenAd;J)Ldev/teogor/ceres/monetisation/admob/formats/CachedAd$AppOpen;
public static synthetic fun copy$default (Ldev/teogor/ceres/monetisation/admob/formats/CachedAd$AppOpen;Lcom/google/android/gms/ads/appopen/AppOpenAd;JILjava/lang/Object;)Ldev/teogor/ceres/monetisation/admob/formats/CachedAd$AppOpen;
public fun equals (Ljava/lang/Object;)Z
public final fun getAd ()Lcom/google/android/gms/ads/appopen/AppOpenAd;
public fun getLoadTime ()J
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}

public final class dev/teogor/ceres/monetisation/admob/formats/CacheAdModel$Interstitial : dev/teogor/ceres/monetisation/admob/formats/CacheAdModel {
public final class dev/teogor/ceres/monetisation/admob/formats/CachedAd$Interstitial : dev/teogor/ceres/monetisation/admob/formats/CachedAd {
public static final field $stable I
public fun <init> (Lcom/google/android/gms/ads/interstitial/InterstitialAd;J)V
public final fun component1 ()Lcom/google/android/gms/ads/interstitial/InterstitialAd;
public final fun component2 ()J
public final fun copy (Lcom/google/android/gms/ads/interstitial/InterstitialAd;J)Ldev/teogor/ceres/monetisation/admob/formats/CacheAdModel$Interstitial;
public static synthetic fun copy$default (Ldev/teogor/ceres/monetisation/admob/formats/CacheAdModel$Interstitial;Lcom/google/android/gms/ads/interstitial/InterstitialAd;JILjava/lang/Object;)Ldev/teogor/ceres/monetisation/admob/formats/CacheAdModel$Interstitial;
public final fun copy (Lcom/google/android/gms/ads/interstitial/InterstitialAd;J)Ldev/teogor/ceres/monetisation/admob/formats/CachedAd$Interstitial;
public static synthetic fun copy$default (Ldev/teogor/ceres/monetisation/admob/formats/CachedAd$Interstitial;Lcom/google/android/gms/ads/interstitial/InterstitialAd;JILjava/lang/Object;)Ldev/teogor/ceres/monetisation/admob/formats/CachedAd$Interstitial;
public fun equals (Ljava/lang/Object;)Z
public final fun getAd ()Lcom/google/android/gms/ads/interstitial/InterstitialAd;
public fun getLoadTime ()J
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}

public final class dev/teogor/ceres/monetisation/admob/formats/CacheAdModel$Native : dev/teogor/ceres/monetisation/admob/formats/CacheAdModel {
public final class dev/teogor/ceres/monetisation/admob/formats/CachedAd$Native : dev/teogor/ceres/monetisation/admob/formats/CachedAd {
public static final field $stable I
public fun <init> (Lcom/google/android/gms/ads/nativead/NativeAd;J)V
public final fun component1 ()Lcom/google/android/gms/ads/nativead/NativeAd;
public final fun component2 ()J
public final fun copy (Lcom/google/android/gms/ads/nativead/NativeAd;J)Ldev/teogor/ceres/monetisation/admob/formats/CacheAdModel$Native;
public static synthetic fun copy$default (Ldev/teogor/ceres/monetisation/admob/formats/CacheAdModel$Native;Lcom/google/android/gms/ads/nativead/NativeAd;JILjava/lang/Object;)Ldev/teogor/ceres/monetisation/admob/formats/CacheAdModel$Native;
public final fun copy (Lcom/google/android/gms/ads/nativead/NativeAd;J)Ldev/teogor/ceres/monetisation/admob/formats/CachedAd$Native;
public static synthetic fun copy$default (Ldev/teogor/ceres/monetisation/admob/formats/CachedAd$Native;Lcom/google/android/gms/ads/nativead/NativeAd;JILjava/lang/Object;)Ldev/teogor/ceres/monetisation/admob/formats/CachedAd$Native;
public fun equals (Ljava/lang/Object;)Z
public final fun getAd ()Lcom/google/android/gms/ads/nativead/NativeAd;
public fun getLoadTime ()J
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}

public final class dev/teogor/ceres/monetisation/admob/formats/CacheAdModel$Rewarded : dev/teogor/ceres/monetisation/admob/formats/CacheAdModel {
public final class dev/teogor/ceres/monetisation/admob/formats/CachedAd$Rewarded : dev/teogor/ceres/monetisation/admob/formats/CachedAd {
public static final field $stable I
public fun <init> (Lcom/google/android/gms/ads/rewarded/RewardedAd;J)V
public final fun component1 ()Lcom/google/android/gms/ads/rewarded/RewardedAd;
public final fun component2 ()J
public final fun copy (Lcom/google/android/gms/ads/rewarded/RewardedAd;J)Ldev/teogor/ceres/monetisation/admob/formats/CacheAdModel$Rewarded;
public static synthetic fun copy$default (Ldev/teogor/ceres/monetisation/admob/formats/CacheAdModel$Rewarded;Lcom/google/android/gms/ads/rewarded/RewardedAd;JILjava/lang/Object;)Ldev/teogor/ceres/monetisation/admob/formats/CacheAdModel$Rewarded;
public final fun copy (Lcom/google/android/gms/ads/rewarded/RewardedAd;J)Ldev/teogor/ceres/monetisation/admob/formats/CachedAd$Rewarded;
public static synthetic fun copy$default (Ldev/teogor/ceres/monetisation/admob/formats/CachedAd$Rewarded;Lcom/google/android/gms/ads/rewarded/RewardedAd;JILjava/lang/Object;)Ldev/teogor/ceres/monetisation/admob/formats/CachedAd$Rewarded;
public fun equals (Ljava/lang/Object;)Z
public final fun getAd ()Lcom/google/android/gms/ads/rewarded/RewardedAd;
public fun getLoadTime ()J
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}

public final class dev/teogor/ceres/monetisation/admob/formats/CacheAdModel$RewardedInterstitial : dev/teogor/ceres/monetisation/admob/formats/CacheAdModel {
public final class dev/teogor/ceres/monetisation/admob/formats/CachedAd$RewardedInterstitial : dev/teogor/ceres/monetisation/admob/formats/CachedAd {
public static final field $stable I
public fun <init> (Lcom/google/android/gms/ads/rewardedinterstitial/RewardedInterstitialAd;J)V
public final fun component1 ()Lcom/google/android/gms/ads/rewardedinterstitial/RewardedInterstitialAd;
public final fun component2 ()J
public final fun copy (Lcom/google/android/gms/ads/rewardedinterstitial/RewardedInterstitialAd;J)Ldev/teogor/ceres/monetisation/admob/formats/CacheAdModel$RewardedInterstitial;
public static synthetic fun copy$default (Ldev/teogor/ceres/monetisation/admob/formats/CacheAdModel$RewardedInterstitial;Lcom/google/android/gms/ads/rewardedinterstitial/RewardedInterstitialAd;JILjava/lang/Object;)Ldev/teogor/ceres/monetisation/admob/formats/CacheAdModel$RewardedInterstitial;
public final fun copy (Lcom/google/android/gms/ads/rewardedinterstitial/RewardedInterstitialAd;J)Ldev/teogor/ceres/monetisation/admob/formats/CachedAd$RewardedInterstitial;
public static synthetic fun copy$default (Ldev/teogor/ceres/monetisation/admob/formats/CachedAd$RewardedInterstitial;Lcom/google/android/gms/ads/rewardedinterstitial/RewardedInterstitialAd;JILjava/lang/Object;)Ldev/teogor/ceres/monetisation/admob/formats/CachedAd$RewardedInterstitial;
public fun equals (Ljava/lang/Object;)Z
public final fun getAd ()Lcom/google/android/gms/ads/rewardedinterstitial/RewardedInterstitialAd;
public fun getLoadTime ()J
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,39 @@
package dev.teogor.ceres.monetisation.admob.formats

object AdCache {
private val adCache: MutableMap<String, MutableList<CacheAdModel>> = mutableMapOf()
private val cachedAds: MutableMap<String, MutableList<CachedAd>> = mutableMapOf()

@Synchronized
fun cacheAd(adId: String, ad: CacheAdModel) {
adCache.getOrPut(adId) {
fun cacheAd(adId: String, ad: CachedAd) {
cachedAds.getOrPut(adId) {
mutableListOf()
}.apply {
add(ad)
}
}

@Synchronized
fun getAd(adId: String): CacheAdModel? {
return adCache[adId]?.firstOrNull()
fun getAd(adId: String): CachedAd? {
return cachedAds[adId]?.firstOrNull()
}

@Synchronized
fun getAds(adId: String, count: Int): List<CacheAdModel> {
return adCache[adId]?.take(count) ?: emptyList()
fun getAds(adId: String, count: Int): List<CachedAd> {
return cachedAds[adId]?.take(count) ?: emptyList()
}

@Synchronized
fun removeAd(adId: String) {
adCache[adId]?.let { adList ->
cachedAds[adId]?.let { adList ->
if (adList.isNotEmpty()) {
adList.removeAt(0)
}
if (adList.isEmpty()) {
adCache.remove(adId)
cachedAds.remove(adId)
}
}
}

@Synchronized
fun getAdCount(adId: String) = adCache[adId]?.size ?: 0
fun getAdCount(adId: String) = cachedAds[adId]?.size ?: 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ abstract class AppOpenAd : Ad() {
log("onAdLoaded.")
AdCache.cacheAd(
adId = id,
ad = CacheAdModel.AppOpen(ad, Date().time),
ad = CachedAd.AppOpen(ad, Date().time),
)
onListener(AdEvent.AdLoaded)
}
Expand Down Expand Up @@ -92,12 +92,11 @@ abstract class AppOpenAd : Ad() {
return
}

val appOpenAd = (ad as CacheAdModel.AppOpen).ad
val appOpenAd = (ad as CachedAd.AppOpen).ad

if (!wasLoadTimeLessThanNHoursAgo(ad.loadTime, 4)) {
reloadExpiredAd()
return
} else {
log("App open ad is still valid; no need to reload.")
}

if (isShowing) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAd
*
* @property loadTime The time when the ad was loaded.
*/
sealed class CacheAdModel(
sealed class CachedAd(
open val loadTime: Long,
) {
/**
Expand All @@ -39,7 +39,7 @@ sealed class CacheAdModel(
data class AppOpen(
val ad: AppOpenAd,
override val loadTime: Long,
) : CacheAdModel(loadTime)
) : CachedAd(loadTime)

/**
* Data class representing a cached Interstitial ad.
Expand All @@ -50,7 +50,7 @@ sealed class CacheAdModel(
data class Interstitial(
val ad: InterstitialAd,
override val loadTime: Long,
) : CacheAdModel(loadTime)
) : CachedAd(loadTime)

/**
* Data class representing a cached Rewarded Interstitial ad.
Expand All @@ -61,7 +61,7 @@ sealed class CacheAdModel(
data class RewardedInterstitial(
val ad: RewardedInterstitialAd,
override val loadTime: Long,
) : CacheAdModel(loadTime)
) : CachedAd(loadTime)

/**
* Data class representing a cached Rewarded ad.
Expand All @@ -72,7 +72,7 @@ sealed class CacheAdModel(
data class Rewarded(
val ad: RewardedAd,
override val loadTime: Long,
) : CacheAdModel(loadTime)
) : CachedAd(loadTime)

/**
* Data class representing a cached Native ad.
Expand All @@ -83,5 +83,5 @@ sealed class CacheAdModel(
data class Native(
val ad: NativeAd,
override val loadTime: Long,
) : CacheAdModel(loadTime)
) : CachedAd(loadTime)
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ abstract class InterstitialAd(
log("Ad was loaded.")
AdCache.cacheAd(
adId = id,
ad = CacheAdModel.Interstitial(ad, Date().time),
ad = CachedAd.Interstitial(ad, Date().time),
)
onListener(AdEvent.AdLoaded)
}
Expand All @@ -84,7 +84,7 @@ abstract class InterstitialAd(
load()
return
}
val interstitialAd = (ad as CacheAdModel.Interstitial).ad
val interstitialAd = (ad as CachedAd.Interstitial).ad
if (isShowing) {
log("The interstitial ad is already showing.")
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ abstract class RewardedAd(
log("Ad was loaded.")
AdCache.cacheAd(
adId = id,
ad = CacheAdModel.Rewarded(ad, Date().time),
ad = CachedAd.Rewarded(ad, Date().time),
)
onListener(AdEvent.AdLoaded)
}
Expand All @@ -84,7 +84,7 @@ abstract class RewardedAd(
load()
return
}
val rewardedAd = (ad as CacheAdModel.Rewarded).ad
val rewardedAd = (ad as CachedAd.Rewarded).ad
if (isShowing) {
log("The rewarded interstitial ad is already showing.")
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ abstract class RewardedInterstitialAd(
log("Ad was loaded.")
AdCache.cacheAd(
adId = id,
ad = CacheAdModel.RewardedInterstitial(ad, Date().time),
ad = CachedAd.RewardedInterstitial(ad, Date().time),
)
onListener(AdEvent.AdLoaded)
}
Expand All @@ -84,7 +84,7 @@ abstract class RewardedInterstitialAd(
load()
return
}
val rewardedInterstitialAd = (ad as CacheAdModel.RewardedInterstitial).ad
val rewardedInterstitialAd = (ad as CachedAd.RewardedInterstitial).ad
if (isShowing) {
log("The rewarded interstitial ad is already showing.")
return
Expand Down

0 comments on commit b0cabcb

Please sign in to comment.