0

I try to handle clicks on my buttons and send action to 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) }
    }
}

Only action from login button comes to my view model. How can I merge these three flows into one? Probably that's the problem there are 3 action streams to view model

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Wafi_ck
  • 1,045
  • 17
  • 41

1 Answers1

1
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)
}
IR42
  • 8,587
  • 2
  • 23
  • 34