refactor: ensure NetworkRepository flows on IO thread

This commit is contained in:
andrekir 2024-11-19 10:53:27 -03:00
parent c70b0d512a
commit 4855576248
2 changed files with 17 additions and 7 deletions

View file

@ -2,21 +2,31 @@ package com.geeksville.mesh.repository.network
import android.net.ConnectivityManager
import android.net.nsd.NsdManager
import android.net.nsd.NsdServiceInfo
import com.geeksville.mesh.CoroutineDispatchers
import com.geeksville.mesh.android.Logging
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.conflate
import kotlinx.coroutines.flow.flowOn
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class NetworkRepository @Inject constructor(
private val nsdManagerLazy: dagger.Lazy<NsdManager?>,
private val nsdManagerLazy: dagger.Lazy<NsdManager>,
private val connectivityManager: dagger.Lazy<ConnectivityManager>,
private val dispatchers: CoroutineDispatchers,
) : Logging {
val networkAvailable get() = connectivityManager.get().networkAvailable()
val networkAvailable: Flow<Boolean>
get() = connectivityManager.get().networkAvailable()
.flowOn(dispatchers.io)
.conflate()
val resolvedList
get() = nsdManagerLazy.get()?.serviceList(SERVICE_TYPES, SERVICE_NAME) ?: flowOf(emptyList())
val resolvedList: Flow<List<NsdServiceInfo>>
get() = nsdManagerLazy.get().serviceList(SERVICE_TYPES, SERVICE_NAME)
.flowOn(dispatchers.io)
.conflate()
companion object {
// To find all available services use SERVICE_TYPE = "_services._dns-sd._udp"

View file

@ -19,8 +19,8 @@ class NetworkRepositoryModule {
}
@Provides
fun provideNsdManager(application: Application): NsdManager? {
return application.getSystemService(Context.NSD_SERVICE) as NsdManager?
fun provideNsdManager(application: Application): NsdManager {
return application.getSystemService(Context.NSD_SERVICE) as NsdManager
}
}
}