Add multiplatform connectivity check

This commit is contained in:
davidoskky 2022-08-17 14:51:00 +02:00 committed by davidoskky
parent 1392e2a571
commit e1e43adde4
6 changed files with 62 additions and 2 deletions

View File

@ -13,6 +13,7 @@ import bou.amine.apps.readerforselfossv2.DI.networkModule
import bou.amine.apps.readerforselfossv2.android.utils.Config
import bou.amine.apps.readerforselfossv2.android.utils.glide.loadMaybeBasicAuth
import bou.amine.apps.readerforselfossv2.repository.Repository
import bou.amine.apps.readerforselfossv2.utils.NetworkStatus
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.ftinc.scoop.Scoop
@ -25,7 +26,7 @@ class MyApp : MultiDexApplication(), DIAware {
override val di by DI.lazy {
import(networkModule)
bind<Repository>() with singleton { Repository(instance(), instance()) }
bind<Repository>() with singleton { Repository(instance(), instance(), NetworkStatus(applicationContext)) }
}
private lateinit var config: Config

View File

@ -36,6 +36,9 @@ kotlin {
//Logging
implementation("io.github.aakira:napier:2.6.1")
// Network information
implementation("com.github.ln-12:multiplatform-connectivity-status:1.1.0")
}
}
val commonTest by getting {

View File

@ -0,0 +1,17 @@
package bou.amine.apps.readerforselfossv2.utils
import android.content.Context
import com.github.`ln-12`.library.ConnectivityStatus
actual class NetworkStatus(context: Context) {
private val connectivityStatus = ConnectivityStatus(context)
actual val current = connectivityStatus.isNetworkConnected
actual fun start() {
connectivityStatus.start()
}
actual fun stop() {
connectivityStatus.stop()
}
}

View File

@ -5,16 +5,18 @@ import bou.amine.apps.readerforselfossv2.rest.SelfossModel
import bou.amine.apps.readerforselfossv2.service.ApiDetailsService
import bou.amine.apps.readerforselfossv2.utils.DateUtils
import bou.amine.apps.readerforselfossv2.utils.ItemType
import bou.amine.apps.readerforselfossv2.utils.NetworkStatus
import com.russhwolf.settings.Settings
import io.github.aakira.napier.Napier
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
class Repository(private val api: SelfossApi, private val apiDetails: ApiDetailsService) {
class Repository(private val api: SelfossApi, private val apiDetails: ApiDetailsService, networkStatus: NetworkStatus) {
val settings = Settings()
var items = ArrayList<SelfossModel.Item>()
private val isConnectionAvailable = networkStatus.current
var baseUrl = apiDetails.getBaseUrl()
lateinit var dateUtils: DateUtils
@ -36,6 +38,7 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
set(value) {field = if (value < 0) { 0 } else { value } }
init {
networkStatus.start()
// TODO: Dispatchers.IO not available in KMM, an alternative solution should be found
CoroutineScope(Dispatchers.Main).launch {
updateApiVersion()

View File

@ -0,0 +1,9 @@
package bou.amine.apps.readerforselfossv2.utils
import kotlinx.coroutines.flow.MutableStateFlow
expect class NetworkStatus {
val current: MutableStateFlow<Boolean>
fun start()
fun stop()
}

View File

@ -0,0 +1,27 @@
package bou.amine.apps.readerforselfossv2.utils
import com.github.`ln-12`.library.ConnectivityStatus
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch
actual class NetworkStatus {
private val connectivityStatus: ConnectivityStatus = ConnectivityStatus()
actual val current: MutableStateFlow<Boolean> = connectivityStatus.isNetworkConnected
actual fun start() {
connectivityStatus.start()
}
actual fun stop() {
connectivityStatus.stop()
}
fun getStatus(success: (Boolean) -> Unit) {
MainScope().launch {
connectivityStatus.isNetworkConnected.collect { status ->
success(status)
}
}
}
}