package com.paulvarry.intra42.ui;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.support.design.widget.BottomSheetDialogFragment;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;

import com.paulvarry.intra42.R;

public class ListenedBottomSheetDialogFragment extends BottomSheetDialogFragment {

    private DialogInterface.OnDismissListener mTheListener;

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);

        if (mTheListener != null) {
            mTheListener.onDismiss(dialog);
        }
    }

    public void setOnDismissListener(DialogInterface.OnDismissListener listen) {
        mTheListener = listen;
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            int width = getContext().getResources().getDimensionPixelSize(R.dimen.bottom_sheet_width);

            Window window = dialog.getWindow();
            if (window != null) {
                window.setLayout(width > 0 ? width : ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            }
        }
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                BottomSheetDialog showedDialog = (BottomSheetDialog) dialog;
                FrameLayout bottomSheet = showedDialog.findViewById(android.support.design.R.id.design_bottom_sheet);
                if (bottomSheet == null)
                    return;
                BottomSheetBehavior behaviour = BottomSheetBehavior.from(bottomSheet);
                behaviour.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
                    @Override
                    public void onStateChanged(@NonNull View bottomSheet, int newState) {
                        if (newState == BottomSheetBehavior.STATE_EXPANDED) {
                            Log.d("ex", "ex");
                        }
                    }

                    @Override
                    public void onSlide(@NonNull View bottomSheet, float slideOffset) {

                    }
                });
            }
        });
        return dialog;
    }
}

@@ -114,289 +114,55 @@
 oid.
-os.Bundle;%0Aimport android.support.annotation.NonNull;%0Aimport android.support.design.widget.BottomSheetBehavior;%0Aimport android.support.design.widget.BottomSheetDialog;%0Aimport android.support.design.widget.BottomSheetDialogFragment;%0Aimport android.util.Log;%0Aimport android.view.View
+support.design.widget.BottomSheetDialogFragment
 ;%0Aim
@@ -221,43 +221,8 @@
 dow;
-%0Aimport android.widget.FrameLayout;
 %0A%0Aim
@@ -871,19 +871,23 @@
 width = 
-get
+require
 Context(
@@ -1171,32 +1171,32 @@
 ;%0A            %7D%0A
+
         %7D%0A    %7D%0A
@@ -1198,1272 +1198,7 @@
    %7D
-%0A%0A    @NonNull%0A    @Override%0A    public Dialog onCreateDialog(Bundle savedInstanceState) %7B%0A        Dialog dialog = super.onCreateDialog(savedInstanceState);%0A        dialog.setOnShowListener(new DialogInterface.OnShowListener() %7B%0A            @Override%0A            public void onShow(DialogInterface dialog) %7B%0A                BottomSheetDialog showedDialog = (BottomSheetDialog) dialog;%0A                FrameLayout bottomSheet = showedDialog.findViewById(android.support.design.R.id.design_bottom_sheet);%0A                if (bottomSheet == null)%0A                    return;%0A                BottomSheetBehavior behaviour = BottomSheetBehavior.from(bottomSheet);%0A                behaviour.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() %7B%0A                    @Override%0A                    public void onStateChanged(@NonNull View bottomSheet, int newState) %7B%0A                        if (newState == BottomSheetBehavior.STATE_EXPANDED) %7B%0A                            Log.d(%22ex%22, %22ex%22);%0A                        %7D%0A                    %7D%0A%0A                    @Override%0A                    public void onSlide(@NonNull View bottomSheet, float slideOffset) %7B%0A%0A                    %7D%0A                %7D);%0A            %7D%0A        %7D);%0A        return dialog;%0A    %7D
 %0A%7D%0A

remove useless state listener on bottom sheet