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 package net.plil.clubinfo.etunicorn.app.personne; 1 package net.plil.clubinfo.etunicorn.app.personne;
2 2
  3 +import android.app.DatePickerDialog;
3 import android.app.Dialog; 4 import android.app.Dialog;
4 import android.content.DialogInterface; 5 import android.content.DialogInterface;
5 import android.os.Bundle; 6 import android.os.Bundle;
6 import android.support.v4.app.DialogFragment; 7 import android.support.v4.app.DialogFragment;
7 import android.support.v7.app.AlertDialog; 8 import android.support.v7.app.AlertDialog;
8 import android.view.LayoutInflater; 9 import android.view.LayoutInflater;
  10 +import android.view.MotionEvent;
9 import android.view.View; 11 import android.view.View;
  12 +import android.widget.ArrayAdapter;
  13 +import android.widget.DatePicker;
10 import android.widget.EditText; 14 import android.widget.EditText;
11 import android.widget.ProgressBar; 15 import android.widget.ProgressBar;
  16 +import android.widget.Spinner;
12 17
13 import com.android.volley.Request; 18 import com.android.volley.Request;
14 import com.android.volley.Response; 19 import com.android.volley.Response;
15 import com.android.volley.VolleyError; 20 import com.android.volley.VolleyError;
16 import com.android.volley.toolbox.JsonObjectRequest; 21 import com.android.volley.toolbox.JsonObjectRequest;
  22 +import com.google.gson.Gson;
  23 +import com.google.gson.JsonArray;
17 24
18 import net.plil.clubinfo.etunicorn.R; 25 import net.plil.clubinfo.etunicorn.R;
  26 +import net.plil.clubinfo.etunicorn.data.Role;
19 import net.plil.clubinfo.etunicorn.utils.VolleyUtils; 27 import net.plil.clubinfo.etunicorn.utils.VolleyUtils;
20 28
  29 +import org.json.JSONArray;
21 import org.json.JSONException; 30 import org.json.JSONException;
22 import org.json.JSONObject; 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 public class CreatePersonne extends DialogFragment { 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 @Override 59 @Override
35 public Dialog onCreateDialog(Bundle savedInstanceState) { 60 public Dialog onCreateDialog(Bundle savedInstanceState) {
  61 + myCalendar.set(Calendar.YEAR, myCalendar.get(Calendar.YEAR)-18);
36 // Use the Builder class for convenient dialog construction 62 // Use the Builder class for convenient dialog construction
37 LayoutInflater inflater = getActivity().getLayoutInflater(); 63 LayoutInflater inflater = getActivity().getLayoutInflater();
38 View view = inflater.inflate(R.layout.fragment_create_personne, null); 64 View view = inflater.inflate(R.layout.fragment_create_personne, null);
39 mLogin = (EditText) view.findViewById(R.id.create_personne_login); 65 mLogin = (EditText) view.findViewById(R.id.create_personne_login);
40 mCarte = (EditText) view.findViewById(R.id.create_personne_carte); 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 mProgressBar = (ProgressBar) view.findViewById(R.id.create_personne_progress_bar); 83 mProgressBar = (ProgressBar) view.findViewById(R.id.create_personne_progress_bar);
44 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 84 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
45 builder 85 builder
@@ -56,8 +96,8 @@ public class CreatePersonne extends DialogFragment { @@ -56,8 +96,8 @@ public class CreatePersonne extends DialogFragment {
56 try { 96 try {
57 jsonObject.put("login", mLogin.getText().toString()); 97 jsonObject.put("login", mLogin.getText().toString());
58 jsonObject.put("carte", mCarte.getText().toString()); 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 } catch (JSONException e){ 101 } catch (JSONException e){
62 e.printStackTrace(); 102 e.printStackTrace();
63 } 103 }
@@ -82,9 +122,68 @@ public class CreatePersonne extends DialogFragment { @@ -82,9 +122,68 @@ public class CreatePersonne extends DialogFragment {
82 // User cancelled the dialog 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 return builder.create(); 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,17 +19,17 @@
19 android:id="@+id/create_personne_carte" 19 android:id="@+id/create_personne_carte"
20 android:hint="@string/create_personne_carte"/> 20 android:hint="@string/create_personne_carte"/>
21 21
22 - <EditText 22 + <Spinner
23 android:layout_width="match_parent" 23 android:layout_width="match_parent"
24 android:layout_height="wrap_content" 24 android:layout_height="wrap_content"
25 android:id="@+id/create_personne_naissance" 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 android:layout_width="match_parent" 30 android:layout_width="match_parent"
30 android:layout_height="wrap_content" 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 <ProgressBar 35 <ProgressBar
app/src/main/res/layout/simple_item_layout.xml 0 → 100644
@@ -0,0 +1,7 @@ @@ -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 \ No newline at end of file 8 \ No newline at end of file