How can i handle two way binding in MVI architecture?

285 Views Asked by At

I see many articles handling two way binding with MVVM. But i want to handle this issue with single source of truth. I dont want to create multiple stateflows for this case. I have a StateFlow which has all view state inside entity. How can i provide to sync with multiple EditText values to the StateFlow ?

I have added multiple TextWatcher's to sending event to ViewModel but this way causes so boilerplate code.

1

There are 1 best solutions below

0
On

I got your question and I think that you need to use viewBinding feature,

so the first step you need to enable viewBinding features in your gradle (module).

android{
   buildFeatures {
        viewBinding = true //this is mandatory
   }
   ...
}

then you can bind your view I use my code as example

inside the activity class you can declare viewBinding of your activity using lateinit and inflate your activity inside onCreate fun

class HomeActivity : AppCompatActivity() {
    private lateinit var binding: ActivityHomeBinding

    override fun onCreate(savedInstanceState: Bundle?) {
       super.onCreate(savedInstanceState)
       binding = ActivityHomeBinding.inflate(layoutInflater)
       setContentView(binding.root)
       ...
    }
}

and finally you can easily access your view using binding val

example

binding.textView.text = "example" 

the code above will be similar to

findViewById<TextView>(R.id.text_view).text ="example"