Wafi_ck
Wafi_ck

Reputation: 1398

How to use flow binding

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

Upvotes: 0

Views: 370

Answers (1)

IR42
IR42

Reputation: 9732

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)
}

Upvotes: 1

Related Questions