https://kotlinlang.org logo
#compose
Title
# compose
v

Vivek Modi

07/18/2022, 1:28 PM
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

Alex Styl

07/18/2022, 1:35 PM
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

Vivek Modi

07/18/2022, 1:46 PM
So what's the difference between in
observeAsState()
&&
LaunchedEffect()
?
a

Alejandro Rios

07/18/2022, 1:48 PM
a

Alex Styl

07/18/2022, 1:53 PM
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

Vivek Modi

07/18/2022, 1:56 PM
Okk I got it, but what the use of
LaunchedEffect
when we cannot use the value ?
a

Alex Styl

07/18/2022, 3:11 PM
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

Zach Klippenstein (he/him) [MOD]

07/18/2022, 3:51 PM
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

Vivek Modi

07/18/2022, 3:52 PM
Okk I'll take a look
Thank you guys 👍
70 Views