CreateConsommation.java 6.19 KB
package net.plil.clubinfo.etunicorn.app.consommation;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;

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.utils.VolleyUtils;

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

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


public class CreateConsommation extends DialogFragment {

    EditText mNomConsomation;
    EditText mPrice;
    ProgressBar mProgressBar;
    TextInputLayout mNomInput;
    TextInputLayout mPriceInput;


    @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_consomation, null);
        mNomConsomation = (EditText) view.findViewById(R.id.create_consomation_name);
        mPrice = (EditText) view.findViewById(R.id.create_consomation_price);
        mProgressBar = (ProgressBar) view.findViewById(R.id.create_consomation_progress_bar);
        mNomInput = (TextInputLayout) view.findViewById(R.id.create_consomation_name_input);
        mPriceInput = (TextInputLayout) view.findViewById(R.id.create_consomation_price_input);
        AlertDialog dialog = new AlertDialog.Builder(getActivity())
                .setTitle(R.string.create_consumable)
                .setView(view)
                .setPositiveButton(R.string.valid, null)
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        VolleyUtils.getInstance(getContext()).getRequestQueue().cancelAll(CreateConsommation.class);
                    }
                })
                .create();
        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) {
                        boolean testDateInputOk = true;
                        if (mPrice.getText().toString().isEmpty()) {
                            mPriceInput.setError(getString(R.string.error_create_consomation_input_price));
                            testDateInputOk = false;
                        } else {
                            mPriceInput.setError(null);
                        }
                        if (mNomConsomation.getText().toString().isEmpty()){
                            mNomInput.setError(getString(R.string.error_create_consomation_input_nom));
                            testDateInputOk = false;
                        } else {
                            mNomInput.setError(null);
                        }
                        if (testDateInputOk) {
                            mPriceInput.setError(null);
                            mNomInput.setError(null);
                            mProgressBar.setVisibility(View.VISIBLE);
                            mPriceInput.setVisibility(View.GONE);
                            mNomInput.setVisibility(View.GONE);
                            JSONObject jsonObject = new JSONObject();
                            try {
                                jsonObject.put("nom", mNomConsomation.getText().toString());
                                jsonObject.put("prix", Double.parseDouble(mPrice.getText().toString()));
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, VolleyUtils.baseUri + "/transaction/debit", jsonObject, new Response.Listener<JSONObject>() {
                                @Override
                                public void onResponse(JSONObject response) {
                                    mProgressBar.setVisibility(View.GONE);
                                    dismiss();
                                }
                            }, new Response.ErrorListener() {
                                @Override
                                public void onErrorResponse(VolleyError error) {
                                    mProgressBar.setVisibility(View.GONE);
                                    dismiss();
                                }
                            }
                            ){
                                @Override
                                public Map<String, String> getHeaders() throws AuthFailureError {
                                    Map<String, String> headers = new HashMap<String, String>();
                                    headers.put("Authorization", MainActivity.session.getToken());
                                    return headers;
                                }};
                            jsonObjectRequest.setTag(CreateConsommation.class);
                            VolleyUtils.getInstance(getContext()).addToRequestQueue(jsonObjectRequest);
                        }
                    }
                });
            }
        });
        dialog.setCanceledOnTouchOutside(false);
        return dialog;
    }


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


}