Commit 393ff95e4df30558b07719766112f0e897f98f1b

Authored by badetitou
1 parent 6645bb22

Start getting role from Rest

app/src/main/java/net/plil/clubinfo/etunicorn/app/personne/CreatePersonne.java
1 1 package net.plil.clubinfo.etunicorn.app.personne;
2 2  
  3 +import android.app.DatePickerDialog;
3 4 import android.app.Dialog;
4 5 import android.content.DialogInterface;
5 6 import android.os.Bundle;
6 7 import android.support.v4.app.DialogFragment;
7 8 import android.support.v7.app.AlertDialog;
8 9 import android.view.LayoutInflater;
  10 +import android.view.MotionEvent;
9 11 import android.view.View;
  12 +import android.widget.ArrayAdapter;
  13 +import android.widget.DatePicker;
10 14 import android.widget.EditText;
11 15 import android.widget.ProgressBar;
  16 +import android.widget.Spinner;
12 17  
13 18 import com.android.volley.Request;
14 19 import com.android.volley.Response;
15 20 import com.android.volley.VolleyError;
16 21 import com.android.volley.toolbox.JsonObjectRequest;
  22 +import com.google.gson.Gson;
  23 +import com.google.gson.JsonArray;
17 24  
18 25 import net.plil.clubinfo.etunicorn.R;
  26 +import net.plil.clubinfo.etunicorn.data.Role;
19 27 import net.plil.clubinfo.etunicorn.utils.VolleyUtils;
20 28  
  29 +import org.json.JSONArray;
21 30 import org.json.JSONException;
22 31 import org.json.JSONObject;
23 32  
  33 +import java.text.SimpleDateFormat;
  34 +import java.util.ArrayList;
  35 +import java.util.Calendar;
  36 +import java.util.List;
  37 +import java.util.Locale;
  38 +
24 39  
25 40 public class CreatePersonne extends DialogFragment {
26 41  
27   - EditText mLogin;
28   - EditText mCarte;
29   - EditText mNaissance;
30   - EditText mRole;
31   - ProgressBar mProgressBar;
  42 +
  43 + Calendar myCalendar = Calendar.getInstance();
  44 +
  45 +
  46 + private EditText mLogin;
  47 + private EditText mCarte;
  48 + private Spinner mNaissance;
  49 + private Spinner mRole;
  50 + private ProgressBar mProgressBar;
  51 +
  52 + private String[] arraySpinnerNaissance;
  53 + private ArrayAdapter<String> adapterNaissance;
  54 +
  55 + private List<Role> arraySpinnerRole;
  56 + private ArrayAdapter<Role> adapterRole;
32 57  
33 58  
34 59 @Override
35 60 public Dialog onCreateDialog(Bundle savedInstanceState) {
  61 + myCalendar.set(Calendar.YEAR, myCalendar.get(Calendar.YEAR)-18);
36 62 // Use the Builder class for convenient dialog construction
37 63 LayoutInflater inflater = getActivity().getLayoutInflater();
38 64 View view = inflater.inflate(R.layout.fragment_create_personne, null);
39 65 mLogin = (EditText) view.findViewById(R.id.create_personne_login);
40 66 mCarte = (EditText) view.findViewById(R.id.create_personne_carte);
41   - mRole = (EditText) view.findViewById(R.id.create_personne_role);
42   - mNaissance = (EditText) view.findViewById(R.id.create_personne_naissance);
  67 + mRole = (Spinner) view.findViewById(R.id.create_personne_role);
  68 + arraySpinnerRole = new ArrayList<>();
  69 + Role r = new Role();
  70 + r.setName("Role");
  71 + arraySpinnerRole.add(r);
  72 + adapterRole = new ArrayAdapter<>(getActivity(),
  73 + R.layout.simple_item_layout, arraySpinnerRole);
  74 + mRole.setAdapter(adapterRole);
  75 + changeRolePossibility();
  76 + mNaissance = (Spinner) view.findViewById(R.id.create_personne_naissance);
  77 + arraySpinnerNaissance = new String[] {
  78 + "Date d'anniversaire"
  79 + };
  80 + adapterNaissance = new ArrayAdapter<String>(getActivity(),
  81 + R.layout.simple_item_layout, arraySpinnerNaissance);
  82 + mNaissance.setAdapter(adapterNaissance);
43 83 mProgressBar = (ProgressBar) view.findViewById(R.id.create_personne_progress_bar);
44 84 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
45 85 builder
... ... @@ -56,8 +96,8 @@ public class CreatePersonne extends DialogFragment {
56 96 try {
57 97 jsonObject.put("login", mLogin.getText().toString());
58 98 jsonObject.put("carte", mCarte.getText().toString());
59   - jsonObject.put("role", mRole.getText().toString());
60   - jsonObject.put("naissance", mNaissance.getText().toString());
  99 + jsonObject.put("role", mRole.getSelectedItem());
  100 + jsonObject.put("naissance", mNaissance.getSelectedItem());
61 101 } catch (JSONException e){
62 102 e.printStackTrace();
63 103 }
... ... @@ -82,9 +122,68 @@ public class CreatePersonne extends DialogFragment {
82 122 // User cancelled the dialog
83 123 }
84 124 });
85   - // Create the AlertDialog object and return it
  125 +
  126 + mNaissance.setOnTouchListener(new View.OnTouchListener() {
  127 + @Override
  128 + public boolean onTouch(View v, MotionEvent event) {
  129 + if(event.getAction() == MotionEvent.ACTION_UP) {
  130 + new DatePickerDialog(getContext(), date, myCalendar
  131 + .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
  132 + myCalendar.get(Calendar.DAY_OF_MONTH)).show();
  133 + }
  134 + return true;
  135 + }
  136 + });
  137 +
86 138 return builder.create();
87 139 }
88 140  
  141 + private void changeRolePossibility(){
  142 + JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, VolleyUtils.baseUri + "/role/" ,null , new Response.Listener<JSONObject>() {
  143 + @Override
  144 + public void onResponse(JSONObject response) {
  145 + JSONArray jsonArray = null;
  146 + try {
  147 + jsonArray = response.getJSONArray("roles");
  148 + arraySpinnerRole.clear();
  149 + for (int i =0; i<jsonArray.length();++i){
  150 + arraySpinnerRole.add(new Gson().fromJson(String.valueOf(jsonArray.getJSONObject(i)), Role.class));
  151 + }
  152 + response.getJSONArray("hello").length();
  153 + } catch (JSONException e) {
  154 + e.printStackTrace();
  155 + }
  156 +
  157 +
  158 + }
  159 + }, new Response.ErrorListener() {
  160 + @Override
  161 + public void onErrorResponse(VolleyError error) {
  162 +
  163 + }
  164 + }
  165 + );
  166 +
  167 + VolleyUtils.getInstance(getContext()).addToRequestQueue(jsonObjectRequest);
  168 + }
  169 +
  170 + DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
  171 +
  172 + @Override
  173 + public void onDateSet(DatePicker view, int year, int monthOfYear,
  174 + int dayOfMonth) {
  175 + myCalendar.set(Calendar.YEAR, year);
  176 + myCalendar.set(Calendar.MONTH, monthOfYear);
  177 + myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
  178 + updateLabel();
  179 + }
  180 + };
  181 +
  182 + private void updateLabel() {
  183 + String myFormat = "yyyy-MM-dd"; //In which you need put here
  184 + SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.FRANCE);
  185 + arraySpinnerNaissance[0] = sdf.format(myCalendar.getTime());
  186 + adapterNaissance.notifyDataSetChanged();
  187 + }
89 188  
90 189 }
... ...
app/src/main/res/layout/fragment_create_personne.xml
... ... @@ -19,17 +19,17 @@
19 19 android:id="@+id/create_personne_carte"
20 20 android:hint="@string/create_personne_carte"/>
21 21  
22   - <EditText
  22 + <Spinner
23 23 android:layout_width="match_parent"
24 24 android:layout_height="wrap_content"
25 25 android:id="@+id/create_personne_naissance"
26   - android:hint="@string/create_personne_naissance"/>
  26 + android:prompt="@string/create_personne_naissance"
  27 + android:entries="@string/action_delete" />
27 28  
28   - <EditText
  29 + <Spinner
29 30 android:layout_width="match_parent"
30 31 android:layout_height="wrap_content"
31   - android:id="@+id/create_personne_role"
32   - android:hint="@string/create_personne_role"/>
  32 + android:id="@+id/create_personne_role" />
33 33  
34 34  
35 35 <ProgressBar
... ...
app/src/main/res/layout/simple_item_layout.xml 0 → 100644
... ... @@ -0,0 +1,7 @@
  1 +<?xml version="1.0" encoding="utf-8"?>
  2 +<TextView xmlns:android="http://schemas.android.com/apk/res/android"
  3 + android:layout_width="match_parent"
  4 + android:layout_height="match_parent"
  5 + android:textColor="#9E9E9E"
  6 + android:textSize="18sp"
  7 + android:paddingLeft="4dp" />
0 8 \ No newline at end of file
... ...