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

Actions

Adrián Rivero edited this page May 4, 2017 · 33 revisions

Actions permit to write easy to understand and clean code in java, adding simplicity to the code, and writing effectively much less code than with any library that can be created by any traditional method.

Using Action Syntax

Action Syntax can be used in any classed with an Enhanced Annotation (Ex. @EActivity, @EFragment, @EBean, ...). They cannot be used in some data structures (like inside loops), inside Anonymous Classes, or in "private" or "static" methods.

To access easily to the actions, it is recommended to include the Action model statically:

import static com.dspot.declex.Action.*;

Actions in Fields

When annotating fields, an action can be executed immediately after the field element is triggered.

The field name can be:

A method

@RunWith
$FragmentLogin onCreate;

The id of an element in the layout

@Click
$MainActivity btnLogin;

@LongClick
$InformationFragment btnLogin; 

An event

@RunWith
$EventReceivedFragment onMessageReceived;

Parameters for the Actions in Fields

Parameters can be specified, assigning to the field the action constructor:

@Click
$MainActivity btnLogin = $MainActivity().extraParam("Cool!");

Actions in methods

The actions in methods are a powerful and the most efficient way to describe an action to be executed after a specific event is triggered.

The action language permits to get rid of the verbose java code involved in the listeners. Using If's statements to select the event, the actions permit to improve the code, make it look better, and speed up the development in Java.

Ex.: How to display an alert dialog to the user, go to a different fragment if "OK" button is pressed, or show a Toast to the user if the "Cancel" button is pressed.

@Click
void btnLogout() {
    $AlertDialog().title("My Application").message("Are you sure you want to exit?").
                  .positiveButton("OK").negativeButton("Cancel");

    if ($AlertDialog.NegativeButtonPressed) {
        $Toast("You decided to continue in the app");
    }

    if ($AlertDialog.PositiveButtonPressed) {
        $LoginActivity();
        finish();
    }
}

Ex.: Showing a list of countries to the user to select, and displaying the selected country in the user interface.

@Click
void btnSelectCountry(TextView selectedCountry) {
    String[] countries = new String[]{"United State", "France", "England", "China", "Japan"};
    $AlertDialog().items(countries);

    if ($AlertDialog.ItemSelected) {
        int $position = 0;
        selectedCountry.setText(countries.get($position));
    }
}

Action Rules and features

The actions are commanded by a set of very simple rules and includes several features.

Actions are sequences

All the actions are a sequence of statements, that is triggered by the Action identifier.

Ex.: Ask to the user if he wants to send some information to the server, if so, do this information in a background thread, and then show a toast back to the user (this should be done in the Main Thread). In this example, is seen that each action is executed only after the previous action has concluded, the method enters in background after the user presses "Yes", then the information is sent to the server, after this operations concludes, it goes to the Main Thread (UIThread), and shows a toast to the user.

@Model
Information info;

void sendInfo() {
    $AlertDialog().message("Want to send the information")
        .positiveButton("Yes").negativeButton("No");

    if ($AlertDialog.NegativeButtonPressed) {
        $Toast("It was canceled");
    }

    $Background();
    $PutModel(info);

    $UIThread();
    $Toast("Information sent to the server");
}

Actions can be executed in parallel

Sometimes, is required to executed to actions in parallel, for this, a block of code can be always opened, the block of codes restrict the actions to within their limits..

Ex.: Send information in parallel to a server

void sendInfo() {
    $AlertDialog().message("Press continue to send all the information").positiveButton("Continue");
    
    {
        $Background();
        $PutModel(info1);
        $Toast("The information number 1 has been sent");
    }

    {
        $Background();
        $PutModel(info2);
        $Toast("The information number 2 has been sent");
    }
}

Any variable can be injected using the "$" symbol:

If a value is needed from within the context, the dollar sign can be used to inject it, for instance, to access to the variables of a normal java listener (like the position of the item that was selected, or the values presented in the listener).

Ex.: Accessing to the variables returned by a DatePickerDialog:

@Click
void selectDate() {
    $DateDialog();
      
    int $year = 0, $month = 0, $day = 0;
    //Do something with the variables. 
}

Formatted Syntax

The Formatted Syntax can be used in most of the methods in actions that accepts a String as parameter.

Ex.:

void showInformation(String information) {
    $AlertDialog().message("The received information is {information}");
}

Actions Limitations

  • Cannot be used in classes that are not annotated with an Enhanced Annotation (Ex. @EActivity, @EFragment, @EBean, ...)
  • Actions cannot be used inside methods of Anonymous classes.
  • Actions cannot be used in "private" or "static" methods.

See Also:

Clone this wiki locally