<commit_before>package net.neevek.android.lib.paginize;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import net.neevek.android.lib.paginize.anim.PageAnimator;

import java.util.Iterator;
import java.util.LinkedList;

/**
 * PageManager manages the pages(of type Page), it swaps(push and pop) the pages
 * when requested, it uses PageAnimationManager to animate the transition when
 * swapping Pages.
 *
 * Date: 9/30/13
 * Time: 10:32 AM
 *
 * @author <EMAIL>
 * @version 1.0.0
 * @since 1.0.0
 */
public class PageManager {
    private ViewGroup mContainerView;

    private LinkedList<Page> mPageStack = new LinkedList<Page>();
    private Page mCurPage;

    private PageAnimator mPageAnimator;

    public PageManager(ViewGroup containerView) {
        this(containerView, null);
    }

    public PageManager(ViewGroup containerView, PageAnimator pageAnimator) {
        mContainerView = containerView;
        mPageAnimator = pageAnimator;
    }

    public void setPageAnimator(PageAnimator pageAnimator) {
        mPageAnimator = pageAnimator ;
    }

    public PageAnimator getPageAnimator() {
        return mPageAnimator;
    }

    public void pushPage(Page page) {
        pushPage(page, null, false);
    }

    public void pushPage(Page page, Object arg, boolean animated) {
        pushPage(page, arg, animated, false);
    }

    public void pushPage(final Page newPage, final Object arg, boolean animated, boolean hint) {
        if (newPage == mCurPage) {
            return;
        }

        Page oldPage = mCurPage;

        mCurPage = newPage;
        mPageStack.addLast(newPage);
        mContainerView.addView(newPage.getView());
        newPage.onAttach();

        if (oldPage != null) {
            if (newPage.keepSingleInstance() && newPage.getClass() == oldPage.getClass()) {
                mPageStack.removeLastOccurrence(oldPage);
                mContainerView.removeView(oldPage.getView());
                oldPage.onDetach();
                oldPage.onHidden();
            } else {
                if (newPage.getType() != Page.TYPE.TYPE_DIALOG) {
                    oldPage.getView().setVisibility(View.GONE);
                }

                oldPage.onCovered();
            }
        }

        if (animated && mPageAnimator != null && !newPage.onPushPageAnimation(oldPage, newPage, hint)) {
            mPageAnimator.onPushPageAnimation(oldPage, newPage, hint);
        }

        int animationDuration = newPage.getAnimationDuration();
        if (animationDuration == -1 && mPageAnimator != null) {
            animationDuration = mPageAnimator.getAnimationDuration();
        }
        if (animated && animationDuration != -1) {
            newPage.getView().postDelayed(new Runnable() {
                @Override
                public void run() {
                    newPage.onShown(arg);
                }
            }, animationDuration);
        } else {
            newPage.onShown(arg);
        }
    }

    public void popPage() {
        popPage(false, false);
    }

    /**
     * @param animated
     * @param hint true=left, false=right
     */
    public void popPage(boolean animated, boolean hint) {
        popTopNPages(1, animated, hint);
    }

    public void popTopNPages(int n, boolean animated, boolean hint) {
        if (n <= 0 || mPageStack.size() <= 0) {
            return;
        }

        Page oldPage = mPageStack.removeLast();
        --n;    // for mPageStack.removeLast() above

        while (--n >= 0) {
            Page page = mPageStack.removeLast();
            mContainerView.removeView(page.getView());
            page.onDetach();
            page.onHidden();
        }

        popPageInternal(oldPage, animated, hint);
    }

    /**
     * "pop" operation ends if destPage is found,
     * if destPage is not found, the method call is a no-op
     *
     * @param destPage page as the destination for this pop operation
     * @param animated
     * @param hint
     */
    public void popToPage(Page destPage, boolean animated, boolean hint) {
        if (destPage == null) {
            throw new IllegalArgumentException("cannot call popToPage() with null destPage.");
        }

        if (mPageStack.size() <= 0 || mPageStack.lastIndexOf(destPage) == -1 || mPageStack.peekLast() == destPage) {
            return;
        }


        Page oldPage = mPageStack.removeLast();

        while (mPageStack.size() > 1) {
            if (mPageStack.peekLast() == destPage) {
                break;
            }

            Page page = mPageStack.removeLast();
            mContainerView.removeView(page.getView());
            page.onDetach();
            page.onHidden();
        }

        popPageInternal(oldPage, animated, hint);
    }

    /**
     * "pop" operation ends if the pageClass is found,
     * if the class is not found, the method call is a no-op
     *
     * @param pageClass class of page as the destination for this pop operation
     * @param animated
     * @param hint
     */
    public void popToClass(Class<? extends Page> pageClass, boolean animated, boolean hint) {
        if (pageClass == null) {
            throw new IllegalArgumentException("cannot call popToClass() with null pageClass.");
        }

        popToClasses(new Class[]{ pageClass }, animated, hint);<commit_msg>added log messages for PageManager<commit_after>package net.neevek.android.lib.paginize;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import net.neevek.android.lib.paginize.anim.PageAnimator;

import java.util.Iterator;
import java.util.LinkedList;

/**
 * PageManager manages the pages(of type Page), it swaps(push and pop) the pages
 * when requested, it uses PageAnimationManager to animate the transition when
 * swapping Pages.
 *
 * Date: 9/30/13
 * Time: 10:32 AM
 *
 * @author <EMAIL>
 * @version 1.0.0
 * @since 1.0.0
 */
public class PageManager {
    private final String TAG = PageManager.class.getSimpleName();
    private final boolean DEBUG = true;

    private ViewGroup mContainerView;

    private LinkedList<Page> mPageStack = new LinkedList<Page>();
    private Page mCurPage;

    private PageAnimator mPageAnimator;

    public PageManager(ViewGroup containerView) {
        this(containerView, null);
    }

    public PageManager(ViewGroup containerView, PageAnimator pageAnimator) {
        mContainerView = containerView;
        mPageAnimator = pageAnimator;
    }

    public void setPageAnimator(PageAnimator pageAnimator) {
        mPageAnimator = pageAnimator ;
    }

    public PageAnimator getPageAnimator() {
        return mPageAnimator;
    }

    public void pushPage(Page page) {
        pushPage(page, null, false);
    }

    public void pushPage(Page page, Object arg, boolean animated) {
        pushPage(page, arg, animated, false);
    }

    public void pushPage(final Page newPage, final Object arg, boolean animated, boolean hint) {
        if (newPage == mCurPage) {
            return;
        }

        Page oldPage = mCurPage;

        mCurPage = newPage;
        mPageStack.addLast(newPage);
        mContainerView.addView(newPage.getView());
        newPage.onAttach();

        if (DEBUG) {
            Log.d(TAG, String.format(">>>> pushPage, pagestack=%d, %s, arg=%s", mPageStack.size(), newPage, arg));
        }

        if (oldPage != null) {
            if (newPage.keepSingleInstance() && newPage.getClass() == oldPage.getClass()) {
                mPageStack.removeLastOccurrence(oldPage);
                mContainerView.removeView(oldPage.getView());
                oldPage.onDetach();
                oldPage.onHidden();
            } else {
                if (newPage.getType() != Page.TYPE.TYPE_DIALOG) {
                    oldPage.getView().setVisibility(View.GONE);
                }

                oldPage.onCovered();
            }
        }

        if (animated && mPageAnimator != null && !newPage.onPushPageAnimation(oldPage, newPage, hint)) {
            mPageAnimator.onPushPageAnimation(oldPage, newPage, hint);
        }

        int animationDuration = newPage.getAnimationDuration();
        if (animationDuration == -1 && mPageAnimator != null) {
            animationDuration = mPageAnimator.getAnimationDuration();
        }
        if (animated && animationDuration != -1) {
            newPage.getView().postDelayed(new Runnable() {
                @Override
                public void run() {
                    newPage.onShown(arg);
                }
            }, animationDuration);
        } else {
            newPage.onShown(arg);
        }
    }

    public void popPage() {
        popPage(false, false);
    }

    /**
     * @param animated
     * @param hint true=left, false=right
     */
    public void popPage(boolean animated, boolean hint) {
        popTopNPages(1, animated, hint);
    }

    public void popTopNPages(int n, boolean animated, boolean hint) {
        if (n <= 0 || mPageStack.size() <= 0) {
            return;
        }

        Page oldPage = mPageStack.removeLast();
        --n;    // for mPageStack.removeLast() above

        while (--n >= 0) {
            Page page = mPageStack.removeLast();
            mContainerView.removeView(page.getView());
            page.onDetach();
            page.onHidden();

            if (DEBUG) {
                Log.d(TAG, String.format(">>>> popPage, pagestack=%d, %s", mPageStack.size(), page));
            }
        }

        popPageInternal(oldPage, animated, hint);
    }

    /**
     * "pop" operation ends if destPage is found,
     * if destPage is not found, the method call is a no-op
     *
     * @param destPage page as the destination for this pop operation
     * @param animated
     * @param hint
     */
    public void popToPage(Page destPage, boolean animated, boolean hint) {
        if (destPage == null) {
            throw new IllegalArgumentException("cannot call popToPage() with null destPage.");
        }

        if (mPageStack.size() <= 0 || mPageStack.lastIndexOf(destPage) == -1 || mPageStack.peekLast() == destPage) {
            return;
        }

        Page oldPage = mPageStack.removeLast();

        if (DEBUG) {
            Log.d(TAG, String.format(">>>> popPage, pagestack=%d, %s", mPageStack.size(), oldPage));
        }

        while (mPageStack.size() > 1) {
            if (mPageStack.peekLast() == destPage) {
                break;
            }

            Page page = mPageStack.removeLast();
            mContainerView.removeView(page.getView());
            page.onDetach();
            page.onHidden();
        }

        popPageInternal(oldPage, animated, hint);
    }

    /**
     * "pop" operation ends if the pageClass is found,
     * if the class is not found, the method call is a no-op
     *
     * @param pageClass class of page as the destination for this pop operation
     * @param animated
     * @param hint
     */
    public void popToClass(Class<? extends Page> pageClass, boolean animated, boolean hint) {
        if (pageClass == null) {
            throw new IllegalArgumentException("cannot call popToClass() with null pageClass.");
        }

        popToClasses(new Class[]{ pageClass }, animated, hint);
{'fraction_non_alphanumeric': 0.10871876661350345, 'fraction_numerical': 0.004253056884635832, 'mean_word_length': 2.1580861779518745, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1115013', 'n_tokens_mistral': 3260, 'n_tokens_neox': 3134, 'n_words': 800}