Skip to content

Commit bb46b2c

Browse files
committed
Fixed rotating bt game with empty board and bt still running notification now properly closes
1 parent cb869a4 commit bb46b2c

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

app/src/main/java/com/henrykvdb/sttt/MainActivity.kt

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -273,13 +273,11 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
273273
if (btService!!.getState() === BtService.State.CONNECTED) {
274274
//Fetch latest board
275275
val newBoard = btService!!.getLocalBoard()
276-
if (newBoard !== gs!!.board())
277-
play(Source.Bluetooth, newBoard.lastMove()!!)
276+
if (newBoard !== gs!!.board()) newBoard.lastMove()?.let { play(Source.Bluetooth, it) }
278277

279278
//Update subtitle
280279
setSubTitle(getString(R.string.connected_to, btService!!.getConnectedDeviceName()))
281-
} else
282-
turnLocal()
280+
} else turnLocal()
283281
}
284282
}
285283

@@ -337,16 +335,19 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
337335
}
338336

339337
private fun turnLocal() {
340-
if (!gs!!.isAi() && !gs!!.isHuman())
338+
if (!gs!!.isAi() && !gs!!.isHuman()) {
341339
newGame(GameState.Builder().boards(gs!!.boards).build())
340+
NotificationManagerCompat.from(this@MainActivity).cancel(BT_STILL_RUNNING)
341+
}
342342
}
343343

344344
private fun newLocal() {
345345
newGame(GameState.Builder().swapped(false).build())
346346
}
347347

348348
private inner class GameThread : Thread(), Closeable {
349-
@Volatile private var running: Boolean = false
349+
@Volatile
350+
private var running: Boolean = false
350351
private var timer: Timer? = null
351352

352353
override fun run() {
@@ -506,11 +507,11 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
506507
if (t) newLocal()
507508
}
508509
}, this)
509-
R.id.nav_local_ai -> newAi(object :Callback<GameState>{
510+
R.id.nav_local_ai -> newAi(object : Callback<GameState> {
510511
override fun invoke(t: GameState) {
511512
newGame(t)
512513
}
513-
},this)
514+
}, this)
514515
R.id.nav_bt_host -> hostBt()
515516
R.id.nav_bt_join -> joinBt()
516517
R.id.nav_other_feedback -> feedbackSender(this)
@@ -537,7 +538,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
537538
val layout = View.inflate(this, R.layout.dialog_bt_host, null)
538539
(layout.findViewById<View>(R.id.bt_host_desc) as TextView).text = getString(R.string.host_desc, btService!!.getLocalBluetoothName())
539540

540-
val onCheckedChangeListener = RadioGroup.OnCheckedChangeListener{ _, _ ->
541+
val onCheckedChangeListener = RadioGroup.OnCheckedChangeListener { _, _ ->
541542
//Get board type
542543
val newBoard = (layout.findViewById<View>(R.id.board_new) as RadioButton).isChecked
543544

@@ -591,7 +592,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
591592
if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION))
592593
ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.ACCESS_COARSE_LOCATION), REQUEST_COARSE_LOCATION)
593594
else
594-
btDialog = BtPicker(this, btAdapter!!, object :Callback<String>{
595+
btDialog = BtPicker(this, btAdapter!!, object : Callback<String> {
595596
override fun invoke(t: String) {
596597
btService?.connect(t)
597598
}
@@ -618,7 +619,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
618619
if (askDialog != null && askDialog!!.isShowing)
619620
askDialog!!.dismiss()
620621

621-
val dialogClickListener = DialogInterface.OnClickListener{ _, which ->
622+
val dialogClickListener = DialogInterface.OnClickListener { _, which ->
622623
if (which == DialogInterface.BUTTON_POSITIVE)
623624
callBack.invoke(true)
624625
else if (which == DialogInterface.BUTTON_NEGATIVE)

0 commit comments

Comments
 (0)