Repository: corona-warn-app/cwa-app-android
File: Corona-Warn-App/src/main/java/de/rki/coronawarnapp/familytest/ui/consent/FamilyTestConsentFragment.kt (Kotlin)

Code before:
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        viewModel.routeToScreen.observe2(this) {
            when (it) {
                is FamilyTestConsentNavigationEvents.NavigateBack -> {
                    binding.root.hideKeyboard()
                    popBackStack()
                }
                is FamilyTestConsentNavigationEvents.NavigateToDataPrivacy -> doNavigate(
                    FamilyTestConsentFragmentDirections.actionFamilyTestConsentFragmentToInformationPrivacyFragment()
                )
                is FamilyTestConsentNavigationEvents.NavigateToCertificateRequest -> findNavController().navigate(
                    NavGraphDirections.actionRequestCovidCertificateFragment(
                        testRegistrationRequest = it.coronaTestQRCode,
                        coronaTestConsent = it.consentGiven,
                        allowTestReplacement = it.allowReplacement,
                        personName = it.personName
                    ),
                    navOptions
                )
                else -> Unit
            }
        }

        viewModel.registrationState.observe2(this) { state ->
            val isWorking = state is TestRegistrationStateProcessor.State.Working
            binding.apply {
                progressSpinner.isVisible = isWorking
                consentButton.isEnabled = !isWorking && viewModel.isValid.value ?: false
            }
            when (state) {
                TestRegistrationStateProcessor.State.Idle,
                TestRegistrationStateProcessor.State.Working -> {
                    // Handled above
                }
                is TestRegistrationStateProcessor.State.Error -> {
                    val dialog = state.getDialogBuilder(requireContext())
                    dialog.setPositiveButton(android.R.string.ok) { _, _ -> popBackStack() }
                    dialog.show()
                }
                is TestRegistrationStateProcessor.State.TestRegistered -> when {
                    state.test.isPositive ->
                        doNavigate(
                            NavGraphDirections.actionToSubmissionTestResultAvailableFragment(
                                testIdentifier = state.test.identifier
                            )
                        )
                    else -> doNavigate(
                        NavGraphDirections.actionSubmissionTestResultPendingFragment(
                            testIdentifier = state.test.identifier

Review comment (suggestion):
The state of the button is handled by isValid, don't do this here too. Rather adjust the isValid logic inside the viewmodel.

Code after:
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        viewModel.routeToScreen.observe2(this) {
            when (it) {
                is FamilyTestConsentNavigationEvents.NavigateBack -> {
                    binding.root.hideKeyboard()
                    popBackStack()
                }
                is FamilyTestConsentNavigationEvents.NavigateToDataPrivacy -> doNavigate(
                    FamilyTestConsentFragmentDirections.actionFamilyTestConsentFragmentToInformationPrivacyFragment()
                )
                is FamilyTestConsentNavigationEvents.NavigateToCertificateRequest -> findNavController().navigate(
                    NavGraphDirections.actionRequestCovidCertificateFragment(
                        testRegistrationRequest = it.coronaTestQRCode,
                        coronaTestConsent = it.consentGiven,
                        allowTestReplacement = it.allowReplacement,
                        personName = it.personName
                    ),
                    navOptions
                )
                else -> Unit
            }
        }

        viewModel.registrationState.observe2(this) { state ->
            val isWorking = state is TestRegistrationStateProcessor.State.Working
            binding.apply {
                progressSpinner.isVisible = isWorking
            }
            when (state) {
                TestRegistrationStateProcessor.State.Idle,
                TestRegistrationStateProcessor.State.Working -> Unit
                is TestRegistrationStateProcessor.State.Error -> {
                    val dialog = state.getDialogBuilder(requireContext())
                    dialog.setPositiveButton(android.R.string.ok) { _, _ -> popBackStack() }
                    dialog.show()
                }
                is TestRegistrationStateProcessor.State.TestRegistered -> when {
                    state.test.isPositive ->
                        doNavigate(
                            NavGraphDirections.actionToSubmissionTestResultAvailableFragment(
                                testIdentifier = state.test.identifier
                            )
                        )
                    else -> doNavigate(
                        NavGraphDirections.actionSubmissionTestResultPendingFragment(
                            testIdentifier = state.test.identifier
                        )
                    )
                }