Simplify network connectivity status check

This commit is contained in:
davidoskky 2022-08-17 19:56:24 +02:00
parent 2105044920
commit 5ab9db586d
6 changed files with 10 additions and 60 deletions

View File

@ -199,4 +199,7 @@ dependencies {
kapt("androidx.room:room-compiler:2.4.0-beta01")
implementation("android.arch.work:work-runtime-ktx:1.0.1")
// Network information
implementation("com.github.ln-12:multiplatform-connectivity-status:1.1.0")
}

View File

@ -13,10 +13,10 @@ 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
import com.github.`ln-12`.library.ConnectivityStatus
import com.mikepenz.materialdrawer.util.AbstractDrawerImageLoader
import com.mikepenz.materialdrawer.util.DrawerImageLoader
import com.russhwolf.settings.Settings
@ -26,7 +26,7 @@ class MyApp : MultiDexApplication(), DIAware {
override val di by DI.lazy {
import(networkModule)
bind<Repository>() with singleton { Repository(instance(), instance(), NetworkStatus(applicationContext)) }
bind<Repository>() with singleton { Repository(instance(), instance(), ConnectivityStatus(applicationContext)) }
}
private lateinit var config: Config

View File

@ -1,17 +0,0 @@
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,18 +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.github.`ln-12`.library.ConnectivityStatus
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, networkStatus: NetworkStatus) {
class Repository(private val api: SelfossApi, private val apiDetails: ApiDetailsService, connectivityStatus: ConnectivityStatus) {
val settings = Settings()
var items = ArrayList<SelfossModel.Item>()
private val isConnectionAvailable = networkStatus.current
private val isConnectionAvailable = connectivityStatus.isNetworkConnected
var baseUrl = apiDetails.getBaseUrl()
lateinit var dateUtils: DateUtils
@ -38,7 +38,7 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
set(value) {field = if (value < 0) { 0 } else { value } }
init {
networkStatus.start()
connectivityStatus.start()
// TODO: Dispatchers.IO not available in KMM, an alternative solution should be found
CoroutineScope(Dispatchers.Main).launch {
updateApiVersion()
@ -240,7 +240,7 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
suspend fun markAllAsRead(items: ArrayList<SelfossModel.Item>): Boolean {
var success = false
if (isConnectionAvailable) {
if (isConnectionAvailable.value) {
success = api.markAllAsRead(items.map { it.id.toString() })?.isSuccess == true
}

View File

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

View File

@ -1,27 +0,0 @@
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)
}
}
}
}