<commit_before>package org.codepond.wizardroid;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.ViewGroup;

import org.codepond.wizardroid.infrastructure.Bus;
import org.codepond.wizardroid.infrastructure.Disposable;
import org.codepond.wizardroid.infrastructure.Subscriber;
import org.codepond.wizardroid.infrastructure.events.StepCompletedEvent;
import org.codepond.wizardroid.persistence.ContextManager;

/**
 * The engine of the Wizard. This class controls the flow of the wizard
 * and is using {@link ViewPager} under the hood. You would normally want to
 * extend {@link WizardFragment} instead of using this class directly and make calls to the wizard API
 * via {@link org.codepond.wizardroid.WizardFragment#wizard} field. Use this
 * class only if you wish to create a custom WizardFragment to control the wizard.
 */
public class Wizard implements Disposable, Subscriber {
    /**
     * Interface for key wizard events. Implement this interface if you wish to create
     * a custom WizardFragment.
     */
    public static interface WizardCallbacks {
        /**
         * Event called when the wizard is completed
         */
        public void onWizardComplete();

        /**
         * Event called after a step was changed
         */
        public void onStepChanged();
    }

    private static final String TAG = Wizard.class.getSimpleName();
	private final WizardFlow wizardFlow;
    private final ContextManager contextManager;
    private final WizardCallbacks callbacks;
    private final ViewPager mPager;

    private boolean fingerSlide;


    /**
     * Constructor for Wizard
     * @param wizardFlow WizardFlow instance. See WizardFlow.Builder for more information on creating WizardFlow objects.
     * @param contextManager ContextManager instance would normally be {@link org.codepond.wizardroid.persistence.ContextManagerImpl}
     * @param callbacks implementation of WizardCallbacks
     * @param activity the hosting activity
     */
	public Wizard(final WizardFlow wizardFlow,
                  final ContextManager contextManager,
                  final WizardCallbacks callbacks,
                  final FragmentActivity activity) {
		this.wizardFlow = wizardFlow;
        this.contextManager = contextManager;
        this.callbacks = callbacks;
        this.mPager = (ViewPager) activity.findViewById(R.id.step_container);
        if (mPager == null) {
            throw new RuntimeException("Cannot initialize Wizard. View with ID: step_container not found!" +
                    " The hosting Activity/Fragment must have a ViewPager in its layout with ID: step_container");
        }
        mPager.setAdapter(new WizardPagerAdapter(activity.getSupportFragmentManager()));

        //Implementation of OnPageChangeListener to handle wizard control via user finger slides
        mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            private boolean initialOffsetIsSet;
            private float initialOffset;
            private boolean consumedPageSelectedEvent;

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //Check if the page started to be dragged by the user
                //and avoid positionOffset 0
                if (!initialOffsetIsSet && positionOffset > 0) {
                    //Store the initialOffset for later comparison
                    initialOffset = positionOffset;
                    //Signal that initial offset has been set for the current page drag sequence
                    initialOffsetIsSet = true;
                }
                //Check slide direction and decide if to call goNext() or goBack()
                //Once page is "selected" (visible to the user)), skip checking slide direction
                if (!consumedPageSelectedEvent && positionOffset > 0) {
                    if (positionOffset > initialOffset) {
                        //Sliding right
                        goNext();
                        fingerSlide = true;
                    }
                    else if (positionOffset < initialOffset){
                        //Sliding left
                        goBack();
                        fingerSlide = true;
                    }
                }
            }

            @Override
            public void onPageSelected(int position) {
                //Signal that the page is now "selected"
                consumedPageSelectedEvent = true;
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                if (state == ViewPager.SCROLL_STATE_IDLE) {
                    //No animation is on going, reset flags
                    consumedPageSelectedEvent = false;
                    initialOffsetIsSet = false;
                    fingerSlide = false;
                }

            }
        });
        Bus.getInstance().register(this, StepCompletedEvent.class);
	}

    @Override
    public void dispose() {<commit_msg>Alter back key press behavior<commit_after>package org.codepond.wizardroid;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentManager.OnBackStackChangedListener;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.ViewGroup;

import org.codepond.wizardroid.infrastructure.Bus;
import org.codepond.wizardroid.infrastructure.Disposable;
import org.codepond.wizardroid.infrastructure.Subscriber;
import org.codepond.wizardroid.infrastructure.events.StepCompletedEvent;
import org.codepond.wizardroid.persistence.ContextManager;

/**
 * The engine of the Wizard. This class controls the flow of the wizard
 * and is using {@link ViewPager} under the hood. You would normally want to
 * extend {@link WizardFragment} instead of using this class directly and make calls to the wizard API
 * via {@link org.codepond.wizardroid.WizardFragment#wizard} field. Use this
 * class only if you wish to create a custom WizardFragment to control the wizard.
 */
public class Wizard implements Disposable, Subscriber {
    /**
     * Interface for key wizard events. Implement this interface if you wish to create
     * a custom WizardFragment.
     */
    public static interface WizardCallbacks {
        /**
         * Event called when the wizard is completed
         */
        public void onWizardComplete();

        /**
         * Event called after a step was changed
         */
        public void onStepChanged();
    }

    private static final String TAG = Wizard.class.getSimpleName();
	private final WizardFlow wizardFlow;
    private final ContextManager contextManager;
    private final WizardCallbacks callbacks;
    private final ViewPager mPager;
    private final FragmentManager mFragmentManager;

    private boolean fingerSlide;
    private int backStackEntryCount;


    /**
     * Constructor for Wizard
     * @param wizardFlow WizardFlow instance. See WizardFlow.Builder for more information on creating WizardFlow objects.
     * @param contextManager ContextManager instance would normally be {@link org.codepond.wizardroid.persistence.ContextManagerImpl}
     * @param callbacks implementation of WizardCallbacks
     * @param activity the hosting activity
     */
	public Wizard(final WizardFlow wizardFlow,
                  final ContextManager contextManager,
                  final WizardCallbacks callbacks,
                  final FragmentActivity activity) {
		this.wizardFlow = wizardFlow;
        this.contextManager = contextManager;
        this.callbacks = callbacks;
        this.mPager = (ViewPager) activity.findViewById(R.id.step_container);
        this.mFragmentManager = activity.getSupportFragmentManager();

        if (mPager == null) {
            throw new RuntimeException("Cannot initialize Wizard. View with ID: step_container not found!" +
                    " The hosting Activity/Fragment must have a ViewPager in its layout with ID: step_container");
        }

        mPager.setAdapter(new WizardPagerAdapter(activity.getSupportFragmentManager()));

        backStackEntryCount = mFragmentManager.getBackStackEntryCount();
        mFragmentManager.addOnBackStackChangedListener(new OnBackStackChangedListener() {
            @Override
            public void onBackStackChanged() {
                backStackEntryCount = mFragmentManager.getBackStackEntryCount();

                //onBackPressed
                if (backStackEntryCount < getCurrentStepPosition()){
                    mPager.setCurrentItem(getCurrentStepPosition() - 1);
                }
            }
        });

        //Implementation of OnPageChangeListener to handle wizard control via user finger slides
        mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            private boolean initialOffsetIsSet;
            private float initialOffset;
            private boolean consumedPageSelectedEvent;

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //Check if the page started to be dragged by the user
                //and avoid positionOffset 0
                if (!initialOffsetIsSet && positionOffset > 0) {
                    //Store the initialOffset for later comparison
                    initialOffset = positionOffset;
                    //Signal that initial offset has been set for the current page drag sequence
                    initialOffsetIsSet = true;
                }
                //Check slide direction and decide if to call goNext() or goBack()
                //Once page is "selected" (visible to the user)), skip checking slide direction
                if (!consumedPageSelectedEvent && positionOffset > 0) {
                    if (positionOffset > initialOffset) {
                        //Sliding right
                        goNext();
                        fingerSlide = true;
                    }
                    else if (positionOffset < initialOffset){
                        //Sliding left
                        goBack();
                        fingerSlide = true;
                    }
                }
            }

            @Override
            public void onPageSelected(int position) {
                //Signal that the page is now "selected"
                if (backStackEntryCount < position){
                    mFragmentManager.beginTransaction().addToBackStack(null).commit();
                }
                else if (backStackEntryCount > position){
                    mFragmentManager.popBackStack();
                }
                consumedPageSelectedEvent = true;
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                if (state == ViewPager.SCROLL_STATE_IDLE) {
                    //No animation is on going, reset flags
                    consumedPageSelectedEvent = false;
                    initialOffsetIsSet = false;
                    fingerSlide = false;
                }

            }
        });
        Bus.getInstance().register(this, StepCompletedEvent.class);
	}

    @Override
    public void dispose() {
{'fraction_non_alphanumeric': 0.06537362542211447, 'fraction_numerical': 0.0015585765001298814, 'mean_word_length': 2.166986564299424, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 10, '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': '2402414', 'n_tokens_mistral': 2777, 'n_tokens_neox': 2614, 'n_words': 842}