Skip to content

Commit

Permalink
Merge pull request #28 from vipulyaara/develop
Browse files Browse the repository at this point in the history
Release 0.36.0
  • Loading branch information
vipulyaara authored Aug 14, 2024
2 parents a292f71 + 54e03dd commit badc22a
Show file tree
Hide file tree
Showing 28 changed files with 1,672 additions and 153 deletions.
2 changes: 1 addition & 1 deletion .idea/appInsightsSettings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ android {

defaultConfig {
applicationId = "com.kafka.user"
versionCode = 75
versionName = "0.35.0"
versionCode = 76
versionName = "0.36.0"

val properties = Properties()
properties.load(project.rootProject.file("local.properties").inputStream())
Expand Down
1 change: 1 addition & 0 deletions core/ads/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
35 changes: 35 additions & 0 deletions core/ads/build.gradle
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
}
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 core/ads/src/main/java/org/kafka/ads/admob/NativeAdInitializer.kt
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 core/ads/src/main/java/org/kafka/ads/admob/NativeAdProvider.kt
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 core/ads/src/main/java/org/kafka/ads/admob/NativeAdState.kt
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 core/ads/src/main/java/org/kafka/ads/admob/NativeAdView.kt
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) }
}
)
}
Loading

0 comments on commit badc22a

Please sign in to comment.