Commit 299af019216591ef48e6013d6f68bcf193717d0d

Authored by badetitou
1 parent 73b70b1e

Add payment consommation

app/src/main/java/net/plil/clubinfo/etunicorn/app/Crediter.java
... ... @@ -119,7 +119,6 @@ public class Crediter extends FragmentNFC {
119 119 * >Communicating with Other Fragments</a> for more information.
120 120 */
121 121 public interface OnFragmentInteractionListener {
122   - // TODO: Update argument type and name
123 122 void onFragmentInteraction(Uri uri);
124 123 }
125 124 }
... ...
app/src/main/java/net/plil/clubinfo/etunicorn/app/MainActivity.java
... ... @@ -14,14 +14,14 @@ import android.support.v7.app.AppCompatActivity;
14 14 import android.support.v7.widget.Toolbar;
15 15 import android.view.Menu;
16 16 import android.view.MenuItem;
17   -import android.view.View;
18 17  
19 18 import net.plil.clubinfo.etunicorn.R;
20   -import net.plil.clubinfo.etunicorn.app.consomation.FragmentConsomation;
21   -import net.plil.clubinfo.etunicorn.data.Consomation;
  19 +import net.plil.clubinfo.etunicorn.app.consommation.FragmentConsommation;
  20 +import net.plil.clubinfo.etunicorn.app.consommation.PaiementConsommation;
  21 +import net.plil.clubinfo.etunicorn.data.Consommation;
22 22  
23 23  
24   -public class MainActivity extends AppCompatActivity implements Crediter.OnFragmentInteractionListener, Debiter.OnFragmentInteractionListener, FragmentConsomation.OnListFragmentInteractionListener{
  24 +public class MainActivity extends AppCompatActivity implements Crediter.OnFragmentInteractionListener, Debiter.OnFragmentInteractionListener, FragmentConsommation.OnListFragmentInteractionListener{
25 25  
26 26 /**
27 27 * The {@link android.support.v4.view.PagerAdapter} that will provide
... ... @@ -121,9 +121,11 @@ public class MainActivity extends AppCompatActivity implements Crediter.OnFragme
121 121  
122 122 }
123 123  
124   - @Override
125   - public void onListFragmentInteraction(Consomation item) {
126 124  
  125 + @Override
  126 + public void onListFragmentInteraction(Consommation item) {
  127 + PaiementConsommation paiementConsommation = PaiementConsommation.newInstance(item);
  128 + paiementConsommation.show(getSupportFragmentManager(), "paiementConsommation");
127 129 }
128 130  
129 131 /**
... ... @@ -143,7 +145,7 @@ public class MainActivity extends AppCompatActivity implements Crediter.OnFragme
143 145 else if (position == Action.DEBITER.getValue())
144 146 return Debiter.newInstance();
145 147 else if (position == Action.CONSOMATION.getValue())
146   - return FragmentConsomation.newInstance();
  148 + return FragmentConsommation.newInstance();
147 149 else
148 150 return null;
149 151 }
... ... @@ -160,7 +162,7 @@ public class MainActivity extends AppCompatActivity implements Crediter.OnFragme
160 162 else if (position == Action.CREDITER.getValue())
161 163 return "Crediter";
162 164 else if (position == Action.CONSOMATION.getValue())
163   - return "Consomation";
  165 + return "Consommation";
164 166 return null;
165 167 }
166 168 }
... ...
app/src/main/java/net/plil/clubinfo/etunicorn/app/consomation/ConsomationRecyclerViewAdapter.java renamed to app/src/main/java/net/plil/clubinfo/etunicorn/app/consommation/ConsommationRecyclerViewAdapter.java
1   -package net.plil.clubinfo.etunicorn.app.consomation;
  1 +package net.plil.clubinfo.etunicorn.app.consommation;
2 2  
3 3 import android.support.v7.widget.RecyclerView;
4 4 import android.view.LayoutInflater;
5 5 import android.view.View;
6 6 import android.view.ViewGroup;
7 7 import android.widget.TextView;
8   -import android.widget.Toast;
9 8  
10 9 import net.plil.clubinfo.etunicorn.R;
11   -import net.plil.clubinfo.etunicorn.data.Consomation;
  10 +import net.plil.clubinfo.etunicorn.data.Consommation;
12 11  
13 12 import java.util.List;
14 13 import java.util.Locale;
15 14  
16 15  
17   -public class ConsomationRecyclerViewAdapter extends RecyclerView.Adapter<ConsomationRecyclerViewAdapter.ViewHolder> {
  16 +public class ConsommationRecyclerViewAdapter extends RecyclerView.Adapter<ConsommationRecyclerViewAdapter.ViewHolder> {
18 17  
19   - private final List<net.plil.clubinfo.etunicorn.data.Consomation> mValues;
20   - private final FragmentConsomation.OnListFragmentInteractionListener mListener;
  18 + private final List<Consommation> mValues;
  19 + private final FragmentConsommation.OnListFragmentInteractionListener mListener;
21 20  
22   - public ConsomationRecyclerViewAdapter(List<net.plil.clubinfo.etunicorn.data.Consomation> items, FragmentConsomation.OnListFragmentInteractionListener listener) {
  21 + public ConsommationRecyclerViewAdapter(List<Consommation> items, FragmentConsommation.OnListFragmentInteractionListener listener) {
23 22 mValues = items;
24 23 mListener = listener;
25 24 }
... ... @@ -37,7 +36,7 @@ public class ConsomationRecyclerViewAdapter extends RecyclerView.Adapter&lt;Consoma
37 36 holder.mItem = mValues.get(position);
38 37 holder.mIdView.setText(String.format(Locale.FRANCE, "%d", mValues.get(position).getIdConsomation()));
39 38 holder.mContentView.setText(mValues.get(position).getNomConsomation());
40   - holder.mPriceView.setText(String.format(Locale.FRANCE, "%f",mValues.get(position).getPrix()));
  39 + holder.mPriceView.setText(String.format(Locale.FRANCE, "%.2f",mValues.get(position).getPrix()));
41 40  
42 41 holder.mView.setOnClickListener(new View.OnClickListener() {
43 42 @Override
... ... @@ -67,7 +66,7 @@ public class ConsomationRecyclerViewAdapter extends RecyclerView.Adapter&lt;Consoma
67 66 public final TextView mIdView;
68 67 public final TextView mContentView;
69 68 public final TextView mPriceView;
70   - public Consomation mItem;
  69 + public Consommation mItem;
71 70  
72 71 public ViewHolder(View view) {
73 72 super(view);
... ...
app/src/main/java/net/plil/clubinfo/etunicorn/app/consomation/CreateConsomation.java renamed to app/src/main/java/net/plil/clubinfo/etunicorn/app/consommation/CreateConsommation.java
1   -package net.plil.clubinfo.etunicorn.app.consomation;
  1 +package net.plil.clubinfo.etunicorn.app.consommation;
2 2  
3 3 import android.app.Dialog;
4   -import android.content.Context;
5 4 import android.content.DialogInterface;
6   -import android.net.Uri;
7 5 import android.os.Bundle;
8 6 import android.support.v4.app.DialogFragment;
9   -import android.support.v4.app.Fragment;
10 7 import android.support.v7.app.AlertDialog;
11 8 import android.view.LayoutInflater;
12 9 import android.view.View;
13   -import android.view.ViewGroup;
14 10 import android.widget.EditText;
15 11 import android.widget.ProgressBar;
16 12  
... ... @@ -26,7 +22,7 @@ import org.json.JSONException;
26 22 import org.json.JSONObject;
27 23  
28 24  
29   -public class CreateConsomation extends DialogFragment {
  25 +public class CreateConsommation extends DialogFragment {
30 26  
31 27 EditText mNomConsomation;
32 28 EditText mPrice;
... ... @@ -43,7 +39,7 @@ public class CreateConsomation extends DialogFragment {
43 39 mProgressBar = (ProgressBar) view.findViewById(R.id.create_consomation_progress_bar);
44 40 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
45 41 builder
46   - .setTitle(R.string.create_consomation)
  42 + .setTitle(R.string.create_consumable)
47 43 .setView(view)
48 44 .setPositiveButton(R.string.valid, new DialogInterface.OnClickListener() {
49 45 public void onClick(DialogInterface dialog, int id) {
... ...
app/src/main/java/net/plil/clubinfo/etunicorn/app/consomation/FragmentConsomation.java renamed to app/src/main/java/net/plil/clubinfo/etunicorn/app/consommation/FragmentConsommation.java
1   -package net.plil.clubinfo.etunicorn.app.consomation;
  1 +package net.plil.clubinfo.etunicorn.app.consommation;
2 2  
3 3 import android.content.Context;
4 4 import android.os.Bundle;
... ... @@ -10,9 +10,18 @@ import android.view.View;
10 10 import android.view.ViewGroup;
11 11 import android.widget.Toast;
12 12  
  13 +import com.android.volley.Request;
  14 +import com.android.volley.Response;
  15 +import com.android.volley.VolleyError;
  16 +import com.android.volley.toolbox.JsonObjectRequest;
  17 +
13 18 import net.plil.clubinfo.etunicorn.R;
14 19 import net.plil.clubinfo.etunicorn.app.FragmentNFC;
15   -import net.plil.clubinfo.etunicorn.data.Consomation;
  20 +import net.plil.clubinfo.etunicorn.data.Consommation;
  21 +import net.plil.clubinfo.etunicorn.utils.VolleyUtils;
  22 +
  23 +import org.json.JSONException;
  24 +import org.json.JSONObject;
16 25  
17 26 import java.util.ArrayList;
18 27 import java.util.List;
... ... @@ -23,7 +32,7 @@ import java.util.List;
23 32 * Activities containing this fragment MUST implement the {@link OnListFragmentInteractionListener}
24 33 * interface.
25 34 */
26   -public class FragmentConsomation extends FragmentNFC {
  35 +public class FragmentConsommation extends FragmentNFC {
27 36  
28 37 private OnListFragmentInteractionListener mListener;
29 38  
... ... @@ -31,11 +40,11 @@ public class FragmentConsomation extends FragmentNFC {
31 40 * Mandatory empty constructor for the fragment manager to instantiate the
32 41 * fragment (e.g. upon screen orientation changes).
33 42 */
34   - public FragmentConsomation() {
  43 + public FragmentConsommation() {
35 44 }
36 45  
37   - public static FragmentConsomation newInstance() {
38   - return new FragmentConsomation();
  46 + public static FragmentConsommation newInstance() {
  47 + return new FragmentConsommation();
39 48 }
40 49  
41 50 @Override
... ... @@ -53,27 +62,27 @@ public class FragmentConsomation extends FragmentNFC {
53 62  
54 63 recyclerView.setLayoutManager(new LinearLayoutManager(context));
55 64  
56   - List<Consomation> consomationList = new ArrayList<>();
57   - Consomation c1 = new Consomation();
  65 + List<Consommation> consommationList = new ArrayList<>();
  66 + Consommation c1 = new Consommation();
58 67 c1.setIdConsomation(1);
59 68 c1.setNomConsomation("Hello");
60 69 c1.setPrix(12.5);
61   - consomationList.add(c1);
  70 + consommationList.add(c1);
62 71 for (int i = 0; i< 100; ++i){
63   - c1 = new Consomation();
  72 + c1 = new Consommation();
64 73 c1.setIdConsomation(i);
65 74 c1.setNomConsomation("World");
66 75 c1.setPrix(42.407);
67   - consomationList.add(c1);
  76 + consommationList.add(c1);
68 77 }
69   - recyclerView.setAdapter(new ConsomationRecyclerViewAdapter(consomationList, mListener));
  78 + recyclerView.setAdapter(new ConsommationRecyclerViewAdapter(consommationList, mListener));
70 79  
71 80 FloatingActionButton fAB = (FloatingActionButton) view.findViewById(R.id.consomation_add);
72 81 fAB.setOnClickListener(new View.OnClickListener() {
73 82 @Override
74 83 public void onClick(View v) {
75   - CreateConsomation newFragment = new CreateConsomation();
76   - newFragment.show(getFragmentManager(), "CreateConsomation");
  84 + CreateConsommation newFragment = new CreateConsommation();
  85 + newFragment.show(getFragmentManager(), "CreateConsommation");
77 86 }
78 87 });
79 88  
... ... @@ -101,6 +110,13 @@ public class FragmentConsomation extends FragmentNFC {
101 110 @Override
102 111 public void processNFC() {
103 112  
  113 + PaiementConsommation paiementConsommation = (PaiementConsommation) getFragmentManager().findFragmentByTag("paiementConsommation");
  114 + if (paiementConsommation == null){
  115 + Toast.makeText(getContext(), R.string.payment_consomation_alert_no_selection, Toast.LENGTH_LONG).show();
  116 + }
  117 + else {
  118 + paiementConsommation.processNFC();
  119 + }
104 120 }
105 121  
106 122 /**
... ... @@ -114,6 +130,6 @@ public class FragmentConsomation extends FragmentNFC {
114 130 * >Communicating with Other Fragments</a> for more information.
115 131 */
116 132 public interface OnListFragmentInteractionListener {
117   - void onListFragmentInteraction(Consomation item);
  133 + void onListFragmentInteraction(Consommation item);
118 134 }
119 135 }
... ...
app/src/main/java/net/plil/clubinfo/etunicorn/app/consommation/PaiementConsommation.java 0 → 100644
... ... @@ -0,0 +1,113 @@
  1 +package net.plil.clubinfo.etunicorn.app.consommation;
  2 +
  3 +import android.app.Dialog;
  4 +import android.content.DialogInterface;
  5 +import android.os.Bundle;
  6 +import android.support.v4.app.DialogFragment;
  7 +import android.support.v7.app.AlertDialog;
  8 +import android.view.LayoutInflater;
  9 +import android.view.View;
  10 +import android.widget.ProgressBar;
  11 +import android.widget.TextView;
  12 +import android.widget.Toast;
  13 +
  14 +import com.android.volley.Request;
  15 +import com.android.volley.Response;
  16 +import com.android.volley.VolleyError;
  17 +import com.android.volley.toolbox.JsonObjectRequest;
  18 +
  19 +import net.plil.clubinfo.etunicorn.R;
  20 +import net.plil.clubinfo.etunicorn.data.Consommation;
  21 +import net.plil.clubinfo.etunicorn.utils.VolleyUtils;
  22 +
  23 +import org.json.JSONException;
  24 +import org.json.JSONObject;
  25 +
  26 +import java.util.Locale;
  27 +
  28 +
  29 +public class PaiementConsommation extends DialogFragment {
  30 +
  31 + ProgressBar mProgressBar;
  32 + TextView mPaiementConsommationName;
  33 + TextView mPaiementConsommationPrice;
  34 + int consommationId;
  35 +
  36 + public PaiementConsommation(){}
  37 +
  38 + /**
  39 + * Create a new instance of MyDialogFragment, providing "num"
  40 + * as an argument.
  41 + */
  42 + public static PaiementConsommation newInstance(Consommation consommation) {
  43 + PaiementConsommation f = new PaiementConsommation();
  44 +
  45 + // Supply num input as an argument.
  46 + Bundle args = new Bundle();
  47 + args.putSerializable("consommation", consommation);
  48 + f.setArguments(args);
  49 +
  50 + return f;
  51 + }
  52 +
  53 +
  54 + @Override
  55 + public Dialog onCreateDialog(Bundle savedInstanceState) {
  56 + // Use the Builder class for convenient dialog construction
  57 + LayoutInflater inflater = getActivity().getLayoutInflater();
  58 + View view = inflater.inflate(R.layout.fragment_paiment_consomation, null);
  59 + mProgressBar = (ProgressBar) view.findViewById(R.id.paiment_consommation_progress_bar);
  60 + mPaiementConsommationName = (TextView) view.findViewById(R.id.paiment_consommation_name);
  61 + mPaiementConsommationPrice = (TextView) view.findViewById(R.id.paiment_consommation_price);
  62 +
  63 +
  64 + Consommation consommation = (Consommation) getArguments().getSerializable("consommation");
  65 + mPaiementConsommationName.setText(consommation.getNomConsomation());
  66 + mPaiementConsommationPrice.setText(String.format(Locale.FRANCE, "%.2f €", consommation.getPrix()));
  67 +
  68 + consommationId = consommation.getIdConsomation();
  69 +
  70 + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  71 + builder
  72 + .setTitle(R.string.payment_consumable)
  73 + .setView(view)
  74 + .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
  75 + public void onClick(DialogInterface dialog, int id) {
  76 + // User cancelled the dialog
  77 + }
  78 + });
  79 + // Create the AlertDialog object and return it
  80 + return builder.create();
  81 + }
  82 +
  83 +
  84 + public void processNFC() {
  85 + mProgressBar.setVisibility(View.VISIBLE);
  86 + JSONObject jsonObject = new JSONObject();
  87 + try {
  88 + jsonObject.put("participant", 1);
  89 + jsonObject.put("acteur", 2);
  90 + jsonObject.put("id", consommationId);
  91 + } catch (JSONException e) {
  92 + e.printStackTrace();
  93 + }
  94 + JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, VolleyUtils.baseUri + "/transaction/consommation", jsonObject, new Response.Listener<JSONObject>() {
  95 + @Override
  96 + public void onResponse(JSONObject response) {
  97 + mProgressBar.setVisibility(View.GONE);
  98 + Toast.makeText(getContext(), R.string.payment_done, Toast.LENGTH_LONG).show();
  99 + dismiss();
  100 + }
  101 + }, new Response.ErrorListener() {
  102 + @Override
  103 + public void onErrorResponse(VolleyError error) {
  104 + mProgressBar.setVisibility(View.GONE);
  105 + Toast.makeText(getContext(), R.string.payment_refused, Toast.LENGTH_LONG).show();
  106 + dismiss();
  107 + }
  108 + }
  109 + );
  110 +
  111 + VolleyUtils.getInstance(getContext()).addToRequestQueue(jsonObjectRequest);
  112 + }
  113 +}
... ...
app/src/main/java/net/plil/clubinfo/etunicorn/data/Consomation.java renamed to app/src/main/java/net/plil/clubinfo/etunicorn/data/Consommation.java
1 1 package net.plil.clubinfo.etunicorn.data;
2 2  
  3 +import java.io.Serializable;
  4 +
3 5 /**
4 6 * Created by badetitou on 29/01/2017.
5 7 */
6 8  
7   -public class Consomation {
  9 +public class Consommation implements Serializable{
  10 +
8 11 private int idConsomation;
9 12 private String nomConsomation;
10 13 private double prix;
11 14  
12   - public Consomation() {
  15 + public Consommation() {
13 16 }
14 17  
15 18 public int getIdConsomation() {
... ...
app/src/main/res/layout/fragment_consomation_item_list.xml
... ... @@ -14,7 +14,7 @@
14 14 android:layout_marginLeft="16dp"
15 15 android:layout_marginRight="16dp"
16 16 app:layoutManager="LinearLayoutManager"
17   - tools:context="net.plil.clubinfo.etunicorn.app.consomation.FragmentConsomation"
  17 + tools:context="net.plil.clubinfo.etunicorn.app.consommation.FragmentConsommation"
18 18 tools:listitem="@layout/fragment_consomation_item"
19 19 />
20 20  
... ...
app/src/main/res/layout/fragment_create_consomation.xml
1 1 <LinearLayout
2 2 xmlns:android="http://schemas.android.com/apk/res/android"
3 3 xmlns:tools="http://schemas.android.com/tools"
4   - tools:context="net.plil.clubinfo.etunicorn.app.consomation.CreateConsomation"
  4 + tools:context="net.plil.clubinfo.etunicorn.app.consommation.CreateConsommation"
5 5 android:orientation="vertical"
6 6 android:layout_width="match_parent"
7 7 android:layout_height="match_parent"
... ... @@ -11,12 +11,12 @@
11 11 android:layout_width="match_parent"
12 12 android:layout_height="wrap_content"
13 13 android:id="@+id/create_consomation_name"
14   - android:hint="@string/create_consomation_name"/>
  14 + android:hint="@string/create_consumable_name"/>
15 15  
16 16 <EditText
17 17 android:layout_width="match_parent"
18 18 android:layout_height="match_parent"
19   - android:hint="@string/create_consomation_price"
  19 + android:hint="@string/create_consumable_price"
20 20 android:id="@+id/create_consomation_price"
21 21 android:inputType="numberSigned|numberDecimal"
22 22 android:maxLines="1" />
... ...
app/src/main/res/layout/fragment_paiment_consomation.xml 0 → 100644
... ... @@ -0,0 +1,28 @@
  1 +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2 + xmlns:tools="http://schemas.android.com/tools"
  3 + android:layout_width="match_parent"
  4 + android:layout_height="match_parent"
  5 + tools:context="net.plil.clubinfo.etunicorn.app.consommation.PaiementConsommation"
  6 + android:orientation="vertical">
  7 +
  8 + <TextView
  9 + android:layout_width="wrap_content"
  10 + android:layout_height="wrap_content"
  11 + android:id="@+id/paiment_consommation_name"
  12 + android:layout_gravity="center"
  13 + android:textAppearance="@android:style/TextAppearance.Material.Medium" />
  14 +
  15 + <TextView
  16 + android:layout_width="wrap_content"
  17 + android:layout_height="wrap_content"
  18 + android:id="@+id/paiment_consommation_price"
  19 + android:layout_gravity="center"
  20 + android:textAppearance="@android:style/TextAppearance.Material.Medium" />
  21 +
  22 + <ProgressBar
  23 + android:layout_width="match_parent"
  24 + android:layout_height="match_parent"
  25 + android:id="@+id/paiment_consommation_progress_bar"
  26 + android:visibility="gone"/>
  27 +
  28 +</LinearLayout>
... ...
app/src/main/res/values-fr/strings.xml
... ... @@ -9,10 +9,11 @@
9 9 <string name="prompt_username">Nom d\'utilisateur</string>
10 10 <string name="action_settings">Paramètres</string>
11 11 <string name="cancel">Annuler</string>
12   - <string name="create_consomation">Créer une consomation</string>
13   - <string name="create_consomation_name">Nom</string>
14   - <string name="create_consomation_price">Prix</string>
  12 + <string name="create_consumable">Créer une consommation</string>
  13 + <string name="create_consumable_name">Nom</string>
  14 + <string name="create_consumable_price">Prix</string>
15 15 <string name="enter_money">Entrer une valeur</string>
16 16 <string name="valid">OK</string>
17 17 <string name="title_activity_main">Etunicorn - Bonjour</string>
  18 + <string name="payment_consumable">Paiement d\'un Consommable</string>
18 19 </resources>
19 20 \ No newline at end of file
... ...
app/src/main/res/values/strings.xml
... ... @@ -13,8 +13,12 @@
13 13 <string name="enter_money">Enter value</string>
14 14 <string name="valid">OK</string>
15 15 <string name="cancel">Cancel</string>
16   - <string name="create_consomation">Create Consomation</string>
17   - <string name="create_consomation_name">Name</string>
18   - <string name="create_consomation_price">Price</string>
  16 + <string name="create_consumable">Create Consumable</string>
  17 + <string name="create_consumable_name">Name</string>
  18 + <string name="create_consumable_price">Price</string>
19 19 <string name="title_activity_main">Etunicorn - Hello</string>
  20 + <string name="payment_consumable">Consumable Payment</string>
  21 + <string name="payment_consomation_alert_no_selection">Select a product before paying</string>
  22 + <string name="payment_done">Payment done</string>
  23 + <string name="payment_refused">Payment refused</string>
20 24 </resources>
... ...