嘿,我是jetpack作曲新手。我正在检查互联网是否可用,并通过实时数据消费。现在我开始学习jetpack作曲,所以我想使用Flow,所以有人帮我把这个LiveData转换成Flow,并在jetpack作曲中使用。
NetworkConnection.kt
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)
}
}
有谁能帮我在jetpack compose中推荐kotlin流的方法吗?
MainActivity.kt
class MainActivity : ComponentActivity() {
private lateinit var checkNetworkConnection: NetworkConnection
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
checkNetworkConnection = NetworkConnection(application)
setContent {
SportsResultTheme {
SetupView()
}
}
}
}
我也很困惑如何在我的写作中使用流推荐方式。由于
NetworkConnection。kt→
class NetworkConnection(private val connectivityManager: ConnectivityManager) {
init {
registerNetworkCallback()
}
constructor(application: Application) : this(application.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager)
val stateFlow: StateFlow<State>
get() = _stateFlow
private val _stateFlow: MutableStateFlow<State> = MutableStateFlow(State.Init)
private val networkCallback = object : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network) {
super.onAvailable(network)
_stateFlow.value = State.Available
}
override fun onLost(network: Network) {
super.onLost(network)
_stateFlow.value = State.Gone
}
}
fun registerNetworkCallback(){
val builder = NetworkRequest.Builder()
connectivityManager.registerNetworkCallback(builder.build(), networkCallback)
}
fun unregisterNetworkCallback() {
connectivityManager.unregisterNetworkCallback(networkCallback)
}
sealed interface State {
object Init : State
object Available : State
object Gone : State
}
}
MainActivity.kt
class MainActivity : ComponentActivity() {
private lateinit var checkNetworkConnection: NetworkConnection
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
checkNetworkConnection = NetworkConnection(application)
setContent {
SportsResultTheme {
checkNetworkConnection.stateFlow.collectAsState()
SetupView()
}
}
}
}
Jetpack compose函数(LiveData. observeasstate())可用于将LiveData转换为状态,您可以在撰写UI中使用。如果您的代码库中已经有LiveData,则不必使用Flow。将实时数据传递到可组合对象中,并将其转换为状态。您还可以在调用SetupView
可组合之前将实时数据转换为状态,并仅传递状态本身。
@Composable
fun SetupView(networkConnection : NetworkConnection) {
val isConnected = networkConnection.observeAsState()
if(isConnected) {
// update for connected state
} else {
// handle offline state
}
}