如何使用流绑定
How to use flow binding
我尝试处理按钮上的点击并将操作发送到 viewModel
private fun subscribeUI() {
lifecycleScope.launch {
binding.loginButton
.clicks()
.onEach { }
.map { Action.WelcomeAction.SelectLogin }
.collect { viewModel.actions.offer(it) }
binding.homeButton
.clicks()
.onEach { }
.map { Action.WelcomeAction.SelectHome }
.collect { viewModel.actions.offer(it) }
binding.registerButton
.clicks()
.onEach {}
.map { Action.WelcomeAction.SelectRegister }
.collect { viewModel.actions.offer(it) }
}
}
只有来自登录按钮的操作才会出现在我的视图模型中。如何将这三个流程合并为一个?可能这就是问题所在,有 3 个动作流可以查看模型
private fun subscribeUI() {
merge(
binding.loginButton.clicks().map { Action.WelcomeAction.SelectLogin },
binding.homeButton.clicks().map { Action.WelcomeAction.SelectHome },
binding.registerButton.clicks().map { Action.WelcomeAction.SelectRegister }
)
.onEach { viewModel.actions.offer(it) }
.launchIn(lifecycleScope)
}
我尝试处理按钮上的点击并将操作发送到 viewModel
private fun subscribeUI() {
lifecycleScope.launch {
binding.loginButton
.clicks()
.onEach { }
.map { Action.WelcomeAction.SelectLogin }
.collect { viewModel.actions.offer(it) }
binding.homeButton
.clicks()
.onEach { }
.map { Action.WelcomeAction.SelectHome }
.collect { viewModel.actions.offer(it) }
binding.registerButton
.clicks()
.onEach {}
.map { Action.WelcomeAction.SelectRegister }
.collect { viewModel.actions.offer(it) }
}
}
只有来自登录按钮的操作才会出现在我的视图模型中。如何将这三个流程合并为一个?可能这就是问题所在,有 3 个动作流可以查看模型
private fun subscribeUI() {
merge(
binding.loginButton.clicks().map { Action.WelcomeAction.SelectLogin },
binding.homeButton.clicks().map { Action.WelcomeAction.SelectHome },
binding.registerButton.clicks().map { Action.WelcomeAction.SelectRegister }
)
.onEach { viewModel.actions.offer(it) }
.launchIn(lifecycleScope)
}