Skip to content

Commit

Permalink
Implement Network Monitor in Native Ads Composable
Browse files Browse the repository at this point in the history
  • Loading branch information
teogor committed Oct 27, 2023
1 parent 78dfe78 commit 2fd20e8
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 22 deletions.
15 changes: 0 additions & 15 deletions app/src/main/kotlin/dev/teogor/ceres/ads/HomeNativeAd.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand All @@ -43,7 +42,6 @@ import com.google.android.gms.ads.nativead.NativeAd
import com.skydoves.landscapist.ImageOptions
import com.skydoves.landscapist.glide.GlideImage
import dagger.hilt.android.lifecycle.HiltViewModel
import dev.teogor.ceres.core.foundation.compositions.LocalNetworkMonitor
import dev.teogor.ceres.monetisation.admob.DemoAdUnitIds
import dev.teogor.ceres.monetisation.admob.annotations.AdProperty
import dev.teogor.ceres.monetisation.admob.formats.nativead.NativeAd
Expand Down Expand Up @@ -146,19 +144,6 @@ class HomeNativeAdBeta : NativeAdManager() {
)
.build()

@Composable
override fun IsOnline(
displayAd: @Composable () -> Unit,
) {
val networkMonitor = LocalNetworkMonitor.current
val isOffline by remember {
derivedStateOf { networkMonitor.isOffline }
}
if (!isOffline) {
displayAd()
}
}

@Composable
override fun Display() {
IsOnline {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import dev.teogor.ceres.core.foundation.utils.getSystemService
import javax.inject.Inject
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.conflate
import javax.inject.Inject

private var Impl: NetworkConnectivityImpl? = null

Expand Down
2 changes: 1 addition & 1 deletion monetisation/admob/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ dependencies {

api(project(":monetisation:ads"))
implementation(project(":core:runtime"))
implementation(project(":core:network"))
implementation(project(":core:foundation"))
implementation(project(":ui:designsystem"))

implementation(libs.androidx.compose.foundation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import android.content.Context
import android.provider.Settings
import com.google.android.gms.ads.MobileAds
import com.google.android.gms.ads.RequestConfiguration
import dev.teogor.ceres.core.network.ConnectivityManagerNetworkMonitor
import dev.teogor.ceres.core.runtime.AppMetadataManager
import dev.teogor.ceres.monetisation.ads.AdsControl
import dev.teogor.ceres.monetisation.ads.AdsControlProvider
Expand All @@ -35,8 +34,6 @@ import java.security.NoSuchAlgorithmException

object AdMobInitializer {

private var connectivityManager: ConnectivityManagerNetworkMonitor? = null

fun configureAdsControl(adsControl: AdsControl) {
AdsControlProvider.initialize(adsControl)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
package dev.teogor.ceres.monetisation.admob.formats.nativead

import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import dev.teogor.ceres.core.foundation.compositions.LocalNetworkMonitor

/**
* Abstract class representing a Native Ad Manager.
Expand Down Expand Up @@ -55,8 +58,13 @@ abstract class NativeAdManager {
open fun IsOnline(
displayAd: @Composable () -> Unit,
) {
// TODO connectivity flow
displayAd()
val networkMonitor = LocalNetworkMonitor.current
val isOffline by remember {
derivedStateOf { networkMonitor.isOffline }
}
if (!isOffline) {
displayAd()
}
}
}

Expand Down

0 comments on commit 2fd20e8

Please sign in to comment.