如何观察来自 Compose state holder class 的 ViewModel LiveData 变化?
How to observe ViewModel LiveData changes from Compose state holder class?
我正在关注 Android 开发人员在 Compose 中管理状态指南。
https://developer.android.com/jetpack/compose/state#managing-state
我已经实现了我自己的状态持有者,它按照下图将 viewModel 作为参数。在状态持有者内部,我有像 mutableStateOf()
这样的状态对象,它会触发重新组合。
我遇到的问题是如何让 MyStateHolder
观察 MyViewModel
中 LiveData
的变化。由于 MyStateHolder
是一个 POJO,我无法使用 observeAsState()
.
等功能
google 推荐这种方法,但没有提供开箱即用的方法,这似乎很奇怪。
如何观察 state holder 中 viewModel 的变化?我错过了什么吗?
class MyStateHolder(
private val viewModel: MyViewModel,
) {
//initialise state variables and observe _portfolioData
var portfolioDataMap = mutableStateMapOf<String, PortfolioDataModel>().apply {
putAll(viewModel.portfolioData.value!!)
viewModel.portfolioData.observeAsState() /*error here: cannot observeAsState in POJO*/
}
}
class MyViewModel : ViewModel() {
private val _portfolioData: MutableLiveData<Map<String, PortfolioDataModel>> =
MutableLiveData()
val portfolioData: LiveData<Map<String, PortfolioDataModel>>
get() = _portfolioData
fun setStateEvent(mainStateEvent: MainStateEvent<String>) {
//send events to update _portfolioData
}
}
观察生命模型需要一个生命周期所有者。您可以将此参数添加到您的状态持有者:
class MyStateHolder(
private val lifecycleOwner: LifecycleOwner,
private val viewModel: MyViewModel,
) {
//initialise state variables and observe _portfolioData
var portfolioDataMap = mutableStateMapOf<String, PortfolioDataModel>().apply {
putAll(viewModel.portfolioData.value!!)
}
init {
viewModel.portfolioData.observe(lifecycleOwner) {
portfolioDataMap.clear()
portfolioDataMap.putAll(it)
}
}
}
并且在初始化期间你可以从LocalLifecycleOwner.current
传递它,基本上是这样的:
@Composable
fun rememberMyStateHolder() : MyStateHolder {
val lifecycleOwner = LocalLifecycleOwner.current
val viewModel = viewModel<MyViewModel>()
// consider using rememberSaveable instead of remember with a custom saver
// in case of configuration change, e.g. screen rotation
return remember(lifecycleOwner, viewModel) {
MyStateHolder(lifecycleOwner, viewModel)
}
}
我正在关注 Android 开发人员在 Compose 中管理状态指南。 https://developer.android.com/jetpack/compose/state#managing-state
我已经实现了我自己的状态持有者,它按照下图将 viewModel 作为参数。在状态持有者内部,我有像 mutableStateOf()
这样的状态对象,它会触发重新组合。
我遇到的问题是如何让 MyStateHolder
观察 MyViewModel
中 LiveData
的变化。由于 MyStateHolder
是一个 POJO,我无法使用 observeAsState()
.
google 推荐这种方法,但没有提供开箱即用的方法,这似乎很奇怪。
如何观察 state holder 中 viewModel 的变化?我错过了什么吗?
class MyStateHolder(
private val viewModel: MyViewModel,
) {
//initialise state variables and observe _portfolioData
var portfolioDataMap = mutableStateMapOf<String, PortfolioDataModel>().apply {
putAll(viewModel.portfolioData.value!!)
viewModel.portfolioData.observeAsState() /*error here: cannot observeAsState in POJO*/
}
}
class MyViewModel : ViewModel() {
private val _portfolioData: MutableLiveData<Map<String, PortfolioDataModel>> =
MutableLiveData()
val portfolioData: LiveData<Map<String, PortfolioDataModel>>
get() = _portfolioData
fun setStateEvent(mainStateEvent: MainStateEvent<String>) {
//send events to update _portfolioData
}
}
观察生命模型需要一个生命周期所有者。您可以将此参数添加到您的状态持有者:
class MyStateHolder(
private val lifecycleOwner: LifecycleOwner,
private val viewModel: MyViewModel,
) {
//initialise state variables and observe _portfolioData
var portfolioDataMap = mutableStateMapOf<String, PortfolioDataModel>().apply {
putAll(viewModel.portfolioData.value!!)
}
init {
viewModel.portfolioData.observe(lifecycleOwner) {
portfolioDataMap.clear()
portfolioDataMap.putAll(it)
}
}
}
并且在初始化期间你可以从LocalLifecycleOwner.current
传递它,基本上是这样的:
@Composable
fun rememberMyStateHolder() : MyStateHolder {
val lifecycleOwner = LocalLifecycleOwner.current
val viewModel = viewModel<MyViewModel>()
// consider using rememberSaveable instead of remember with a custom saver
// in case of configuration change, e.g. screen rotation
return remember(lifecycleOwner, viewModel) {
MyStateHolder(lifecycleOwner, viewModel)
}
}