11package com.anachronistic.daniel.psakse
22
33import android.content.Context
4+ import android.content.Intent
45import android.graphics.Color
56import android.graphics.drawable.GradientDrawable
67import 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 \n Game" , 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}
0 commit comments