package com.bryanjswift.simplenote.view;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ScrollView;
import com.bryanjswift.simplenote.Constants;

/**
 * Custom EditText to allow the ScrollView to handle displaying the proper rectangle by killing requests for
 * different rectangles
 * @author bryanjswift
 */
public class ScrollWrappableEditText extends EditText {
    private static final String LOGGING_TAG = Constants.TAG + ScrollWrappableEditText.class.getSimpleName();
    private OnChangeListener onChangeListener = null;
    private String oldText = null;

    /**
     * Default constructor
     * @param context to which this View is being added
     */
    public ScrollWrappableEditText(Context context) {
        super(context);
        oldText = getText().toString();
    }

    /**
     * Default constructor
     * @param context to which this View is being added
     * @param attrs used when creating the View
     */
    public ScrollWrappableEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        oldText = getText().toString();
    }

    /**
     * Default constructor
     * @param context to which this View is being added
     * @param attrs used when creating the View
     * @param defStyle id of style definition to add to attrs
     */
    public ScrollWrappableEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        oldText = getText().toString();
    }

    /**
     * Don't request this view move if it is wrapped in a ScrollView
     * @see android.view.View#requestRectangleOnScreen(android.graphics.Rect, boolean)
     */
    @Override
    public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
        // Detect text changes here because onKey only fires for certain key presses
        final String newText = getText().toString();
        if (onChangeListener != null && !newText.equals(oldText)) {
            onChangeListener.onChange(this, oldText, newText);
            oldText = newText;
        }
        // Always return true, the ScrollView around this will handle the proper rectangle being on screen
        if (getParent() instanceof ScrollView) {
            ((ScrollView) getParent()).scrollBy(rectangle.left - getLeft(), rectangle.top - getTop());
            return true;
        } else {
            return super.requestRectangleOnScreen(rectangle, immediate);
        }
    }

    /**
     * Get the OnChangeListener
     * @return OnChangeListener set on thie instance
     */
    public OnChangeListener getOnChangeListener() {
        return onChangeListener;
    }

    /**
     * Set the OnChangeListener
     * @param onChangeListener to set
     */
    public void setOnChangeListener(OnChangeListener onChangeListener) {
        this.onChangeListener = onChangeListener;
    }

    /**
     * Interface for firing change events on edit text
     */
    public interface OnChangeListener {
        public void onChange(View v, String oldText, String newText);
    }
}

@@ -1851,16 +1851,22 @@
 nScreen(
+final 
 Rect rec
@@ -1872,16 +1872,22 @@
 ctangle,
+ final
  boolean
@@ -2222,256 +2222,855 @@
     
-// Always return true, the ScrollView around this will handle the proper rectangle being on screen%0A        if (getParent() instanceof ScrollView) %7B%0A            ((ScrollView) getParent()).scrollBy(rectangle.left - getLeft(), rectangle.top - getTop()
+if (getParent() instanceof ScrollView) %7B%0A            // Move the EditText to 0,0%0A            scrollTo(0, 0);%0A            // Handle moving the surrounding ScrollView so the requested rectangle is in view%0A            final int paddingTop = getPaddingTop();%0A            final int paddingLeft = getPaddingLeft();%0A            final int top;%0A            Log.d(LOGGING_TAG, String.format(%22paddingTop: %25d :: paddingLeft: %25d%22, paddingTop, paddingLeft));%0A            if (rectangle.top %3C paddingTop) %7B%0A                top = 0;%0A            %7D else %7B%0A                top = rectangle.top - paddingTop;%0A            %7D%0A            final int left;%0A            if (rectangle.left %3C paddingTop) %7B%0A                left = 0;%0A            %7D else %7B%0A                left = rectangle.left - paddingLeft;%0A            %7D%0A            ((ScrollView) getParent()).scrollTo(left, top
 );%0A 

Change how requested rectangles are processed