-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
812 additions
and
86 deletions.
There are no files selected for viewing
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
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
59 changes: 59 additions & 0 deletions
59
composeApp/src/androidMain/kotlin/dev/datlag/aniflow/other/ConsentInfo.android.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,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) | ||
} | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
composeApp/src/androidMain/kotlin/dev/datlag/aniflow/ui/custom/AdView.android.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,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 | ||
) | ||
} |
Oops, something went wrong.