Skip to content
This repository has been archived by the owner on May 4, 2022. It is now read-only.

Recollecting Data

Adrián Rivero edited this page Mar 8, 2017 · 6 revisions

To recollect all the data written by an user to a model, we annotate the model injected field with @Recollect.

@Model
@Recollect
User user;

This will use the methods and fields from the model to recollect the data from the user interface. In the layout, the IDs should be assigned following one of the next rules:

  • The ID should start with the field name plus the symbol "_" plus the field or method name. Ex: user_name to recollect to user.setName(String).
  • The ID should start with the field name and then the field or method name starting with a capital letter. Ex: userName to recollect to user.setName(String).

To access to the model fields that are Models, it can be done following the same rules... let's say that the User model has a field named "partner" that it's also an User, and you want to recollect from a view the "partner" name, then you assign the id in the layout to the view to be populated as user_partner_name.

DecleX supports recollecting by default from the following views:

  1. TextView and any of its subclasses (Ex. EditText). TextViews are recollected using the method getText(), the value is automatically cast to the field type in the case of recollecting a primitive value.

Recollecting customization for specific views

If you want to recollect from a different view, then you should declare a method named "readField" in your Enhanced Component.

public String readField(View view) {
    //Return the value that is gonna to be read from "view"
}

For instance, if we have a Spinner and we want to recollect within a specific field of our injected model the Index of the element selected by the user, then we should declare a function like this:

void String readField(View view) {
    if (view instanceof Spinner) {
        return String.valueOf(((Spinner)view).getSelectedItemPosition());
    }
}

Recollect to primitives or Strings

Primitives and Strings can also be recollected from the user interface, to use this, you should assign an arbitrary ID to the View that you want to recollect, then you declare a field matching the same Id:

@EActivity(R.layout.activity_enhanced)
public class EnhancedActivity extends Activity {

    @Recollect
    String text;

    @Recollect
    int age;
}

Events and Actions in Model Injection with Recollectors

When a Recollector is applied to a Model, if any Event or Action is used to Put the model, this will result in recollecting the User Interface Components linked to the field by the Recollector. So, in this example, every time that the button btnSave is clicked, all the data written by the user is recollected and saved to the model, prior to Putting the field itself.

@Model
@Recollect
@PutOnAction(R.id.btnSave)
User user;

Validating

@Model
@Recollect(validate=true)
@PutOnAction(R.id.btnSave)
User user;