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 10 additions and 60 deletions
Showing only changes of commit d4c2373bac - Show all commits

View File

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

Please refactor this

Please refactor this
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)
}
}
}
}