Vivek Modi
07/18/2022, 1:28 PMVivek Modi
07/18/2022, 1:29 PMimport android.app.Application
import android.content.Context
import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkRequest
import androidx.lifecycle.LiveData
class NetworkConnection(private val connectivityManager: ConnectivityManager) : LiveData<Boolean>() {
constructor(application: Application) : this(application.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager)
private val networkCallback = object : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network) {
super.onAvailable(network)
postValue(true)
}
override fun onLost(network: Network) {
super.onLost(network)
postValue(false)
}
}
override fun onActive() {
super.onActive()
val builder = NetworkRequest.Builder()
connectivityManager.registerNetworkCallback(builder.build(), networkCallback)
}
override fun onInactive() {
super.onInactive()
connectivityManager.unregisterNetworkCallback(networkCallback)
}
}
Can someone help me which way of doing recommendations for kotlin flow in jetpack compose.
MainActivity.kt
class MainActivity : ComponentActivity() {
private lateinit var checkNetworkConnection: NetworkConnection
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
checkNetworkConnection = NetworkConnection(application)
setContent {
SportsResultTheme {
SetupView()
}
}
}
}
I am confused also how can I use flow in my compose in recommendation way. ThanksAlex Styl
07/18/2022, 1:35 PMobserveAsState()
. It’s part of the “androidx.compose.runtimeruntime livedata1.3.0-alpha01” artifact. (https://developer.android.com/jetpack/androidx/releases/compose-runtime)Alex Styl
07/18/2022, 1:35 PMLaunchedEffect()
and observe the values thereVivek Modi
07/18/2022, 1:46 PMobserveAsState()
&& LaunchedEffect()
?Alejandro Rios
07/18/2022, 1:48 PMAlex Styl
07/18/2022, 1:53 PMSo what’s the difference between in&&observeAsState()
?LaunchedEffect()
observeAsState()
will allow u to use the value on your UI, while a LaunchedEffect
won’t.Vivek Modi
07/18/2022, 1:56 PMLaunchedEffect
when we cannot use the value ?Alex Styl
07/18/2022, 3:11 PMZach Klippenstein (he/him) [MOD]
07/18/2022, 3:51 PM*AsState
all use LaunchedEffect
or one of its siblings internally. Take a look at the implementations, I think collectAsState
might be the most straightforward.Vivek Modi
07/18/2022, 3:52 PMVivek Modi
07/18/2022, 3:52 PM