package com.kaichunlin.transition.adapter;

import android.app.Activity;
import android.support.annotation.Nullable;
import android.view.View;

import com.kaichunlin.transition.ITransition;
import com.sothree.slidinguppanel.SlidingUpPanelLayout;

/**
 * Adapter for SlidingUpPanelLayout, the transition range goes from 0.0f to 1.0f, where 0.0f is the collapsed state and 1.0f is the expanded state.
 * <p>
 * Created by Kai-Chun Lin on 2015/4/14.
 */
public class SlidingUpPanelLayoutAdapter extends MenuBaseAdapter implements SlidingUpPanelLayout.PanelSlideListener {
    private boolean mIsExpanded;
    private SlidingUpPanelLayout.PanelSlideListener mListener;

    /**
     * Sets a listener that can provide further customization, the respective calls to the listener is performed after the adapter has completed its own processing.
     *
     * @param listener
     */
    public void setPanelSlideListener(@Nullable SlidingUpPanelLayout.PanelSlideListener listener) {
        mListener = listener;
    }

    @Override
    public void onPanelSlide(View panel, float slideOffset) {
        startTransition();
        for (ITransition trans : mTransitionList.values()) {
            trans.updateProgress(slideOffset);
        }

        if (mListener != null) {
            mListener.onPanelSlide(panel, slideOffset);
        }
    }

    @Override
    public void stopTransition() {
        super.stopTransition();
    }

    @Override
    public void onPanelCollapsed(View panel) {
        stopTransition();
        mIsExpanded = false;

        if (mListener != null) {
            mListener.onPanelCollapsed(panel);
        }
    }

    @Override
    public void onPanelExpanded(View panel) {
        stopTransition();
        mIsExpanded = true;

        if (mListener != null) {
            mListener.onPanelExpanded(panel);
        }
    }

    @Override
    public void onPanelAnchored(View panel) {
        stopTransition();

        if (mListener != null) {
            mListener.onPanelAnchored(panel);
        }
    }

    @Override
    public void onPanelHidden(View panel) {
        stopTransition();

        if (mListener != null) {
            mListener.onPanelHidden(panel);
        }
    }

    @Override
    protected IMenuOptionHandler createMenuHandler() {
        return new DefaultMenuOptionHandler(this, new IMenuOptionHandler.AdapterState() {
            @Override
            public boolean isOpened(Activity activity) {
                return mIsExpanded;
            }
        });
    }
}

@@ -41,37 +41,8 @@
 r;%0A%0A
-import android.app.Activity;%0A
 impo
@@ -537,41 +537,8 @@
 r %7B%0A
-    private boolean mIsExpanded;%0A
     
@@ -1307,180 +1307,119 @@
 oid 
-stopTransition() %7B%0A        super.stopTransition();%0A    %7D%0A%0A    @Override%0A    public void onPanelCollapsed(View panel) %7B%0A        stopTransition();%0A        mIsExpanded = false
+onPanelCollapsed(View panel) %7B%0A        getAdapterState().setState(AdapterState.CLOSE);%0A        stopTransition()
 ;%0A%0A 
@@ -1586,52 +1586,79 @@
     
-stopTransition();%0A        mIsExpanded = true
+getAdapterState().setState(AdapterState.OPEN);%0A        stopTransition()
 ;%0A%0A 
@@ -2237,186 +2237,25 @@
 is, 
-new IMenuOptionHandler.AdapterState() %7B%0A            @Override%0A            public boolean isOpened(Activity activity) %7B%0A                return mIsExpanded;%0A            %7D%0A        %7D
+getAdapterState()
 );%0A 

Update SlidingUpPanelAdapter