package me.yokeyword.fragmentation.queue;

import android.os.Handler;

import java.util.LinkedList;
import java.util.Queue;

/**
 * The queue of perform action.
 * <p>
 * Created by YoKey on 17/12/29.
 */
public class ActionQueue {
    private Queue<Action> mQueue = new LinkedList<>();
    private Handler mMainHandler;

    public ActionQueue(Handler mainHandler) {
        this.mMainHandler = mainHandler;
    }

    public void enqueue(final Action action) {
        if (isThrottleBACK(action)) return;

        mMainHandler.post(new Runnable() {
            @Override
            public void run() {
                enqueueAction(action);
            }
        });
    }

    private void enqueueAction(Action action) {
        mQueue.add(action);
        if (mQueue.size() == 1) {
            handleAction();
        }
    }

    private void handleAction() {
        if (mQueue.isEmpty()) return;

        Action action = mQueue.peek();
        action.run();

        mMainHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mQueue.poll();
                handleAction();
            }
        }, action.duration);
    }

    private boolean isThrottleBACK(Action action) {
        if (action.action == Action.ACTION_BACK) {
            for (Action item : mQueue) {
                if (item.action == Action.ACTION_BACK) {
                    return true;
                }
            }
        }
        return false;
    }
}

@@ -1296,36 +1296,36 @@
     
-for (
 Action 
-item :
+head =
  mQueue
-) %7B
+.peek();
 %0A   
@@ -1337,20 +1337,32 @@
     
-    if (item
+if (head != null && head
 .act
@@ -1378,36 +1378,31 @@
 tion.ACTION_
-BACK) %7B%0A    
+POP) %7B%0A
             
@@ -1422,26 +1422,8 @@
 ue;%0A
-                %7D%0A
     

Optimize the BACK event