package yuku.alkitab.base.widget;

import android.content.Context;
import android.support.v4.view.MotionEventCompat;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Button;

public class GotoButton extends Button {
	public static final String TAG = GotoButton.class.getSimpleName();

	public interface FloaterDragListener {
		void onFloaterDragStart(float screenX, float screenY);
		void onFloaterDragMove(float screenX, float screenY);
		void onFloaterDragComplete(float screenX, float screenY);
	}

	int[] screenLocation = {0, 0};
	boolean inFloaterDrag;
	boolean inLongClicked;
	FloaterDragListener floaterDragListener;

	public GotoButton(final Context context) {
		super(context);
	}

	public GotoButton(final Context context, final AttributeSet attrs) {
		super(context, attrs);
	}

	public GotoButton(final Context context, final AttributeSet attrs, final int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	public boolean onTouchEvent(final MotionEvent event) {
		final int action = MotionEventCompat.getActionMasked(event);

		float x = event.getX();
		float y = event.getY();

		getLocationOnScreen(screenLocation);
		float screenX = x + screenLocation[0];
		float screenY = y + screenLocation[1];

		if (action == MotionEvent.ACTION_DOWN) { // reset long-clicked status
			inLongClicked = false;
		}

		if (!inLongClicked) { // do not continue if finger is still down but it's because long click is in progress
			if (!inFloaterDrag) {
				if (action == MotionEvent.ACTION_MOVE) {
					if (x < 0 || y < 0 || x > getWidth() || y > getHeight()) {
						cancelLongPress();
						inFloaterDrag = true;
						floaterDragListener.onFloaterDragStart(screenX, screenY);
					}
				}
			}

			// do not use "else"!
			if (inFloaterDrag) {
				if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
					inFloaterDrag = false;
					floaterDragListener.onFloaterDragComplete(screenX, screenY);
				} else {
					floaterDragListener.onFloaterDragMove(screenX, screenY);
				}
			}
		}

		return super.onTouchEvent(event);
	}

	public void setFloaterDragListener(final FloaterDragListener floaterDragListener) {
		this.floaterDragListener = floaterDragListener;
	}

	@Override
	public boolean performLongClick() {
		inLongClicked = true;
		return super.performLongClick();
	}
}

@@ -60,16 +60,58 @@
 ontext;%0A
+import android.content.res.Configuration;%0A
 import a
@@ -248,16 +248,45 @@
 .Button;
+%0Aimport yuku.alkitab.debug.R;
 %0A%0Apublic
@@ -686,16 +686,63 @@
 licked;%0A
+%09int untouchableSideWidth = Integer.MIN_VALUE;%0A
 %09Floater
@@ -1087,177 +1087,695 @@
 e%0A%09p
-ublic boolean onTouchEvent(final MotionEvent event) %7B%0A%09%09final int action = MotionEventCompat.getActionMasked(event);%0A%0A%09%09float x = event.getX();%0A%09%09float y = event.getY();
+rotected void onConfigurationChanged(final Configuration newConfig) %7B%0A%09%09super.onConfigurationChanged(newConfig);%0A%0A%09%09untouchableSideWidth = Integer.MIN_VALUE;%0A%09%7D%0A%0A%09@Override%0A%09public boolean onTouchEvent(final MotionEvent event) %7B%0A%09%09final int action = MotionEventCompat.getActionMasked(event);%0A%0A%09%09float x = event.getX();%0A%09%09float y = event.getY();%0A%0A%09%09if (untouchableSideWidth == Integer.MIN_VALUE) %7B%0A%09%09%09untouchableSideWidth = getResources().getDimensionPixelSize(R.dimen.nav_prevnext_width) - getResources().getDimensionPixelSize(R.dimen.nav_goto_side_margin);%0A%09%09%7D%0A%0A%09%09if (x %3E= 0 && x %3C untouchableSideWidth %7C%7C x %3C getWidth() && x %3E= getWidth() - untouchableSideWidth) %7B%0A%09%09%09return false;%0A%09%09%7D
 %0A%0A%09%09

Fix bug where left/right verse navigation buttons were very hard to click