CreatePersonne.java 7.53 KB
package net.plil.clubinfo.etunicorn.app.personne;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Spinner;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.google.gson.JsonArray;

import net.plil.clubinfo.etunicorn.R;
import net.plil.clubinfo.etunicorn.data.Role;
import net.plil.clubinfo.etunicorn.utils.VolleyUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;


public class CreatePersonne extends DialogFragment {


    Calendar myCalendar = Calendar.getInstance();


    private EditText mLogin;
    private EditText mCarte;
    private Spinner mNaissance;
    private Spinner mRole;
    private ProgressBar mProgressBar;

    private String[] arraySpinnerNaissance;
    private ArrayAdapter<String> adapterNaissance;

    private List<Role> arraySpinnerRole;
    private ArrayAdapter<Role> adapterRole;


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        myCalendar.set(Calendar.YEAR, myCalendar.get(Calendar.YEAR)-18);
        // Use the Builder class for convenient dialog construction
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.fragment_create_personne, null);
        mLogin = (EditText) view.findViewById(R.id.create_personne_login);
        mCarte = (EditText) view.findViewById(R.id.create_personne_carte);
        mRole = (Spinner) view.findViewById(R.id.create_personne_role);
        arraySpinnerRole = new ArrayList<>();
        Role r = new Role();
        r.setName("Role");
        arraySpinnerRole.add(r);
        adapterRole = new ArrayAdapter<>(getActivity(),
                R.layout.simple_item_layout, arraySpinnerRole);
        mRole.setAdapter(adapterRole);
        changeRolePossibility();
        mNaissance = (Spinner) view.findViewById(R.id.create_personne_naissance);
        arraySpinnerNaissance = new String[] {
                "Date d'anniversaire"
        };
        adapterNaissance = new ArrayAdapter<String>(getActivity(),
                R.layout.simple_item_layout, arraySpinnerNaissance);
        mNaissance.setAdapter(adapterNaissance);
        mProgressBar = (ProgressBar) view.findViewById(R.id.create_personne_progress_bar);
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder
                .setTitle(R.string.create_personne)
                .setView(view)
                .setPositiveButton(R.string.valid, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                        mProgressBar.setVisibility(View.VISIBLE);
                        mLogin.setVisibility(View.GONE);
                        mCarte.setVisibility(View.GONE);
                        mNaissance.setVisibility(View.GONE);
                        JSONObject jsonObject = new JSONObject();
                        try {
                            jsonObject.put("login", mLogin.getText().toString());
                            jsonObject.put("carte", mCarte.getText().toString());
                            jsonObject.put("role", mRole.getSelectedItem());
                            jsonObject.put("naissance", mNaissance.getSelectedItem());
                        } catch (JSONException e){
                            e.printStackTrace();
                        }
                        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, VolleyUtils.baseUri + "/personne"   ,jsonObject , new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response) {
                                mProgressBar.setVisibility(View.GONE);
                            }
                        }, new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                mProgressBar.setVisibility(View.GONE);
                            }
                        }
                        );

                        VolleyUtils.getInstance(getContext()).addToRequestQueue(jsonObjectRequest);
                    }
                })
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // User cancelled the dialog
                    }
                });

        mNaissance.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_UP) {
                    new DatePickerDialog(getContext(), date, myCalendar
                            .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                            myCalendar.get(Calendar.DAY_OF_MONTH)).show();
                }
                return true;
            }
        });

        return builder.create();
    }

    private void changeRolePossibility(){
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, VolleyUtils.baseUri + "/role/" ,null , new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                JSONArray jsonArray = null;
                try {
                    //TODO change this by the real name of the array
                    jsonArray = response.getJSONArray("roles");
                    arraySpinnerRole.clear();
                    for (int i =0; i<jsonArray.length();++i){
                        arraySpinnerRole.add(new Gson().fromJson(String.valueOf(jsonArray.getJSONObject(i)), Role.class));
                    }
                    adapterRole.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }
        );

        VolleyUtils.getInstance(getContext()).addToRequestQueue(jsonObjectRequest);
    }

    DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {
            myCalendar.set(Calendar.YEAR, year);
            myCalendar.set(Calendar.MONTH, monthOfYear);
            myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            updateLabel();
        }
    };

        private void updateLabel() {
            String myFormat = "yyyy-MM-dd"; //In which you need put here
            SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.FRANCE);
            arraySpinnerNaissance[0] = sdf.format(myCalendar.getTime());
            adapterNaissance.notifyDataSetChanged();
        }

}