package com.bryanjswift.simplenote.view;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
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();
    /**
     * Default constructor
     * @param context to which this View is being added
     */
    public ScrollWrappableEditText(Context context) {
        super(context);
    }
    /**
     * 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);
    }
    /**
     * 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);
    }
    /**
     * 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) {
        // Always return true, the ScrollView around this will handle the proper rectangle being on screen
        if (getParent() instanceof ScrollView) {
            Log.d(LOGGING_TAG, "Wrapped in ScrollView tell call request is complete");
            ((ScrollView) getParent()).scrollBy(rectangle.left - getLeft(), rectangle.top - getTop());
            return true;
        } else {
            Log.d(LOGGING_TAG, "Not wrapped by ScrollView, handle normally");
            return super.requestRectangleOnScreen(rectangle, immediate);
        }
    }
}

@@ -156,16 +156,42 @@
 il.Log;%0A
+import android.view.View;%0A
 import a
@@ -244,25 +244,24 @@
 ScrollView;%0A
-%0A
 import com.b
@@ -623,16 +623,106 @@
 Name();%0A
+    private OnChangeListener onChangeListener = null;%0A    private String oldText = null;%0A%0A
     /**%0A
@@ -890,22 +890,63 @@
 ntext);%0A
+        oldText = getText().toString();%0A
     %7D%0A
+%0A
     /**%0A
@@ -1189,22 +1189,63 @@
 attrs);%0A
+        oldText = getText().toString();%0A
     %7D%0A
+%0A
     /**%0A
@@ -1574,22 +1574,63 @@
 Style);%0A
+        oldText = getText().toString();%0A
     %7D%0A
+%0A
     /**%0A
@@ -1884,24 +1884,417 @@
 mmediate) %7B%0A
+        // Detect text changes here because onKey only fires for certain key presses%0A        final String newText = getText().toString();%0A        Log.d(LOGGING_TAG, String.format(%22Comparing %25s to %25s%22, newText, oldText));%0A        if (onChangeListener != null && !newText.equals(oldText)) %7B%0A            onChangeListener.onChange(this, oldText, newText);%0A            oldText = newText;%0A        %7D%0A
         // A
@@ -2441,95 +2441,8 @@
 ) %7B%0A
-            Log.d(LOGGING_TAG, %22Wrapped in ScrollView tell call request is complete%22);%0A
     
@@ -2598,156 +2598,675 @@
     
-Log.d(LOGGING_TAG, %22Not wrapped by ScrollView, handle normally%22)
+return super.requestRectangleOnScreen(rectangle, immediate);%0A        %7D%0A    %7D%0A%0A    /**%0A     * Get the OnChangeListener%0A     * @return OnChangeListener set on thie instance%0A     */%0A    public OnChangeListener getOnChangeListener() %7B%0A        return onChangeListener
 ;%0A    
+%7D%0A%0A
     
-    return super.requestRectangleOnScreen(rectangle, immediate);%0A        %7D
+/**%0A     * Set the OnChangeListener%0A     * @param onChangeListener to set%0A     */%0A    public void setOnChangeListener(OnChangeListener onChangeListener) %7B%0A        this.onChangeListener = onChangeListener;%0A    %7D%0A%0A    /**%0A     * Interface for firing change events on edit text%0A     */%0A    public interface OnChangeListener %7B%0A        public void onChange(View v, String oldText, String newText);
 %0A   

Create an OnChangeListener interface to catch onChange of EditText