Hey I am new in jetpack compose. I am checking in ...
# compose
v
Hey I am new in jetpack compose. I am checking in internet is available or not and consume through live data. Now I started learning jetpack compose so I want to use Flow, So any guys help me to convert this LiveData to flow and use in jetpack compose.
NetworkConnection.kt
Copy code
import 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
Copy code
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. Thanks
a
there are two different ways of doing this depending on what you are trying to achieve. if this is meant to be used to represent state on your UI, there are extensions to use for it, called
observeAsState()
. It’s part of the “androidx.compose.runtimeruntime livedata1.3.0-alpha01” artifact. (https://developer.android.com/jetpack/androidx/releases/compose-runtime)
the other way is to use a
LaunchedEffect()
and observe the values there
v
So what's the difference between in
observeAsState()
&&
LaunchedEffect()
?
a
a
So what’s the difference between in
observeAsState()
&&
LaunchedEffect()
?
observeAsState()
will allow u to use the value on your UI, while a
LaunchedEffect
won’t.
v
Okk I got it, but what the use of
LaunchedEffect
when we cannot use the value ?
a
it’s nice to use for side effects. those don’t neccessarily change your UI state, but you can use it as a trigger for toasts and snackbars
z
The
*AsState
all use
LaunchedEffect
or one of its siblings internally. Take a look at the implementations, I think
collectAsState
might be the most straightforward.
v
Okk I'll take a look
Thank you guys 👍