From db2235673b427d787101e29d2eecc3ac4529c218 Mon Sep 17 00:00:00 2001 From: BenoƮt Verhaeghe Date: Sun, 5 Feb 2017 13:58:06 +0100 Subject: [PATCH] Evenement modify beautiful dialog --- app/src/main/java/net/plil/clubinfo/etunicorn/app/Event/CreateEvent.java | 155 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------- app/src/main/java/net/plil/clubinfo/etunicorn/app/Event/DeleteEvent.java | 30 ++++++++++++++++++------------ app/src/main/java/net/plil/clubinfo/etunicorn/app/Event/ModifyEvent.java | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------ app/src/main/java/net/plil/clubinfo/etunicorn/data/Event.java | 2 +- app/src/main/res/layout/fragment_create_event.xml | 27 +++++++++++++++++++-------- app/src/main/res/layout/fragment_modify_event.xml | 26 +++++++++++++++++++++----- app/src/main/res/values-fr/strings.xml | 10 ++++++++++ app/src/main/res/values/strings.xml | 6 ++++++ 8 files changed, 284 insertions(+), 75 deletions(-) diff --git a/app/src/main/java/net/plil/clubinfo/etunicorn/app/Event/CreateEvent.java b/app/src/main/java/net/plil/clubinfo/etunicorn/app/Event/CreateEvent.java index 0550205..590c227 100644 --- a/app/src/main/java/net/plil/clubinfo/etunicorn/app/Event/CreateEvent.java +++ b/app/src/main/java/net/plil/clubinfo/etunicorn/app/Event/CreateEvent.java @@ -1,14 +1,22 @@ 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; @@ -16,18 +24,31 @@ import com.android.volley.VolleyError; import com.android.volley.toolbox.JsonObjectRequest; import net.plil.clubinfo.etunicorn.R; -import net.plil.clubinfo.etunicorn.app.consommation.ModifyConsommation; 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 CreateEvent extends DialogFragment { + Calendar myCalendar = Calendar.getInstance(); + + EditText mName; EditText mPrice; - EditText mDate; + Spinner mDate; + + TextInputLayout mPriceInput; + TextInputLayout mNameInput; + + private String[] arraySpinnerDate; + private ArrayAdapter adapterDate; + ProgressBar mProgressBar; @@ -38,46 +59,99 @@ public class CreateEvent extends DialogFragment { View view = inflater.inflate(R.layout.fragment_create_event, null); mName = (EditText) view.findViewById(R.id.create_event_name); mPrice = (EditText) view.findViewById(R.id.create_event_price); - mDate = (EditText) view.findViewById(R.id.create_event_date); + mDate = (Spinner) view.findViewById(R.id.create_event_date); + mPriceInput = (TextInputLayout) view.findViewById(R.id.create_event_price_input); + mNameInput = (TextInputLayout) view.findViewById(R.id.create_event_name_input); + arraySpinnerDate = new String[] { + getString(R.string.create_event_date_input) + }; + adapterDate = new ArrayAdapter<>(getActivity(), + R.layout.simple_item_layout, arraySpinnerDate); + mDate.setAdapter(adapterDate); + mProgressBar = (ProgressBar) view.findViewById(R.id.create_event_progress_bar); AlertDialog dialog = new AlertDialog.Builder(getActivity()) .setTitle(R.string.create_event) .setView(view) - .setPositiveButton(R.string.valid, new DialogInterface.OnClickListener() { + .setPositiveButton(R.string.valid, null) + .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { + VolleyUtils.getInstance(getContext()).getRequestQueue().cancelAll(CreateEvent.class); + } + }).create(); + + 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; + } + }); - mProgressBar.setVisibility(View.VISIBLE); - mPrice.setVisibility(View.GONE); - mName.setVisibility(View.GONE); - JSONObject jsonObject = new JSONObject(); - try { - jsonObject.put("nomEvenement", mName.getText().toString()); - jsonObject.put("prix", Double.parseDouble(mPrice.getText().toString())); - jsonObject.put("date", mDate.getText().toString()); - } catch (JSONException e){ - e.printStackTrace(); + 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) { + boolean testDateInputOk = true; + if (mPrice.getText().toString().isEmpty()) { + mPriceInput.setError(getString(R.string.error_create_event_input_price)); + testDateInputOk = false; + } else { + mPriceInput.setError(null); } - JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, VolleyUtils.baseUri + "/evenement" ,jsonObject , new Response.Listener() { - @Override - public void onResponse(JSONObject response) { - mProgressBar.setVisibility(View.GONE); + if (mName.getText().toString().isEmpty()){ + mNameInput.setError(getString(R.string.error_create_event_input_nom)); + testDateInputOk = false; + } else { + mNameInput.setError(null); + } + if (((String)mDate.getSelectedItem()).equals(getString(R.string.create_event_date))) { + testDateInputOk = false; + Toast.makeText(getContext(), getString(R.string.error_create_event_date), Toast.LENGTH_LONG).show(); + } + if (testDateInputOk) { + mProgressBar.setVisibility(View.VISIBLE); + mPriceInput.setVisibility(View.GONE); + mNameInput.setVisibility(View.GONE); + mDate.setVisibility(View.GONE); + JSONObject jsonObject = new JSONObject(); + try { + jsonObject.put("nom", mName.getText().toString()); + jsonObject.put("prix", Double.parseDouble(mPrice.getText().toString())); + jsonObject.put("date", mDate.getAdapter().getItem(0)); + } catch (JSONException e){ + e.printStackTrace(); } - }, new Response.ErrorListener() { - @Override - public void onErrorResponse(VolleyError error) { - mProgressBar.setVisibility(View.GONE); + JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, VolleyUtils.baseUri + "/evenement" ,jsonObject , new Response.Listener() { + @Override + public void onResponse(JSONObject response) { + mProgressBar.setVisibility(View.GONE); + } + }, new Response.ErrorListener() { + @Override + public void onErrorResponse(VolleyError error) { + mProgressBar.setVisibility(View.GONE); + mPriceInput.setVisibility(View.VISIBLE); + mNameInput.setVisibility(View.VISIBLE); + mDate.setVisibility(View.VISIBLE); + Toast.makeText(getContext(), error.getMessage(), Toast.LENGTH_LONG).show(); + } } + ); + jsonObjectRequest.setTag(CreateEvent.class); + VolleyUtils.getInstance(getContext()).addToRequestQueue(jsonObjectRequest); } - ); - jsonObjectRequest.setTag(CreateEvent.class); - VolleyUtils.getInstance(getContext()).addToRequestQueue(jsonObjectRequest); - } - }) - .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - VolleyUtils.getInstance(getContext()).getRequestQueue().cancelAll(CreateEvent.class); } - }).create(); + }); + } + }); dialog.setCanceledOnTouchOutside(false); return dialog; } @@ -89,4 +163,23 @@ public class CreateEvent extends DialogFragment { 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(); + } + } diff --git a/app/src/main/java/net/plil/clubinfo/etunicorn/app/Event/DeleteEvent.java b/app/src/main/java/net/plil/clubinfo/etunicorn/app/Event/DeleteEvent.java index 66fca37..e88408d 100644 --- a/app/src/main/java/net/plil/clubinfo/etunicorn/app/Event/DeleteEvent.java +++ b/app/src/main/java/net/plil/clubinfo/etunicorn/app/Event/DeleteEvent.java @@ -7,6 +7,7 @@ import android.support.v4.app.DialogFragment; import android.support.v7.app.AlertDialog; import android.view.LayoutInflater; import android.view.View; +import android.widget.Button; import android.widget.ProgressBar; import android.widget.Toast; @@ -51,16 +52,27 @@ public class DeleteEvent extends DialogFragment { LayoutInflater inflater = getActivity().getLayoutInflater(); View view = inflater.inflate(R.layout.fragment_delete_event, null); - mProgressBar = (ProgressBar) view.findViewById(R.id.delete_consommation_progress_bar); - final Event event = (Event) getArguments().getSerializable("event"); + mProgressBar = (ProgressBar) view.findViewById(R.id.delete_event_progress_bar); + final Event event = (Event) getArguments().getSerializable("evenement"); AlertDialog dialog = new AlertDialog.Builder(getActivity()) .setTitle(R.string.verif_delete_event) .setView(view) - .setPositiveButton(R.string.delete, new DialogInterface.OnClickListener() { + .setPositiveButton(R.string.delete, null) + .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + VolleyUtils.getInstance(getContext()).getRequestQueue().cancelAll(DeleteEvent.class); + } + }).create(); + 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(DialogInterface dialog, int which) { + public void onClick(View v) { mProgressBar.setVisibility(View.VISIBLE); JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.DELETE, VolleyUtils.baseUri + "/evenement/" + event.getIdEvent(), null, new Response.Listener() { @@ -75,20 +87,14 @@ public class DeleteEvent extends DialogFragment { public void onErrorResponse(VolleyError error) { mProgressBar.setVisibility(View.GONE); Toast.makeText(getContext(), R.string.delete_refused, Toast.LENGTH_LONG).show(); - dismiss(); } } ); jsonObjectRequest.setTag(DeleteEvent.class); VolleyUtils.getInstance(getContext()).addToRequestQueue(jsonObjectRequest); } - }) - .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - VolleyUtils.getInstance(getContext()).getRequestQueue().cancelAll(DeleteEvent.class); - } - }).create(); - dialog.setCanceledOnTouchOutside(false); + }); + }}); return dialog; } diff --git a/app/src/main/java/net/plil/clubinfo/etunicorn/app/Event/ModifyEvent.java b/app/src/main/java/net/plil/clubinfo/etunicorn/app/Event/ModifyEvent.java index f1685a4..9a43a87 100644 --- a/app/src/main/java/net/plil/clubinfo/etunicorn/app/Event/ModifyEvent.java +++ b/app/src/main/java/net/plil/clubinfo/etunicorn/app/Event/ModifyEvent.java @@ -1,14 +1,21 @@ 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; @@ -23,16 +30,25 @@ 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; - EditText mDate; + Spinner mDate; ProgressBar mProgressBar; + TextInputLayout mNomInput; + TextInputLayout mPriceInput; + public static ModifyEvent newInstance(Event event) { ModifyEvent f = new ModifyEvent(); @@ -52,30 +68,64 @@ public class ModifyEvent extends DialogFragment { 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 = (EditText) view.findViewById(R.id.modify_event_date); + 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.FRANCE, "%.2f", event.getPrice())); - mDate.setText(event.getDate().toString()); + 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, new DialogInterface.OnClickListener() { + .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); - mPrice.setVisibility(View.GONE); - mNom.setVisibility(View.GONE); + 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.getText().toString()); + jsonObject.put("date", mDate.getSelectedItem()); } catch (JSONException e){ e.printStackTrace(); } @@ -90,6 +140,9 @@ public class ModifyEvent extends DialogFragment { @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(); } @@ -98,14 +151,10 @@ public class ModifyEvent extends DialogFragment { jsonObjectRequest.setTag(ModifyEvent.class); VolleyUtils.getInstance(getContext()).addToRequestQueue(jsonObjectRequest); } - }) - .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 - dialog.setCanceledOnTouchOutside(false); + }); + + } + }); return dialog; } @@ -116,5 +165,23 @@ public class ModifyEvent extends DialogFragment { 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(); + } -} +} \ No newline at end of file diff --git a/app/src/main/java/net/plil/clubinfo/etunicorn/data/Event.java b/app/src/main/java/net/plil/clubinfo/etunicorn/data/Event.java index 59d1bfa..5e39651 100644 --- a/app/src/main/java/net/plil/clubinfo/etunicorn/data/Event.java +++ b/app/src/main/java/net/plil/clubinfo/etunicorn/data/Event.java @@ -1,7 +1,7 @@ package net.plil.clubinfo.etunicorn.data; import java.io.Serializable; -import java.sql.Date; +import java.util.Date; /** * Created by badetitou on 04/02/2017. diff --git a/app/src/main/res/layout/fragment_create_event.xml b/app/src/main/res/layout/fragment_create_event.xml index cf7b5bd..796ab66 100644 --- a/app/src/main/res/layout/fragment_create_event.xml +++ b/app/src/main/res/layout/fragment_create_event.xml @@ -7,21 +7,32 @@ android:layout_height="match_parent" > - + android:hint="@string/create_event_name" + android:id="@+id/create_event_name_input"> + + - + android:id="@+id/create_event_price_input"> + + - - + android:id="@+id/modify_event_name_input"> - + + + + + android:id="@+id/modify_event_price_input"> + + + + - Veuillez entrer une valeur avant d\'utiliser la carte NFC Entrer le nom Entrer le prix + Date + Entrer la date + Entrer le nom + Entrer le prix + Entrer le nouveau nom + Entrer le nouveau prix + Nom + Prix + Nom + Prix \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a41fb8d..76b35eb 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -48,4 +48,10 @@ Enter the new name Name Price + Date + Enter the price + Enter the name + Enter the date + Name + Price -- libgit2 0.21.2