Refactor "Disconnect" and "Track" logic

This commit is contained in:
Rui Oliveira committed 2026-07-24 20:08:23 +01:00
1 parent d700432829
commit f9b1a97a37
3 files changed
+26 -21

No files matched your search

@@ -84,19 +84,21 @@ class Ic705Controller(
} }
} }
override suspend fun disconnect() = withContext(Dispatchers.IO) { override suspend fun disconnect() {
try { withContext(Dispatchers.IO) {
inputStream?.close() try {
outputStream?.close() inputStream?.close()
socket?.close() outputStream?.close()
} catch (e: Exception) { socket?.close()
Log.e(tag, "Disconnect error: ${e.message}") } catch (e: Exception) {
} finally { Log.e(tag, "Disconnect error: ${e.message}")
inputStream = null } finally {
outputStream = null inputStream = null
socket = null outputStream = null
isConnected = false socket = null
Log.i(tag, "Disconnected from $deviceAddress") isConnected = false
Log.i(tag, "Disconnected from $deviceAddress")
}
} }
} }
@@ -31,6 +31,7 @@ import com.rtbishop.look4sat.core.domain.repository.RadioTrackingState
import com.rtbishop.look4sat.core.domain.utility.TransponderMapper import com.rtbishop.look4sat.core.domain.utility.TransponderMapper
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
@@ -189,7 +190,7 @@ class RadioTrackingService(
var lastReadFreq = 0L var lastReadFreq = 0L
var stableCount = 0 var stableCount = 0
while (isActive) { while (currentCoroutineContext().isActive) {
val currentState = _state.value val currentState = _state.value
if (!currentState.isActive) break if (!currentState.isActive) break
@@ -360,7 +361,7 @@ class RadioTrackingService(
// After initial setup we never change VFO or band. // After initial setup we never change VFO or band.
// We only update the active-VFO frequency using CMD 0x25 sub 0x00. // We only update the active-VFO frequency using CMD 0x25 sub 0x00.
// The IC-705 automatically makes VFO-B active while PTT is pressed. // The IC-705 automatically makes VFO-B active while PTT is pressed.
while (isActive) { while (currentCoroutineContext().isActive) {
val currentState = _state.value val currentState = _state.value
if (!currentState.isActive) break if (!currentState.isActive) break
@@ -473,12 +473,14 @@ fun RadioControlDialog(
// If current baud rate is not in the new list, default to the first available // If current baud rate is not in the new list, default to the first available
if (baudRate.intValue !in baudRates) baudRate.intValue = baudRates.first() if (baudRate.intValue !in baudRates) baudRate.intValue = baudRates.first()
val pairedDevices = remember { val pairedDevices: List<Pair<String, String>> = remember {
try { buildList {
val manager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager try {
manager.adapter?.bondedDevices?.map { it.name ?: "Unknown" to it.address } ?: emptyList() val manager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
} catch (_: SecurityException) { manager.adapter?.bondedDevices?.forEach {
emptyList() add(Pair(it.name ?: "Unknown", it.address ?: ""))
}
} catch (_: SecurityException) { }
} }
} }