Skip to content

Commit

Permalink
add ActionFireInterceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
roman.donchenko committed Feb 8, 2017
1 parent 0fbfb1c commit a7ce6e5
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 33 deletions.
4 changes: 2 additions & 2 deletions actionhandler/build.gradle
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
apply plugin: 'com.android.library'

ext {
libraryVersionRevision = 0
libraryVersionMinor = 0
libraryVersionMajor = 1
libraryVersionMinor = 0
libraryVersionRevision = 1

libraryVersion = libraryVersionMajor + '.' + libraryVersionMinor + '.' + libraryVersionRevision

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.drextended.actionhandler.action.BaseAction;
import com.drextended.actionhandler.action.Cancelable;
import com.drextended.actionhandler.listener.ActionClickListener;
import com.drextended.actionhandler.listener.ActionFireInterceptor;
import com.drextended.actionhandler.listener.ActionInterceptor;
import com.drextended.actionhandler.listener.OnActionDismissListener;
import com.drextended.actionhandler.listener.OnActionErrorListener;
Expand All @@ -37,7 +38,7 @@
/**
* Use ActionHandler to manage action and bind them to view
*/
public class ActionHandler implements ActionClickListener, OnActionFiredListener, OnActionErrorListener, OnActionDismissListener {
public class ActionHandler implements ActionClickListener, OnActionFiredListener, OnActionErrorListener, OnActionDismissListener, ActionFireInterceptor {

// Actions which was added to the handler
protected final List<ActionPair> mActions;
Expand All @@ -51,9 +52,13 @@ public class ActionHandler implements ActionClickListener, OnActionFiredListener
// Callbacks to be invoked when an action is executed but dismissed
protected Set<OnActionDismissListener> mOnActionDismissListeners;

// Callback to be invoked right before specific action will be fired.
// Can intercept an action to prevent it to be fired
protected Set<ActionFireInterceptor> mActionFireInterceptors;

// Callback to be invoked after a view with an action is clicked and before action handling started.
// Can intercept an action to prevent it to be fired
private List<ActionInterceptor> mActionInterceptors;
private Set<ActionInterceptor> mActionInterceptors;

/**
* @param actions list of actions to handle by this handler
Expand All @@ -67,6 +72,7 @@ protected ActionHandler(List<ActionPair> actions) {
baseAction.addActionFiredListener(this);
baseAction.addActionErrorListener(this);
baseAction.addActionDismissListener(this);
baseAction.addActionFireInterceptor(this);
}
}
}
Expand Down Expand Up @@ -191,16 +197,6 @@ public void removeAllActionDismissListeners() {
}
}

/**
* Remove all callbacks for action intercept, fire, error and dismiss events
*/
public void removeAllActionListeners() {
removeAllActionFiredListeners();
removeAllActionDismissListeners();
removeAllActionInterceptors();
removeAllActionErrorListeners();
}

/**
* Set new callback to be invoked after a view with an action is clicked and before action handling started.
* Can intercept an action to prevent it to be fired
Expand All @@ -219,14 +215,14 @@ public void setActionInterceptor(ActionInterceptor actionInterceptor) {
}

/**
* Add new callback to be invoked after a view with an action is clicked and before action handling started.
* Can intercept an action to prevent it to be fired
* Add new callback to be invoked after a view with an action is clicked and before action type handling started.
* Can intercept an action type to prevent it to be handled
*
* @param actionInterceptor The interceptor, which can prevent actions to be fired
* @param actionInterceptor The interceptor, which can prevent action type to be handled
*/
public void addActionInterceptor(ActionInterceptor actionInterceptor) {
if (mActionInterceptors == null) {
mActionInterceptors = new ArrayList<>(1);
mActionInterceptors = new HashSet<>(1);
}
mActionInterceptors.add(actionInterceptor);
}
Expand All @@ -238,19 +234,63 @@ public void addActionInterceptor(ActionInterceptor actionInterceptor) {
*/
public void removeActionInterceptor(ActionInterceptor actionInterceptor) {
if (mActionInterceptors != null) {
mActionInterceptors.clear();
mActionInterceptors.remove(actionInterceptor);
}
}

/**
* Remove all interceptors
* Remove all action interceptors
*/
public void removeAllActionInterceptors() {
if (mActionInterceptors != null) {
mActionInterceptors.clear();
}
}

/**
* Add new callback to be invoked right before specific action will be fired..
* Can intercept an action to prevent it to be fired
*
* @param actionFireInterceptor The interceptor, which can prevent action to be fired
*/
public void addActionFireInterceptor(ActionFireInterceptor actionFireInterceptor) {
if (mActionFireInterceptors == null) {
mActionFireInterceptors = new HashSet<>(1);
}
mActionFireInterceptors.add(actionFireInterceptor);
}

/**
* Remove action fire interceptor
*
* @param actionFireInterceptor The interceptor to remove
*/
public void removeActionFireInterceptor(ActionFireInterceptor actionFireInterceptor) {
if (mActionFireInterceptors != null) {
mActionFireInterceptors.remove(actionFireInterceptor);
}
}

/**
* Remove all action fire interceptors
*/
public void removeAllActionFireInterceptors() {
if (mActionFireInterceptors != null) {
mActionFireInterceptors.clear();
}
}

/**
* Remove all callbacks for action intercept, fire, error and dismiss events
*/
public void removeAllActionListeners() {
removeAllActionFiredListeners();
removeAllActionDismissListeners();
removeAllActionInterceptors();
removeAllActionFireInterceptors();
removeAllActionErrorListeners();
}

@Override
public void onActionFired(View view, String actionType, Object model) {
if (mOnActionFiredListeners != null) {
Expand Down Expand Up @@ -278,6 +318,11 @@ public void onActionDismiss(String reason, View view, String actionType, Object
}
}

@Override
public boolean onInterceptActionFire(Context context, View view, String actionType, Object model, Action action) {
return interceptActionFire(context, view, actionType, model, action);
}

/**
* Check if there is at least one action that can try to handle {@code actionType}
*
Expand Down Expand Up @@ -327,13 +372,23 @@ public void fireAction(Context context, View view, String actionType, Object mod
if (actionPair.actionType == null || actionPair.actionType.equals(actionType)) {
final Action action = actionPair.action;
if (action != null && action.isModelAccepted(model)) {
if (interceptActionFire(context, view, actionType, model, action)) continue;
//noinspection unchecked
action.onFireAction(context, view, actionType, model);
}
}
}
}

private boolean interceptActionFire(Context context, View view, String actionType, Object model, Action action) {
if (mActionFireInterceptors != null) {
for (ActionFireInterceptor interceptor : mActionFireInterceptors) {
if (interceptor.onInterceptActionFire(context, view, actionType, model, action)) return true;
}
}
return false;
}

/**
* Call this method to force actions to cancel.
* Usually, you may need to call this on Activity destroy to free resources which
Expand Down Expand Up @@ -362,7 +417,8 @@ public static final class Builder {
private Set<OnActionFiredListener> mActionFiredListeners;
private Set<OnActionErrorListener> mActionErrorListeners;
private Set<OnActionDismissListener> mActionDismissListeners;
private List<ActionInterceptor> mActionInterceptors;
private Set<ActionInterceptor> mActionInterceptors;
private Set<ActionFireInterceptor> mActionFireInterceptors;

public Builder() {
mActions = new ArrayList<>();
Expand Down Expand Up @@ -452,19 +508,33 @@ public Builder setActionInterceptor(ActionInterceptor actionInterceptor) {
}

/**
* Add callback to be invoked after a view with an action is clicked and before action handling started.
* Can intercept an action to prevent it to be fired
* Add callback to be invoked after a view with an action is clicked and before action type handling started.
* Can intercept an action type to prevent it to be handled
*
* @param actionInterceptor The interceptor, which can prevent actions to be fired
*/
public Builder addActionInterceptor(ActionInterceptor actionInterceptor) {
if (mActionInterceptors == null) {
mActionInterceptors = new ArrayList<>(1);
mActionInterceptors = new HashSet<>(1);
}
mActionInterceptors.add(actionInterceptor);
return this;
}

/**
* Add callback to be invoked before specific action will be fired.
* Can intercept an action to prevent it to be fired
*
* @param actionFireInterceptor The interceptor, which can prevent actions to be fired
*/
public Builder addActionFireInterceptor(ActionFireInterceptor actionFireInterceptor) {
if (mActionFireInterceptors == null) {
mActionFireInterceptors = new HashSet<>(1);
}
mActionFireInterceptors.add(actionFireInterceptor);
return this;
}

public ActionHandler build() {
final ActionHandler actionHandler = new ActionHandler(mActions);
if (mActionFiredListeners != null) {
Expand All @@ -479,6 +549,9 @@ public ActionHandler build() {
if (mActionInterceptors != null && mActionInterceptors.size() > 0) {
actionHandler.mActionInterceptors = mActionInterceptors;
}
if (mActionFireInterceptors != null && mActionFireInterceptors.size() > 0) {
actionHandler.mActionFireInterceptors = mActionFireInterceptors;
}
return actionHandler;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
*/
package com.drextended.actionhandler.action;

import android.content.Context;
import android.view.View;

import com.drextended.actionhandler.listener.ActionFireInterceptor;
import com.drextended.actionhandler.listener.OnActionDismissListener;
import com.drextended.actionhandler.listener.OnActionErrorListener;
import com.drextended.actionhandler.listener.OnActionFiredListener;
Expand Down Expand Up @@ -47,6 +49,12 @@ public abstract class BaseAction<M> implements Action<M> {
*/
protected Set<OnActionDismissListener> mActionDismissListeners = new HashSet<>(1);

/**
* Callbacks to be invoked after a view with an action is clicked and before action handling started.
* Can intercept an action to prevent it to be fired
*/
protected Set<ActionFireInterceptor> mActionFireInterceptors = new HashSet<>(1);

/**
* Add a listener that will be called when method {@link #notifyOnActionFired(View, String, Object)}
* called. Generally if action fired successfully.
Expand Down Expand Up @@ -126,13 +134,40 @@ public void removeAllActionDismissListeners() {
mActionDismissListeners.clear();
}

/**
* Add callback to be invoked right before specific action will be fired.
* Can intercept an action to prevent it to be fired
*
* @param interceptor The interceptor.
*/
public void addActionFireInterceptor(ActionFireInterceptor interceptor) {
if (interceptor != null) mActionFireInterceptors.add(interceptor);
}

/**
* Remove interceptor.
*
* @param interceptor The interceptor.
*/
public void removeActionFireInterceptor(ActionFireInterceptor interceptor) {
if (interceptor != null) mActionFireInterceptors.remove(interceptor);
}

/**
* Remove all interceptors.
*/
public void removeAllActionFireInterceptors() {
mActionFireInterceptors.clear();
}

/**
* Remove all listeners for action fire, error and dismiss events.
*/
public void removeAllActionListeners() {
mActionFiredListeners.clear();
mActionErrorListeners.clear();
mActionDismissListeners.clear();
mActionFireInterceptors.clear();
}


Expand Down Expand Up @@ -179,4 +214,13 @@ public void notifyOnActionDismiss(String reason, View view, String actionType, O
listener.onActionDismiss(reason, view, actionType, model);
}
}

protected boolean interceptActionFire(Context context, View view, String actionType, Object model, Action action) {
if (mActionFireInterceptors != null) {
for (ActionFireInterceptor interceptor : mActionFireInterceptors) {
if (interceptor.onInterceptActionFire(context, view, actionType, model, action)) return true;
}
}
return false;
}
}
Loading

0 comments on commit a7ce6e5

Please sign in to comment.