From f54bdf93d31f847f46bdf60922dfeedb90df953a Mon Sep 17 00:00:00 2001 From: davidoskky Date: Wed, 17 Aug 2022 19:56:24 +0200 Subject: [PATCH] Simplify network connectivity status check --- androidApp/build.gradle.kts | 3 +++ .../apps/readerforselfossv2/android/MyApp.kt | 4 +-- .../readerforselfossv2/utils/NetworkStatus.kt | 17 ------------ .../repository/RepositoryImpl.kt | 10 +++---- .../readerforselfossv2/utils/NetworkStatus.kt | 9 ------- .../readerforselfossv2/utils/NetworkStatus.kt | 27 ------------------- 6 files changed, 10 insertions(+), 60 deletions(-) delete mode 100644 shared/src/androidMain/kotlin/bou/amine/apps/readerforselfossv2/utils/NetworkStatus.kt delete mode 100644 shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/NetworkStatus.kt delete mode 100644 shared/src/iosMain/kotlin/bou/amine/apps/readerforselfossv2/utils/NetworkStatus.kt diff --git a/androidApp/build.gradle.kts b/androidApp/build.gradle.kts index e48be33..b37680b 100644 --- a/androidApp/build.gradle.kts +++ b/androidApp/build.gradle.kts @@ -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") } \ No newline at end of file diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/MyApp.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/MyApp.kt index a149342..70d94af 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/MyApp.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/MyApp.kt @@ -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() with singleton { Repository(instance(), instance(), NetworkStatus(applicationContext)) } + bind() with singleton { Repository(instance(), instance(), ConnectivityStatus(applicationContext)) } } private lateinit var config: Config diff --git a/shared/src/androidMain/kotlin/bou/amine/apps/readerforselfossv2/utils/NetworkStatus.kt b/shared/src/androidMain/kotlin/bou/amine/apps/readerforselfossv2/utils/NetworkStatus.kt deleted file mode 100644 index 03ba29e..0000000 --- a/shared/src/androidMain/kotlin/bou/amine/apps/readerforselfossv2/utils/NetworkStatus.kt +++ /dev/null @@ -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() - } -} \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt index 0944cbd..fc92c4e 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt @@ -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() - 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): Boolean { var success = false - if (isConnectionAvailable) { + if (isConnectionAvailable.value) { success = api.markAllAsRead(items.map { it.id.toString() })?.isSuccess == true } diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/NetworkStatus.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/NetworkStatus.kt deleted file mode 100644 index da7b9b9..0000000 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/NetworkStatus.kt +++ /dev/null @@ -1,9 +0,0 @@ -package bou.amine.apps.readerforselfossv2.utils - -import kotlinx.coroutines.flow.MutableStateFlow - -expect class NetworkStatus { - val current: MutableStateFlow - fun start() - fun stop() -} \ No newline at end of file diff --git a/shared/src/iosMain/kotlin/bou/amine/apps/readerforselfossv2/utils/NetworkStatus.kt b/shared/src/iosMain/kotlin/bou/amine/apps/readerforselfossv2/utils/NetworkStatus.kt deleted file mode 100644 index fbd45a0..0000000 --- a/shared/src/iosMain/kotlin/bou/amine/apps/readerforselfossv2/utils/NetworkStatus.kt +++ /dev/null @@ -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 = 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) - } - } - } -} \ No newline at end of file