Updated on 2026-08-14
This commit is contained in:
parent
50ecb380a1
commit
6af64f8a33
6 changed files with 31 additions and 64 deletions
|
|
@ -50,7 +50,6 @@ dependencies {
|
|||
implementation(deps.okHttp.prettyLogging)
|
||||
implementation(deps.retrofit)
|
||||
implementation(deps.retrofit.moshi)
|
||||
implementation(deps.reactive.network)
|
||||
|
||||
/** Time */
|
||||
implementation(deps.jodatime)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,26 @@
|
|||
package com.tangem.datasource.connection
|
||||
|
||||
import android.content.Context
|
||||
import android.net.ConnectivityManager
|
||||
import android.net.Network
|
||||
import android.net.NetworkCapabilities
|
||||
import androidx.core.content.ContextCompat
|
||||
|
||||
internal class AndroidNetworkConnectionManager(
|
||||
private val applicationContext: Context,
|
||||
) : NetworkConnectionManager {
|
||||
|
||||
override val isOnline: Boolean
|
||||
get() = isNetworkAvailable()
|
||||
|
||||
private fun isNetworkAvailable(): Boolean {
|
||||
val connectivityManager = ContextCompat.getSystemService(applicationContext, ConnectivityManager::class.java)
|
||||
|
||||
// Network that is currently in use (several networks can be presented at the same time)
|
||||
val defaultNetwork: Network = connectivityManager?.activeNetwork ?: return false
|
||||
val networkCapabilities: NetworkCapabilities =
|
||||
connectivityManager.getNetworkCapabilities(defaultNetwork) ?: return false
|
||||
|
||||
return networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,13 +1,8 @@
|
|||
package com.tangem.datasource.connection
|
||||
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
|
||||
/** Network connection manager */
|
||||
interface NetworkConnectionManager {
|
||||
|
||||
/** Connection status */
|
||||
val isOnline: Boolean
|
||||
|
||||
/** Connection status flow */
|
||||
val isOnlineFlow: StateFlow<Boolean>
|
||||
}
|
||||
|
|
@ -1,53 +0,0 @@
|
|||
package com.tangem.datasource.connection
|
||||
|
||||
import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork
|
||||
import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingSettings
|
||||
import com.tangem.utils.coroutines.FeatureCoroutineExceptionHandler
|
||||
import io.reactivex.BackpressureStrategy
|
||||
import io.reactivex.schedulers.Schedulers
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
import kotlinx.coroutines.reactive.asFlow
|
||||
|
||||
private const val PING_SERVER = "https://clients3.google.com/generate_204"
|
||||
private const val PING_INTERVAL = 5_000
|
||||
|
||||
internal class RealInternetConnectionManager : NetworkConnectionManager {
|
||||
|
||||
private val scope =
|
||||
CoroutineScope(
|
||||
SupervisorJob() + Dispatchers.IO + FeatureCoroutineExceptionHandler.create("RealInternetConnectionManager"),
|
||||
)
|
||||
|
||||
private val initialNetworkResult: Boolean by lazy {
|
||||
ReactiveNetwork
|
||||
.checkInternetConnectivity(
|
||||
InternetObservingSettings.builder()
|
||||
.host(PING_SERVER)
|
||||
.port(443)
|
||||
.build(),
|
||||
)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(Schedulers.io())
|
||||
.blockingGet()
|
||||
}
|
||||
|
||||
override val isOnlineFlow: StateFlow<Boolean> = ReactiveNetwork
|
||||
.observeInternetConnectivity(
|
||||
InternetObservingSettings.builder()
|
||||
.host(PING_SERVER)
|
||||
.port(443)
|
||||
.interval(PING_INTERVAL)
|
||||
.build(),
|
||||
)
|
||||
.toFlowable(BackpressureStrategy.LATEST)
|
||||
.asFlow()
|
||||
.stateIn(scope, SharingStarted.Lazily, initialValue = initialNetworkResult)
|
||||
|
||||
override val isOnline: Boolean
|
||||
get() = isOnlineFlow.value
|
||||
}
|
||||
|
|
@ -1,10 +1,12 @@
|
|||
package com.tangem.datasource.di
|
||||
|
||||
import android.content.Context
|
||||
import com.tangem.datasource.connection.AndroidNetworkConnectionManager
|
||||
import com.tangem.datasource.connection.NetworkConnectionManager
|
||||
import com.tangem.datasource.connection.RealInternetConnectionManager
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import javax.inject.Singleton
|
||||
|
||||
|
|
@ -14,7 +16,7 @@ internal class NetworkConnectionModule {
|
|||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideNetworkConnectionManager(): NetworkConnectionManager {
|
||||
return RealInternetConnectionManager()
|
||||
fun provideNetworkConnectionManager(@ApplicationContext applicationContext: Context): NetworkConnectionManager {
|
||||
return AndroidNetworkConnectionManager(applicationContext = applicationContext)
|
||||
}
|
||||
}
|
||||
|
|
@ -69,7 +69,6 @@ zxingQrCode = "3.5.1"
|
|||
mviCore = "1.3.1"
|
||||
kotlinSerialization = "1.4.1"
|
||||
arrow = "1.2.3"
|
||||
reactiveNetwork = "3.0.8"
|
||||
walletConnectCore = "1.18.0"
|
||||
walletConnectWeb3 = "1.11.0"
|
||||
prettyLogger = "2.2.0"
|
||||
|
|
@ -252,7 +251,6 @@ mviCore-watcher = { module = "com.github.badoo.mvicore:mvicore-diff", version.re
|
|||
kotlin-serialization = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinSerialization" }
|
||||
arrow-core = { module = "io.arrow-kt:arrow-core", version.ref = "arrow" }
|
||||
arrow-fx = { module = "io.arrow-kt:arrow-fx-coroutines", version.ref = "arrow" }
|
||||
reactive-network = { module = "com.github.pwittchen:reactivenetwork-rx2", version.ref = "reactiveNetwork" }
|
||||
walletConnectCore = { module = "com.walletconnect:android-core", version.ref = "walletConnectCore" }
|
||||
walletConnectWeb3 = { module = "com.walletconnect:web3wallet", version.ref = "walletConnectWeb3" }
|
||||
prettyLogger = { module = "com.orhanobut:logger", version.ref = "prettyLogger" }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue