-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from vipulyaara/develop
Release 0.36.0
- Loading branch information
Showing
28 changed files
with
1,672 additions
and
153 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
plugins { | ||
alias(libs.plugins.android.library) | ||
alias(libs.plugins.cacheFixPlugin) | ||
alias(libs.plugins.kotlin.android) | ||
alias(libs.plugins.kotlin.compose.compiler) | ||
alias(libs.plugins.kotlin.kapt) | ||
} | ||
|
||
kapt { | ||
correctErrorTypes = true | ||
useBuildCache = true | ||
} | ||
|
||
android { | ||
namespace 'org.kafka.core.ads' | ||
|
||
viewBinding { | ||
enabled = true | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation projects.base.domain | ||
implementation projects.core.analytics | ||
implementation projects.ui.common | ||
implementation projects.ui.components | ||
|
||
implementation libs.compose.ui.viewbinding | ||
|
||
implementation libs.google.admob | ||
|
||
implementation libs.hilt.android | ||
|
||
kapt libs.hilt.compiler | ||
} |
2 changes: 1 addition & 1 deletion
2
...emote-config/src/main/AndroidManifest.xml → core/ads/src/main/AndroidManifest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> | ||
|
||
<uses-permission android:name="android.permission.INTERNET" /> | ||
</manifest> |
28 changes: 28 additions & 0 deletions
28
core/ads/src/main/java/org/kafka/ads/admob/NativeAdInitializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.kafka.ads.admob | ||
|
||
import android.app.Application | ||
import android.util.Log | ||
import com.google.android.gms.ads.MobileAds | ||
import org.kafka.base.AppInitializer | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class NativeAdInitializer @Inject constructor( | ||
private val application: Application, | ||
) : AppInitializer { | ||
override fun init() { | ||
MobileAds.initialize(application) { initializationStatus -> | ||
val statusMap = initializationStatus.adapterStatusMap | ||
for (adapterClass in statusMap.keys) { | ||
val status = statusMap[adapterClass] | ||
Log.d( | ||
"MyApp", String.format( | ||
"Adapter name: %s, Description: %s, Latency: %d", | ||
adapterClass, status!!.description, status.latency | ||
) | ||
) | ||
} | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
core/ads/src/main/java/org/kafka/ads/admob/NativeAdProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package org.kafka.ads.admob | ||
|
||
import android.content.Context | ||
import com.google.android.gms.ads.AdListener | ||
import com.google.android.gms.ads.AdLoader | ||
import com.google.android.gms.ads.AdRequest | ||
import com.google.android.gms.ads.LoadAdError | ||
import com.google.android.gms.ads.nativead.NativeAd | ||
import com.google.android.gms.ads.nativead.NativeAdOptions | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import org.kafka.base.debug | ||
|
||
object NativeAdProvider { | ||
val nativeAdFlow = MutableStateFlow<NativeAd?>(null) | ||
|
||
fun loadAd(context: Context) { | ||
val adLoader = AdLoader.Builder(context, testAdId) | ||
.forNativeAd { ad: NativeAd -> | ||
// Show the ad. | ||
|
||
debug { "Admob loaded: $ad" } | ||
nativeAdFlow.value = ad | ||
// if (adLoader.isLoading) { | ||
// // The AdLoader is still loading ads. | ||
// // Expect more adLoaded or onAdFailedToLoad callbacks. | ||
// } else { | ||
// // The AdLoader has finished loading ads. | ||
// } | ||
|
||
// If this callback occurs after the activity is destroyed, you | ||
// must call destroy and return or you may get a memory leak. | ||
// Note `isDestroyed` is a method on Activity. | ||
// if (isDestroyed) { | ||
// nativeAd.destroy() | ||
// return@forNativeAd | ||
// } | ||
} | ||
.withAdListener(object : AdListener() { | ||
override fun onAdFailedToLoad(adError: LoadAdError) { | ||
// Handle the failure. | ||
debug { "Admob failed to load: $adError" } | ||
} | ||
}) | ||
.withNativeAdOptions( | ||
NativeAdOptions.Builder() | ||
// Methods in the NativeAdOptions.Builder class can be | ||
// used here to specify individual options settings. | ||
.build() | ||
) | ||
.build() | ||
|
||
adLoader.loadAd(AdRequest.Builder().build()) | ||
} | ||
|
||
// ad.destroy() | ||
} | ||
|
||
const val testAdId = "ca-app-pub-3940256099942544/2247696110" |
79 changes: 79 additions & 0 deletions
79
core/ads/src/main/java/org/kafka/ads/admob/NativeAdState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package org.kafka.ads.admob | ||
|
||
import android.content.Context | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import com.google.android.gms.ads.AdListener | ||
import com.google.android.gms.ads.AdLoader | ||
import com.google.android.gms.ads.AdRequest | ||
import com.google.android.gms.ads.LoadAdError | ||
import com.google.android.gms.ads.nativead.NativeAd | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.withContext | ||
|
||
@Composable | ||
fun rememberNativeAdState( | ||
context: Context, | ||
adUnitId: String, | ||
refreshInterval: Long = 60000L, | ||
): NativeAd? { | ||
var state by remember { | ||
mutableStateOf<NativeAd?>(null) | ||
} | ||
LaunchedEffect(Unit) { | ||
// Load the ad only if it's not already loaded | ||
if (state == null) { | ||
val adLoader = AdLoader.Builder(context, adUnitId) | ||
.forNativeAd { ad -> | ||
state = ad | ||
} | ||
.withAdListener(object : AdListener() { | ||
override fun onAdLoaded() { | ||
super.onAdLoaded() | ||
|
||
} | ||
|
||
override fun onAdFailedToLoad(error: LoadAdError) { | ||
super.onAdFailedToLoad(error) | ||
|
||
} | ||
}) | ||
.build() | ||
adLoader.loadAd(AdRequest.Builder().build()) | ||
} | ||
} | ||
if (refreshInterval > 0) { | ||
LaunchedEffect(Unit) { | ||
while (true) { | ||
delay(refreshInterval) | ||
state = null | ||
|
||
withContext(Dispatchers.IO) { | ||
val adLoader = AdLoader.Builder(context, adUnitId) | ||
.forNativeAd { ad -> | ||
state = ad | ||
} | ||
.withAdListener(object : AdListener() { | ||
override fun onAdLoaded() { | ||
super.onAdLoaded() | ||
|
||
} | ||
|
||
override fun onAdFailedToLoad(error: LoadAdError) { | ||
super.onAdFailedToLoad(error) | ||
|
||
} | ||
}) | ||
.build() | ||
adLoader.loadAd(AdRequest.Builder().build()) | ||
} | ||
} | ||
} | ||
} | ||
return state | ||
} |
39 changes: 39 additions & 0 deletions
39
core/ads/src/main/java/org/kafka/ads/admob/NativeAdView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package org.kafka.ads.admob | ||
|
||
import android.view.View | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableIntStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.platform.ComposeView | ||
import androidx.compose.ui.viewinterop.AndroidView | ||
import com.google.android.gms.ads.nativead.NativeAd | ||
import com.google.android.gms.ads.nativead.NativeAdView | ||
|
||
@Composable | ||
internal fun NativeAdView( | ||
ad: NativeAd, | ||
adContent: @Composable (ad: NativeAd, contentView: View) -> Unit, | ||
) { | ||
val contentViewId by remember { mutableIntStateOf(View.generateViewId()) } | ||
val adViewId by remember { mutableIntStateOf(View.generateViewId()) } | ||
AndroidView( | ||
factory = { context -> | ||
val contentView = ComposeView(context).apply { | ||
id = contentViewId | ||
} | ||
NativeAdView(context).apply { | ||
id = adViewId | ||
addView(contentView) | ||
} | ||
}, | ||
update = { view -> | ||
val adView = view.findViewById<NativeAdView>(adViewId) | ||
val contentView = view.findViewById<ComposeView>(contentViewId) | ||
|
||
adView.setNativeAd(ad) | ||
adView.callToActionView = contentView | ||
contentView.setContent { adContent(ad, contentView) } | ||
} | ||
) | ||
} |
Oops, something went wrong.