@@ -63,7 +63,7 @@ import java.io.Closeable
63
63
import java.util.*
64
64
import java.util.concurrent.atomic.AtomicReference
65
65
66
- fun log (text : String ) = if (BuildConfig .DEBUG ) Log .e(" STTT" ,text) else 0
66
+ fun log (text : String ) = if (BuildConfig .DEBUG ) Log .e(" STTT" , text) else 0
67
67
68
68
@SuppressLint(" ShowToast" )
69
69
class MainActivity : AppCompatActivity (), NavigationView.OnNavigationItemSelectedListener {
@@ -75,7 +75,8 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
75
75
private var remoteServiceBound = false
76
76
private var remoteServiceStarted = false
77
77
private var remoteService: RemoteService ? = null
78
- val remote get() = remoteService?.remoteGame()
78
+ private val remote get() = remoteService?.remoteGame()
79
+ private val remoteConnected get() = remote?.state == RemoteState .CONNECTED
79
80
80
81
// Bluetooth fields
81
82
private var btAdapter = BluetoothAdapter .getDefaultAdapter()
@@ -170,7 +171,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
170
171
171
172
private var killService = false
172
173
override fun onSaveInstanceState (outState : Bundle ) {
173
- killService = ! isChangingConfigurations && remote?.state != RemoteState . CONNECTED
174
+ killService = ! isChangingConfigurations && ! remoteConnected
174
175
outState.putBoolean(BTSERVICE_STARTED_KEY , remoteServiceStarted && ! killService)
175
176
outState.putBoolean(KEEP_BT_ON_KEY , keepBtOn)
176
177
outState.putSerializable(GAMESTATE_KEY , gs)
@@ -181,7 +182,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
181
182
isInBackground = true
182
183
183
184
// Notification telling the user that BtService is still open
184
- if (! killService && remote?.state == RemoteState . CONNECTED ) remoteRunningNotification()
185
+ if (! killService && remoteConnected ) remoteRunningNotification()
185
186
186
187
// Unbind remoteService and stop if needed
187
188
unbindRemoteService(killService)
@@ -262,12 +263,15 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
262
263
}
263
264
264
265
private val remoteReceiver = object : BroadcastReceiver () {
265
- override fun onReceive (context : Context , intent : Intent ) = when (intent.action) {
266
+ override fun onReceive (context : Context , intent : Intent ): Unit = when (intent.action) {
266
267
INTENT_MOVE -> gameThread.play(Source .REMOTE , intent.getSerializableExtra(INTENT_DATA ) as Byte )
267
- INTENT_NEWGAME -> newGame(intent.getSerializableExtra(INTENT_DATA ) as GameState )
268
268
INTENT_UNDO -> undo(intent.getBooleanExtra(INTENT_DATA , false ))
269
269
INTENT_TOAST -> toast(intent.getStringExtra(INTENT_DATA ))
270
270
INTENT_TURNLOCAL -> turnLocal()
271
+ INTENT_NEWGAME -> {
272
+ btDialog?.apply { setOnDismissListener { };dismiss() }
273
+ newGame(intent.getSerializableExtra(INTENT_DATA ) as GameState )
274
+ }
271
275
else -> throw IllegalStateException (intent.action)
272
276
}
273
277
}
@@ -281,7 +285,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
281
285
282
286
if (isInBackground) unbindRemoteService(killService)
283
287
else if (gs.type == Source .REMOTE ) {
284
- if (remote?.state == RemoteState . CONNECTED ) {
288
+ if (remoteConnected ) {
285
289
// Fetch latest board
286
290
val newBoard = remote!! .lastBoard
287
291
if (newBoard != gs.board()) newBoard.lastMove()?.let { gameThread.play(Source .REMOTE , it) }
@@ -329,7 +333,6 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
329
333
})
330
334
331
335
if (gs.type == Source .REMOTE ) {
332
- btDialog?.dismiss()
333
336
val remoteName = remote?.remoteName
334
337
if (remoteName != null ) setSubTitle(getString(R .string.connected_to, remoteName))
335
338
else setSubTitle(getString(R .string.connected))
@@ -439,29 +442,29 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
439
442
override fun onOptionsItemSelected (item : MenuItem ): Boolean {
440
443
if (item.itemId != R .id.action_undo) return false
441
444
442
- if (gs.boards.size == 1 ) {
445
+ if (gs.boards.size == 1 || (gs.boards.size == 2 && gs.otherSource() == Source . AI ) ) {
443
446
toast(getString(R .string.no_prev_moves))
444
447
return true
445
448
}
446
449
447
- if (gs.type == Source .REMOTE && remote?.state == RemoteState . CONNECTED )
450
+ if (gs.type == Source .REMOTE && remoteConnected )
448
451
remote?.sendUndo(false )
449
- else undo(false )
452
+ else if (gs.otherSource() == Source .AI )
453
+ repeat(2 ) { undo(true ) }
454
+ else undo(true )
450
455
451
456
return true
452
457
}
453
458
454
459
fun undo (force : Boolean ) {
455
- if (! force && remote?.state == RemoteState . CONNECTED && gs.type == Source .REMOTE ) {
460
+ if (! force && remoteConnected && gs.type == Source .REMOTE ) {
456
461
askUser(getString(R .string.undo_request, remote?.remoteName), { allow ->
457
462
if (allow) {
458
463
undo(true )
459
464
remote?.sendUndo(true )
460
465
}
461
466
})
462
- } else newGame(GameState .Builder ().gs(gs).build().apply {
463
- repeat(if (Source .AI == gs.otherSource() && boards.size > 1 ) 2 else 1 ) { popBoard() }
464
- })
467
+ } else newGame(GameState .Builder ().gs(gs).build().apply { popBoard() })
465
468
}
466
469
467
470
override fun onNavigationItemSelected (item : MenuItem ): Boolean {
@@ -519,7 +522,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
519
522
.setView(layout)
520
523
.setCustomTitle(newTitle(getString(R .string.host_bluetooth_game)))
521
524
.setNegativeButton(getString(R .string.close)) { _, _ -> btDialog?.dismiss() }
522
- .setOnDismissListener({ remote?.close() })
525
+ .setOnDismissListener({ remote?.close() }) // TODO fix, onconnect dialog gets dismissed lmao
523
526
.show())
524
527
} else {
525
528
val discoverableIntent = Intent (BluetoothAdapter .ACTION_REQUEST_DISCOVERABLE )
0 commit comments