Android-ViewModelBinding icon indicating copy to clipboard operation
Android-ViewModelBinding copied to clipboard

Add aggregate view models.

Open karlsonj opened this issue 6 years ago • 0 comments

This adds the ability to have viewmodels that contain other viewmodels. In the Android layout files, you might have, say, a (shared) widget that manages phone number entry and validation, and a viewmodel that contains it and manages the validation of the rest of a form. Something like this:

<layout>
  <data>
    <variable name="formViewModel" type="com.company.FormViewModel"/>
    <variable name="phoneViewModel" type="com.company.PhoneViewModel"/>
  </data>
  <LinearLayout>
    <!-- Bound components for the form... -->
    <include layout="@layout/phoneentry" app:viewModel="@{phoneViewModel}"/>
  </LinearLayout>
</layout>

This PR allows you to create an outer FormViewModel that extends AggregateViewModel and a shared PhoneViewModel that extends SubViewModel and is aggreagated within the FormViewModel:

class FormViewModel extends AggregateViewModel {

  PhoneViewModel phoneViewModel = new PhoneViewModel();

  class FormViewModel() {
    addSubviews(phoneViewModel);
  }
}

karlsonj avatar Nov 23 '17 00:11 karlsonj