<commit_before>package com.sak.ultilviewlib;

import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ScrollView;

/**
 * Created by engineer on 2017/4/19.
 */

public class UltimateRefreshView extends LinearLayout {
    private static final String TAG = "UtrilPulltoRefreshView";
    // refresh states
    private static final int PULL_TO_REFRESH = 2;
    private static final int RELEASE_TO_REFRESH = 3;
    private static final int REFRESHING = 4;
    // pull state
    private static final int PULL_UP_STATE = 0;
    private static final int PULL_DOWN_STATE = 1;

    /**
     * list or grid
     */
    private AdapterView<?> mAdapterView;
    /**
     * RecyclerView
     */
    private RecyclerView mRecyclerView;
    /**
     * ScrollView
     */
    private ScrollView mScrollView;
    /**
     * WebView
     */
    private WebView mWebView;

    private int mHeaderState;
    /**
     * pull state,pull up or pull down;PULL_UP_STATE or PULL_DOWN_STATE
     */
    private int mPullState;

    private View headerView;

    private int headViewHeight;

    //action
    private int lastY;

    private BaseHeaderAdapter mBaseHeaderAdapter;

    public void setBaseHeaderAdapter(BaseHeaderAdapter baseHeaderAdapter) {
        mBaseHeaderAdapter = baseHeaderAdapter;
    }

    public UltimateRefreshView(Context context) {
        super(context);
        init(context);
    }

    public UltimateRefreshView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public UltimateRefreshView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        setOrientation(VERTICAL);
        if (mBaseHeaderAdapter == null) {
            mBaseHeaderAdapter = new ProgressBarAdapter(context);
        }
        initView();
    }

    private void initView() {
        headerView = mBaseHeaderAdapter.getHeaderView();
        measureView(headerView);
        headViewHeight = headerView.getMeasuredHeight();
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, headViewHeight);
        params.topMargin = -headViewHeight;
        addView(headerView, params);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        initSubViewType();
    }

    private void initSubViewType() {
        int count = getChildCount();
        if (count < 2) {
            throw new IllegalArgumentException(
                    "this layout must contain 2 child views,and AdapterView or ScrollView must in the second position!");
        }

        View view = getChildAt(1);

        if (view instanceof AdapterView<?>) {
            mAdapterView = (AdapterView<?>) view;
        }

        if (view instanceof RecyclerView) {
            mRecyclerView = (RecyclerView) view;
        }

        if (view instanceof ScrollView) {
            mScrollView = (ScrollView) view;
        }

        if (view instanceof WebView) {
            mWebView = (WebView) view;
        }

    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        int y = (int) ev.getRawY();
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastY = y;
                break;
            case MotionEvent.ACTION_MOVE:
                int deltaY = y - lastY;
                if (isParentViewScroll(deltaY)) {
                    Log.e(TAG, "onInterceptTouchEvent: belong to ParentView");
                    return true; //此时,触发onTouchEvent事件
                }
                break;
            default:
                break;
        }
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int y = (int) event.getRawY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                int deltaY = y - lastY;
                if (mPullState == PULL_DOWN_STATE) {
                    Log.e(TAG, "onTouchEvent: pull down begin-->" + deltaY);
                    initHeaderViewToRefresh(deltaY);
                }
                lastY = y;
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                int topMargin = getHeaderTopMargin();
                Log.e(TAG, "onTouchEvent: topMargin==" + topMargin);
                if (mPullState == PULL_DOWN_STATE) {
                    if (topMargin >= 0) {
                        headerRefreshing();
                    } else {
                        setHeaderTopMargin(-headViewHeight);
                    }
                }
                break;
        }

        return super.onTouchEvent(event);
    }

    private void initHeaderViewToRefresh(int deltaY) {
        int topDistance = UpdateHeadViewMarginTop(deltaY);
        if (topDistance < 0 && topDistance > -headViewHeight) {
            mBaseHeaderAdapter.pullViewToRefresh();
            mHeaderState = PULL_TO_REFRESH;
        } else if (topDistance > 0 && mHeaderState != RELEASE_TO_REFRESH) {
            mBaseHeaderAdapter.releaseViewToRefresh();
            mHeaderState = RELEASE_TO_REFRESH;
        }

    }

    private int UpdateHeadViewMarginTop(int deltaY) {
        LayoutParams params = (LayoutParams) headerView.getLayoutParams();
        float topMargin = params.topMargin + deltaY * 0.3f;
        params.topMargin = (int) topMargin;
        headerView.setLayoutParams(params);
        invalidate();
        return params.topMargin;
    }


    private void headerRefreshing() {
        mHeaderState = REFRESHING;
        setHeaderTopMargin(0);
        mBaseHeaderAdapter.headerRefreshing();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                refreshComplete();
            }
        }, 2000);
    }

    private void refreshComplete() {
        setHeaderTopMargin(-headViewHeight);
        mBaseHeaderAdapter.headerRefreshComplete();<commit_msg>fix bugs<commit_after>package com.sak.ultilviewlib;

import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ScrollView;

/**
 * Created by engineer on 2017/4/19.
 */

public class UltimateRefreshView extends LinearLayout {
    private static final String TAG = "UtrilPulltoRefreshView";
    // refresh states
    private static final int PULL_TO_REFRESH = 2;
    private static final int RELEASE_TO_REFRESH = 3;
    private static final int REFRESHING = 4;
    // pull state
    private static final int PULL_UP_STATE = 0;
    private static final int PULL_DOWN_STATE = 1;

    /**
     * list or grid
     */
    private AdapterView<?> mAdapterView;
    /**
     * RecyclerView
     */
    private RecyclerView mRecyclerView;
    /**
     * ScrollView
     */
    private ScrollView mScrollView;
    /**
     * WebView
     */
    private WebView mWebView;

    private int mHeaderState;
    /**
     * pull state,pull up or pull down;PULL_UP_STATE or PULL_DOWN_STATE
     */
    private int mPullState;

    private View headerView;

    private int headViewHeight;

    //action
    private int lastY;

    private BaseHeaderAdapter mBaseHeaderAdapter;

    public void setBaseHeaderAdapter(BaseHeaderAdapter baseHeaderAdapter) {
        mBaseHeaderAdapter = baseHeaderAdapter;
    }

    public UltimateRefreshView(Context context) {
        super(context);
        init(context);
    }

    public UltimateRefreshView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public UltimateRefreshView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        setOrientation(VERTICAL);
        if (mBaseHeaderAdapter == null) {
            mBaseHeaderAdapter = new ProgressBarAdapter(context);
        }
        initView();
    }

    private void initView() {
        headerView = mBaseHeaderAdapter.getHeaderView();
        measureView(headerView);
        headViewHeight = headerView.getMeasuredHeight();
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, headViewHeight);
        params.topMargin = -headViewHeight;
        addView(headerView, params);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        initSubViewType();
    }

    private void initSubViewType() {
        int count = getChildCount();
        if (count < 2) {
            throw new IllegalArgumentException(
                    "this layout must contain 2 child views,and AdapterView or ScrollView must in the second position!");
        }

        View view = getChildAt(1);

        if (view instanceof AdapterView<?>) {
            mAdapterView = (AdapterView<?>) view;
        }

        if (view instanceof RecyclerView) {
            mRecyclerView = (RecyclerView) view;
        }

        if (view instanceof ScrollView) {
            mScrollView = (ScrollView) view;
        }

        if (view instanceof WebView) {
            mWebView = (WebView) view;
        }

    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        int y = (int) ev.getRawY();
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastY = y;
                break;
            case MotionEvent.ACTION_MOVE:
                int deltaY = y - lastY;
                if (isParentViewScroll(deltaY)) {
                    Log.e(TAG, "onInterceptTouchEvent: belong to ParentView");
                    return true; //此时,触发onTouchEvent事件
                }
                break;
            default:
                break;
        }
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int y = (int) event.getRawY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                int deltaY = y - lastY;
                if (mPullState == PULL_DOWN_STATE) {
                    Log.e(TAG, "onTouchEvent: pull down begin-->" + deltaY);
                    initHeaderViewToRefresh(deltaY);
                }
                lastY = y;
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                int topMargin = getHeaderTopMargin();
                Log.e(TAG, "onTouchEvent: topMargin==" + topMargin);
                if (mPullState == PULL_DOWN_STATE) {
                    if (topMargin >= 0) {
                        headerRefreshing();
                    } else {
                        setHeaderTopMargin(-headViewHeight);
                    }
                }
                break;
        }

        return super.onTouchEvent(event);
    }

    private void initHeaderViewToRefresh(int deltaY) {
        int topDistance = UpdateHeadViewMarginTop(deltaY);
        if (topDistance < 0 && topDistance > -headViewHeight) {
            mBaseHeaderAdapter.pullViewToRefresh(deltaY);
            mHeaderState = PULL_TO_REFRESH;
        } else if (topDistance > 0 && mHeaderState != RELEASE_TO_REFRESH) {
            mBaseHeaderAdapter.releaseViewToRefresh(deltaY);
            mHeaderState = RELEASE_TO_REFRESH;
        }

    }

    private int UpdateHeadViewMarginTop(int deltaY) {
        LayoutParams params = (LayoutParams) headerView.getLayoutParams();
        float topMargin = params.topMargin + deltaY * 0.3f;
        params.topMargin = (int) topMargin;
        headerView.setLayoutParams(params);
        invalidate();
        return params.topMargin;
    }


    private void headerRefreshing() {
        mHeaderState = REFRESHING;
        setHeaderTopMargin(0);
        mBaseHeaderAdapter.headerRefreshing();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                refreshComplete();
            }
        }, 2000);
    }

    private void refreshComplete() {
        setHeaderTopMargin(-headViewHeight);
        mBaseHeaderAdapter.headerRefreshComplete();
{'fraction_non_alphanumeric': 0.08515606756441192, 'fraction_numerical': 0.004203315949248852, 'mean_word_length': 2.06342393896042, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, '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': '6080288', 'n_tokens_mistral': 3628, 'n_tokens_neox': 3431, 'n_words': 802}