package com.tenlionsoft.baselib.core.widget.views;

import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;

public class DoubleAndOneClicklistener implements View.OnTouchListener {
    private static int timeout = 400;
    private int clickCount = 0;
    private Handler handler;
    private ClickCallBack myClickCallBack;


    public interface ClickCallBack {
        void oneClick();

        void doubleClick();

    }


    public DoubleAndOneClicklistener(ClickCallBack myClickCallBack) {
        this.myClickCallBack = myClickCallBack;
        handler = new Handler();
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            clickCount++;
            handler.postDelayed(() -> {
                if (clickCount == 1) {
                    myClickCallBack.oneClick();
                } else if (clickCount == 2) {
                    myClickCallBack.doubleClick();
                }
                handler.removeCallbacksAndMessages(null);
                clickCount = 0;
            }, timeout);
        }
        return false;
    }
}
