1
\$\begingroup\$

In the context of MVVM, is this an efficient way to communicate to the view that I should display some TextViews? Any advice on how I can improve it?

ViewModel code:

private var _shouldDisplayDischargeCurrent = MutableLiveData<Boolean>()
    val shouldDisplayDischargeCurrent: LiveData<Boolean>
        get() = _shouldDisplayDischargeCurrent

Activity code:

viewModel.shouldDisplayDischargeCurrent.observe(this, Observer { shouldDisplay ->
            if (!shouldDisplay) {
                binding.textviewDischargeCurrentTitle.visibility = GONE
                binding.textviewDischargeCurrent.visibility = GONE
                binding.textviewDischargeTimeTitle.visibility = GONE
                binding.textviewDischargeTime.visibility = GONE
            }
        })
\$\endgroup\$
1
  • \$\begingroup\$ Do you also want to show the view? currently, your code will only hide it \$\endgroup\$ Sep 2, 2022 at 8:35

1 Answer 1

1
\$\begingroup\$

Will asume you are using ConstraintLayout and also want to show the views (currently your code only hides the views) so I think it can be improved in 2 ways.

The first way is to use View.isVisible extension from google's lib instead of setting GONE. So you code will become like this:

viewModel.shouldDisplayDischargeCurrent.observe(this, Observer { shouldDisplay ->
    binding.textviewDischargeCurrentTitle.isVisible = !shouldDisplay
    binding.textviewDischargeCurrent.isVisible = !shouldDisplay
    binding.textviewDischargeTimeTitle.isVisible = !shouldDisplay
    binding.textviewDischargeTime.isVisible = !shouldDisplay
})

and the second way is to use Constraint Group. Add a new element in XML with all the view's ids you want to hide:

<androidx.constraintlayout.widget.Group
    android:id="@+id/textview_group"
    app:constraint_referenced_ids="textviewDischargeCurrent, textviewDischargeCurrentTitle,textviewDischargeTime,textviewDischargeTimeTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

and then in your code:

viewModel.shouldDisplayDischargeCurrent.observe(this, Observer { shouldDisplay ->
// those are no longer needed
//    binding.textviewDischargeCurrentTitle.isVisible = !shouldDisplay
//    binding.textviewDischargeCurrent.isVisible = !shouldDisplay
//    binding.textviewDischargeTimeTitle.isVisible = !shouldDisplay
//    binding.textviewDischargeTime.isVisible = !shouldDisplay
    binding.textviewGroup.isVisible = !shouldDisplay
})

Also Observer { can be removed so the final result with Constraint Group will be:

viewModel.shouldDisplayDischargeCurrent.observe(this) { shouldDisplay ->
    binding.textviewGroup.isVisible = !shouldDisplay
}

Regarding performance I could not find much about this. Maybe someone else can comment about it. Personaly I would go with Constraint Group and View.isVisible

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.