From 37fa4a1a8ea0c27b55f961cb6d1516251370803a Mon Sep 17 00:00:00 2001 From: davidoskky Date: Wed, 17 Aug 2022 14:51:00 +0200 Subject: [PATCH] Add multiplatform connectivity check --- .../apps/readerforselfossv2/android/MyApp.kt | 3 ++- shared/build.gradle.kts | 3 +++ .../readerforselfossv2/utils/NetworkStatus.kt | 17 ++++++++++++ .../repository/RepositoryImpl.kt | 5 +++- .../readerforselfossv2/utils/NetworkStatus.kt | 9 +++++++ .../readerforselfossv2/utils/NetworkStatus.kt | 27 +++++++++++++++++++ 6 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 shared/src/androidMain/kotlin/bou/amine/apps/readerforselfossv2/utils/NetworkStatus.kt create mode 100644 shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/NetworkStatus.kt create mode 100644 shared/src/iosMain/kotlin/bou/amine/apps/readerforselfossv2/utils/NetworkStatus.kt 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 a59b0d4..622017c 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,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 @@ -27,7 +28,7 @@ class MyApp : MultiDexApplication(), DIAware { override val di by DI.lazy { import(networkModule) - bind() with singleton { Repository(instance(), instance()) } + bind() with singleton { Repository(instance(), instance(), NetworkStatus(applicationContext)) } } private lateinit var config: Config diff --git a/shared/build.gradle.kts b/shared/build.gradle.kts index ec4c4b3..9a5992d 100644 --- a/shared/build.gradle.kts +++ b/shared/build.gradle.kts @@ -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 { 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 new file mode 100644 index 0000000..03ba29e --- /dev/null +++ b/shared/src/androidMain/kotlin/bou/amine/apps/readerforselfossv2/utils/NetworkStatus.kt @@ -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() + } +} \ 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 2dcaa46..97c83d2 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,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() + 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() 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 new file mode 100644 index 0000000..da7b9b9 --- /dev/null +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/NetworkStatus.kt @@ -0,0 +1,9 @@ +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 new file mode 100644 index 0000000..fbd45a0 --- /dev/null +++ b/shared/src/iosMain/kotlin/bou/amine/apps/readerforselfossv2/utils/NetworkStatus.kt @@ -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 = 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