Skip to content

Commit

Permalink
[feat] adds preference. readds support for S-Pen.
Browse files Browse the repository at this point in the history
  • Loading branch information
coderPaddyS committed Dec 20, 2024
1 parent a2afcd6 commit 37c425c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class PFApplicationData private constructor(context: Context) {
private set
lateinit var prefColorScheme: Preferable<String>
private set
lateinit var prefDisplayLock: Preferable<Boolean>
private set

private val preferences = appPreferences(context) {
preferences {
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,26 @@ 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
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.
Expand All @@ -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(
Expand All @@ -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)
Expand All @@ -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)
}

Expand All @@ -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()

Expand All @@ -137,7 +122,7 @@ class GameActivity: BaseActivityWithoutNavBar() {
grid.adapter = adapter
grid.setHasFixedSize(true)

if (!animationActivated) {
if (!PFApplicationData.instance(this).animationActivated.value) {
grid.itemAnimator = null
}

Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 37c425c

Please sign in to comment.