package com.github.rubensousa.bottomsheetbuilder;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.support.design.widget.CoordinatorLayout;
import android.util.TypedValue;
import android.view.View;

import com.github.rubensousa.bottomsheetbuilder.adapter.BottomSheetItemClickListener;
import com.github.rubensousa.bottomsheetbuilder.adapter.BottomSheetMenuItem;

public class BottomSheetMenuDialog extends BottomSheetDialog implements BottomSheetItemClickListener {

    private BottomSheetBehavior.BottomSheetCallback mCallback;
    private BottomSheetBehavior mBehavior;
    private BottomSheetItemClickListener mClickListener;

    public BottomSheetMenuDialog(Context context) {
        super(context);
    }

    public BottomSheetMenuDialog(Context context, int theme) {
        super(context, theme);
    }

    @Override
    public void setContentView(View view) {
        getDelegate().setContentView(wrapInBottomSheet(view));
    }

    public void setBottomSheetCallback(BottomSheetBehavior.BottomSheetCallback callback) {
        mCallback = callback;
    }

    public void setBottomSheetItemClickListener(BottomSheetItemClickListener listener) {
        mClickListener = listener;
    }

    public BottomSheetBehavior getBehavior() {
        return mBehavior;
    }

    private View wrapInBottomSheet(View view) {
        final CoordinatorLayout coordinator = (CoordinatorLayout) View.inflate(getContext(),
                R.layout.bottomsheetbuilder_dialog, null);

        CoordinatorLayout.LayoutParams layoutParams
                = new CoordinatorLayout.LayoutParams(CoordinatorLayout.LayoutParams.MATCH_PARENT,
                CoordinatorLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.setBehavior(new BottomSheetBehavior());
        coordinator.addView(view, layoutParams);
        mBehavior = BottomSheetBehavior.from(view);
        mBehavior.setBottomSheetCallback(mBottomSheetCallback);
        mBehavior.setHideable(true);

        if (shouldWindowCloseOnTouchOutside()) {
            coordinator.findViewById(R.id.touch_outside)
                    .setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (isShowing()) {
                                cancel();
                            }
                        }
                    });
        }
        return coordinator;
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (mBehavior != null) {
            mBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        }
    }

    private boolean shouldWindowCloseOnTouchOutside() {
        TypedValue value = new TypedValue();

        return getContext().getTheme()
                .resolveAttribute(android.R.attr.windowCloseOnTouchOutside, value, true)
                && value.data != 0;
    }

    private BottomSheetBehavior.BottomSheetCallback mBottomSheetCallback
            = new BottomSheetBehavior.BottomSheetCallback() {
        @Override
        public void onStateChanged(@NonNull View bottomSheet,
                                   @BottomSheetBehavior.State int newState) {

            if (mCallback != null) {
                mCallback.onStateChanged(bottomSheet, newState);
            }

            if (newState == BottomSheetBehavior.STATE_HIDDEN
                    || newState == BottomSheetBehavior.STATE_COLLAPSED) {
                dismiss();
            }
        }

        @Override
        public void onSlide(@NonNull View bottomSheet, float slideOffset) {
            if (mCallback != null) {
                mCallback.onSlide(bottomSheet, slideOffset);
            }
        }
    };

    @Override
    public void onBottomSheetItemClick(BottomSheetMenuItem item) {
        if (mBehavior != null) {
            mBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
        }

        if (mClickListener != null) {
            mClickListener.onBottomSheetItemClick(item);
        }
    }

}

@@ -252,105 +252,52 @@
 oid.
-support.design.widget.CoordinatorLayout;%0Aimport android.util.TypedValue;%0Aimport android.view.View
+view.View;%0Aimport android.widget.FrameLayout
 ;%0A%0Ai
@@ -915,136 +915,8 @@
  %7D%0A%0A
-    @Override%0A    public void setContentView(View view) %7B%0A        getDelegate().setContentView(wrapInBottomSheet(view));%0A    %7D%0A%0A
     
@@ -1258,1249 +1258,173 @@
     
-private View wrapInBottomSheet(View view) %7B%0A        final CoordinatorLayout coordinator = (CoordinatorLayout) View.inflate(getContext(),%0A                R.layout.bottomsheetbuilder_dialog, null);%0A%0A        CoordinatorLayout.LayoutParams layoutParams%0A                = new CoordinatorLayout.LayoutParams(CoordinatorLayout.LayoutParams.MATCH_PARENT,%0A                CoordinatorLayout.LayoutParams.WRAP_CONTENT);%0A        layoutParams.setBehavior(new BottomSheetBehavior());%0A        coordinator.addView(view, layoutParams);%0A        mBehavior = BottomSheetBehavior.from(view);%0A        mBehavior.setBottomSheetCallback(mBottomSheetCallback);%0A        mBehavior.setHideable(true);%0A%0A        if (shouldWindowCloseOnTouchOutside()) %7B%0A            coordinator.findViewById(R.id.touch_outside)%0A                    .setOnClickListener(new View.OnClickListener() %7B%0A                        @Override%0A                        public void onClick(View v) %7B%0A                            if (isShowing()) %7B%0A                                cancel();%0A                            %7D%0A                        %7D%0A                    %7D);%0A        %7D%0A        return coordinator
+@Override%0A    protected void onStart() %7B%0A        super.onStart();%0A        FrameLayout sheet = (FrameLayout) findViewById(R.id.design_bottom_sheet)
 ;%0A    
-%7D%0A%0A
     
-@Override%0A    protected void onStart() %7B%0A        super.onStart();%0A        if (mBehavior
+if (sheet
  != 
@@ -1448,34 +1448,27 @@
    mBehavior
-.setState(
+ = 
 BottomSheetB
@@ -1479,307 +1479,98 @@
 ior.
-STATE_EXPANDED);%0A        %7D%0A    %7D%0A%0A    private boolean shouldWindowCloseOnTouchOutside() %7B%0A        TypedValue value = new TypedValue();%0A%0A        return getContext().getTheme()%0A                .resolveAttribute(android.R.attr.windowCloseOnTouchOutside, value, true)%0A                && value.data != 0;
+from(sheet);%0A            mBehavior.setBottomSheetCallback(mBottomSheetCallback);%0A        %7D
 %0A   
@@ -2048,79 +2048,8 @@
 DDEN
-%0A                    %7C%7C newState == BottomSheetBehavior.STATE_COLLAPSED
 ) %7B%0A

Remove workarounds since the latest support library fixed some bugs