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

Code before:
        binding.toolbar.setNavigationOnClickListener { viewModel.onCancel() }

        viewModel.srsTestType.observe2(this) {
            updateButtons(it)
        }
    }

    private fun updateButtons(typeSelectionItem: SrsTypeSelectionItem) {

        binding.typeList.children.forEachIndexed { i, view ->
            (view as Button).apply {
                handleColors(typeSelectionItem.submissionType, typeList[i].submissionType)
            }
        }

        binding.typeSelectionNextButton.apply {
            isEnabled = typeSelectionItem.submissionType != null
            setOnClickListener { viewModel.onNextClicked() }
        }
    }

    private fun Button.handleColors(submissionType: SrsSubmissionType?, state: SrsSubmissionType?) {
        setTextColor(formatSrsTypeSelectionButtonTextStyleByState(context, submissionType, state))
        backgroundTintList =
            ColorStateList.valueOf(formatSrsTypeSelectionBackgroundButtonStyleByState(context, submissionType, state))
    }

    private fun showCancelDialog() {
        displayDialog {
            title(R.string.srs_cancel_dialog_title)
            message(R.string.srs_cancel_dialog_body)
            positiveButton(R.string.srs_cancel_dialog_positive_button)
            negativeButton(R.string.srs_cancel_dialog_negative_button) { viewModel.onCancelConfirmed() }
        }
    }
}


Review comment (suggestion):
I think we can do better here:
1- Use `CheckedTextView` instead of `Button` 
```xml
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/type_name"
    style="@style/selectionButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="12dp"
    android:layout_marginTop="@dimen/spacing_small"
    android:layout_marginEnd="12dp"
    android:backgroundTint="@color/srs_type_bg_color"
    android:textColor="@color/srs_type_text_color"
    tools:text="@string/target_button_rat_registered_no_result_text" />
```
2- Set color `selector`s for  background  and  text that changes its background based on `checked` state 
```xml
# Background 
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/colorCalendarBackgroundSelected" android:state_checked="true" />
    <item android:color="@color/colorCalendarBackgroundUnselected" />
</selector>

# Text 
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/colorCalendarTextSelected" android:state_checked="true" />
    <item android:color="@color/colorCalendarTextUnselected" />
</selector>
```  
3- Add checked state to the model 
```kotlin 
data class SrsTypeSelectionItem(
    val checked: Boolean = false,
    val submissionType: SrsSubmissionType?
)
```
4- Based on `SrsTypeSelectionItem` in item VH
```
You just need in `VH` binding.text.isChecked = SrsTypeSelectionItem.checked
binding.text = when(type){
.....
}
```

Code after:
package de.rki.coronawarnapp.srs.ui.typeselection

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.View
import androidx.activity.OnBackPressedCallback
import androidx.navigation.fragment.findNavController
import de.rki.coronawarnapp.R
import de.rki.coronawarnapp.databinding.FragmentSrsTypeSelectionBinding
import de.rki.coronawarnapp.util.di.AutoInject
import de.rki.coronawarnapp.util.ui.observe2
import de.rki.coronawarnapp.util.ui.viewBinding
import de.rki.coronawarnapp.util.viewmodel.CWAViewModelFactoryProvider
import de.rki.coronawarnapp.util.viewmodel.cwaViewModels
import javax.inject.Inject

class SrsTypeSelectionFragment : Fragment(R.layout.fragment_srs_type_selection), AutoInject {

    @Inject lateinit var viewModelFactory: CWAViewModelFactoryProvider.Factory
    private val viewModel: SrsTypeSelectionFragmentViewModel by cwaViewModels { viewModelFactory }

    private val binding by viewBinding<FragmentSrsTypeSelectionBinding>()

    @Inject lateinit var itemAdapter: SrsTypeSelectionItemAdapter

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

        viewModel.navigation.observe2(this) {
            when (it) {
                SrsTypeSelectionNavigationEvents.NavigateToCloseDialog -> {
                    // Common close dialog
                }
                SrsTypeSelectionNavigationEvents.NavigateToMainScreen ->
                    findNavController().navigate(
                        SrsTypeSelectionFragmentDirections.actionSrsSubmissionTypeSelectionFragmentToMainFragment()
                    )

                is SrsTypeSelectionNavigationEvents.NavigateToShareCheckins ->
                    findNavController().navigate(
                        SrsTypeSelectionFragmentDirections
                            .actionSrsSubmissionTypeSelectionFragmentToSrsCheckinsFragment()
                    )

                is SrsTypeSelectionNavigationEvents.NavigateToShareSymptoms ->
                    findNavController().navigate(
                        SrsTypeSelectionFragmentDirections
                            .actionSrsSubmissionTypeSelectionFragmentToSrsSymptomsFragment()
                    )
            }
        }

        val backCallback = object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() = viewModel.onCancel()
        }
        requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner, backCallback)
        binding.typeList.adapter = itemAdapter
        viewModel.types.observe(viewLifecycleOwner) { types ->
            itemAdapter.data = types
            binding.typeSelectionNextButton.isEnabled = types.any { it.checked }
        }

        itemAdapter.onItemClickListener = { viewModel.selectTypeListItem(it) }
        binding.toolbar.setNavigationOnClickListener { viewModel.onCancel() }
        binding.typeSelectionNextButton.setOnClickListener {
            viewModel.onNextClicked()
        }
    }
}