ModifyPersonne.java
12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
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.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.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import net.plil.clubinfo.etunicorn.R;
import net.plil.clubinfo.etunicorn.app.MainActivity;
import net.plil.clubinfo.etunicorn.data.Personne;
import net.plil.clubinfo.etunicorn.data.Role;
import net.plil.clubinfo.etunicorn.utils.ConvertBytesToString;
import net.plil.clubinfo.etunicorn.utils.JsonConverter;
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.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
public class ModifyPersonne extends DialogFragment {
Calendar myCalendar = Calendar.getInstance();
EditText mCarte;
Spinner mNaissance;
EditText mLogin;
Spinner mRole;
ProgressBar mProgressBar;
TextInputLayout mCarteInput;
TextInputLayout mLoginInput;
private String[] arraySpinnerNaissance;
private ArrayAdapter<String> adapterNaissance;
private List<Role> arraySpinnerRole;
private ArrayAdapter<Role> adapterRole;
public static ModifyPersonne newInstance(Personne personne) {
ModifyPersonne f = new ModifyPersonne();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putSerializable("personne", personne);
f.setArguments(args);
return f;
}
@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_modify_personne, null);
mLogin = (EditText) view.findViewById(R.id.modify_personne_login);
mCarte = (EditText) view.findViewById(R.id.modify_personne_carte);
mNaissance = (Spinner) view.findViewById(R.id.modify_personne_naissance);
mRole = (Spinner) view.findViewById(R.id.modify_personne_role);
mProgressBar = (ProgressBar) view.findViewById(R.id.modify_personne_progress_bar);
mLoginInput = (TextInputLayout) view.findViewById(R.id.modify_personne_login_input);
mCarteInput = (TextInputLayout) view.findViewById(R.id.modify_personne_carte_input);
final Personne personne = (Personne) getArguments().getSerializable("personne");
mCarte.setText(personne.getCarte());
mLogin.setText(personne.getLogin());
arraySpinnerRole = new ArrayList<>();
arraySpinnerRole.add(personne.getRole());
adapterRole = new ArrayAdapter<>(getActivity(),
R.layout.simple_item_layout, arraySpinnerRole);
mRole.setAdapter(adapterRole);
changeRolePossibility();
mNaissance = (Spinner) view.findViewById(R.id.modify_personne_naissance);
final String myFormat = "yyyy-MM-dd"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.FRANCE);
if (personne.getNaissance() == null){
arraySpinnerNaissance = new String[]{
getString(R.string.modify_personne_hint_date)
};
} else {
arraySpinnerNaissance = new String[]{
sdf.format(personne.getNaissance())
};
}
adapterNaissance = new ArrayAdapter<String>(getActivity(),
R.layout.simple_item_layout, arraySpinnerNaissance);
mNaissance.setAdapter(adapterNaissance);
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(ModifyPersonne.class);
}
}).create();
// Create the AlertDialog object and return it
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);
mRole.setVisibility(View.GONE);
mLoginInput.setVisibility(View.GONE);
mCarteInput.setVisibility(View.GONE);
mNaissance.setVisibility(View.GONE);
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("carte", mCarte.getText().toString());
jsonObject.put("login", mLogin.getText().toString());
jsonObject.put("role", JsonConverter.convertToJSONObject(mRole.getSelectedItem()));
if (!mNaissance.getSelectedItem().equals(getString(R.string.modify_personne_hint_date)))
jsonObject.put("naissance", mNaissance.getSelectedItem().toString());
} catch (JSONException e){
e.printStackTrace();
}
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.PUT, VolleyUtils.baseUri + "/personne/" + personne.getId() ,jsonObject , new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
mProgressBar.setVisibility(View.GONE);
//Toast.makeText(getContext(), R.string.modify_done, Toast.LENGTH_LONG).show();
Toast.makeText(getContext(), "good response", Toast.LENGTH_SHORT).show();
dismiss();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mProgressBar.setVisibility(View.GONE);
if (error.networkResponse != null)
Toast.makeText(getContext(), R.string.modify_refused + " : " + ConvertBytesToString.bytesToStringVolLey(error.networkResponse.data), Toast.LENGTH_LONG).show();
else
Toast.makeText(getContext(), "!!" + error.getStackTrace().toString(), Toast.LENGTH_SHORT).show();
mRole.setVisibility(View.VISIBLE);
mCarteInput.setVisibility(View.VISIBLE);
mLoginInput.setVisibility(View.VISIBLE);
mNaissance.setVisibility(View.VISIBLE);
}
}
){
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
headers.put("Authorization", MainActivity.session.getToken());
return headers;
}};
jsonObjectRequest.setTag(ModifyPersonne.class);
try {
for (String s : jsonObjectRequest.getHeaders().keySet()){
System.out.println(s + " " + jsonObjectRequest.getHeaders().get(s));
}
} catch (AuthFailureError authFailureError) {
authFailureError.printStackTrace();
}
System.out.println(ConvertBytesToString.bytesToStringVolLey(jsonObjectRequest.getBody()));
VolleyUtils.getInstance(getContext()).addToRequestQueue(jsonObjectRequest);
}
});
}
});
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;
}
});
dialog.setCanceledOnTouchOutside(false);
return dialog;
}
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();
}
@Override
public void onStop() {
VolleyUtils.getInstance(getContext()).getRequestQueue().cancelAll(ModifyPersonne.class);
dismissAllowingStateLoss();
super.onStop();
}
private void changeRolePossibility(){
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, VolleyUtils.baseUri + "/role" ,null , new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
arraySpinnerRole.clear();
for (int i =0; i<response.length();++i){
arraySpinnerRole.add(JsonConverter.getConverter().fromJson(String.valueOf(response.getJSONObject(i)), Role.class));
}
adapterRole.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if(error.networkResponse == null)
Toast.makeText(getContext(), "Verifier votre connection Internet", Toast.LENGTH_LONG).show();
else
Toast.makeText(getContext(), ConvertBytesToString.bytesToStringVolLey(error.networkResponse.data), Toast.LENGTH_LONG).show();
}
}
);
jsonArrayRequest.setTag(ModifyPersonne.class);
VolleyUtils.getInstance(getContext()).addToRequestQueue(jsonArrayRequest);
}
}