From 8114e0c2130c96ac31dc5d1b63d05ab7b69cd33a Mon Sep 17 00:00:00 2001 From: Boby Ilea Date: Sat, 8 Aug 2020 17:45:03 +0300 Subject: [PATCH] Boby - Added backwards compatible method of running the evaluation synchronous --- .../com/deltadna/android/sdk/EventAction.java | 69 ++++++++++++------- 1 file changed, 46 insertions(+), 23 deletions(-) diff --git a/library/src/main/java/com/deltadna/android/sdk/EventAction.java b/library/src/main/java/com/deltadna/android/sdk/EventAction.java index f4f22df0..3e442b8e 100644 --- a/library/src/main/java/com/deltadna/android/sdk/EventAction.java +++ b/library/src/main/java/com/deltadna/android/sdk/EventAction.java @@ -44,6 +44,9 @@ public EventAction add(EventActionHandler handler) { @Override public void run() {} + + @Override + public void run(boolean runAsync) {} }; private final Event event; @@ -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{ + /** + * 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 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 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{ + + @Override + protected Void doInBackground(Void... voids) { + evaluateTriggers(); return null; } }