Fix unexpected error in support ad when no internet (#2030)

This commit is contained in:
Rafał Borcz 2022-11-02 16:44:05 +01:00 committed by GitHub
parent 62b7d42a73
commit 50b6d380b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 6 deletions

View File

@ -31,13 +31,22 @@ class AdsPresenter @Inject constructor(
view?.showLoadingSupportAd(true) view?.showLoadingSupportAd(true)
presenterScope.launch { presenterScope.launch {
runCatching { adsHelper.getSupportAd() } runCatching { adsHelper.getSupportAd() }
.onFailure(errorHandler::dispatch) .onFailure {
.onSuccess { it?.let { view?.showAd(it) } } errorHandler.dispatch(it)
view?.run { view?.run {
showLoadingSupportAd(false) showLoadingSupportAd(false)
showWatchAdOncePerVisit(true) showWatchAdOncePerVisit(false)
} }
}
.onSuccess {
it?.let { view?.showAd(it) }
view?.run {
showLoadingSupportAd(false)
showWatchAdOncePerVisit(true)
}
}
} }
} }

View File

@ -1,8 +1,12 @@
package io.github.wulkanowy.utils package io.github.wulkanowy.utils
import android.content.Context import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.view.View import android.view.View
import androidx.core.content.getSystemService
import com.google.ads.mediation.admob.AdMobAdapter import com.google.ads.mediation.admob.AdMobAdapter
import com.google.android.gms.ads.* import com.google.android.gms.ads.*
import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAd import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAd
@ -10,6 +14,7 @@ import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAdLoa
import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.android.qualifiers.ApplicationContext
import io.github.wulkanowy.BuildConfig import io.github.wulkanowy.BuildConfig
import io.github.wulkanowy.data.repositories.PreferencesRepository import io.github.wulkanowy.data.repositories.PreferencesRepository
import java.net.UnknownHostException
import javax.inject.Inject import javax.inject.Inject
import kotlin.coroutines.resume import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException import kotlin.coroutines.resumeWithException
@ -28,6 +33,10 @@ class AdsHelper @Inject constructor(
} }
suspend fun getSupportAd(): RewardedInterstitialAd? { suspend fun getSupportAd(): RewardedInterstitialAd? {
if (!context.isInternetConnected()) {
throw UnknownHostException()
}
val extra = Bundle().apply { putString("npa", "1") } val extra = Bundle().apply { putString("npa", "1") }
val adRequest = AdRequest.Builder() val adRequest = AdRequest.Builder()
.apply { .apply {
@ -84,4 +93,18 @@ class AdsHelper @Inject constructor(
} }
} }
@Suppress("DEPRECATION")
private fun Context.isInternetConnected(): Boolean {
val connectivityManager = getSystemService<ConnectivityManager>() ?: return false
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val currentNetwork = connectivityManager.activeNetwork
val networkCapabilities = connectivityManager.getNetworkCapabilities(currentNetwork)
networkCapabilities?.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED) == true
} else {
connectivityManager.activeNetworkInfo?.isConnected == true
}
}
data class AdBanner(val view: View) data class AdBanner(val view: View)