Combining two listeners kotlin coroutines

牧云@^-^@ 提交于 2020-06-29 04:08:30

问题


I have created a library, in it I have a room database with one Table OfflineData. I listen to new rows inserted in the OfflineData table. I also listen to changes happening in the network connection.

I want to combine them both. what I want to listen to a function which returns list of from the table when

  1. when there is new row inserted and has internet connection
  2. whet the connection comes back on and there is data in the OfflineData table.

OfflineDatabaseManager - I have getOfflineData() and networkConnectionActivated which listen to changes in the database and the network, I want to combine them both to full the above two points.

I am not sure how to do that, Really appreciate your suggestions

class OfflineDatabaseManager private constructor(private val dp: LibraryDatabase) {

    //LISTENS TO CHANGES IN THE DATABASE
    fun getOfflineData() : Flow<List<OfflineData>> {
        return dp.getOfflineDataDao().getOfflineData()
    }

    suspend fun insertOfflineData(offlineData: OfflineData) {
        dp.getOfflineDataDao().insertOfflineData(offlineData)
    }

    //LISTENS TO CHANGES IN THE NETWORK
    val _networkConnectionActivated = MutableStateFlow(false)
    val networkConnectionActivated: StateFlow<Boolean>
        get() = _networkConnectionActivated

    companion object {
        @Volatile
        private var INSTANCE: OfflineDatabaseManager? = null

        fun getInstance(context: Context): OfflineDatabaseManager {
            return INSTANCE ?: synchronized(this) {
                INSTANCE ?: run {
                    val db = Room.databaseBuilder(
                        context,
                        LibraryDatabase::class.java, "database-name"
                    ).build()
                    OfflineDatabaseManager(db).also { INSTANCE = it }
                }
            }
        }
    }
}

ConnectionChangeReceiver - listens to network changes

class ConnectionChangeReceiver: BroadcastReceiver() {

    override fun onReceive(context: Context, p1: Intent?) {
        OfflineDatabaseManager.getInstance(context)._networkConnectionActivated.value = isNetworkConnectionActive(context)

    }

    fun isNetworkConnectionActive(context: Context?): Boolean {
        val connectivityManager: ConnectivityManager = context?.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val nw      = connectivityManager.activeNetwork ?: return false
            val actNw = connectivityManager.getNetworkCapabilities(nw) ?: return false
            return when {
                actNw.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
                actNw.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true
                else -> false
            }
        } else {
            val nwInfo = connectivityManager.activeNetworkInfo ?: return false
            return nwInfo.isConnected
        }
    }
}

Just for info I listen to the above changes seperately from outside the library like this

fun getOfflineData() {
    launch {
        OfflineDatabaseManager.getInstance(app.applicationContext).getOfflineData().collect {
            Timber.d( "observing offline data" + it.toString())
        }
    }
}

fun getIsNetworkAvailable() {
    launch {
        OfflineDatabaseManager.getInstance(app.applicationContext).networkConnectionActivated
            .collect {  isNetworkConnectionActive ->
                Timber.d("I am a suspend fun observing net changes")
            }
    }
}

Thanks R

来源:https://stackoverflow.com/questions/62351191/combining-two-listeners-kotlin-coroutines

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!