Skip to content

Commit 8ce7360

Browse files
Added "back" and "home" buttons, and warnings to onBackPressed()
1 parent 1fc05d8 commit 8ce7360

4 files changed

Lines changed: 119 additions & 5 deletions

File tree

app/src/main/java/com/anachronistic/daniel/psakse/GameViewController.kt

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.anachronistic.daniel.psakse
22

33
import android.content.Context
4+
import android.content.Intent
45
import android.graphics.Color
56
import android.graphics.drawable.GradientDrawable
67
import android.os.Bundle
@@ -29,6 +30,7 @@ class GameViewController: AppCompatActivity() {
2930

3031
private var mainGrid: FrameLayout? = null
3132
private var subGrid: FrameLayout? = null
33+
private var backView: Button? = null
3234
private var newView: Button? = null
3335

3436
override fun onCreate(savedInstanceState: Bundle?) {
@@ -42,6 +44,7 @@ class GameViewController: AppCompatActivity() {
4244
window.decorView.findViewById<View>(R.id.root).doOnLayout {
4345
mainGrid = findViewById(R.id.mainGrid)
4446
subGrid = findViewById(R.id.subGrid)
47+
backView = findViewById(R.id.backView)
4548
newView = findViewById(R.id.newView)
4649
resetGame(it.context)
4750
}
@@ -50,7 +53,7 @@ class GameViewController: AppCompatActivity() {
5053
private fun setupButtonView(button: Button, title: String, color: Colors, action: View.OnClickListener) {
5154
button.text = title
5255
val layer = GradientDrawable()
53-
layer.cornerRadius = 90.0f
56+
layer.cornerRadius = 40.0f
5457
layer.setStroke(9, Color.DKGRAY)
5558
layer.setColor(ContextCompat.getColor(this, color.getColor()))
5659
button.background = layer
@@ -73,9 +76,11 @@ class GameViewController: AppCompatActivity() {
7376

7477
// Create in game controls
7578
if (puzzleID != null) {
79+
setupButtonView(backView!!, "Back", Colors.Orange, goToSelect())
7680
setupButtonView(newView!!, "Reset", Colors.Purple, newGame())
7781
} else {
78-
setupButtonView(newView!!, "New Game", Colors.Purple, newGame())
82+
setupButtonView(backView!!, "Home", Colors.Orange, goToHome())
83+
setupButtonView(newView!!, "New \nGame", Colors.Purple, newGame())
7984
}
8085
}
8186

@@ -320,6 +325,29 @@ class GameViewController: AppCompatActivity() {
320325
return true
321326
}
322327

328+
override fun onBackPressed() {
329+
if (gameComplete) {
330+
if (puzzleID != null) {
331+
startActivity(Intent(this, SelectViewController::class.java))
332+
} else {
333+
startActivity(Intent(this, HomeViewController::class.java))
334+
}
335+
} else {
336+
val alert = AlertDialog.Builder(this)
337+
alert.setTitle("Puzzle not finished!")
338+
alert.setMessage("Are you sure you want to quit? All progress on this puzzle will be lost.")
339+
alert.setPositiveButton("Yes") { _, _ ->
340+
if (puzzleID != null) {
341+
startActivity(Intent(this, SelectViewController::class.java))
342+
} else {
343+
startActivity(Intent(this, HomeViewController::class.java))
344+
}
345+
}
346+
alert.setNegativeButton("No", null)
347+
alert.show()
348+
}
349+
}
350+
323351
private fun newGame(): View.OnClickListener {
324352
return View.OnClickListener {
325353
if (gameComplete) {
@@ -334,4 +362,38 @@ class GameViewController: AppCompatActivity() {
334362
}
335363
}
336364
}
365+
366+
private fun goToHome(): View.OnClickListener {
367+
return View.OnClickListener {
368+
if (gameComplete) {
369+
startActivity(Intent(this, HomeViewController::class.java))
370+
} else {
371+
val alert = AlertDialog.Builder(this)
372+
alert.setTitle("Puzzle not finished!")
373+
alert.setMessage("Are you sure you want to quit? All progress on this puzzle will be lost.")
374+
alert.setPositiveButton("Yes") { _, _ ->
375+
startActivity(Intent(this, HomeViewController::class.java))
376+
}
377+
alert.setNegativeButton("No", null)
378+
alert.show()
379+
}
380+
}
381+
}
382+
383+
private fun goToSelect(): View.OnClickListener {
384+
return View.OnClickListener {
385+
if (gameComplete) {
386+
startActivity(Intent(this, SelectViewController::class.java))
387+
} else {
388+
val alert = AlertDialog.Builder(this)
389+
alert.setTitle("Puzzle not finished!")
390+
alert.setMessage("Are you sure you want to quit? All progress on this puzzle will be lost.")
391+
alert.setPositiveButton("Yes") { _, _ ->
392+
startActivity(Intent(this, SelectViewController::class.java))
393+
}
394+
alert.setNegativeButton("No", null)
395+
alert.show()
396+
}
397+
}
398+
}
337399
}

app/src/main/java/com/anachronistic/daniel/psakse/HomeViewController.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,8 @@ class HomeViewController : AppCompatActivity() {
6060
setupButtonView(randomView!!, "Random Puzzle", Colors.Purple, goToGame())
6161
}
6262
}
63+
64+
override fun onBackPressed() {
65+
return
66+
}
6367
}

app/src/main/java/com/anachronistic/daniel/psakse/SelectViewController.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,8 @@ class SelectViewController : AppCompatActivity() {
104104
getJSON()
105105
}
106106
}
107+
108+
override fun onBackPressed() {
109+
startActivity(Intent(this, HomeViewController::class.java))
110+
}
107111
}

app/src/main/res/layout/activity_game_view_controller.xml

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,59 @@
3838

3939
</FrameLayout>
4040

41-
<Button
42-
android:id="@+id/newView"
41+
<androidx.constraintlayout.widget.ConstraintLayout
42+
android:id="@+id/buttonContainer"
4343
android:layout_width="0dp"
4444
android:layout_height="wrap_content"
4545
android:layout_marginStart="20dp"
4646
android:layout_marginTop="35dp"
4747
android:layout_marginEnd="20dp"
4848
app:layout_constraintEnd_toEndOf="parent"
4949
app:layout_constraintStart_toStartOf="parent"
50-
app:layout_constraintTop_toBottomOf="@+id/subGrid" />
50+
app:layout_constraintTop_toBottomOf="@+id/subGrid">
51+
52+
<androidx.constraintlayout.widget.ConstraintLayout
53+
android:id="@+id/LButton"
54+
android:layout_width="0dp"
55+
android:layout_height="wrap_content"
56+
app:layout_constraintBottom_toBottomOf="parent"
57+
app:layout_constraintEnd_toStartOf="@+id/RButton"
58+
app:layout_constraintHorizontal_bias="0.5"
59+
app:layout_constraintStart_toStartOf="parent"
60+
app:layout_constraintTop_toTopOf="parent">
61+
62+
<Button
63+
android:id="@+id/backView"
64+
android:layout_width="wrap_content"
65+
android:layout_height="0dp"
66+
android:layout_marginEnd="15dp"
67+
app:layout_constraintBottom_toBottomOf="parent"
68+
app:layout_constraintDimensionRatio="h,1:1"
69+
app:layout_constraintEnd_toEndOf="parent"
70+
app:layout_constraintTop_toTopOf="parent" />
71+
</androidx.constraintlayout.widget.ConstraintLayout>
72+
73+
<androidx.constraintlayout.widget.ConstraintLayout
74+
android:id="@+id/RButton"
75+
android:layout_width="0dp"
76+
android:layout_height="wrap_content"
77+
app:layout_constraintBottom_toBottomOf="parent"
78+
app:layout_constraintEnd_toEndOf="parent"
79+
app:layout_constraintHorizontal_bias="0.5"
80+
app:layout_constraintStart_toEndOf="@+id/LButton"
81+
app:layout_constraintTop_toTopOf="parent">
82+
83+
<Button
84+
android:id="@+id/newView"
85+
android:layout_width="wrap_content"
86+
android:layout_height="0dp"
87+
android:layout_marginStart="15dp"
88+
app:layout_constraintBottom_toBottomOf="parent"
89+
app:layout_constraintDimensionRatio="h,1:1"
90+
app:layout_constraintStart_toStartOf="parent"
91+
app:layout_constraintTop_toTopOf="parent" />
92+
</androidx.constraintlayout.widget.ConstraintLayout>
93+
94+
</androidx.constraintlayout.widget.ConstraintLayout>
5195

5296
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)