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,8 +688,7 @@ 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()
@ -710,7 +709,6 @@ class MainActivity : AppCompatActivity() {
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,27 +5,28 @@ 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>()
vibratorManager?.defaultVibrator ?: throw IllegalStateException("No vibrator available")
} else { } else {
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator 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)
fun vibrateForLineClear(lineCount: Int) { fun vibrateForLineClear(lineCount: Int) {

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,22 +911,17 @@ 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 true
} }
}
return super.onTouchEvent(event)
}
/** /**
* Get the current score * Get the current score