1 year ago

#73379

test-img

Taimoor Khan

Click Handling on Views in Clean Architecture

I have to implement click listener using binding and ViewModel as per clean architecture.

I have two button to select language like English and Chinese.

LanguageActivity.kt

@AndroidEntryPoint
class LanguageActivity : PBActivity(R.layout.activity_language) {
    private val mViewModel:LanguageViewModel by viewModels()
    private val mBinding:ActivityLanguageBinding by viewbind()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        mBinding.apply {
            lifecycleOwner = this@LanguageActivity
            viewModel = mViewModel
        }

        collectFlow(mViewModel.uiState){
            Toast.makeText(this, it.name, Toast.LENGTH_SHORT).show()
            when(it){
                LanguageSelected.ENGLISH -> {

                }
                LanguageSelected.CHINESE -> {

                }
                LanguageSelected.NONE -> {

                }
            }
        }
    }
}

enum class LanguageSelected{
    ENGLISH,CHINESE,NONE
}

LanguageViewModel.kt

@HiltViewModel
class LanguageViewModel @Inject constructor(
    private val pbPrefs: PBPrefs
): ViewModel(){

    // Backing property to avoid state updates from other classes
    private val _uiState = MutableStateFlow(LanguageSelected.NONE)
    // The UI collects from this StateFlow to get its state updates
    val uiState: StateFlow<LanguageSelected> = _uiState

    fun englishSelected()= viewModelScope.launch {
        _uiState.value = LanguageSelected.ENGLISH
        pbPrefs.setLanguageSelect()
    }

    fun urduSelected() = viewModelScope.launch {
        _uiState.value = LanguageSelected.URDU
        pbPrefs.setLanguageSelect()
    }

}

activity_language.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <variable
            name="viewModel"
            type="uk.co.planetbeyond.telenorbluecollar.ui.language.LanguageViewModel" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ui.language.LanguageActivity">

        <androidx.constraintlayout.widget.Guideline
                android:id="@+id/center_vertical_gl"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:orientation="vertical"
                app:layout_constraintGuide_percent="0.5" />

        <Button
            android:id="@+id/englishTv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="English "
            android:onClick="@{() -> viewModel.englishSelected()}"
            android:layout_marginEnd="8dp"
            app:layout_constraintEnd_toStartOf="@id/center_vertical_gl"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/urduTv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Urdu"
            android:onClick="@{() -> viewModel.urduSelected()}"
            android:layout_marginStart="8dp"
            app:layout_constraintStart_toEndOf="@id/center_vertical_gl"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

I have to select language on click of a button. What's the best approach as per clean architecture to implement

I have created an interface LanguageSelected but for that I have to created multiple methods in viewModel, As languages increase I have to create more methods in viewmodel.

How could I make it short or I mean extensible?

android

data-binding

android-viewmodel

clean-architecture

0 Answers

Your Answer

Accepted video resources