vendredi 26 novembre 2021

Reduce function and avoid repetition

I want to reduce the size of my function, and therefore avoid repetition. By building a single function that contains all my commands at the same time. the "if" function is present in three different configurations, is it possible to adjust everything to be able to write it once? Can we do the same for "else" functions? If you have any suggestions, don't hesitate, I'm new to Java so my level is not yet advanced enough.

   if (text.substring(15, 18).contains("A"))
            {
                R2.setBackgroundResource(R.drawable.picto_rouge);
            }
            else {
                R2.setBackgroundResource(R.drawable.picto_vert);
            }
             R2.setOnClickListener(a -> {

                 setContentView(R.layout.activity_autodiagnostic_repartiteur);
                 {
                     Repartiteur = findViewById(R.id.Repartiteur);
                     Repartiteur.setBackgroundResource(R.drawable.repartiteur2);

                     BottomNavigationView bandeauNavigationView = findViewById(R.id.bandeau_navigation);
                     bandeauNavigationView.setSelectedItemId(R.id.accueil);
                     bandeauNavigationView.setOnNavigationItemSelectedListener(item -> {
                         switch (item.getItemId()) {
                             case R.id.accueil:
                                 return true;
                             case R.id.commerce:
                                 startActivity(new Intent(getApplicationContext(), PageCommercial.class));
                                 overridePendingTransition(0, 0);
                                 return true;
                             case R.id.services:
                                 startActivity(new Intent(getApplicationContext(), Support.class));
                                 overridePendingTransition(0, 0);
                                 return true;
                             case R.id.info:
                                 startActivity(new Intent(getApplicationContext(), Info.class));
                                 overridePendingTransition(0, 0);
                                 return true;
                         }

                         return false;
                     });
                 }
             });



            if (text.substring(19, 22).contains("A"))
            {
                R3.setBackgroundResource(R.drawable.picto_rouge);
            }
            else {
                R3.setBackgroundResource(R.drawable.picto_vert);
            }
            R3.setOnClickListener(a -> {

                setContentView(R.layout.activity_autodiagnostic_repartiteur);
                {
                    Repartiteur = findViewById(R.id.Repartiteur);
                    Repartiteur.setBackgroundResource(R.drawable.repartiteur3);

                    BottomNavigationView bandeauNavigationView = findViewById(R.id.bandeau_navigation);
                    bandeauNavigationView.setSelectedItemId(R.id.accueil);
                    bandeauNavigationView.setOnNavigationItemSelectedListener(item -> {
                        switch (item.getItemId()) {
                            case R.id.accueil:
                                return true;
                            case R.id.commerce:
                                startActivity(new Intent(getApplicationContext(), PageCommercial.class));
                                overridePendingTransition(0, 0);
                                return true;
                            case R.id.services:
                                startActivity(new Intent(getApplicationContext(), Support.class));
                                overridePendingTransition(0, 0);
                                return true;
                            case R.id.info:
                                startActivity(new Intent(getApplicationContext(), Info.class));
                                overridePendingTransition(0, 0);
                                return true;
                        }

                        return false;
                    });
                }
            });
            
            if (text.substring(23, 26).contains("A"))
            {
                R4.setBackgroundResource(R.drawable.picto_rouge);
            }
            else {
                R4.setBackgroundResource(R.drawable.picto_vert);
            }
            R4.setOnClickListener(a -> {

                setContentView(R.layout.activity_autodiagnostic_repartiteur);
                {
                    Repartiteur = findViewById(R.id.Repartiteur);
                    Repartiteur.setBackgroundResource(R.drawable.repartiteur4);

                    BottomNavigationView bandeauNavigationView = findViewById(R.id.bandeau_navigation);
                    bandeauNavigationView.setSelectedItemId(R.id.accueil);
                    bandeauNavigationView.setOnNavigationItemSelectedListener(item -> {
                        switch (item.getItemId()) {
                            case R.id.accueil:
                                return true;
                            case R.id.commerce:
                                startActivity(new Intent(getApplicationContext(), PageCommercial.class));
                                overridePendingTransition(0, 0);
                                return true;
                            case R.id.services:
                                startActivity(new Intent(getApplicationContext(), Support.class));
                                overridePendingTransition(0, 0);
                                return true;
                            case R.id.info:
                                startActivity(new Intent(getApplicationContext(), Info.class));
                                overridePendingTransition(0, 0);
                                return true;
                        }

                        return false;
                    });
                }
            });

Aucun commentaire:

Enregistrer un commentaire