séparation option et Menu - respect model MVC

This commit is contained in:
2026-03-28 15:51:55 +01:00
parent 48f6a479aa
commit 1e1fa2b734
4 changed files with 95 additions and 69 deletions
+5 -22
View File
@@ -1,12 +1,10 @@
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 {
public class MenuActivity extends Activity {
private Button btnNouvellePartie;
private Button btnPartieGraine;
@@ -21,24 +19,9 @@ public class MenuActivity extends Activity implements View.OnClickListener {
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);
}
MenuController controller = new MenuController(this);
btnNouvellePartie.setOnClickListener(controller);
btnPartieGraine.setOnClickListener(controller);
btnOptions.setOnClickListener(controller);
}
}
@@ -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);
}
}
}
+4 -26
View File
@@ -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);
}
}
@@ -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);
}
}
}