DeleteEvent.java 5.17 KB
package net.plil.clubinfo.etunicorn.app.event;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;

import net.plil.clubinfo.etunicorn.R;
import net.plil.clubinfo.etunicorn.app.MainActivity;
import net.plil.clubinfo.etunicorn.data.Event;
import net.plil.clubinfo.etunicorn.utils.VolleyUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;


public class DeleteEvent extends DialogFragment {

    private DialogInterface.OnDismissListener onDismissListener;


    ProgressBar mProgressBar;

    public DeleteEvent() {
    }

    /**
     * Create a new instance of MyDialogFragment, providing "num"
     * as an argument.
     */
    public static DeleteEvent newInstance(Event event) {
        DeleteEvent f = new DeleteEvent();

        // Supply num input as an argument.
        Bundle args = new Bundle();
        args.putSerializable("evenement", event);
        f.setArguments(args);

        return f;
    }


    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.fragment_delete_event, null);

        mProgressBar = (ProgressBar) view.findViewById(R.id.delete_event_progress_bar);

        final Event event = (Event) getArguments().getSerializable("evenement");
        assert event != null;


        AlertDialog dialog = new AlertDialog.Builder(getActivity())
                .setTitle(R.string.verif_delete_event)
                .setView(view)
                .setPositiveButton(R.string.delete, null)
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        VolleyUtils.getInstance(getContext()).getRequestQueue().cancelAll(DeleteEvent.class);
                    }
                }).create();
        dialog.setCanceledOnTouchOutside(false);
        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                Button button = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mProgressBar.setVisibility(View.VISIBLE);

                        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.DELETE, VolleyUtils.baseUri + "/evenement/" + event.getId(), null, new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response) {
                                mProgressBar.setVisibility(View.GONE);
                                Toast.makeText(getContext(), R.string.delete_done, Toast.LENGTH_LONG).show();
                                dismiss();
                            }
                        }, new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                mProgressBar.setVisibility(View.GONE);
                                Toast.makeText(getContext(), R.string.delete_refused, Toast.LENGTH_LONG).show();
                            }
                        }
                        ) {
                            @Override
                            public Map<String, String> getHeaders() throws AuthFailureError {
                                Map<String, String> headers = new HashMap<>();
                                headers.put("Authorization", MainActivity.session.getToken());
                                return headers;
                            }
                        };
                        jsonObjectRequest.setTag(DeleteEvent.class);
                        VolleyUtils.getInstance(getContext()).addToRequestQueue(jsonObjectRequest);
                    }
                });
            }
        });
        return dialog;
    }

    @Override
    public void onStop() {
        VolleyUtils.getInstance(getContext()).getRequestQueue().cancelAll(DeleteEvent.class);
        dismissAllowingStateLoss();
        super.onStop();
    }

    public void setOnDismissListener(DialogInterface.OnDismissListener onDismissListener) {
        this.onDismissListener = onDismissListener;
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        if (onDismissListener != null) {
            onDismissListener.onDismiss(dialog);
        }
    }

}