Skip to content

Commit

Permalink
refactor: ensure NetworkRepository flows on IO thread
Browse files Browse the repository at this point in the history
  • Loading branch information
andrekir committed Nov 19, 2024
1 parent c70b0d5 commit 4855576
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

0 comments on commit 4855576

Please sign in to comment.