PaiementConsommation.java 5.05 KB
package net.plil.clubinfo.etunicorn.app.consommation;

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.ProgressBar;
import android.widget.TextView;
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.app.NFCSupport;
import net.plil.clubinfo.etunicorn.data.Consommation;
import net.plil.clubinfo.etunicorn.utils.VolleyUtils;

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

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


public class PaiementConsommation extends DialogFragment implements NFCSupport {

    ProgressBar mProgressBar;
    TextView mPaiementConsommationName;
    TextView mPaiementConsommationPrice;
    int consommationId;

    public PaiementConsommation() {
    }

    /**
     * Create a new instance of MyDialogFragment, providing "num"
     * as an argument.
     */
    public static PaiementConsommation newInstance(Consommation consommation) {
        PaiementConsommation f = new PaiementConsommation();
        Bundle args = new Bundle();
        args.putSerializable("consommation", consommation);
        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_paiment_consomation, null);
        mProgressBar = (ProgressBar) view.findViewById(R.id.paiment_consommation_progress_bar);
        mPaiementConsommationName = (TextView) view.findViewById(R.id.paiment_consommation_name);
        mPaiementConsommationPrice = (TextView) view.findViewById(R.id.paiment_consommation_price);


        Consommation consommation = (Consommation) getArguments().getSerializable("consommation");
        assert consommation != null;

        mPaiementConsommationName.setText(consommation.getNomConsomation());
        mPaiementConsommationPrice.setText(String.format(Locale.FRANCE, "%.2f €", consommation.getPrix()));

        consommationId = consommation.getIdConsomation();

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder
                .setTitle(R.string.payment_consumable)
                .setView(view)
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        VolleyUtils.getInstance(getContext()).getRequestQueue().cancelAll(PaiementConsommation.class);
                    }
                });

        Dialog dialog = builder.create();
        dialog.setCanceledOnTouchOutside(false);
        return dialog;
    }


    public void processNFC(String idCardUser) {
        Toast.makeText(getContext(), idCardUser, Toast.LENGTH_LONG).show();
        mProgressBar.setVisibility(View.VISIBLE);
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("participant", idCardUser);
            jsonObject.put("id", consommationId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, VolleyUtils.baseUri + "/transaction/consommation", jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                mProgressBar.setVisibility(View.GONE);
                Toast.makeText(getContext(), R.string.payment_done, Toast.LENGTH_LONG).show();
                dismiss();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                mProgressBar.setVisibility(View.GONE);
                Toast.makeText(getContext(), R.string.payment_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(PaiementConsommation.class);
        VolleyUtils.getInstance(getContext()).addToRequestQueue(jsonObjectRequest);
    }


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

}