Properly state handling in profiles

This commit is contained in:
Sylwester Zielinski
2023-04-24 16:54:47 +02:00
parent 878d36aff4
commit a2e86a819c
19 changed files with 81 additions and 24 deletions

View File

@@ -77,7 +77,9 @@ fun BPSScreen() {
.padding(16.dp) .padding(16.dp)
.verticalScroll(rememberScrollState()) .verticalScroll(rememberScrollState())
) { ) {
when (state.result.connectionState?.state) { if (state.deviceName == null) {
DeviceConnectingView { NavigateUpButton(navigateUp) }
} else when (state.result.connectionState?.state) {
null, null,
GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) } GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) }
GattConnectionState.STATE_DISCONNECTED, GattConnectionState.STATE_DISCONNECTED,

View File

@@ -130,6 +130,11 @@ internal class BPSViewModel @Inject constructor(
.onEach { logAnalytics(it.state) } .onEach { logAnalytics(it.state) }
.launchIn(viewModelScope) .launchIn(viewModelScope)
if (!client.isConnected) {
_state.value = _state.value.copy(deviceName = device.name)
return@launch
}
client.discoverServices() client.discoverServices()
.filterNotNull() .filterNotNull()
.onEach { configureGatt(it) } .onEach { configureGatt(it) }

View File

@@ -75,7 +75,6 @@ class CGMRepository @Inject constructor(
val highestSequenceNumber = data.value.records.maxOfOrNull { it.sequenceNumber } ?: -1 val highestSequenceNumber = data.value.records.maxOfOrNull { it.sequenceNumber } ?: -1
fun launch(device: ServerDevice) { fun launch(device: ServerDevice) {
_data.value = _data.value.copy(deviceName = device.name)
serviceManager.startService(CGMService::class.java, device) serviceManager.startService(CGMService::class.java, device)
} }
@@ -88,7 +87,6 @@ class CGMRepository @Inject constructor(
} }
fun onConnectionStateChanged(connectionState: GattConnectionStateWithStatus?) { fun onConnectionStateChanged(connectionState: GattConnectionStateWithStatus?) {
Log.i("AAATESTAAA", "Connection state: $connectionState")
_data.value = _data.value.copy(connectionState = connectionState) _data.value = _data.value.copy(connectionState = connectionState)
} }
@@ -104,6 +102,10 @@ class CGMRepository @Inject constructor(
_loggerEvent.tryEmit(OpenLoggerEvent()) _loggerEvent.tryEmit(OpenLoggerEvent())
} }
fun onInitComplete(device: ServerDevice) {
_data.value = _data.value.copy(deviceName = device.name)
}
fun clear() { fun clear() {
_data.value = _data.value.copy(records = emptyList()) _data.value = _data.value.copy(records = emptyList())
} }

View File

@@ -146,13 +146,18 @@ internal class CGMService : NotificationService() {
.onEach { stopIfDisconnected(it) } .onEach { stopIfDisconnected(it) }
.launchIn(lifecycleScope) .launchIn(lifecycleScope)
if (!client.isConnected) {
repository.onInitComplete(device)
return@launch
}
client.discoverServices() client.discoverServices()
.filterNotNull() .filterNotNull()
.onEach { configureGatt(it) } .onEach { configureGatt(it, device) }
.launchIn(lifecycleScope) .launchIn(lifecycleScope)
} }
private suspend fun configureGatt(services: BleGattServices) { private suspend fun configureGatt(services: BleGattServices, device: ServerDevice) {
val cgmService = services.findService(CGMS_SERVICE_UUID)!! val cgmService = services.findService(CGMS_SERVICE_UUID)!!
val statusCharacteristic = cgmService.findCharacteristic(CGM_STATUS_UUID)!! val statusCharacteristic = cgmService.findCharacteristic(CGM_STATUS_UUID)!!
val featureCharacteristic = cgmService.findCharacteristic(CGM_FEATURE_UUID)!! val featureCharacteristic = cgmService.findCharacteristic(CGM_FEATURE_UUID)!!
@@ -223,6 +228,8 @@ internal class CGMService : NotificationService() {
if (sessionStartTime == 0L) { if (sessionStartTime == 0L) {
opsControlPointCharacteristic.write(CGMSpecificOpsControlPointData.startSession(secured).value!!) opsControlPointCharacteristic.write(CGMSpecificOpsControlPointData.startSession(secured).value!!)
} }
repository.onInitComplete(device)
} }
private fun onAccessControlPointDataReceived(data: RecordAccessControlPointData) = lifecycleScope.launch { private fun onAccessControlPointDataReceived(data: RecordAccessControlPointData) = lifecycleScope.launch {

View File

@@ -81,7 +81,9 @@ fun CGMScreen() {
.padding(16.dp) .padding(16.dp)
.verticalScroll(rememberScrollState()) .verticalScroll(rememberScrollState())
) { ) {
when (state.connectionState?.state) { if (state.deviceName == null) {
DeviceConnectingView { NavigateUpButton(navigateUp) }
} else when (state.connectionState?.state) {
null, null,
GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) } GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) }

View File

@@ -102,6 +102,11 @@ internal class CSCService : NotificationService() {
.onEach { stopIfDisconnected(it) } .onEach { stopIfDisconnected(it) }
.launchIn(lifecycleScope) .launchIn(lifecycleScope)
if (!client.isConnected) {
repository.onInitComplete(device)
return@launch
}
client.discoverServices() client.discoverServices()
.filterNotNull() .filterNotNull()
.onEach { configureGatt(it, device) } .onEach { configureGatt(it, device) }

View File

@@ -77,7 +77,9 @@ fun CSCScreen() {
.padding(16.dp) .padding(16.dp)
.verticalScroll(rememberScrollState()) .verticalScroll(rememberScrollState())
) { ) {
when (state.connectionState?.state) { if (state.deviceName == null) {
DeviceConnectingView { NavigateUpButton(navigateUp) }
} else when (state.connectionState?.state) {
null, null,
GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) } GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) }
GattConnectionState.STATE_DISCONNECTED, GattConnectionState.STATE_DISCONNECTED,

View File

@@ -77,7 +77,9 @@ fun GLSScreen() {
.padding(16.dp) .padding(16.dp)
.verticalScroll(rememberScrollState()) .verticalScroll(rememberScrollState())
) { ) {
when (state.glsServiceData.connectionState?.state) { if (state.deviceName == null) {
DeviceConnectingView { NavigateUpButton(navigateUp) }
} else when (state.glsServiceData.connectionState?.state) {
null, null,
GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) } GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) }
GattConnectionState.STATE_DISCONNECTED, GattConnectionState.STATE_DISCONNECTED,

View File

@@ -158,7 +158,7 @@ internal class GLSViewModel @Inject constructor(
} }
private fun startGattClient(device: ServerDevice) = viewModelScope.launch { private fun startGattClient(device: ServerDevice) = viewModelScope.launch {
logger = NordicBlekLogger(context, stringConst.APP_NAME, "BPS", device.address) logger = NordicBlekLogger(context, stringConst.APP_NAME, "GLS", device.address)
client = device.connect(context, logger = logger) client = device.connect(context, logger = logger)
@@ -168,6 +168,11 @@ internal class GLSViewModel @Inject constructor(
.onEach { logAnalytics(it) } .onEach { logAnalytics(it) }
.launchIn(viewModelScope) .launchIn(viewModelScope)
if (!client.isConnected) {
_state.value = _state.value.copy(deviceName = device.name)
return@launch
}
client.discoverServices() client.discoverServices()
.filterNotNull() .filterNotNull()
.onEach { configureGatt(it, device) } .onEach { configureGatt(it, device) }

View File

@@ -104,6 +104,11 @@ internal class HRSService : NotificationService() {
.onEach { stopIfDisconnected(it) } .onEach { stopIfDisconnected(it) }
.launchIn(lifecycleScope) .launchIn(lifecycleScope)
if (!client.isConnected) {
repository.onInitComplete(device)
return@launch
}
client.discoverServices() client.discoverServices()
.filterNotNull() .filterNotNull()
.onEach { configureGatt(it, device) } .onEach { configureGatt(it, device) }

View File

@@ -77,7 +77,9 @@ fun HRSScreen() {
.padding(16.dp) .padding(16.dp)
.verticalScroll(rememberScrollState()) .verticalScroll(rememberScrollState())
) { ) {
when (state.connectionState?.state) { if (state.deviceName == null) {
DeviceConnectingView { NavigateUpButton(navigateUp) }
} else when (state.connectionState?.state) {
null, null,
GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) } GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) }
GattConnectionState.STATE_DISCONNECTED, GattConnectionState.STATE_DISCONNECTED,

View File

@@ -102,6 +102,11 @@ internal class HTSService : NotificationService() {
.onEach { stopIfDisconnected(it) } .onEach { stopIfDisconnected(it) }
.launchIn(lifecycleScope) .launchIn(lifecycleScope)
if (!client.isConnected) {
repository.onInitComplete(device)
return@launch
}
client.discoverServices() client.discoverServices()
.filterNotNull() .filterNotNull()
.onEach { configureGatt(it, device) } .onEach { configureGatt(it, device) }

View File

@@ -81,7 +81,9 @@ fun HTSScreen() {
.padding(16.dp) .padding(16.dp)
.verticalScroll(rememberScrollState()) .verticalScroll(rememberScrollState())
) { ) {
when (state.connectionState?.state) { if (state.deviceName == null) {
DeviceConnectingView { NavigateUpButton(navigateUp) }
} else when (state.connectionState?.state) {
null, null,
GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) } GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) }
GattConnectionState.STATE_DISCONNECTED, GattConnectionState.STATE_DISCONNECTED,

View File

@@ -172,6 +172,11 @@ internal class PRXService : NotificationService() {
.onEach { stopIfDisconnected(it.state, it.status) } .onEach { stopIfDisconnected(it.state, it.status) }
.launchIn(lifecycleScope) .launchIn(lifecycleScope)
if (!client.isConnected) {
repository.onInitComplete(device)
return@launch
}
client.discoverServices() client.discoverServices()
.filterNotNull() .filterNotNull()
.onEach { configureGatt(it, device) } .onEach { configureGatt(it, device) }

View File

@@ -77,8 +77,9 @@ fun PRXScreen() {
.padding(16.dp) .padding(16.dp)
.verticalScroll(rememberScrollState()) .verticalScroll(rememberScrollState())
) { ) {
Log.i("AAATESTAAA", "State: ${state.connectionState?.state}") if (state.deviceName == null) {
when (state.connectionState?.state) { DeviceConnectingView { NavigateUpButton(navigateUp) }
} else when (state.connectionState?.state) {
null, null,
GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) } GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) }
GattConnectionState.STATE_DISCONNECTED, GattConnectionState.STATE_DISCONNECTED,

View File

@@ -102,6 +102,11 @@ internal class RSCSService : NotificationService() {
.onEach { stopIfDisconnected(it) } .onEach { stopIfDisconnected(it) }
.launchIn(lifecycleScope) .launchIn(lifecycleScope)
if (!client.isConnected) {
repository.onInitComplete(device)
return@launch
}
client.discoverServices() client.discoverServices()
.filterNotNull() .filterNotNull()
.onEach { configureGatt(it, device) } .onEach { configureGatt(it, device) }

View File

@@ -77,7 +77,9 @@ fun RSCSScreen() {
.padding(16.dp) .padding(16.dp)
.verticalScroll(rememberScrollState()) .verticalScroll(rememberScrollState())
) { ) {
when (state.connectionState?.state) { if (state.deviceName == null) {
DeviceConnectingView { NavigateUpButton(navigateUp) }
} else when (state.connectionState?.state) {
null, null,
GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) } GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) }
GattConnectionState.STATE_DISCONNECTED, GattConnectionState.STATE_DISCONNECTED,

View File

@@ -97,8 +97,6 @@ internal class UARTService : NotificationService() {
client = device.connect(this@UARTService, logger = logger) client = device.connect(this@UARTService, logger = logger)
Log.d("AAATESTAAA","connect finish")
client.requestMtu(Mtu.max) client.requestMtu(Mtu.max)
repository.loggerEvent repository.loggerEvent
@@ -111,6 +109,11 @@ internal class UARTService : NotificationService() {
.onEach { stopIfDisconnected(it) } .onEach { stopIfDisconnected(it) }
.launchIn(lifecycleScope) .launchIn(lifecycleScope)
if (!client.isConnected) {
repository.onInitComplete(device)
return@launch
}
client.discoverServices() client.discoverServices()
.filterNotNull() .filterNotNull()
.onEach { configureGatt(it, device, logger) } .onEach { configureGatt(it, device, logger) }
@@ -140,8 +143,6 @@ internal class UARTService : NotificationService() {
.onEach { logger.log(10, "Sent: $it") } .onEach { logger.log(10, "Sent: $it") }
.launchIn(lifecycleScope) .launchIn(lifecycleScope)
repository.onInitComplete(device) repository.onInitComplete(device)
} }

View File

@@ -35,8 +35,6 @@ import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Scaffold import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
@@ -47,7 +45,6 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel import androidx.hilt.navigation.compose.hiltViewModel
import com.google.accompanist.pager.ExperimentalPagerApi
import no.nordicsemi.android.common.theme.view.PagerView import no.nordicsemi.android.common.theme.view.PagerView
import no.nordicsemi.android.common.theme.view.PagerViewEntity import no.nordicsemi.android.common.theme.view.PagerViewEntity
import no.nordicsemi.android.common.theme.view.PagerViewItem import no.nordicsemi.android.common.theme.view.PagerViewItem
@@ -57,8 +54,6 @@ import no.nordicsemi.android.common.ui.scanner.view.Reason
import no.nordicsemi.android.kotlin.ble.core.data.GattConnectionState import no.nordicsemi.android.kotlin.ble.core.data.GattConnectionState
import no.nordicsemi.android.uart.R import no.nordicsemi.android.uart.R
import no.nordicsemi.android.uart.viewmodel.UARTViewModel import no.nordicsemi.android.uart.viewmodel.UARTViewModel
import no.nordicsemi.android.ui.view.BackIconAppBar
import no.nordicsemi.android.ui.view.LoggerIconAppBar
import no.nordicsemi.android.ui.view.NavigateUpButton import no.nordicsemi.android.ui.view.NavigateUpButton
import no.nordicsemi.android.ui.view.ProfileAppBar import no.nordicsemi.android.ui.view.ProfileAppBar
@@ -85,7 +80,9 @@ fun UARTScreen() {
Column( Column(
modifier = Modifier.padding(it) modifier = Modifier.padding(it)
) { ) {
when (state.uartManagerState.connectionState?.state) { if (state.uartManagerState.deviceName == null) {
DeviceConnectingView { NavigateUpButton(navigateUp) }
} else when (state.uartManagerState.connectionState?.state) {
null, null,
GattConnectionState.STATE_CONNECTING -> PaddingBox { DeviceConnectingView { NavigateUpButton(navigateUp) } } GattConnectionState.STATE_CONNECTING -> PaddingBox { DeviceConnectingView { NavigateUpButton(navigateUp) } }
GattConnectionState.STATE_DISCONNECTED, GattConnectionState.STATE_DISCONNECTED,