Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boby - Added backwards compatible method of running the evaluation synchronous #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 46 additions & 23 deletions library/src/main/java/com/deltadna/android/sdk/EventAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public EventAction add(EventActionHandler handler) {

@Override
public void run() {}

@Override
public void run(boolean runAsync) {}
};

private final Event event;
Expand Down Expand Up @@ -74,39 +77,59 @@ public EventAction add(EventActionHandler<?> handler) {

/**
* Evaluates the registered handlers against the event and triggers
* associated for the event.
* associated for the event. The evaluation is done async.
*/
public void run() {
new EvaluateTriggersAsyncTask().execute();
}

private class EvaluateTriggersAsyncTask extends AsyncTask<Void, Void, Void>{
/**
* Evaluates the registered handlers against the event and triggers
* associated for the event.
*
* @param runAsync Choose to run the evaluation async(true) or on the current thread(false)
*
*/
public void run(boolean runAsync) {
if (runAsync) new EvaluateTriggersAsyncTask().execute();
else evaluateTriggers();
}

@Override
protected Void doInBackground(Void... voids) {
Set<EventActionHandler> modifiedHandlerSet = new LinkedHashSet<>(handlers);
if (settings != null) {
if (settings.getDefaultGameParametersHandler() != null) {
modifiedHandlerSet.add(settings.getDefaultGameParametersHandler());
}
if (settings.getDefaultImageMessageHandler() != null) {
modifiedHandlerSet.add(settings.getDefaultImageMessageHandler());
}
/**
* Evaluates the registered handlers against the event and triggers
* associated for the event.
*/
private void evaluateTriggers() {
Set<EventActionHandler> modifiedHandlerSet = new LinkedHashSet<>(handlers);
if (settings != null) {
if (settings.getDefaultGameParametersHandler() != null) {
modifiedHandlerSet.add(settings.getDefaultGameParametersHandler());
}
if (settings.getDefaultImageMessageHandler() != null) {
modifiedHandlerSet.add(settings.getDefaultImageMessageHandler());
}
boolean handledImageMessage = false;
for (final EventTrigger trigger : triggers) {
if (trigger.evaluate(event)) {
for (final EventActionHandler handler : modifiedHandlerSet) {
if (handledImageMessage && "imageMessage".equals(trigger.getAction())) break;
boolean handled = handler.handle(trigger, store);
if (handled) {
if (!settings.isMultipleActionsForEventTriggerEnabled()) return null;
if ("imageMessage".equals(trigger.getAction())) handledImageMessage = true;
break;
}
}
boolean handledImageMessage = false;
for (final EventTrigger trigger : triggers) {
if (trigger.evaluate(event)) {
for (final EventActionHandler handler : modifiedHandlerSet) {
if (handledImageMessage && "imageMessage".equals(trigger.getAction())) break;
boolean handled = handler.handle(trigger, store);
if (handled) {
if (!settings.isMultipleActionsForEventTriggerEnabled()) return;
if ("imageMessage".equals(trigger.getAction())) handledImageMessage = true;
break;
}
}
}
}
}

private class EvaluateTriggersAsyncTask extends AsyncTask<Void, Void, Void>{

@Override
protected Void doInBackground(Void... voids) {
evaluateTriggers();
return null;
}
}
Expand Down