refactor: modernize codebase - Use KTX extension functions for system services - Update performClick handling in touch events - Modernize back gesture handling with KTX - Improve vibrator service initialization

This commit is contained in:
cmclark00 2025-03-28 20:21:25 -04:00
parent ebff618fa4
commit af0082a6db
3 changed files with 37 additions and 41 deletions

View file

@ -688,29 +688,27 @@ class MainActivity : AppCompatActivity() {
// Add an on back pressed callback to handle back button/gesture // Add an on back pressed callback to handle back button/gesture
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
onBackPressedDispatcher.addCallback(this, object : androidx.activity.OnBackPressedCallback(true) { onBackPressedDispatcher.addCallback(this) {
override fun handleOnBackPressed() { // If we're playing the game, handle it as a pause action instead of exiting
// If we're playing the game, handle it as a pause action instead of exiting if (gameView.visibility == View.VISIBLE && !gameView.isPaused && !gameView.isGameOver()) {
if (gameView.visibility == View.VISIBLE && !gameView.isPaused && !gameView.isGameOver()) { gameView.pause()
gameView.pause() gameMusic.pause()
gameMusic.pause() showPauseMenu()
showPauseMenu() binding.pauseStartButton.visibility = View.GONE
binding.pauseStartButton.visibility = View.GONE binding.resumeButton.visibility = View.VISIBLE
binding.resumeButton.visibility = View.VISIBLE } else if (binding.pauseContainer.visibility == View.VISIBLE) {
} else if (binding.pauseContainer.visibility == View.VISIBLE) { // If pause menu is showing, handle as a resume
// If pause menu is showing, handle as a resume resumeGame()
resumeGame() } else if (binding.gameOverContainer.visibility == View.VISIBLE) {
} else if (binding.gameOverContainer.visibility == View.VISIBLE) { // If game over is showing, go back to title
// If game over is showing, go back to title hideGameOver()
hideGameOver() showTitleScreen()
showTitleScreen() } else if (titleScreen.visibility == View.VISIBLE) {
} else if (titleScreen.visibility == View.VISIBLE) { // If title screen is showing, allow normal back behavior (exit app)
// If title screen is showing, allow normal back behavior (exit app) isEnabled = false
isEnabled = false onBackPressedDispatcher.onBackPressed()
onBackPressedDispatcher.onBackPressed()
}
} }
}) }
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// For Android 11 (R) to Android 12 (S), use the WindowInsetsController to disable gestures // For Android 11 (R) to Android 12 (S), use the WindowInsetsController to disable gestures
window.insetsController?.systemBarsBehavior = window.insetsController?.systemBarsBehavior =

View file

@ -5,26 +5,27 @@ import android.os.Build
import android.os.VibrationEffect import android.os.VibrationEffect
import android.os.Vibrator import android.os.Vibrator
import android.os.VibratorManager import android.os.VibratorManager
import android.util.Log
import android.view.HapticFeedbackConstants import android.view.HapticFeedbackConstants
import android.view.View import android.view.View
import android.util.Log import androidx.core.content.getSystemService
/** /**
* Handles haptic feedback for game events * Handles haptic feedback for game events
*/ */
class GameHaptics(private val context: Context) { class GameHaptics(private val context: Context) {
companion object { private val TAG = "GameHaptics"
private const val TAG = "GameHaptics"
}
// Vibrator service // Vibrator service
private val vibrator: Vibrator = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { private val vibrator: Vibrator by lazy {
val vibratorManager = context.getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
vibratorManager.defaultVibrator val vibratorManager = context.getSystemService<VibratorManager>()
} else { vibratorManager?.defaultVibrator ?: throw IllegalStateException("No vibrator available")
@Suppress("DEPRECATION") } else {
context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator @Suppress("DEPRECATION")
context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
}
} }
// Vibrate for line clear (more intense for more lines) // Vibrate for line clear (more intense for more lines)

View file

@ -820,6 +820,7 @@ class GameView @JvmOverloads constructor(
} }
} }
lastTapTime = currentTime lastTapTime = currentTime
return true
} }
MotionEvent.ACTION_MOVE -> { MotionEvent.ACTION_MOVE -> {
@ -877,6 +878,7 @@ class GameView @JvmOverloads constructor(
// No direction lock yet, don't process movement // No direction lock yet, don't process movement
} }
} }
return true
} }
MotionEvent.ACTION_UP -> { MotionEvent.ACTION_UP -> {
@ -909,21 +911,16 @@ class GameView @JvmOverloads constructor(
} else if (moveTime < minTapTime && } else if (moveTime < minTapTime &&
abs(deltaY) < maxTapMovement && abs(deltaY) < maxTapMovement &&
abs(deltaX) < maxTapMovement) { abs(deltaX) < maxTapMovement) {
// Quick tap with minimal movement (rotation) // This was a tap, perform click
if (currentTime - lastRotationTime >= rotationCooldown) { performClick()
Log.d("GameView", "Rotation detected")
gameBoard.rotate()
lastRotationTime = currentTime
invalidate()
}
} }
// Reset direction lock // Reset direction lock
lockedDirection = null lockedDirection = null
return true
} }
} }
return super.onTouchEvent(event)
return true
} }
/** /**