/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.support.design.widget;

import android.os.Build;
import android.support.v4.view.ViewCompat;
import android.view.View;

/**
 * Utility helper for moving a {@link android.view.View} around using
 * {@link android.view.View#offsetLeftAndRight(int)} and
 * {@link android.view.View#offsetTopAndBottom(int)}.
 * <p>
 * Also the setting of absolute offsets (similar to translationX/Y), rather than additive
 * offsets.
 */
class ViewOffsetHelper {

    private final View mView;

    private int mLayoutTop;
    private int mLayoutLeft;
    private int mOffsetTop;
    private int mOffsetLeft;

    public ViewOffsetHelper(View view) {
        mView = view;
    }

    public void onViewLayout() {
        // Now grab the intended top
        mLayoutTop = mView.getTop();
        mLayoutLeft = mView.getLeft();

        // And offset it as needed
        updateOffsets();
    }

    private void updateOffsets()  {
        if (Build.VERSION.SDK_INT == 22) {
            // FIXME When running on L MR1, we have a problem with view's which are offset off
            // screen then back on. Using translationX/Y fixes it for now.
            ViewCompat.setTranslationY(mView, mOffsetTop);
            ViewCompat.setTranslationX(mView, mOffsetLeft);
        } else {
            // Else we can use the offset position methods
            ViewCompat.offsetTopAndBottom(mView, mOffsetTop - mView.getTop() - mLayoutTop);
            ViewCompat.offsetLeftAndRight(mView, mOffsetLeft - mView.getLeft() - mLayoutLeft);
        }
    }

    /**
     * Set the top and bottom offset for this {@link ViewOffsetHelper}'s view.
     *
     * @param offset the offset in px.
     * @return true if the offset has changed
     */
    public boolean setTopAndBottomOffset(int offset) {
        if (mOffsetTop != offset) {
            mOffsetTop = offset;
            updateOffsets();
            return true;
        }
        return false;
    }

    /**
     * Set the left and right offset for this {@link ViewOffsetHelper}'s view.
     *
     * @param offset the offset in px.
     * @return true if the offset has changed
     */
    public boolean setLeftAndRightOffset(int offset) {
        if (mOffsetLeft != offset) {
            mOffsetLeft = offset;
            updateOffsets();
            return true;
        }
        return false;
    }

    public int getTopAndBottomOffset() {
        return mOffsetTop;
    }

    public int getLeftAndRightOffset() {
        return mOffsetLeft;
    }
}
@@ -658,33 +658,8 @@
 t;%0A%0A
-import android.os.Build;%0A
 impo
@@ -722,16 +722,48 @@
 ew.View;
+%0Aimport android.view.ViewParent;
 %0A%0A/**%0A *
@@ -1562,208 +1562,46 @@
 if (
-Build.VERSION.SDK_INT == 22) %7B%0A            // FIXME When running on L MR1, we have a problem with view's which are offset off%0A            // screen then back on. Using translationX/Y fixes it for now.
+mOffsetLeft != 0 %7C%7C mOffsetTop != 0) %7B
 %0A   
@@ -1624,23 +1624,26 @@
 pat.
+off
 setT
-ranslationY
+opAndBottom
 (mVi
@@ -1652,24 +1652,56 @@
 , mOffsetTop
+ - (mView.getTop() - mLayoutTop)
 );%0A         
@@ -1718,23 +1718,26 @@
 pat.
-setTranslationX
+offsetLeftAndRight
 (mVi
@@ -1755,273 +1755,270 @@
 Left
-);%0A        %7D else %7B%0A            // Else we can use the offset position methods%0A            ViewCompat.offsetTopAndBottom(mView, mOffsetTop - mView.getTop() - mLayoutTop);%0A            ViewCompat.offsetLeftAndRight(mView, mOffsetLeft - mView.getLeft() - mLayoutLeft);
+ - (mView.getLeft() - mLayoutLeft));%0A%0A            // Manually invalidate the parent to make sure we get drawn%0A            ViewParent parent = mView.getParent();%0A            if (parent instanceof View) %7B%0A                ((View) parent).invalidate();%0A            %7D
 %0A   

Fix offset calculation math