package net.plil.clubinfo.etunicorn.app.event; import android.app.DatePickerDialog; import android.app.Dialog; import android.content.DialogInterface; import android.os.Bundle; import android.support.design.widget.TextInputLayout; 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.Button; import android.widget.DatePicker; import android.widget.EditText; import android.widget.ProgressBar; import android.widget.Spinner; import android.widget.Toast; import com.android.volley.Request; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.JsonObjectRequest; import net.plil.clubinfo.etunicorn.R; import net.plil.clubinfo.etunicorn.data.Event; import net.plil.clubinfo.etunicorn.utils.VolleyUtils; import org.json.JSONException; import org.json.JSONObject; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Locale; public class ModifyEvent extends DialogFragment { Calendar myCalendar = Calendar.getInstance(); private String[] arraySpinnerDate; private ArrayAdapter adapterDate; EditText mNom; EditText mPrice; Spinner mDate; ProgressBar mProgressBar; TextInputLayout mNomInput; TextInputLayout mPriceInput; public static ModifyEvent newInstance(Event event) { ModifyEvent f = new ModifyEvent(); // Supply num input as an argument. Bundle args = new Bundle(); args.putSerializable("event", event); f.setArguments(args); return f; } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the Builder class for convenient dialog construction LayoutInflater inflater = getActivity().getLayoutInflater(); View view = inflater.inflate(R.layout.fragment_modify_event, null); mNom = (EditText) view.findViewById(R.id.modify_event_name); mPrice = (EditText) view.findViewById(R.id.modify_event_price); mDate = (Spinner) view.findViewById(R.id.modify_event_date); mProgressBar = (ProgressBar) view.findViewById(R.id.modify_event_progress_bar); mNomInput = (TextInputLayout) view.findViewById(R.id.modify_event_name_input); mPriceInput = (TextInputLayout) view.findViewById(R.id.modify_event_price_input); final Event event = (Event) getArguments().getSerializable("event"); String myFormat = "yyyy-MM-dd"; //In which you need put here SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.FRANCE); arraySpinnerDate = new String[] { sdf.format(event.getDate().getTime()) }; adapterDate = new ArrayAdapter<>(getActivity(), R.layout.simple_item_layout, arraySpinnerDate); mDate.setAdapter(adapterDate); mNom.setText(event.getNomEvent()); mPrice.setText(String.format(Locale.US, "%.2f", event.getPrice())); AlertDialog dialog = new AlertDialog.Builder(getActivity()) .setTitle(R.string.modify) .setView(view) .setPositiveButton(R.string.modify, null) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { VolleyUtils.getInstance(getContext()).getRequestQueue().cancelAll(ModifyEvent.class); } }).create(); // Create the AlertDialog object and return it mDate.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; } }); dialog.setCanceledOnTouchOutside(false); dialog.setOnShowListener(new DialogInterface.OnShowListener() { @Override public void onShow(DialogInterface dialog) { Button button = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mProgressBar.setVisibility(View.VISIBLE); mPriceInput.setVisibility(View.GONE); mNomInput.setVisibility(View.GONE); mDate.setVisibility(View.GONE); JSONObject jsonObject = new JSONObject(); try { jsonObject.put("nomEvenement", mNom.getText().toString()); jsonObject.put("prix", Double.parseDouble(mPrice.getText().toString())); jsonObject.put("date", mDate.getSelectedItem()); } catch (JSONException e){ e.printStackTrace(); } JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, VolleyUtils.baseUri + "/evenement/" + event.getIdEvent() ,jsonObject , new Response.Listener() { @Override public void onResponse(JSONObject response) { mProgressBar.setVisibility(View.GONE); Toast.makeText(getContext(), R.string.modify_done, Toast.LENGTH_LONG).show(); dismiss(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { mProgressBar.setVisibility(View.GONE); mPriceInput.setVisibility(View.VISIBLE); mNomInput.setVisibility(View.VISIBLE); mDate.setVisibility(View.VISIBLE); Toast.makeText(getContext(), R.string.modify_refused, Toast.LENGTH_LONG).show(); } } ); jsonObjectRequest.setTag(ModifyEvent.class); VolleyUtils.getInstance(getContext()).addToRequestQueue(jsonObjectRequest); } }); } }); return dialog; } @Override public void onStop() { VolleyUtils.getInstance(getContext()).getRequestQueue().cancelAll(ModifyEvent.class); dismissAllowingStateLoss(); super.onStop(); } 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); arraySpinnerDate[0] = sdf.format(myCalendar.getTime()); adapterDate.notifyDataSetChanged(); } }