package com.duben.library.base;

import android.content.Intent;
import androidx.fragment.app.Fragment;

import com.duben.microtribe.R;


/**
 * @author assen
 * @date 2019/7/4
 */
public abstract class TransitionFragment extends Fragment {

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        /*
            judge is the previous page from left to right
         */
        if (!toggleIsBack2Left()) {
            setTransitionAnimation(true);
        } else {
            setTransitionAnimation(false);
        }
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        super.startActivityForResult(intent, requestCode);
        /*
            judge is the previous page from left to right
         */
        if (!toggleIsBack2Left()) {
            setTransitionAnimation(true);
        } else {
            setTransitionAnimation(false);
        }
    }

    /**
     * toggle is back to left
     */
    protected abstract boolean toggleIsBack2Left();

    /**
     * change activity transition animation
     *
     * @param newActivityIn
     */
    private void setTransitionAnimation(boolean newActivityIn) {
        if (newActivityIn) {
            // 新页面从右边进入
            getActivity().overridePendingTransition(R.anim.right_in,
                    R.anim.right_out);
        } else {
            // 上一个页面从左边进入
            getActivity().overridePendingTransition(R.anim.left_in,
                    R.anim.left_out);
        }
    }

}
