network #28

Merged
AmineB merged 28 commits from davidoskky/ReaderForSelfoss-multiplatform:network into master 2022-08-22 19:01:16 +00:00
6 changed files with 62 additions and 2 deletions
Showing only changes of commit 37fa4a1a8e - Show all commits

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
@ -27,7 +28,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()
AmineB marked this conversation as resolved Outdated

networkStatus.stop should be called somewhere.

`networkStatus.stop` should be called somewhere.

I'm not really sure when it could be stopped, maybe on logout.

I'm not really sure when it could be stopped, maybe on logout.

This should be done when the app is sent to the background or closed.

This should be done when the app is sent to the background or closed.

Since the network state is checked by the repository, doing this when the app is sent to background would prevent background jobs.
Calling stop before the app is closed appears redundant to me and I'm not sure how to determine that the app is being closed and not just going in background.

Since the network state is checked by the repository, doing this when the app is sent to background would prevent background jobs. Calling stop before the app is closed appears redundant to me and I'm not sure how to determine that the app is being closed and not just going in background.

That´s why, in the background service, I asked to keep the network check as it was.

The background service should check, when executed, the network status.

Having things like so, let you stop the network status checker when the app is sent to the background.

That´s why, in the background service, I asked to keep the network check as it was. The background service should check, when executed, the network status. Having things like so, let you stop the network status checker when the app is sent to the background.
// 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)
}
}
}
}