package im.ene.lab.obervablescrollers.sample.ui;

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

import butterknife.ButterKnife;
import butterknife.InjectView;
import im.ene.lab.obervablescrollers.sample.R;
import im.ene.lab.obervablescrollers.sample.adapter.DummyDynamicListViewAdapter;
import im.ene.lab.obervablescrollers.sample.util.UIUtil;
import im.ene.lab.observablescrollers.lib.util.LogHelper;
import im.ene.lab.observablescrollers.lib.util.OnScrollObservedListener;
import im.ene.lab.observablescrollers.lib.util.Scrollable;
import im.ene.lab.observablescrollers.lib.widget.ObsListView;

public class ObsListViewFlexibleSpaceWithImageActivity extends BaseActivity {

    public static final String TAG = LogHelper.createLogTag(ObsListViewFlexibleSpaceWithImageActivity.class);

    @InjectView(R.id.listview)
    ObsListView mListView;

    @InjectView(R.id.overlay)
    View mOverLayView;
    @InjectView(R.id.image)
    View mImageView;
    @InjectView(R.id.title)
    View mTitleView;
    @InjectView(R.id.list_background)
    View mListBackground;

    private int mMaxTransition;
    private int mActionBarHeight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_obs_list_view_flexible_space_with_image);
        ButterKnife.inject(this);

        mMaxTransition = getResources().getDimensionPixelSize(R.dimen.flexible_space_image_height);
        mActionBarHeight = UIUtil.getActionbarToolbarHeight(this);
    }

    DummyDynamicListViewAdapter dummyListViewAdapter;

    private final float MAX_TEXT_SCALE_DELTA = 0.3f;

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        ViewCompat.setPivotX(mTitleView, 0);
        ViewCompat.setPivotY(mTitleView, 0);

        dummyListViewAdapter = new DummyDynamicListViewAdapter(this);
        mListView.setAdapter(dummyListViewAdapter);
        mListView.setPadding(mListView.getPaddingLeft(), (int) (mListView.getPaddingTop() + getMaxTransition()),
                mListView.getPaddingRight(), mListView.getPaddingBottom());

        mListView.setOnScrollObservedListener(new OnScrollObservedListener() {
            @Override
            public void onScrollChanged(Scrollable scroller, int dx, int dy) {
                int scrollY = scroller.getVerticalScrollOffset();

                float transition = Math.min(0, Math.max(-getMaxTranslationYRange(), -scrollY));
                ViewCompat.setTranslationY(mOverLayView, transition);
                ViewCompat.setTranslationY(mListBackground, Math.max(0, getMaxTransition() - scrollY));

                float alpha = Math.min(1, Math.max(0, (float) scrollY / getMaxTranslationYRange()));
                ViewCompat.setAlpha(mOverLayView, alpha);

                float parallax = Math.min(0, Math.max(-getMaxTranslationYRange(), (float) -scrollY / 2));
                ViewCompat.setTranslationY(mImageView, parallax);

                float scale = 1 + MAX_TEXT_SCALE_DELTA * Math.min(1.0f, Math.max(0.0f,
                        (getMaxTranslationYRange() - (float) scrollY) / getMaxTranslationYRange()));
                ViewCompat.setScaleX(mTitleView, scale);
                ViewCompat.setScaleY(mTitleView, scale);

                // Translate title text
                int maxTitleTranslationY = (int) (getMaxTransition() - mTitleView.getHeight() * scale);
                float titleTranslationY = Math.min(getMaxTransition() - mTitleView.getHeight() * (1 + MAX_TEXT_SCALE_DELTA),
                        maxTitleTranslationY - scrollY);
                ViewCompat.setTranslationY(mTitleView, titleTranslationY);
            }

            @Override
            public void onScrollStateChanged(Scrollable scroller, Scrollable.ScrollState newState) {

            }
        });

    }

    @Override
    protected float getMinTransition() {
        return mActionBarHeight;
    }

    @Override
    protected float getMaxTransition() {
        return mMaxTransition;
    }

}

@@ -1504,16 +1504,107 @@
 height);
+%0A%0A        // this activity doesn't use an actionbar/toolbar, we use a static height instead
 %0A       
@@ -1674,16 +1674,24 @@
  %7D%0A%0A    
+private 
 DummyDyn
@@ -1907,24 +1907,97 @@
 nceState);%0A%0A
+        // set this only once, nothing to do with RTL layout, please DIY%0A
         View
@@ -2071,24 +2071,124 @@
 eView, 0);%0A%0A
+        // this Adapter is not that good, I just add the dynamic height change to its children view%0A
         dumm
@@ -2296,16 +2296,72 @@
 dapter);
+%0A%0A        // update paddingTop to match the whole layout
 %0A       
@@ -3390,24 +3390,219 @@
 parallax);%0A%0A
+                // originally borrowed from https://github.com/ksoichiro/Android-ObservableScrollView%0A                // but I change it to a multiply to smoothly transiting/scaling the textview%0A
             
@@ -4431,16 +4431,56 @@
 tate) %7B%0A
+                // no magic happens here
 %0A       

add comments