Fix disapearing error message on gatt error

This commit is contained in:
Sylwester Zielinski
2023-04-05 14:37:52 +02:00
parent 8283813dcb
commit 0e0a2e0095
11 changed files with 72 additions and 93 deletions

View File

@@ -70,16 +70,14 @@ fun BPSScreen() {
.padding(16.dp)
.verticalScroll(rememberScrollState())
) {
if (state.deviceName == null) {
DeviceConnectingView()
} else {
when (state.result.connectionState) {
null,
GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) }
GattConnectionState.STATE_DISCONNECTED,
GattConnectionState.STATE_DISCONNECTING -> DeviceDisconnectedView(Reason.UNKNOWN) { NavigateUpButton(navigateUp) }
GattConnectionState.STATE_CONNECTED -> BPSContentView(state.result) { viewModel.onEvent(it) }
when (state.result.connectionState) {
null,
GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) }
GattConnectionState.STATE_DISCONNECTED,
GattConnectionState.STATE_DISCONNECTING -> DeviceDisconnectedView(Reason.UNKNOWN) {
NavigateUpButton(navigateUp)
}
GattConnectionState.STATE_CONNECTED -> BPSContentView(state.result) { viewModel.onEvent(it) }
}
}
}

View File

@@ -71,16 +71,14 @@ fun CGMScreen() {
.padding(16.dp)
.verticalScroll(rememberScrollState())
) {
if (state.deviceName == null) {
DeviceConnectingView()
} else {
when (state.connectionState) {
null,
GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) }
GattConnectionState.STATE_DISCONNECTED,
GattConnectionState.STATE_DISCONNECTING -> DeviceDisconnectedView(Reason.UNKNOWN) { NavigateUpButton(navigateUp) }
GattConnectionState.STATE_CONNECTED -> CGMContentView(state) { viewModel.onEvent(it) }
when (state.connectionState) {
null,
GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) }
GattConnectionState.STATE_DISCONNECTED,
GattConnectionState.STATE_DISCONNECTING -> DeviceDisconnectedView(Reason.UNKNOWN) {
NavigateUpButton(navigateUp)
}
GattConnectionState.STATE_CONNECTED -> CGMContentView(state) { viewModel.onEvent(it) }
}
}
}

View File

@@ -71,16 +71,14 @@ fun CSCScreen() {
.padding(16.dp)
.verticalScroll(rememberScrollState())
) {
if (state.deviceName == null) {
DeviceConnectingView()
} else {
when (state.connectionState) {
null,
GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) }
GattConnectionState.STATE_DISCONNECTED,
GattConnectionState.STATE_DISCONNECTING -> DeviceDisconnectedView(Reason.UNKNOWN) { NavigateUpButton(navigateUp) }
GattConnectionState.STATE_CONNECTED -> CSCContentView(state) { viewModel.onEvent(it) }
when (state.connectionState) {
null,
GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) }
GattConnectionState.STATE_DISCONNECTED,
GattConnectionState.STATE_DISCONNECTING -> DeviceDisconnectedView(Reason.UNKNOWN) {
NavigateUpButton(navigateUp)
}
GattConnectionState.STATE_CONNECTED -> CSCContentView(state) { viewModel.onEvent(it) }
}
}
}

View File

@@ -70,16 +70,14 @@ fun GLSScreen() {
.padding(16.dp)
.verticalScroll(rememberScrollState())
) {
if (state.deviceName == null) {
DeviceConnectingView()
} else {
when (state.glsServiceData.connectionState) {
null,
GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) }
GattConnectionState.STATE_DISCONNECTED,
GattConnectionState.STATE_DISCONNECTING -> DeviceDisconnectedView(Reason.UNKNOWN) { NavigateUpButton(navigateUp) }
GattConnectionState.STATE_CONNECTED -> GLSContentView(state.glsServiceData) { viewModel.onEvent(it) }
when (state.glsServiceData.connectionState) {
null,
GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) }
GattConnectionState.STATE_DISCONNECTED,
GattConnectionState.STATE_DISCONNECTING -> DeviceDisconnectedView(Reason.UNKNOWN) {
NavigateUpButton(navigateUp)
}
GattConnectionState.STATE_CONNECTED -> GLSContentView(state.glsServiceData) { viewModel.onEvent(it) }
}
}
}

View File

@@ -34,6 +34,7 @@ package no.nordicsemi.android.gls.main.viewmodel
import android.annotation.SuppressLint
import android.content.Context
import android.os.ParcelUuid
import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
@@ -163,7 +164,6 @@ internal class GLSViewModel @Inject constructor(
client.connectionState
.filterNotNull()
.onEach { _state.value = _state.value.copyWithNewConnectionState(it) }
.onEach { stopIfDisconnected(it) }
.onEach { logAnalytics(it) }
.launchIn(viewModelScope)
@@ -209,12 +209,6 @@ internal class GLSViewModel @Inject constructor(
_state.value = _state.value.copy(deviceName = device.name) //prevents UI from appearing before BLE connection is set up
}
private fun stopIfDisconnected(connectionState: GattConnectionState) {
if (connectionState == GattConnectionState.STATE_DISCONNECTED) {
navigationManager.navigateUp()
}
}
private fun onAccessControlPointDataReceived(data: RecordAccessControlPointData) = viewModelScope.launch {
when (data) {
is NumberOfRecordsData -> onNumberOfRecordsReceived(data.numberOfRecords)

View File

@@ -71,16 +71,14 @@ fun HRSScreen() {
.padding(16.dp)
.verticalScroll(rememberScrollState())
) {
if (state.deviceName == null) {
DeviceConnectingView()
} else {
when (state.connectionState) {
null,
GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) }
GattConnectionState.STATE_DISCONNECTED,
GattConnectionState.STATE_DISCONNECTING -> DeviceDisconnectedView(Reason.UNKNOWN) { NavigateUpButton(navigateUp) }
GattConnectionState.STATE_CONNECTED -> HRSContentView(state) { viewModel.onEvent(it) }
when (state.connectionState) {
null,
GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) }
GattConnectionState.STATE_DISCONNECTED,
GattConnectionState.STATE_DISCONNECTING -> DeviceDisconnectedView(Reason.UNKNOWN) {
NavigateUpButton(navigateUp)
}
GattConnectionState.STATE_CONNECTED -> HRSContentView(state) { viewModel.onEvent(it) }
}
}
}

View File

@@ -71,16 +71,14 @@ fun HTSScreen() {
.padding(16.dp)
.verticalScroll(rememberScrollState())
) {
if (state.deviceName == null) {
DeviceConnectingView()
} else {
when (state.connectionState) {
null,
GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) }
GattConnectionState.STATE_DISCONNECTED,
GattConnectionState.STATE_DISCONNECTING -> DeviceDisconnectedView(Reason.UNKNOWN) { NavigateUpButton(navigateUp) }
GattConnectionState.STATE_CONNECTED -> HTSContentView(state) { viewModel.onEvent(it) }
when (state.connectionState) {
null,
GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) }
GattConnectionState.STATE_DISCONNECTED,
GattConnectionState.STATE_DISCONNECTING -> DeviceDisconnectedView(Reason.UNKNOWN) {
NavigateUpButton(navigateUp)
}
GattConnectionState.STATE_CONNECTED -> HTSContentView(state) { viewModel.onEvent(it) }
}
}
}

View File

@@ -71,16 +71,14 @@ fun PRXScreen() {
.padding(16.dp)
.verticalScroll(rememberScrollState())
) {
if (state.deviceName == null) {
DeviceConnectingView()
} else {
when (state.connectionState) {
null,
GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) }
GattConnectionState.STATE_DISCONNECTED,
GattConnectionState.STATE_DISCONNECTING -> DeviceDisconnectedView(getReason(state.isLinkLossDisconnected)) { NavigateUpButton(navigateUp) }
GattConnectionState.STATE_CONNECTED -> ContentView(state) { viewModel.onEvent(it) }
when (state.connectionState) {
null,
GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) }
GattConnectionState.STATE_DISCONNECTED,
GattConnectionState.STATE_DISCONNECTING -> DeviceDisconnectedView(getReason(state.isLinkLossDisconnected)) {
NavigateUpButton(navigateUp)
}
GattConnectionState.STATE_CONNECTED -> ContentView(state) { viewModel.onEvent(it) }
}
}
}

View File

@@ -71,16 +71,14 @@ fun RSCSScreen() {
.padding(16.dp)
.verticalScroll(rememberScrollState())
) {
if (state.deviceName == null) {
DeviceConnectingView()
} else {
when (state.connectionState) {
null,
GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) }
GattConnectionState.STATE_DISCONNECTED,
GattConnectionState.STATE_DISCONNECTING -> DeviceDisconnectedView(Reason.UNKNOWN) { NavigateUpButton(navigateUp) }
GattConnectionState.STATE_CONNECTED -> RSCSContentView(state) { viewModel.onEvent(it) }
when (state.connectionState) {
null,
GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) }
GattConnectionState.STATE_DISCONNECTED,
GattConnectionState.STATE_DISCONNECTING -> DeviceDisconnectedView(Reason.UNKNOWN) {
NavigateUpButton(navigateUp)
}
GattConnectionState.STATE_CONNECTED -> RSCSContentView(state) { viewModel.onEvent(it) }
}
}
}

View File

@@ -35,7 +35,7 @@ import no.nordicsemi.android.kotlin.ble.core.data.GattConnectionState
internal data class UARTServiceData(
val messages: List<UARTRecord> = emptyList(),
val connectionState: GattConnectionState = GattConnectionState.STATE_DISCONNECTED,
val connectionState: GattConnectionState? = null,
val batteryLevel: Int? = null,
val deviceName: String? = null
) {

View File

@@ -75,15 +75,14 @@ fun UARTScreen() {
Column(
modifier = Modifier.padding(it)
) {
if (state.uartManagerState.deviceName == null) {
PaddingBox { DeviceConnectingView() }
} else {
when (state.uartManagerState.connectionState) {
GattConnectionState.STATE_CONNECTING -> PaddingBox { DeviceConnectingView { NavigateUpButton(navigateUp) } }
GattConnectionState.STATE_DISCONNECTED,
GattConnectionState.STATE_DISCONNECTING -> PaddingBox { DeviceDisconnectedView(Reason.UNKNOWN) { NavigateUpButton(navigateUp) } }
GattConnectionState.STATE_CONNECTED -> SuccessScreen()
when (state.uartManagerState.connectionState) {
null,
GattConnectionState.STATE_CONNECTING -> PaddingBox { DeviceConnectingView { NavigateUpButton(navigateUp) } }
GattConnectionState.STATE_DISCONNECTED,
GattConnectionState.STATE_DISCONNECTING -> PaddingBox {
DeviceDisconnectedView(Reason.UNKNOWN) { NavigateUpButton(navigateUp) }
}
GattConnectionState.STATE_CONNECTED -> SuccessScreen()
}
}
}
@@ -112,12 +111,14 @@ private fun AppBar(state: UARTViewState, navigateUp: () -> Unit, viewModel: UART
private fun SuccessScreen() {
val input = stringResource(id = R.string.uart_input)
val macros = stringResource(id = R.string.uart_macros)
val viewEntity = remember { PagerViewEntity(
listOf(
PagerViewItem(input) { KeyboardView() },
PagerViewItem(macros) { MacroView() }
val viewEntity = remember {
PagerViewEntity(
listOf(
PagerViewItem(input) { KeyboardView() },
PagerViewItem(macros) { MacroView() }
)
)
) }
}
PagerView(
viewEntity = viewEntity,
modifier = Modifier.fillMaxSize(),