Commit 6790f98855881f381e089a0fa44cbecf87910282

Authored by badetitou
1 parent be985098

Find way to something good

app/src/main/java/net/plil/clubinfo/etunicorn/app/Crediter.java
... ... @@ -40,7 +40,7 @@ public class Crediter extends FragmentNFC {
40 40 *
41 41 * @return A new instance of fragment Crediter.
42 42 */
43   - public static Crediter newInstance(String param1, String param2) {
  43 + public static Crediter newInstance() {
44 44 Crediter fragment = new Crediter();
45 45 return fragment;
46 46 }
... ... @@ -67,6 +67,8 @@ public class Crediter extends FragmentNFC {
67 67 }
68 68  
69 69  
  70 +
  71 +
70 72 @Override
71 73 public void onAttach(Context context) {
72 74 super.onAttach(context);
... ... @@ -86,7 +88,7 @@ public class Crediter extends FragmentNFC {
86 88  
87 89 @Override
88 90 public void processNFC() {
89   -
  91 + Toast.makeText(getContext(), "DONE !!!!", Toast.LENGTH_LONG).show();
90 92 }
91 93  
92 94  
... ...
app/src/main/java/net/plil/clubinfo/etunicorn/app/Debiter.java
1 1 package net.plil.clubinfo.etunicorn.app;
2 2  
3 3 import android.content.Context;
  4 +import android.content.Intent;
4 5 import android.net.Uri;
5 6 import android.os.Bundle;
6 7 import android.support.v4.app.Fragment;
7 8 import android.view.LayoutInflater;
8 9 import android.view.View;
9 10 import android.view.ViewGroup;
  11 +import android.widget.EditText;
  12 +import android.widget.ProgressBar;
  13 +import android.widget.Toast;
  14 +
  15 +import com.android.volley.Request;
  16 +import com.android.volley.Response;
  17 +import com.android.volley.VolleyError;
  18 +import com.android.volley.toolbox.JsonObjectRequest;
  19 +import com.google.gson.Gson;
  20 +import com.google.gson.JsonElement;
  21 +import com.google.gson.JsonObject;
10 22  
11 23 import net.plil.clubinfo.etunicorn.R;
  24 +import net.plil.clubinfo.etunicorn.data.Personne;
  25 +import net.plil.clubinfo.etunicorn.utils.VolleyUtils;
  26 +
  27 +import org.json.JSONException;
  28 +import org.json.JSONObject;
12 29  
13 30 /**
14 31 * A simple {@link Fragment} subclass.
... ... @@ -19,60 +36,37 @@ import net.plil.clubinfo.etunicorn.R;
19 36 * create an instance of this fragment.
20 37 */
21 38 public class Debiter extends FragmentNFC {
22   - // TODO: Rename parameter arguments, choose names that match
23 39 // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
24   - private static final String ARG_PARAM1 = "param1";
25   - private static final String ARG_PARAM2 = "param2";
26 40  
27   - // TODO: Rename and change types of parameters
28   - private String mParam1;
29   - private String mParam2;
  41 + private EditText mMoneyEditText;
  42 + private ProgressBar mProgressBar;
30 43  
31 44 private OnFragmentInteractionListener mListener;
32 45  
33 46 public Debiter() {
34 47 // Required empty public constructor
  48 +
35 49 }
36 50  
37   - /**
38   - * Use this factory method to create a new instance of
39   - * this fragment using the provided parameters.
40   - *
41   - * @param param1 Parameter 1.
42   - * @param param2 Parameter 2.
43   - * @return A new instance of fragment Debiter.
44   - */
45   - // TODO: Rename and change types and number of parameters
46   - public static Debiter newInstance(String param1, String param2) {
  51 + public static Debiter newInstance() {
47 52 Debiter fragment = new Debiter();
48   - Bundle args = new Bundle();
49   - args.putString(ARG_PARAM1, param1);
50   - args.putString(ARG_PARAM2, param2);
51   - fragment.setArguments(args);
52 53 return fragment;
53 54 }
54 55  
55 56 @Override
56 57 public void onCreate(Bundle savedInstanceState) {
57 58 super.onCreate(savedInstanceState);
58   - if (getArguments() != null) {
59   - mParam1 = getArguments().getString(ARG_PARAM1);
60   - mParam2 = getArguments().getString(ARG_PARAM2);
61   - }
  59 +
  60 +
62 61 }
63 62  
64 63 @Override
65 64 public View onCreateView(LayoutInflater inflater, ViewGroup container,
66 65 Bundle savedInstanceState) {
67   - // Inflate the layout for this fragment
68   - return inflater.inflate(R.layout.fragment_debiter, container, false);
69   - }
70   -
71   - // TODO: Rename method, update argument and hook method into UI event
72   - public void onButtonPressed(Uri uri) {
73   - if (mListener != null) {
74   - mListener.onFragmentInteraction(uri);
75   - }
  66 + View view = inflater.inflate(R.layout.fragment_debiter, container, false);
  67 + mProgressBar = (ProgressBar) view.findViewById(R.id.debiter_progress);
  68 + mMoneyEditText = (EditText) view.findViewById(R.id.debiter_edit_text_money);
  69 + return view;
76 70 }
77 71  
78 72 @Override
... ... @@ -94,7 +88,29 @@ public class Debiter extends FragmentNFC {
94 88  
95 89 @Override
96 90 public void processNFC() {
  91 + mProgressBar.setVisibility(View.VISIBLE);
  92 + JSONObject jsonObject = new JSONObject();
  93 + try {
  94 + jsonObject.put("participant", 1);
  95 + jsonObject.put("acteur", 2);
  96 + jsonObject.put("prix", Double.parseDouble(mMoneyEditText.getText().toString()));
  97 + } catch (JSONException e){
  98 + e.printStackTrace();
  99 + }
  100 + JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, VolleyUtils.baseUri + "/transaction/debit" ,jsonObject , new Response.Listener<JSONObject>() {
  101 + @Override
  102 + public void onResponse(JSONObject response) {
  103 + mProgressBar.setVisibility(View.GONE);
  104 + }
  105 + }, new Response.ErrorListener() {
  106 + @Override
  107 + public void onErrorResponse(VolleyError error) {
  108 + mProgressBar.setVisibility(View.GONE);
  109 + }
  110 + }
  111 + );
97 112  
  113 + VolleyUtils.getInstance(getContext()).addToRequestQueue(jsonObjectRequest);
98 114 }
99 115  
100 116 /**
... ...
app/src/main/java/net/plil/clubinfo/etunicorn/app/FragmentNFC.java
1 1 package net.plil.clubinfo.etunicorn.app;
2 2  
3 3 import android.support.v4.app.Fragment;
  4 +import android.view.View;
4 5  
5 6 /**
6 7 * Created by badetitou on 28/01/2017.
... ...
app/src/main/java/net/plil/clubinfo/etunicorn/app/LoginActivity.java
... ... @@ -40,6 +40,7 @@ import com.google.gson.Gson;
40 40  
41 41 import net.plil.clubinfo.etunicorn.R;
42 42 import net.plil.clubinfo.etunicorn.data.Personne;
  43 +import net.plil.clubinfo.etunicorn.data.User;
43 44 import net.plil.clubinfo.etunicorn.utils.VolleyUtils;
44 45  
45 46 import org.json.JSONException;
... ... @@ -165,31 +166,34 @@ public class LoginActivity extends AppCompatActivity {
165 166 * @param password The password of the person
166 167 */
167 168 private void tryLogin (String username, String password){
168   - JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, VolleyUtils.baseUri + "/login?login=" + username + "&password=" + password , null, new Response.Listener<JSONObject>() {
169   - @Override
170   - public void onResponse(JSONObject response) {
171   - showProgress(false);
172   - try {
173   - Personne p = new Gson().fromJson(response.getJSONObject("").toString(), Personne.class);
  169 + User user = new User();
  170 + user.setPassword(password);
  171 + user.setUsername(username);
  172 + JsonObjectRequest jsonObjectRequest = null;
  173 + try {
  174 + jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, VolleyUtils.baseUri , new JSONObject(new Gson().toJson(user)), new Response.Listener<JSONObject>() {
  175 + @Override
  176 + public void onResponse(JSONObject response) {
  177 + showProgress(false);
  178 + Intent intent = new Intent(getApplicationContext(), MainActivity.class);
  179 + startActivity(intent);
  180 + }
  181 + }, new Response.ErrorListener() {
  182 + @Override
  183 + public void onErrorResponse(VolleyError error) {
  184 + showProgress(false);
  185 + Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
  186 +
  187 + //TODO remove this line
174 188 Intent intent = new Intent(getApplicationContext(), MainActivity.class);
175 189 startActivity(intent);
176   - } catch (JSONException e) {
177   - e.printStackTrace();
  190 + // END
178 191 }
179 192 }
180   - }, new Response.ErrorListener() {
181   - @Override
182   - public void onErrorResponse(VolleyError error) {
183   - showProgress(false);
184   - Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
185   -
186   - //TODO remove this line
187   - Intent intent = new Intent(getApplicationContext(), MainActivity.class);
188   - startActivity(intent);
189   - // END
190   - }
  193 + );
  194 + } catch (JSONException e) {
  195 + e.printStackTrace();
191 196 }
192   - );
193 197  
194 198 VolleyUtils.getInstance(getApplicationContext()).addToRequestQueue(jsonObjectRequest);
195 199 }
... ...
app/src/main/java/net/plil/clubinfo/etunicorn/app/MainActivity.java
... ... @@ -2,13 +2,9 @@ package net.plil.clubinfo.etunicorn.app;
2 2  
3 3 import android.app.PendingIntent;
4 4 import android.content.Intent;
5   -import android.nfc.NdefMessage;
  5 +import android.net.Uri;
6 6 import android.nfc.NfcAdapter;
7   -import android.nfc.Tag;
8   -import android.os.Parcelable;
9 7 import android.support.design.widget.TabLayout;
10   -import android.support.design.widget.FloatingActionButton;
11   -import android.support.design.widget.Snackbar;
12 8 import android.support.v7.app.AppCompatActivity;
13 9 import android.support.v7.widget.Toolbar;
14 10  
... ... @@ -17,20 +13,15 @@ import android.support.v4.app.FragmentManager;
17 13 import android.support.v4.app.FragmentPagerAdapter;
18 14 import android.support.v4.view.ViewPager;
19 15 import android.os.Bundle;
20   -import android.view.LayoutInflater;
21 16 import android.view.Menu;
22 17 import android.view.MenuItem;
23   -import android.view.View;
24   -import android.view.ViewGroup;
25 18  
26   -import android.widget.TextView;
27 19 import android.widget.Toast;
28 20  
29 21 import net.plil.clubinfo.etunicorn.R;
30 22  
31   -import java.util.List;
32 23  
33   -public class MainActivity extends AppCompatActivity {
  24 +public class MainActivity extends AppCompatActivity implements Crediter.OnFragmentInteractionListener, Debiter.OnFragmentInteractionListener{
34 25  
35 26 /**
36 27 * The {@link android.support.v4.view.PagerAdapter} that will provide
... ... @@ -69,7 +60,6 @@ public class MainActivity extends AppCompatActivity {
69 60 TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
70 61 tabLayout.setupWithViewPager(mViewPager);
71 62  
72   -
73 63 mAdapter = NfcAdapter.getDefaultAdapter(this);
74 64 if (mAdapter == null) {
75 65 //nfc not support your device.
... ... @@ -106,8 +96,10 @@ public class MainActivity extends AppCompatActivity {
106 96 @Override
107 97 protected void onNewIntent(Intent intent) {
108 98 super.onNewIntent(intent);
109   - ((FragmentNFC)mSectionsPagerAdapter.getItem(mViewPager.getCurrentItem())).processNFC();
110   - Toast.makeText(getBaseContext(),"NFC WORKING", Toast.LENGTH_LONG).show();
  99 + if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
  100 + FragmentNFC fNFC = (FragmentNFC) getSupportFragmentManager().getFragments().get(mViewPager.getCurrentItem());
  101 + fNFC.processNFC();
  102 + }
111 103 }
112 104  
113 105 @Override
... ... @@ -124,43 +116,9 @@ public class MainActivity extends AppCompatActivity {
124 116 }
125 117 }
126 118  
127   - /**
128   - * A placeholder fragment containing a simple view.
129   - */
130   - public static class PlaceholderFragment extends Fragment {
131   - /**
132   - * The fragment argument representing the section number for this
133   - * fragment.
134   - */
135   - private static final String ARG_SECTION_NUMBER = "section_number";
136   -
137   - public PlaceholderFragment() {
138   - }
139   -
140   - /**
141   - * Returns a new instance of this fragment for the given section
142   - * number.
143   - */
144   - public static PlaceholderFragment newInstance(int sectionNumber) {
145   - PlaceholderFragment fragment = new PlaceholderFragment();
146   - Bundle args = new Bundle();
147   - args.putInt(ARG_SECTION_NUMBER, sectionNumber);
148   - fragment.setArguments(args);
149   - return fragment;
150   - }
  119 + @Override
  120 + public void onFragmentInteraction(Uri uri) {
151 121  
152   - @Override
153   - public View onCreateView(LayoutInflater inflater, ViewGroup container,
154   - Bundle savedInstanceState) {
155   - View rootView;
156   - if (getArguments().getInt(ARG_SECTION_NUMBER) == Action.DEBITER.getValue())
157   - rootView = inflater.inflate(R.layout.fragment_debiter, container, false);
158   - else if (getArguments().getInt(ARG_SECTION_NUMBER) == Action.CREDITER.getValue())
159   - rootView = inflater.inflate(R.layout.fragment_crediter, container, false);
160   - else
161   - rootView = inflater.inflate(R.layout.fragment_main, container, false);
162   - return rootView;
163   - }
164 122 }
165 123  
166 124 /**
... ... @@ -177,13 +135,18 @@ public class MainActivity extends AppCompatActivity {
177 135 public Fragment getItem(int position) {
178 136 // getItem is called to instantiate the fragment for the given page.
179 137 // Return a PlaceholderFragment (defined as a static inner class below).
180   - return PlaceholderFragment.newInstance(position + 1);
  138 + if (position == Action.CREDITER.getValue())
  139 + return Crediter.newInstance();
  140 + else if (position == Action.DEBITER.getValue())
  141 + return Debiter.newInstance();
  142 + else
  143 + return null;
181 144 }
182 145  
183 146 @Override
184 147 public int getCount() {
185 148 // Show 3 total pages.
186   - return 3;
  149 + return 2;
187 150 }
188 151  
189 152 @Override
... ...
app/src/main/java/net/plil/clubinfo/etunicorn/data/User.java 0 โ†’ 100644
... ... @@ -0,0 +1,29 @@
  1 +package net.plil.clubinfo.etunicorn.data;
  2 +
  3 +/**
  4 + * Created by badetitou on 28/01/2017.
  5 + */
  6 +
  7 +public class User {
  8 + private String username;
  9 + private String password;
  10 +
  11 + public User() {
  12 + }
  13 +
  14 + public String getUsername() {
  15 + return username;
  16 + }
  17 +
  18 + public void setUsername(String username) {
  19 + this.username = username;
  20 + }
  21 +
  22 + public String getPassword() {
  23 + return password;
  24 + }
  25 +
  26 + public void setPassword(String password) {
  27 + this.password = password;
  28 + }
  29 +}
... ...
app/src/main/java/net/plil/clubinfo/etunicorn/utils/VolleyUtils.java
1 1 package net.plil.clubinfo.etunicorn.utils;
2 2  
3 3 import android.content.Context;
4   -import android.graphics.Bitmap;
5   -import android.util.LruCache;
6 4  
7 5 import com.android.volley.Request;
8 6 import com.android.volley.RequestQueue;
9   -import com.android.volley.toolbox.ImageLoader;
10 7 import com.android.volley.toolbox.Volley;
11 8  
12 9 /**
... ... @@ -22,28 +19,11 @@ public class VolleyUtils {
22 19  
23 20 private static VolleyUtils mInstance;
24 21 private RequestQueue mRequestQueue;
25   - private ImageLoader mImageLoader;
26 22 private static Context mCtx;
27 23  
28 24 private VolleyUtils(Context context) {
29 25 mCtx = context;
30 26 mRequestQueue = getRequestQueue();
31   -
32   - mImageLoader = new ImageLoader(mRequestQueue,
33   - new ImageLoader.ImageCache() {
34   - private final LruCache<String, Bitmap>
35   - cache = new LruCache<String, Bitmap>(20);
36   -
37   - @Override
38   - public Bitmap getBitmap(String url) {
39   - return cache.get(url);
40   - }
41   -
42   - @Override
43   - public void putBitmap(String url, Bitmap bitmap) {
44   - cache.put(url, bitmap);
45   - }
46   - });
47 27 }
48 28  
49 29 public static synchronized VolleyUtils getInstance(Context context) {
... ... @@ -65,9 +45,5 @@ public class VolleyUtils {
65 45 public <T> void addToRequestQueue(Request<T> req) {
66 46 getRequestQueue().add(req);
67 47 }
68   -
69   - public ImageLoader getImageLoader() {
70   - return mImageLoader;
71   - }
72 48 }
73 49  
... ...
app/src/main/res/layout/activity_login.xml
... ... @@ -10,7 +10,6 @@
10 10 android:paddingTop="@dimen/activity_vertical_margin"
11 11 tools:context="net.plil.clubinfo.etunicorn.app.LoginActivity">
12 12  
13   - <!-- Login progress -->
14 13 <ProgressBar
15 14 android:id="@+id/login_progress"
16 15 style="?android:attr/progressBarStyleLarge"
... ...
app/src/main/res/layout/fragment_debiter.xml
1   -<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  1 +<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 2 xmlns:tools="http://schemas.android.com/tools"
3 3 android:layout_width="match_parent"
4 4 android:layout_height="match_parent"
5 5 tools:context="net.plil.clubinfo.etunicorn.app.Debiter">
6 6  
7   - <!-- TODO: Update blank fragment layout -->
8   - <TextView
9   - android:layout_width="match_parent"
10   - android:layout_height="match_parent"
11   - android:text="@string/hello_blank_fragment" />
12 7  
13   -</FrameLayout>
  8 + <EditText
  9 + android:inputType="numberSigned"
  10 + android:ems="10"
  11 + android:id="@+id/debiter_edit_text_money"
  12 + android:gravity="center"
  13 + android:layout_width="wrap_content"
  14 + android:layout_height="wrap_content"
  15 + android:layout_centerVertical="true"
  16 + android:layout_centerHorizontal="true"
  17 + android:hint="@string/enter_money"
  18 + android:maxLines="1" />
  19 +
  20 + <ProgressBar
  21 + style="?android:attr/progressBarStyle"
  22 + android:layout_width="wrap_content"
  23 + android:layout_height="wrap_content"
  24 + android:layout_alignParentTop="true"
  25 + android:layout_alignParentStart="true"
  26 + android:id="@+id/debiter_progress"
  27 + android:layout_alignParentBottom="true"
  28 + android:layout_alignParentEnd="true"
  29 + android:visibility="gone"/>
  30 +</RelativeLayout>
... ...
app/src/main/res/values/strings.xml
... ... @@ -14,4 +14,5 @@
14 14  
15 15 <!-- TODO: Remove or change this placeholder text -->
16 16 <string name="hello_blank_fragment">Hello blank fragment</string>
  17 + <string name="enter_money">Enter value</string>
17 18 </resources>
... ...