Blame view

app/src/main/java/net/plil/clubinfo/etunicorn/app/consommation/PaiementConsommation.java 4.55 KB
299af019   badetitou   Add payment conso...
1
2
3
4
5
6
7
  package net.plil.clubinfo.etunicorn.app.consommation;
  
  import android.app.Dialog;
  import android.content.DialogInterface;
  import android.os.Bundle;
  import android.support.v4.app.DialogFragment;
  import android.support.v7.app.AlertDialog;
6e48e148   badetitou   Consomation handl...
8
  import android.view.KeyEvent;
299af019   badetitou   Add payment conso...
9
10
11
12
13
14
15
16
17
18
19
20
  import android.view.LayoutInflater;
  import android.view.View;
  import android.widget.ProgressBar;
  import android.widget.TextView;
  import android.widget.Toast;
  
  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;
6645bb22   badetitou   Fix some stupid b...
21
  import net.plil.clubinfo.etunicorn.app.event.PaiementEvent;
299af019   badetitou   Add payment conso...
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  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.Locale;
  
  
  public class PaiementConsommation extends DialogFragment {
  
      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();
299af019   badetitou   Add payment conso...
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
          Bundle args = new Bundle();
          args.putSerializable("consommation", consommation);
          f.setArguments(args);
  
          return f;
      }
  
  
      @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_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");
          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) {
6e48e148   badetitou   Consomation handl...
76
                          VolleyUtils.getInstance(getContext()).getRequestQueue().cancelAll(PaiementConsommation.class);
299af019   badetitou   Add payment conso...
77
78
                      }
                  });
f5506a08   badetitou   Fix error leave d...
79
80
81
82
  
          Dialog dialog =  builder.create();
          dialog.setCanceledOnTouchOutside(false);
          return dialog;
299af019   badetitou   Add payment conso...
83
84
85
      }
  
  
6e48e148   badetitou   Consomation handl...
86
87
      public void processNFC(String idCardUser) {
          Toast.makeText(getContext(), "Hello " + idCardUser, Toast.LENGTH_LONG).show();
299af019   badetitou   Add payment conso...
88
89
90
          mProgressBar.setVisibility(View.VISIBLE);
          JSONObject jsonObject = new JSONObject();
          try {
6e48e148   badetitou   Consomation handl...
91
              jsonObject.put("participant", idCardUser);
299af019   badetitou   Add payment conso...
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
              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();
299af019   badetitou   Add payment conso...
108
109
110
              }
          }
          );
6645bb22   badetitou   Fix some stupid b...
111
          jsonObjectRequest.setTag(PaiementConsommation.class);
299af019   badetitou   Add payment conso...
112
113
          VolleyUtils.getInstance(getContext()).addToRequestQueue(jsonObjectRequest);
      }
6645bb22   badetitou   Fix some stupid b...
114
  
6645bb22   badetitou   Fix some stupid b...
115
116
117
118
  
      @Override
      public void onStop() {
          VolleyUtils.getInstance(getContext()).getRequestQueue().cancelAll(PaiementConsommation.class);
6e48e148   badetitou   Consomation handl...
119
          dismissAllowingStateLoss();
6645bb22   badetitou   Fix some stupid b...
120
121
122
          super.onStop();
      }
  
299af019   badetitou   Add payment conso...
123
  }