-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectCategoryActivity.java
More file actions
382 lines (332 loc) · 14.7 KB
/
SelectCategoryActivity.java
File metadata and controls
382 lines (332 loc) · 14.7 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package ihm.tydrichova.upmc.fr.ihmclient;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Parcelable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
import java.util.Set;
import java.util.TreeSet;
import ihm.tydrichova.upmc.fr.ihmclient.CarteActivity;
import ihm.tydrichova.upmc.fr.ihmclient.MainActivity;
import ihm.tydrichova.upmc.fr.ihmclient.R;
import ihm.tydrichova.upmc.fr.ihmclient.model.*;
import ihm.tydrichova.upmc.fr.ihmclient.data.*;
public class SelectCategoryActivity extends AppCompatActivity {
private Menu menu = null;
private String key = null;
private TreeSet<Plat> allPlats = new TreeSet<>();
private TreeSet<Plat> preselectedPlats = new TreeSet<>();
private HashMap<Categorie, Set<Plat>> platsByCategories = new HashMap<>();
private boolean isEnabled = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String layout = getIntent().getStringExtra("layout");
System.out.print(layout);
switch (layout) {
case ("Plats"):
setContentView(R.layout.activity_select_category_plats);
key = "Plats";
break;
case ("Entrées"):
setContentView(R.layout.activity_select_category_entrees);
key = "Entrées";
break;
case ("Desserts"):
setContentView(R.layout.activity_select_category_desserts);
key = "Desserts";
break;
case ("Boissons"):
setContentView(R.layout.activity_select_category_boissons);
key = "Boissons";
break;
case ("Accompagnements"):
setContentView(R.layout.activity_select_category_acc);
key = "Accompagnements";
break;
default:
throw new RuntimeException("Unknow layout");
}
Toolbar mainToolbar = (Toolbar) findViewById(R.id.main_toolbar);
setSupportActionBar(mainToolbar);
getSupportActionBar().setTitle(null);
preselectPlats();
Button valider = (Button) findViewById(R.id.button_valider);
valider.setEnabled(false);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_items, menu);
this.menu = menu;
menu.findItem(R.id.menu_price).setTitle(((Float) MainActivity.SessionData.getPrix()).toString());
menu.findItem(R.id.menu_commande).setEnabled(MainActivity.SessionData.getPrix() > 0);
if(MainActivity.SessionData.clientConnected()){
menu.findItem(R.id.menu_client).setTitle(MainActivity.SessionData.getClient().getLogin());
}
else{
menu.findItem(R.id.menu_client).setTitle("Se connecter");
}
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_return:
finish();
return true;
case R.id.menu_homepage:
goToHomepage();
return true;
case R.id.menu_commande:
goToCommande();
return true;
case R.id.menu_client:
onClientClick();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
protected void preselectPlats() {
LinearLayout container = (LinearLayout) findViewById(R.id.layout_checkBoxes);
for (int i = 0; i < container.getChildCount(); i++) {
View v = container.getChildAt(i);
if (v instanceof CheckBox) {
CheckBox cb = (CheckBox) v;
cb.setEnabled(false);
}
}
allPlats.clear();
ArrayList<Carte> cartes = Cartes.getInstance().getCartes();
ArrayList<Carte> cartes2 = new ArrayList<>();
TreeSet<Plat> plats = new TreeSet<>();
for (Carte carte : cartes) {
cartes2.add(carte);
Set<Categorie> keys = carte.getCarte().keySet();
for (Categorie cat : keys) {
if (cat.getName().toLowerCase().startsWith(key.toLowerCase())) {
int nbPlats = 0;
Set<Plat> platsOfCat = carte.getCarte().get(cat);
Set<Plat> addToPlatsOfCategories = new TreeSet<>();
for (Plat plat : platsOfCat) {
if (noAllergene(plat) || noDiete(plat)) {
addToPlatsOfCategories.add(plat);
nbPlats++;
}
}
for (int i = 0; i < container.getChildCount(); i++) {
View v = container.getChildAt(i);
if (v instanceof CheckBox) {
CheckBox cb = (CheckBox) v;
cb.setEnabled((cb.isEnabled()) || (nbPlats > 0 && cat.getName().toLowerCase().endsWith(cb.getText().toString().toLowerCase())));
if (cat.getName().toLowerCase().endsWith(cb.getText().toString().toLowerCase())) {
platsByCategories.put(cat, addToPlatsOfCategories);
}
}
}
}
}
}
}
protected boolean noDiete(Plat plat){
for(String diete :MainActivity.SessionData.getDietesFrequentes()){
if(diete.equalsIgnoreCase("végétarien") && !isVegetarian(plat)){
return false;
}
if(diete.equalsIgnoreCase("vegan") && !isVegan(plat)){
return false;
}
}
return true;
}
protected boolean noAllergene(Plat plat) {
for (Allergene allergene : MainActivity.SessionData.getAllergenes()) {
for (Ingredient ingredient : plat.getIngredients().keySet()) {
if (plat.getIngredients().get(ingredient)) {
for (Allergene allergene1 : ingredient.getAllergenes()) {
if (allergene.toString().equalsIgnoreCase(allergene1.toString())) {
return false;
}
}
}
}
}
return true;
}
//renvoie true si aucune ingrédient n'est pas de type VIANDE ou POISSON
protected boolean isVegetarian(Plat plat){
for(Ingredient ingredient : plat.getIngredients().keySet()) {
if (plat.getIngredients().get(ingredient)) {
if (ingredient.getType().toString().equalsIgnoreCase("VIANDE") || ingredient.getType().toString().equalsIgnoreCase("POISSOn"))
return false;
}
}
return true;
}
protected boolean isVegan(Plat plat){
for(Ingredient ingredient : plat.getIngredients().keySet()) {
if (plat.getIngredients().get(ingredient)) {
if (ingredient.getType().toString().equalsIgnoreCase("VIANDE") || ingredient.getType().toString().equalsIgnoreCase("POISSOn") ||
ingredient.getType().toString().equalsIgnoreCase("VEGETARIEN"))
return false;
}
}
return true;
}
public void enableValider(View v){
Button valider = (Button) findViewById(R.id.button_valider);
valider.setEnabled(false);
LinearLayout container = (LinearLayout) findViewById(R.id.layout_checkBoxes);
for (int i = 0; i < container.getChildCount(); i++) {
View v2 = container.getChildAt(i);
if (v2 instanceof CheckBox) {
CheckBox cb = (CheckBox) v2;
if(cb.isChecked()){
valider.setEnabled(true);
}
}
}
}
public void goToHomepage() {
startActivity(new Intent(SelectCategoryActivity.this, MainActivity.class));
finish();
}
public void validerAction(View view) {
Intent intent = new Intent(SelectCategoryActivity.this, CarteActivity.class);
//ArrayList<Plat> plats = (ArrayList<Plat>)Plats.getInstance().getPlats();
preselectedPlats.clear();
LinearLayout container = (LinearLayout) findViewById(R.id.layout_checkBoxes);
for (int i = 0; i < container.getChildCount(); i++) {
View v = container.getChildAt(i);
if (v instanceof CheckBox) {
CheckBox cb = (CheckBox) v;
for (Categorie cat : platsByCategories.keySet()) {
if (cb.isChecked() && cat.getName().toLowerCase().endsWith(cb.getText().toString().toLowerCase())) {
preselectedPlats.addAll(platsByCategories.get(cat));
}
}
// isChecked should be a boolean indicating if every Checkbox should be checked or not
}
ArrayList<Plat> platsToSend = new ArrayList<>();
platsToSend.addAll(preselectedPlats);
intent.putParcelableArrayListExtra("plats", platsToSend);
}
startActivity(intent);
}
@Override
protected void onResume() {
super.onResume();
if (menu != null) {
menu.findItem(R.id.menu_price).setTitle(((Float) MainActivity.SessionData.getPrix()).toString());
menu.findItem(R.id.menu_commande).setEnabled(MainActivity.SessionData.getPrix() > 0);
}
preselectPlats();
if(MainActivity.SessionData.clientConnected()){
menu.findItem(R.id.menu_client).setTitle(MainActivity.SessionData.getClient().getLogin());
}
else{
menu.findItem(R.id.menu_client).setTitle("Se connecter");
}
}
public void goToCommande() {
Intent intent = new Intent(SelectCategoryActivity.this, CommandeActivity.class);
startActivity(intent);
}
public void filtreActivity(View view) {
Intent intent = new Intent(SelectCategoryActivity.this, FilterActivity.class);
startActivity(intent);
}
protected void onClientClick() {
if (MainActivity.SessionData.clientConnected()) {
Intent intent = new Intent(SelectCategoryActivity.this, ClientActivity.class);
startActivity(intent);
} else {
showLoginDialog();
}
}
protected void showLoginDialog() {
showLoginDialog(0);
}
protected void showLoginDialog(int id) {
LayoutInflater li = LayoutInflater.from(this);
final View prompt = li.inflate(R.layout.login_dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setView(prompt);
final EditText user = (EditText) prompt.findViewById(R.id.login_name);
final EditText pass = (EditText) prompt.findViewById(R.id.login_password);
if (id == -1) {
((TextView) prompt.findViewById(R.id.message)).setText("Nom d'utilisateur ou mot de passe inconnu !");
}
//user.setText(Login_USER); //login_USER and PASS are loaded from previous session (optional)
//pass.setText(Login_PASS);
alertDialogBuilder.setTitle("Connectez vous :");
alertDialogBuilder.setCancelable(false)
.setPositiveButton("Connecter", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
/*
if (!Utils.hasConnectivity(MainActivity.this,false)) //check for internet connectivity
{
Toast.makeText(MainActivity.this,"No internet access... please connect.", Toast.LENGTH_LONG).show();
showLoginDialog();
return;
}*/
String password = pass.getText().toString();
String username = user.getText().toString();
TreeSet<Client> knownClients = new TreeSet<>();
knownClients.addAll(Clients.getInstance().getClients());
boolean found = false;
for (Client client : knownClients) {
if (client.getLogin().equals(username) && client.passwordCorrect(password)) {
MainActivity.SessionData.connectClient(client);
menu.findItem(R.id.menu_client).setTitle(client.getLogin());
found = true;
}
}
if (!found) {
showLoginDialog(-1);
}
/*
try
{
if ( username.length()<2 || password.length()<2)
{
Toast.makeText(MainActivity.this,"Invalid username or password", Toast.LENGTH_LONG).show();
showLoginDialog();
}
else
{
//password=MCrypt3DES.computeSHA1Hash(password); //password is hashed SHA1
//TODO here any local checks if password or user is valid
//this will do the actual check with my back-end server for valid user/pass and callback with the response
//new CheckLoginAsync(MainActivity.this,username,password).execute("","");
}
}catch(Exception e)
{
Toast.makeText(MainActivity.this,e.getMessage(), Toast.LENGTH_LONG).show();
}*/
}
});
alertDialogBuilder.setNegativeButton("Retour", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
alertDialogBuilder.show();
}
}