Skip to content

Commit

Permalink
added ads
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed May 26, 2024
1 parent 07ee778 commit d32fc1b
Show file tree
Hide file tree
Showing 21 changed files with 812 additions and 86 deletions.
2 changes: 2 additions & 0 deletions composeApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,11 @@ kotlin {
implementation(libs.android)
implementation(libs.activity)
implementation(libs.activity.compose)
implementation(libs.ads)
implementation(libs.appcompat)
implementation(libs.multidex)
implementation(libs.splashscreen)
implementation(libs.ump)
implementation(libs.html.converter)

implementation(libs.ktor.jvm)
Expand Down
14 changes: 14 additions & 0 deletions composeApp/src/androidMain/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="dev.datlag.burningseries.READ_DATABASE" />
<uses-permission android:name="com.google.android.gms.permission.AD_ID" />

<queries>
<package android:name="dev.datlag.burningseries" />
Expand Down Expand Up @@ -251,5 +252,18 @@
<meta-data
android:name="open_source_license"
android:value="GNU AFFERO GENERAL PUBLIC LICENSE Version 3" />

<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713" />

<property
android:name="android.adservices.AD_SERVICES_CONFIG"
android:resource="@xml/gma_ad_services_config"
tools:replace="android:resource" />

<meta-data
android:name="com.google.android.gms.ads.DELAY_APP_MEASUREMENT_INIT"
android:value="true"/>
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class App : MultiDexApplication(), DIAware {
.detectAll()
.permitDiskReads()
.permitDiskWrites()
.permitCustomSlowCalls()
.penaltyLog()
.penaltyDialog()
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ import com.google.android.play.core.appupdate.AppUpdateManagerFactory
import com.google.android.play.core.appupdate.AppUpdateOptions
import com.google.android.play.core.install.model.AppUpdateType
import com.google.android.play.core.ktx.requestAppUpdateInfo
import dev.datlag.aniflow.other.ConsentInfo
import dev.datlag.aniflow.other.DomainVerifier
import dev.datlag.aniflow.other.LocalConsentInfo
import dev.datlag.aniflow.other.UpdateManager
import dev.datlag.aniflow.other.UserHelper
import dev.datlag.aniflow.ui.navigation.RootComponent
Expand Down Expand Up @@ -65,10 +67,13 @@ class MainActivity : AppCompatActivity() {
manager.startUpdateFlow(info, this, AppUpdateOptions.defaultOptions(type))
}

val consentInfo = ConsentInfo(this)

setContent {
CompositionLocalProvider(
LocalLifecycleOwner provides lifecycleOwner,
LocalEdgeToEdge provides true
LocalEdgeToEdge provides true,
LocalConsentInfo provides consentInfo
) {
App(
di = di
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package dev.datlag.aniflow.other

import android.app.Activity
import com.google.android.gms.ads.MobileAds
import com.google.android.ump.ConsentInformation
import com.google.android.ump.ConsentRequestParameters
import com.google.android.ump.UserMessagingPlatform
import java.util.concurrent.atomic.AtomicBoolean

actual class ConsentInfo(private val activity: Activity) {

private var isMobileAdsInitialized = AtomicBoolean(false)

private val consentInformation = UserMessagingPlatform.getConsentInformation(activity)
private val params = ConsentRequestParameters.Builder().build()

actual val privacy: Boolean
get() = consentInformation.privacyOptionsRequirementStatus == ConsentInformation.PrivacyOptionsRequirementStatus.REQUIRED

actual fun initialize() {
consentInformation.requestConsentInfoUpdate(
activity,
params,
{
// success
UserMessagingPlatform.loadAndShowConsentFormIfRequired(activity) {
// dismiss
initializeMobileAds()
}
},
{
// error
initializeMobileAds(true)
}
)
}

actual fun reset() {
consentInformation.reset()
}

actual fun showPrivacyForm() {
UserMessagingPlatform.showPrivacyOptionsForm(activity) {
// dismiss
initializeMobileAds()
}
}

private fun initializeMobileAds(force: Boolean = false) {
if (consentInformation.canRequestAds() || force) {
if (isMobileAdsInitialized.get()) {
return
}

MobileAds.initialize(activity)
isMobileAdsInitialized.set(true)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package dev.datlag.aniflow.ui.custom

import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.viewinterop.AndroidView
import com.google.android.gms.ads.AdListener
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.AdSize
import dev.datlag.aniflow.BuildKonfig
import dev.datlag.aniflow.Sekret
import dev.datlag.aniflow.other.StateSaver
import com.google.android.gms.ads.AdView
import com.google.android.gms.ads.LoadAdError
import com.google.android.gms.ads.VideoOptions
import com.google.android.gms.ads.nativead.NativeAdOptions
import dev.datlag.tooling.decompose.lifecycle.collectAsStateWithLifecycle
import io.github.aakira.napier.Napier

@Composable
actual fun AdView(id: String, type: AdType) {
val nativeAdState = rememberCustomNativeAdState(
adUnit = id,
nativeAdOptions = NativeAdOptions.Builder()
.setVideoOptions(
VideoOptions.Builder()
.setStartMuted(true).setClickToExpandRequested(true)
.build()
).setRequestMultipleImages(true)
.build(),
adListener = object : AdListener() {
override fun onAdFailedToLoad(p0: LoadAdError) {
super.onAdFailedToLoad(p0)
Napier.e(p0.message)
}
}
)

val nativeAd by nativeAdState.nativeAd.collectAsStateWithLifecycle()

nativeAd?.let { NativeAdCard(it, Modifier.fillMaxWidth()) }
}

actual object Ads {
actual fun native(): String? {
return if (StateSaver.sekretLibraryLoaded) {
Sekret.androidAdNative(BuildKonfig.packageName)
} else {
null
}
}

actual fun banner(): String? {
return if (StateSaver.sekretLibraryLoaded) {
Sekret.androidAdBanner(BuildKonfig.packageName)
} else {
null
}
}
}

@Composable
actual fun BannerAd(id: String, modifier: Modifier) {
AndroidView(
factory = { context ->
AdView(context).apply {
adUnitId = id
setAdSize(AdSize.BANNER)
loadAd(AdRequest.Builder().build())
}
},
modifier = modifier
)
}
Loading

0 comments on commit d32fc1b

Please sign in to comment.