Blame view

app/src/main/java/net/plil/clubinfo/etunicorn/app/Event/PaiementEvent.java 4.62 KB
8bcd81b8   badetitou   some staff
1
  package net.plil.clubinfo.etunicorn.app.event;
c8a73f2c   badetitou   Adding Event frag...
2
3
4
5
  
  import android.app.Dialog;
  import android.content.DialogInterface;
  import android.os.Bundle;
3dc52ca9   badetitou   Cleaning code 2.0
6
  import android.support.annotation.NonNull;
c8a73f2c   badetitou   Adding Event frag...
7
8
9
10
11
12
13
14
  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;
  
b7f2b9a4   badetitou   Authorization for...
15
  import com.android.volley.AuthFailureError;
c8a73f2c   badetitou   Adding Event frag...
16
17
18
19
20
21
  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;
b7f2b9a4   badetitou   Authorization for...
22
  import net.plil.clubinfo.etunicorn.app.MainActivity;
515eb393   badetitou   NFCSupport Update
23
  import net.plil.clubinfo.etunicorn.app.NFCSupport;
c8a73f2c   badetitou   Adding Event frag...
24
25
26
27
28
29
  import net.plil.clubinfo.etunicorn.data.Event;
  import net.plil.clubinfo.etunicorn.utils.VolleyUtils;
  
  import org.json.JSONException;
  import org.json.JSONObject;
  
b7f2b9a4   badetitou   Authorization for...
30
  import java.util.HashMap;
c8a73f2c   badetitou   Adding Event frag...
31
  import java.util.Locale;
b7f2b9a4   badetitou   Authorization for...
32
  import java.util.Map;
c8a73f2c   badetitou   Adding Event frag...
33
34
  
  
515eb393   badetitou   NFCSupport Update
35
  public class PaiementEvent extends DialogFragment implements NFCSupport {
c8a73f2c   badetitou   Adding Event frag...
36
37
38
39
40
41
  
      ProgressBar mProgressBar;
      TextView mPaiementEventName;
      TextView mPaiementEventPrice;
      int eventId;
  
3dc52ca9   badetitou   Cleaning code 2.0
42
43
      public PaiementEvent() {
      }
c8a73f2c   badetitou   Adding Event frag...
44
45
46
47
48
49
50
51
52
53
54
  
      public static PaiementEvent newInstance(Event event) {
          PaiementEvent f = new PaiementEvent();
          Bundle args = new Bundle();
          args.putSerializable("event", event);
          f.setArguments(args);
          return f;
      }
  
  
      @Override
3dc52ca9   badetitou   Cleaning code 2.0
55
      @NonNull
c8a73f2c   badetitou   Adding Event frag...
56
57
58
59
60
61
62
63
64
65
      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_event, null);
          mProgressBar = (ProgressBar) view.findViewById(R.id.paiment_event_progress_bar);
          mPaiementEventName = (TextView) view.findViewById(R.id.paiment_event_name);
          mPaiementEventPrice = (TextView) view.findViewById(R.id.paiment_event_price);
  
  
          Event event = (Event) getArguments().getSerializable("event");
3dc52ca9   badetitou   Cleaning code 2.0
66
67
          assert event != null;
  
c8a73f2c   badetitou   Adding Event frag...
68
          mPaiementEventName.setText(event.getNomEvent());
7f1ea456   badetitou   Stabilité app + d...
69
          mPaiementEventPrice.setText(String.format(Locale.US, "%.2f €", event.getPrice()));
c8a73f2c   badetitou   Adding Event frag...
70
71
72
73
74
75
76
77
78
  
          eventId = event.getIdEvent();
  
          AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
          builder
                  .setTitle(R.string.payment_event)
                  .setView(view)
                  .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int id) {
c0f970a0   badetitou   Event handle sto...
79
                          VolleyUtils.getInstance(getContext()).getRequestQueue().cancelAll(PaiementEvent.class);
c8a73f2c   badetitou   Adding Event frag...
80
81
                      }
                  });
3dc52ca9   badetitou   Cleaning code 2.0
82
          Dialog dialog = builder.create();
f5506a08   badetitou   Fix error leave d...
83
84
          dialog.setCanceledOnTouchOutside(false);
          return dialog;
c8a73f2c   badetitou   Adding Event frag...
85
86
87
      }
  
  
6645bb22   badetitou   Fix some stupid b...
88
      public void processNFC(String idCardUser) {
c8a73f2c   badetitou   Adding Event frag...
89
90
91
          mProgressBar.setVisibility(View.VISIBLE);
          JSONObject jsonObject = new JSONObject();
          try {
6645bb22   badetitou   Fix some stupid b...
92
              jsonObject.put("participant", idCardUser);
c8a73f2c   badetitou   Adding Event frag...
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
              jsonObject.put("id", eventId);
          } catch (JSONException e) {
              e.printStackTrace();
          }
          JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, VolleyUtils.baseUri + "/transaction/evenement", 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();
c8a73f2c   badetitou   Adding Event frag...
109
110
              }
          }
3dc52ca9   badetitou   Cleaning code 2.0
111
          ) {
b7f2b9a4   badetitou   Authorization for...
112
113
              @Override
              public Map<String, String> getHeaders() throws AuthFailureError {
3dc52ca9   badetitou   Cleaning code 2.0
114
                  Map<String, String> headers = new HashMap<>();
b7f2b9a4   badetitou   Authorization for...
115
116
                  headers.put("Authorization", MainActivity.session.getToken());
                  return headers;
3dc52ca9   badetitou   Cleaning code 2.0
117
118
              }
          };
c8a73f2c   badetitou   Adding Event frag...
119
  
6645bb22   badetitou   Fix some stupid b...
120
          jsonObjectRequest.setTag(PaiementEvent.class);
c8a73f2c   badetitou   Adding Event frag...
121
122
          VolleyUtils.getInstance(getContext()).addToRequestQueue(jsonObjectRequest);
      }
6645bb22   badetitou   Fix some stupid b...
123
124
  
  
6645bb22   badetitou   Fix some stupid b...
125
126
127
      @Override
      public void onStop() {
          VolleyUtils.getInstance(getContext()).getRequestQueue().cancelAll(PaiementEvent.class);
c0f970a0   badetitou   Event handle sto...
128
          dismissAllowingStateLoss();
6645bb22   badetitou   Fix some stupid b...
129
130
131
          super.onStop();
      }
  
6645bb22   badetitou   Fix some stupid b...
132
  
c8a73f2c   badetitou   Adding Event frag...
133
  }