diff --git a/app/src/main/java/org/secuso/privacyfriendly2048/PFApplicationData.kt b/app/src/main/java/org/secuso/privacyfriendly2048/PFApplicationData.kt index a75c1e8..9391433 100644 --- a/app/src/main/java/org/secuso/privacyfriendly2048/PFApplicationData.kt +++ b/app/src/main/java/org/secuso/privacyfriendly2048/PFApplicationData.kt @@ -29,6 +29,8 @@ class PFApplicationData private constructor(context: Context) { private set lateinit var prefColorScheme: Preferable private set + lateinit var prefDisplayLock: Preferable + private set private val preferences = appPreferences(context) { preferences { @@ -60,6 +62,14 @@ class PFApplicationData private constructor(context: Context) { title { resource(R.string.settings_color) } summary { transform { state, value -> state.entries.find { it.value == value }!!.entry } } } + prefDisplayLock = switch { + key = "settings_display" + title { resource(R.string.settings_display) } + summary { literal("") } + default = true + backup = true + } + } category(ContextCompat.getString(context, R.string.settings_category_report)) { includeDeviceDataInReport = DeviceInformationOnErrorReport().build().invoke(this) diff --git a/app/src/main/java/org/secuso/privacyfriendly2048/activities/GameActivity.kt b/app/src/main/java/org/secuso/privacyfriendly2048/activities/GameActivity.kt index 61c710d..5738cce 100644 --- a/app/src/main/java/org/secuso/privacyfriendly2048/activities/GameActivity.kt +++ b/app/src/main/java/org/secuso/privacyfriendly2048/activities/GameActivity.kt @@ -21,22 +21,19 @@ import android.annotation.SuppressLint import android.app.AlertDialog import android.content.Intent import android.os.Bundle -import android.preference.PreferenceManager import android.util.Log +import android.view.KeyEvent import android.view.MotionEvent import android.view.View import android.view.WindowManager -import android.widget.GridView import android.widget.ImageButton import android.widget.TextView import androidx.activity.viewModels import androidx.recyclerview.widget.GridLayoutManager -import androidx.recyclerview.widget.RecyclerView -import androidx.recyclerview.widget.SimpleItemAnimator +import org.secuso.privacyfriendly2048.PFApplicationData import org.secuso.privacyfriendly2048.R import org.secuso.privacyfriendly2048.activities.adapter.Grid2048Adapter import org.secuso.privacyfriendly2048.activities.adapter.Grid2048BackgroundAdapter -import org.secuso.privacyfriendly2048.activities.helper.BaseActivityWithoutNavBar import org.secuso.privacyfriendly2048.activities.helper.Gestures import org.secuso.privacyfriendly2048.activities.helper.GridRecyclerView import org.secuso.privacyfriendly2048.activities.viewmodel.GameViewModel @@ -44,8 +41,6 @@ import org.secuso.privacyfriendly2048.model.Direction import org.secuso.privacyfriendly2048.model.Game2048 import org.secuso.privacyfriendly2048.model.GameBoard import org.secuso.privacyfriendly2048.model.GameState -import kotlin.math.abs -import kotlin.math.max /** * This activity contains the entire game and draws the game field depending on the selected mode and the screen size. @@ -63,7 +58,7 @@ import kotlin.math.max * @author Patrick Schneider * @version 20241107 */ -class GameActivity: BaseActivityWithoutNavBar() { +class GameActivity: org.secuso.pfacore.ui.activities.BaseActivity() { val viewModel: GameViewModel by viewModels { GameViewModel.GameViewModelFactory( filesDir, Game2048.GameConfig( @@ -85,9 +80,6 @@ class GameActivity: BaseActivityWithoutNavBar() { var gameWon = false - val sharedPreferences by lazy { PreferenceManager.getDefaultSharedPreferences(this) } - val animationActivated by lazy { sharedPreferences.getBoolean("pref_animationActivated", true) } - val gestureListener by lazy { object : Gestures(this@GameActivity) { override fun onSwipeTop() = move(Direction.UP) @@ -108,7 +100,7 @@ class GameActivity: BaseActivityWithoutNavBar() { setContentView(R.layout.activity_game); super.onCreate(savedInstanceState); - if (sharedPreferences.getBoolean("settings_display", true)) { + if (PFApplicationData.instance(this).prefDisplayLock.value) { window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) } @@ -118,14 +110,7 @@ class GameActivity: BaseActivityWithoutNavBar() { viewModel.stop() viewModel.reset() } - undoButton.setOnClickListener { - Log.d("GameActivity", "undo pressed") - viewModel.undo() - adapter.updateGrid(viewModel.board(), listOf()) - @SuppressLint("NotifyDataSetChanged") - adapter.notifyDataSetChanged() - undoButton.visibility = if (viewModel.canUndo()) { View.VISIBLE } else { View.INVISIBLE } - } + undoButton.setOnClickListener { undo() } undoButton.visibility = if (viewModel.canUndo()) { View.VISIBLE } else { View.INVISIBLE } viewModel.start() @@ -137,7 +122,7 @@ class GameActivity: BaseActivityWithoutNavBar() { grid.adapter = adapter grid.setHasFixedSize(true) - if (!animationActivated) { + if (!PFApplicationData.instance(this).animationActivated.value) { grid.itemAnimator = null } @@ -149,6 +134,15 @@ class GameActivity: BaseActivityWithoutNavBar() { gridBackground.setHasFixedSize(true) } + private fun undo() { + Log.d("GameActivity", "undo pressed") + viewModel.undo() + adapter.updateGrid(viewModel.board(), listOf()) + @SuppressLint("NotifyDataSetChanged") + adapter.notifyDataSetChanged() + undoButton.visibility = if (viewModel.canUndo()) { View.VISIBLE } else { View.INVISIBLE } + } + private fun move(direction: Direction): Boolean { adapter.updateGrid(viewModel.board(), viewModel.move(direction)) textPoints.text = viewModel.points.toString() @@ -186,6 +180,32 @@ class GameActivity: BaseActivityWithoutNavBar() { return false } + // This handles actions via a S-Pen + override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean { + when (keyCode) { + KeyEvent.KEYCODE_DPAD_UP -> { + move(Direction.UP) + return true + } + KeyEvent.KEYCODE_DPAD_DOWN -> { + move(Direction.DOWN) + return true + } + KeyEvent.KEYCODE_DPAD_LEFT -> { + move(Direction.LEFT) + return true + } + KeyEvent.KEYCODE_DPAD_RIGHT -> { + move(Direction.RIGHT) + return true + } + KeyEvent.KEYCODE_Z -> if (event?.isCtrlPressed == true) { + undo() + } + } + return super.onKeyDown(keyCode, event) + } + override fun onPause() { viewModel.save() super.onPause()