Fix RSCS profile

This commit is contained in:
Sylwester Zielinski
2023-03-20 13:57:42 +01:00
parent 6e1a23341b
commit 702841fd8d
6 changed files with 18 additions and 62 deletions

View File

@@ -40,7 +40,8 @@ import no.nordicsemi.android.rscs.R
internal data class RSCSServiceData(
val data: RSCSData = RSCSData(),
val batteryLevel: Int? = null,
val connectionState: GattConnectionState? = null
val connectionState: GattConnectionState? = null,
val deviceName: String? = null
) {
@Composable
fun displayActivity(): String {

View File

@@ -68,6 +68,10 @@ class RSCSRepository @Inject constructor(
serviceManager.startService(RSCSService::class.java, device)
}
fun onInitComplete(device: ServerDevice) {
_data.value = _data.value.copy(deviceName = device.name)
}
fun onConnectionStateChanged(connectionState: GattConnectionState?) {
_data.value = _data.value.copy(connectionState = connectionState)
}

View File

@@ -91,11 +91,11 @@ internal class RSCSService : NotificationService() {
client.services
.filterNotNull()
.onEach { configureGatt(it) }
.onEach { configureGatt(it, device) }
.launchIn(lifecycleScope)
}
private suspend fun configureGatt(services: BleGattServices) {
private suspend fun configureGatt(services: BleGattServices, device: ServerDevice) {
val rscsService = services.findService(RSCS_SERVICE_UUID)!!
val rscsMeasurementCharacteristic = rscsService.findCharacteristic(RSC_MEASUREMENT_CHARACTERISTIC_UUID)!!
val batteryService = services.findService(BATTERY_SERVICE_UUID)!!
@@ -110,6 +110,8 @@ internal class RSCSService : NotificationService() {
.mapNotNull { RSCSDataParser.parse(it) }
.onEach { repository.onRSCSDataChanged(it) }
.launchIn(lifecycleScope)
repository.onInitComplete(device)
}
private fun stopIfDisconnected(connectionState: GattConnectionState) {

View File

@@ -48,6 +48,7 @@ import no.nordicsemi.android.common.ui.scanner.view.DeviceDisconnectedView
import no.nordicsemi.android.common.ui.scanner.view.Reason
import no.nordicsemi.android.kotlin.ble.core.data.GattConnectionState
import no.nordicsemi.android.rscs.R
import no.nordicsemi.android.rscs.data.RSCSServiceData
import no.nordicsemi.android.rscs.viewmodel.RSCSViewModel
import no.nordicsemi.android.ui.view.BackIconAppBar
import no.nordicsemi.android.ui.view.LoggerIconAppBar
@@ -70,14 +71,15 @@ fun RSCSScreen() {
.padding(16.dp)
.verticalScroll(rememberScrollState())
) {
when (state.rscsManagerState) {
NoDeviceState -> DeviceConnectingView()
is WorkingState -> when (state.rscsManagerState.result.connectionState) {
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.rscsManagerState.result) { viewModel.onEvent(it) }
GattConnectionState.STATE_CONNECTED -> RSCSContentView(state) { viewModel.onEvent(it) }
}
}
}
@@ -85,7 +87,7 @@ fun RSCSScreen() {
}
@Composable
private fun AppBar(state: RSCSViewState, navigateUp: () -> Unit, viewModel: RSCSViewModel) {
private fun AppBar(state: RSCSServiceData, navigateUp: () -> Unit, viewModel: RSCSViewModel) {
if (state.deviceName?.isNotBlank() == true) {
LoggerIconAppBar(state.deviceName, navigateUp, { viewModel.onEvent(DisconnectEvent) }) {
viewModel.onEvent(OpenLoggerEvent)

View File

@@ -1,45 +0,0 @@
/*
* Copyright (c) 2022, Nordic Semiconductor
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be
* used to endorse or promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package no.nordicsemi.android.rscs.view
import no.nordicsemi.android.rscs.data.RSCSServiceData
internal data class RSCSViewState(
val rscsManagerState: RSCSManagerState = NoDeviceState,
val deviceName: String? = null
)
internal sealed class RSCSManagerState
internal data class WorkingState(val result: RSCSServiceData) : RSCSManagerState()
internal object NoDeviceState : RSCSManagerState()

View File

@@ -35,8 +35,6 @@ import android.os.ParcelUuid
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
@@ -53,9 +51,7 @@ import no.nordicsemi.android.rscs.repository.RSCS_SERVICE_UUID
import no.nordicsemi.android.rscs.view.DisconnectEvent
import no.nordicsemi.android.rscs.view.NavigateUpEvent
import no.nordicsemi.android.rscs.view.OpenLoggerEvent
import no.nordicsemi.android.rscs.view.RSCSViewState
import no.nordicsemi.android.rscs.view.RSCScreenViewEvent
import no.nordicsemi.android.rscs.view.WorkingState
import no.nordicsemi.android.toolbox.scanner.ScannerDestinationId
import javax.inject.Inject
@@ -66,8 +62,7 @@ internal class RSCSViewModel @Inject constructor(
private val analytics: AppAnalytics
) : ViewModel() {
private val _state = MutableStateFlow(RSCSViewState())
val state = _state.asStateFlow()
val state = repository.data
init {
viewModelScope.launch {
@@ -77,8 +72,6 @@ internal class RSCSViewModel @Inject constructor(
}
repository.data.onEach {
_state.value = _state.value.copy(rscsManagerState = WorkingState(it))
if (it.connectionState == GattConnectionState.STATE_CONNECTED) {
analytics.logEvent(ProfileConnectedEvent(Profile.RSCS))
}
@@ -101,7 +94,6 @@ internal class RSCSViewModel @Inject constructor(
}
private fun onDeviceSelected(device: ServerDevice) {
_state.value = _state.value.copy(deviceName = device.name)
repository.launch(device)
}