diff --git a/src/main/java/sae/chuzzle/MenuActivity.java b/src/main/java/sae/chuzzle/MenuActivity.java index a4bd5f5..e2d69eb 100644 --- a/src/main/java/sae/chuzzle/MenuActivity.java +++ b/src/main/java/sae/chuzzle/MenuActivity.java @@ -1,44 +1,27 @@ -package sae.chuzzle; - -import android.app.Activity; -import android.content.Intent; -import android.os.Bundle; -import android.view.View; -import android.widget.Button; - -public class MenuActivity extends Activity implements View.OnClickListener { - - private Button btnNouvellePartie; - private Button btnPartieGraine; - private Button btnOptions; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_menu); - - btnNouvellePartie = findViewById(R.id.btnNouvellePartie); - btnPartieGraine = findViewById(R.id.btnPartieGraine); - btnOptions = findViewById(R.id.btnOptions); - - btnNouvellePartie.setOnClickListener(this); - btnPartieGraine.setOnClickListener(this); - btnOptions.setOnClickListener(this); - } - - @Override - public void onClick(View v) { - if (v == btnNouvellePartie) { - long graine = System.currentTimeMillis(); - Intent intent = new Intent(this, MainActivity.class); - intent.putExtra("graine", graine); - startActivity(intent); - } else if (v == btnPartieGraine) { - Intent intent = new Intent(this, SeedActivity.class); - startActivity(intent); - } else if (v == btnOptions) { - Intent intent = new Intent(this, OptionsActivity.class); - startActivity(intent); - } - } +package sae.chuzzle; + +import android.app.Activity; +import android.os.Bundle; +import android.widget.Button; + +public class MenuActivity extends Activity { + + private Button btnNouvellePartie; + private Button btnPartieGraine; + private Button btnOptions; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_menu); + + btnNouvellePartie = findViewById(R.id.btnNouvellePartie); + btnPartieGraine = findViewById(R.id.btnPartieGraine); + btnOptions = findViewById(R.id.btnOptions); + + MenuController controller = new MenuController(this); + btnNouvellePartie.setOnClickListener(controller); + btnPartieGraine.setOnClickListener(controller); + btnOptions.setOnClickListener(controller); + } } \ No newline at end of file diff --git a/src/main/java/sae/chuzzle/MenuController.java b/src/main/java/sae/chuzzle/MenuController.java new file mode 100644 index 0000000..eb813d5 --- /dev/null +++ b/src/main/java/sae/chuzzle/MenuController.java @@ -0,0 +1,30 @@ +package sae.chuzzle; + +import android.content.Intent; +import android.view.View; + +public class MenuController implements View.OnClickListener { + + private final MenuActivity activity; + + public MenuController(MenuActivity activity) { + this.activity = activity; + } + + @Override + public void onClick(View v) { + int id = v.getId(); + if (id == R.id.btnNouvellePartie) { + long graine = System.currentTimeMillis(); + Intent intent = new Intent(activity, MainActivity.class); + intent.putExtra("graine", graine); + activity.startActivity(intent); + } else if (id == R.id.btnPartieGraine) { + Intent intent = new Intent(activity, SeedActivity.class); + activity.startActivity(intent); + } else if (id == R.id.btnOptions) { + Intent intent = new Intent(activity, OptionsActivity.class); + activity.startActivity(intent); + } + } +} diff --git a/src/main/java/sae/chuzzle/OptionsActivity.java b/src/main/java/sae/chuzzle/OptionsActivity.java index 8841adf..82783fa 100644 --- a/src/main/java/sae/chuzzle/OptionsActivity.java +++ b/src/main/java/sae/chuzzle/OptionsActivity.java @@ -1,22 +1,17 @@ package sae.chuzzle; import android.app.Activity; -import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; -import android.view.View; import android.widget.Button; import android.widget.CheckBox; -import android.widget.CompoundButton; -public class OptionsActivity extends Activity - implements CompoundButton.OnCheckedChangeListener, View.OnClickListener { +public class OptionsActivity extends Activity { private SharedPreferences prefs; private CheckBox checkHard; private Button btnRetour; - @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -24,30 +19,13 @@ public class OptionsActivity extends Activity prefs = getSharedPreferences("chuzzle_prefs", MODE_PRIVATE); - checkHard = findViewById(R.id.checkHard); btnRetour = findViewById(R.id.btnBack); checkHard.setChecked(prefs.getBoolean("hard_mode", false)); - - checkHard.setOnCheckedChangeListener(this); - btnRetour.setOnClickListener(this); - } - - @Override - public void onCheckedChanged(CompoundButton bouton, boolean estCoche) { - if (bouton == checkHard) { - prefs.edit().putBoolean("hard_mode", estCoche).apply(); - } - } - - @Override - public void onClick(View v) { - if (v == btnRetour) { - Intent intent = new Intent(this, MenuActivity.class); - startActivity(intent); - } - + OptionsController controller = new OptionsController(this, prefs); + checkHard.setOnCheckedChangeListener(controller); + btnRetour.setOnClickListener(controller); } } diff --git a/src/main/java/sae/chuzzle/OptionsController.java b/src/main/java/sae/chuzzle/OptionsController.java new file mode 100644 index 0000000..ea98471 --- /dev/null +++ b/src/main/java/sae/chuzzle/OptionsController.java @@ -0,0 +1,35 @@ +package sae.chuzzle; + +import android.content.Intent; +import android.content.SharedPreferences; +import android.view.View; +import android.widget.CompoundButton; + +public class OptionsController + implements CompoundButton.OnCheckedChangeListener, View.OnClickListener { + + private final OptionsActivity activity; + private final SharedPreferences prefs; + + public OptionsController(OptionsActivity activity, SharedPreferences prefs) { + this.activity = activity; + this.prefs = prefs; + } + + @Override + public void onCheckedChanged(CompoundButton bouton, boolean estCoche) { + int id = bouton.getId(); + if (id == R.id.checkHard) { + prefs.edit().putBoolean("hard_mode", estCoche).apply(); + } + } + + @Override + public void onClick(View v) { + int id = v.getId(); + if (id == R.id.btnBack) { + Intent intent = new Intent(activity, MenuActivity.class); + activity.startActivity(intent); + } + } +}