CreateEvent.java 3.93 KB
package net.plil.clubinfo.etunicorn.app.event;

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

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.consommation.ModifyConsommation;
import net.plil.clubinfo.etunicorn.utils.VolleyUtils;

import org.json.JSONException;
import org.json.JSONObject;


public class CreateEvent extends DialogFragment {

    EditText mName;
    EditText mPrice;
    EditText mDate;
    ProgressBar mProgressBar;


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.fragment_create_event, null);
        mName = (EditText) view.findViewById(R.id.create_event_name);
        mPrice = (EditText) view.findViewById(R.id.create_event_price);
        mDate = (EditText) view.findViewById(R.id.create_event_date);
        mProgressBar = (ProgressBar) view.findViewById(R.id.create_event_progress_bar);
        AlertDialog dialog = new AlertDialog.Builder(getActivity())
                .setTitle(R.string.create_event)
                .setView(view)
                .setPositiveButton(R.string.valid, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                        mProgressBar.setVisibility(View.VISIBLE);
                        mPrice.setVisibility(View.GONE);
                        mName.setVisibility(View.GONE);
                        JSONObject jsonObject = new JSONObject();
                        try {
                            jsonObject.put("nomEvenement", mName.getText().toString());
                            jsonObject.put("prix", Double.parseDouble(mPrice.getText().toString()));
                            jsonObject.put("date", mDate.getText().toString());
                        } catch (JSONException e){
                            e.printStackTrace();
                        }
                        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, VolleyUtils.baseUri + "/evenement"   ,jsonObject , new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response) {
                                mProgressBar.setVisibility(View.GONE);
                            }
                        }, new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                mProgressBar.setVisibility(View.GONE);
                            }
                        }
                        );
                        jsonObjectRequest.setTag(CreateEvent.class);
                        VolleyUtils.getInstance(getContext()).addToRequestQueue(jsonObjectRequest);
                    }
                })
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        VolleyUtils.getInstance(getContext()).getRequestQueue().cancelAll(CreateEvent.class);
                    }
                }).create();
        dialog.setCanceledOnTouchOutside(false);
        return dialog;
    }

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

}