declex icon indicating copy to clipboard operation
declex copied to clipboard

View decoupled data binding

Open smaugho opened this issue 7 years ago • 0 comments

The current populate and recollect mechanism will support the following notations:

id="@+id\nameSurname"

This should accept to be linked to:

Field nameSurname Method var nameSurname() getter Method var getNameSurname() getter Method void nameSurname(var varName) setter Method void setNameSurname(var varName) setter

This would be a get/set for a default property that is set for multiple objects:

  • TextView Class/Subclass => text
    • var: String
  • ImageView Class/Subclass => src (it would be set using Picasso library)
    • var: String(url), int(resource), File

Decoupling properties

All the views properties can be decoupled from the view to avoid references to the view object. For doing so, you should use the same notation pointed above, adding the property value:

Ex.

//This is used in the populate phase
public int getNameSurnameVisibility() {
    return View.VISIBLE;
}

//This is used in the recollect phase
public void setNameSurnameVisibility(int visibility) {

}

The last expression can also be written as

public void setNameSurname(int visibility) {

}

Injecting View values

Values from the layout can be injected in any of the methods above (setters or getters).

public void setNameSurname(int visibility, int viewNameVisibility) {
   //do something with both views visibility
}

Using @Autogenerate to create bundle of parameters for views

@Autogenerate annotation as specified in Issue #98 would be used to create bundle of parameters.

When it is passed as a parameter, it would be filled automatically with the properties of the object (if they exist), for instance:

public void setUserName(@Autogenerate ViewProperties userName, @Autogenerate ViewProperties userSurname) {
    if (view.visibility == View.VISIBLE) {
        this.userName = view.text;
        this.userSurname = userSurname.text;
    }
}

Note that in this case, when the recollecting of data is triggered, it is recollected not only userName field, but also userSurname field, in the same method.

When reading a view value, the @Autogenerate can be also used, but in this case, the method should return the same class than an autogenerate parameters with the same method name:

Ex.

@DrawableRes
Drawable myDrawable;

public ViewProperties getUserImage(@Autogenerate ViewProperties userImage) {
    userName.visibility = View.VISIBLE;
    userName.background = Color.RED;
    userName.imageDrawable = myDrawable;
    return userName;
}

Note that the method is using an imageDrawable... but if userImage object is not an ImageView, this would be omitted (if not setImageDrawable(Drawable) method is found)

Behavior

When using decoupled expressions, the methods would be linked only if the view exists.

For parameters of views which doesn't exists, the default value would be used:

defjava

In the programing the programmer should never assume that the parameter has a value assigned.

smaugho avatar Mar 18 '17 02:03 smaugho