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

Enhanced Action Sequences

Adrián Rivero edited this page Feb 22, 2016 · 23 revisions

IMPORTANT!! THIS FEATURE IS NOT PROGRAMMED YET, IT'S INTENDED TO SUPPORT NORMAL ACTIONS IN A WAY THAT GIVES TO THE PROGRAMMER MORE CONTROL OVER THE EXECUTION OF THE ACTION ITSELF. IT CAN BE ALSO EASIER TO UNDERSTAND FOR PROGRAMMERS.

Enhanced Action Sequences are implemented annotating a Method with @Action. They permit a higher control of the action, as well as an easier understanding of the control structures.

The name of the method should follow the same rules explained in [Action Types](Action Types). Each Action have to be passed in the parameter with an EAction object:

@Action
void btnAction(EAction<Toast> toast, EAction<MainActivity> activity) {

}

EAction Object

This is EAction class:

public abstract class EAction<T> {
    private boolean runOnUiThread;
    private Runnable before;
    private Runnable after;
    private Runnable failed;
    private EAction<?> next;

    public T action;

    private boolean halted;

    public abstract void run();

    public EAction runOnUiThread() {
        this.runOnUiThread = true;
        return this;
    }

    public EAction before(Runnable method) {
        this.before = method;
        return this;
    }

    public EAction after(Runnable method) {
        this.after = method;
        return this;
    }

    public EAction failed(Runnable method) {
        this.failed = method;
        return this;
    }

    public EAction next(EAction next) {
        this.next = next;
        return this;
    }

    public EAction stop() {
        this.next = null;
        return this;
    }

    public void halt() {
        this.halted = true;
    }
}

EAction object has a set of fields that simplify the control of the overall action.

action: Represents the action itself, it can be used to initialize parameters of the action:

@Action
void btnAction(EAction<Toast> toast, EAction<MainActivity> activity) {
    toast.action.setMessage("Hello World!");
}

next: Represents the next `EAction1 that's going to be executed. Setting this parameter can change the flow of the Sequence. You can also set to null if you want to avoid executing more actions

@Action
void btnAction(final EAction<Toast> toast, EAction<MainActivity> activity) {
    toast.before(new Runnable() {
        @Override
        public void run() {
            if (isSuccessful) {
                toast.action.setText("Action OK");
            } else {
                toast.action.setText("Action ERROR");
                toast.halt();
            }
        }
    };
}

runOnUiThread: If invoked, the action will be executed on the Ui Thread.

@Action
void btnAction(EAction<PutModel> $model, final EAction<Toast> toast) {
    toast.runOnUiThread().before(new Runnable() {
        @Override
        public void run() {
            if (model.isSaved()) {
                toast.action.setText("Model has been saved");
            } else {
                toast.action.setText("Model couldn't be saved");
            }
        }
    });
}

run(), run(EAction): This methods run the action itself, if an EAction is passed like parameter, then this will set the next parameter for the next action to execute.

@Action
void btnAction(EAction<PutModel> $model, final EAction<Toast> toast) {
    toast.action.setText("Saving object");
    toast.run();   
>
    toast.runOnUiThread().before(new Runnable() {
        @Override
        public void run() {
            if (model.isSaved()) {
                toast.action.setText("Model has been saved");
            } else {
                toast.aciton.setText("Model couldn't be saved");
            }
        }
    });
}

Injecting Needed Parameters

Any variable at which the original Action has accessed can be injected directly into the Action Method.

In this example, parent variable an model are injecting when clicking over a list view item. See [Action Types: On Item Clicked](Action Types#itemclicked)

@Action
void listViewItemClicked(
    EAction<DetailActivity_.IntentBuilder_> activity, 
    ListView parent, User model
) {
    activity.action.user(model);
}

Action Parameters

All the structures explained in [About Flow Control](About Flow Control) are totally applicable to the Enhanced Actions.

@Model
User currentUser;

@Action({"finish();", "user=currentUser -> finish();"})
boolean btnAction(EAction<SignUpActivity> _signUpActivity, EAction<MainActivity> mainActivity) {
    if (isRegistered) return mainActivity;
    return signUpActivity;
}

Result of the Action

The result of the Action can be used to control the start of the action itself. If the action function returns a boolean, returning false will stop cancel the execution of the Action:

@Action
boolean btnAction(EAction<Toast> toast, EAction<MainActivity> activity) {
    if (isReady) return true;

   return false;
}

If EAction is returned, it will be used to start the Action execution:

@Action
EAction btnAction(EAction<SignUpActivity> _signUpActivity, EAction<MainActivity> mainActivity) {
    if (isRegistered) return mainActivity;
    return _signUpActivity;
}

Specials Symbols

$: Typically is used for actions that requires have the parameter name as a self-parameter, to avoid using the same name of fields that can be used later in the Action Method.

_: It is used in the beginning to avoid the action to be listed in the sequence.