diff --git a/app/src/main/java/legion/muyue/best2048/GameStats.java b/app/src/main/java/legion/muyue/best2048/GameStats.java index 5c43b1f..b74ca24 100644 --- a/app/src/main/java/legion/muyue/best2048/GameStats.java +++ b/app/src/main/java/legion/muyue/best2048/GameStats.java @@ -217,6 +217,35 @@ public class GameStats { } } + /** + * Réinitialise toutes les statistiques (solo, multijoueur, high score global) + * à leurs valeurs par défaut. + */ + public void resetStats() { + // Réinitialise toutes les variables membres à 0 ou valeur initiale + overallHighScore = 0; + totalGamesPlayed = 0; + totalGamesStarted = 0; + totalMoves = 0; + totalPlayTimeMs = 0; + totalMerges = 0; + highestTile = 0; + numberOfTimesObjectiveReached = 0; + perfectGames = 0; + bestWinningTimeMs = Long.MAX_VALUE; + worstWinningTimeMs = 0; + + multiplayerGamesWon = 0; + multiplayerGamesPlayed = 0; + multiplayerBestWinningStreak = 0; + multiplayerTotalScore = 0; + multiplayerTotalTimeMs = 0; + totalMultiplayerLosses = 0; + multiplayerHighestScore = 0; + + saveStats(); + } + // --- Getters pour l'affichage --- public int getTotalGamesPlayed() { return totalGamesPlayed; } public int getTotalGamesStarted() { return totalGamesStarted; } diff --git a/app/src/main/java/legion/muyue/best2048/MainActivity.java b/app/src/main/java/legion/muyue/best2048/MainActivity.java index 98bfc8e..d894f48 100644 --- a/app/src/main/java/legion/muyue/best2048/MainActivity.java +++ b/app/src/main/java/legion/muyue/best2048/MainActivity.java @@ -10,7 +10,8 @@ package legion.muyue.best2048; import android.annotation.SuppressLint; import android.app.AlertDialog; import android.content.ActivityNotFoundException; -import android.content.DialogInterface; // Assurez-vous que cet import est présent +import android.provider.Settings; +import com.google.android.material.switchmaterial.SwitchMaterial; import android.content.Intent; import android.content.SharedPreferences; import android.net.Uri; @@ -475,16 +476,146 @@ public class MainActivity extends AppCompatActivity { } /** - * Affiche une boîte de dialogue placeholder pour les paramètres. + * Affiche la boîte de dialogue des paramètres en utilisant un layout personnalisé. + * Gère les interactions avec les différentes options. */ private void showSettingsDialog() { + AlertDialog.Builder builder = new AlertDialog.Builder(this); + LayoutInflater inflater = getLayoutInflater(); + View dialogView = inflater.inflate(R.layout.dialog_settings, null); + builder.setView(dialogView); + builder.setCancelable(true); // Permet de fermer en cliquant à côté + + // Récupération des vues + SwitchMaterial switchSound = dialogView.findViewById(R.id.switchSound); + SwitchMaterial switchNotifications = dialogView.findViewById(R.id.switchNotifications); + Button permissionsButton = dialogView.findViewById(R.id.buttonManagePermissions); + Button shareStatsButton = dialogView.findViewById(R.id.buttonShareStats); + Button resetStatsButton = dialogView.findViewById(R.id.buttonResetStats); + Button quitAppButton = dialogView.findViewById(R.id.buttonQuitApp); + Button closeButton = dialogView.findViewById(R.id.buttonCloseSettings); + + final AlertDialog dialog = builder.create(); + + // Configuration initiale des switches (désactivés pour l'instant) + switchSound.setEnabled(false); + switchSound.setChecked(false); // Mettre à jour avec la vraie valeur si implémenté + switchSound.setOnCheckedChangeListener((buttonView, isChecked) -> { + // TODO: Implémenter la logique Son ON/OFF + Toast.makeText(this, "Gestion du son à venir.", Toast.LENGTH_SHORT).show(); + // switchSound.setChecked(!isChecked); // Revenir en arrière car pas implémenté + }); + + switchNotifications.setEnabled(false); + switchNotifications.setChecked(false); // Mettre à jour avec la vraie valeur si implémenté + switchNotifications.setOnCheckedChangeListener((buttonView, isChecked) -> { + // TODO: Implémenter la logique Notifications ON/OFF + Toast.makeText(this, "Gestion des notifications à venir.", Toast.LENGTH_SHORT).show(); + // switchNotifications.setChecked(!isChecked); + }); + + // Listener bouton Permissions + permissionsButton.setOnClickListener(v -> { + Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); + Uri uri = Uri.fromParts("package", getPackageName(), null); + intent.setData(uri); + try { + startActivity(intent); + } catch (ActivityNotFoundException e) { + Toast.makeText(this, "Impossible d'ouvrir les paramètres de l'application.", Toast.LENGTH_LONG).show(); + } + dialog.dismiss(); // Ferme les paramètres après clic + }); + + // Listener bouton Partager Stats + shareStatsButton.setOnClickListener(v -> { + shareStats(); + dialog.dismiss(); // Ferme après partage (ou tentative) + }); + + // Listener bouton Réinitialiser Stats + resetStatsButton.setOnClickListener(v -> { + dialog.dismiss(); // Ferme d'abord la dialogue des paramètres + showResetStatsConfirmationDialog(); // Ouvre la confirmation + }); + + // Listener bouton Quitter + quitAppButton.setOnClickListener(v -> { + dialog.dismiss(); + finishAffinity(); // Ferme toutes les activités de l'application + }); + + // Listener bouton Fermer + closeButton.setOnClickListener(v -> dialog.dismiss()); + + dialog.show(); + } + + /** + * Crée et lance une Intent pour partager les statistiques du joueur. + */ + private void shareStats() { + if (gameStats == null) return; + + // Construit le message à partager + String shareBody = getString(R.string.share_stats_body, + gameStats.getOverallHighScore(), + gameStats.getHighestTile(), + gameStats.getNumberOfTimesObjectiveReached(), + gameStats.getTotalGamesStarted(), // Ou totalGamesPlayed ? + GameStats.formatTime(gameStats.getTotalPlayTimeMs()), + gameStats.getTotalMoves() + ); + + Intent shareIntent = new Intent(Intent.ACTION_SEND); + shareIntent.setType("text/plain"); + shareIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.share_stats_subject)); + shareIntent.putExtra(Intent.EXTRA_TEXT, shareBody); + + try { + startActivity(Intent.createChooser(shareIntent, getString(R.string.share_stats_title))); + } catch (ActivityNotFoundException e) { + Toast.makeText(this, "Aucune application de partage disponible.", Toast.LENGTH_SHORT).show(); + } + } + + /** + * Affiche une boîte de dialogue pour confirmer la réinitialisation des statistiques. + */ + private void showResetStatsConfirmationDialog() { new AlertDialog.Builder(this) - .setTitle(R.string.settings_title) - .setMessage(R.string.settings_message) // Message placeholder - .setPositiveButton(R.string.ok, (dialog, which) -> dialog.dismiss()) + .setTitle(R.string.reset_stats_confirm_title) + .setMessage(R.string.reset_stats_confirm_message) + .setPositiveButton(R.string.confirm, (dialog, which) -> { + resetStatistics(); // Appelle la méthode de réinitialisation + dialog.dismiss(); + }) + .setNegativeButton(R.string.cancel, (dialog, which) -> dialog.dismiss()) + .setIcon(android.R.drawable.ic_dialog_alert) // Icône d'avertissement .show(); } + /** + * Réinitialise toutes les statistiques via GameStats et sauvegarde les changements. + * Affiche une confirmation à l'utilisateur. + */ + private void resetStatistics() { + if (gameStats != null) { + gameStats.resetStats(); // Réinitialise les stats dans l'objet + gameStats.saveStats(); // Sauvegarde les stats réinitialisées + // Met aussi à jour le highScore de l'objet Game courant (si une partie est en cours) + if(game != null){ + game.setHighestScore(gameStats.getOverallHighScore()); // Le HS est aussi reset dans GameStats + updateScores(); // Rafraichit l'affichage du HS si visible + } + Toast.makeText(this, R.string.stats_reset_confirmation, Toast.LENGTH_SHORT).show(); + // Si les stats étaient visibles, on pourrait vouloir les rafraîchir + if (statisticsVisible && inflatedStatsView != null) { + updateStatisticsTextViews(); + } + } + } + /** * Affiche la boîte de dialogue "À Propos" en utilisant un layout personnalisé, * incluant des informations sur l'application et un lien cliquable vers le site web. diff --git a/app/src/main/res/layout/dialog_settings.xml b/app/src/main/res/layout/dialog_settings.xml new file mode 100644 index 0000000..5eb4930 --- /dev/null +++ b/app/src/main/res/layout/dialog_settings.xml @@ -0,0 +1,89 @@ + + + + + + + + + +