diff --git a/app/src/main/java/com/slprojects/pizzeria/MainActivity.java b/app/src/main/java/com/slprojects/pizzeria/MainActivity.java index 49434c4425e3e7e2032853680443a558fadc09a6..adf6b85a83259f83627c0951484ee06149e32216 100644 --- a/app/src/main/java/com/slprojects/pizzeria/MainActivity.java +++ b/app/src/main/java/com/slprojects/pizzeria/MainActivity.java @@ -1,5 +1,6 @@ package com.slprojects.pizzeria; +import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; @@ -21,53 +22,63 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - boutons = new ArrayList<Button>(); - idIndexes = new ArrayList<Integer>(); - numCommands = new ArrayList<Integer>(); - nomBoutons = new ArrayList<String>(); + boutons = new ArrayList<>(); + idIndexes = new ArrayList<>(); + numCommands = new ArrayList<>(); + nomBoutons = new ArrayList<>(); boutons.add(findViewById(R.id.napolitaine)); - idIndexes.add(R.id.napolitaine); - numCommands.add(idIndexes.indexOf(R.id.napolitaine), 0); - boutons.add(findViewById(R.id.royale)); - idIndexes.add(R.id.royale); - numCommands.add(idIndexes.indexOf(R.id.royale), 0); - boutons.add(findViewById(R.id.quatreFromages)); - idIndexes.add(R.id.quatreFromages); - numCommands.add(idIndexes.indexOf(R.id.quatreFromages), 0); - boutons.add(findViewById(R.id.montagnarde)); - idIndexes.add(R.id.montagnarde); - numCommands.add(idIndexes.indexOf(R.id.montagnarde), 0); - boutons.add(findViewById(R.id.raclette)); - idIndexes.add(R.id.raclette); - numCommands.add(idIndexes.indexOf(R.id.raclette), 0); - boutons.add(findViewById(R.id.hawaii)); - idIndexes.add(R.id.hawaii); - numCommands.add(idIndexes.indexOf(R.id.hawaii), 0); - boutons.add(findViewById(R.id.pannaCotta)); - idIndexes.add(R.id.pannaCotta); - numCommands.add(idIndexes.indexOf(R.id.pannaCotta), 0); - boutons.add(findViewById(R.id.tiramisu)); - idIndexes.add(R.id.tiramisu); - numCommands.add(idIndexes.indexOf(R.id.tiramisu), 0); + // On ajoute un listener sur chaque bouton boutons.forEach((btn) -> btn.setOnClickListener(this)); + + // On ajoute l'id de chaque bouton (normalement dans l'ordre + boutons.forEach((btn) -> idIndexes.add(btn.getId())); + + // Et on ajoute le nom de chaque bouton (également normalement dans l'ordre) + boutons.forEach((btn) -> nomBoutons.add(btn.getText().toString())); + + // On regarde s'il y a déjà eu une instance + if (savedInstanceState != null) { + // On récupère le nombre de commandes + numCommands = savedInstanceState.getIntegerArrayList("numCommands"); + // Et on remet le nom avec les num de commandes aux boutons + boutons.forEach(this::setButtonText); + }else{ + // On réserve les espaces pour les commandes + boutons.forEach((btn) -> numCommands.add(idIndexes.indexOf(btn.getId()), 0)); + } + } + + @Override + protected void onSaveInstanceState(@NonNull Bundle outState) { + super.onSaveInstanceState(outState); + outState.putIntegerArrayList("numCommands", numCommands); } public void onClick(View v) { try{ Button bouton = boutons.get(idIndexes.indexOf(v.getId())); numCommands.set(idIndexes.indexOf(v.getId()), numCommands.get(idIndexes.indexOf(v.getId()))+1); - bouton.setText(bouton.getText() + " : " + numCommands.get(idIndexes.indexOf(v.getId()))); + setButtonText(bouton); }catch ( Exception e ){ Log.e("Erreur", e.getMessage()); } } + + public void setButtonText(Button bouton){ + if(numCommands.get(idIndexes.indexOf(bouton.getId())) == 0){ + bouton.setText(nomBoutons.get(idIndexes.indexOf(bouton.getId()))); + }else{ + String titre = nomBoutons.get(idIndexes.indexOf(bouton.getId())) + " : " + numCommands.get(idIndexes.indexOf(bouton.getId())); + bouton.setText(titre); + } + } } \ No newline at end of file