mirror of
https://github.com/aljazceru/Android-nRF-Toolbox.git
synced 2025-12-28 11:54:24 +01:00
Fix HTS profile
This commit is contained in:
@@ -31,11 +31,14 @@
|
||||
|
||||
package no.nordicsemi.android.hts.data
|
||||
|
||||
import no.nordicsemi.android.hts.view.TemperatureUnit
|
||||
import no.nordicsemi.android.kotlin.ble.core.data.GattConnectionState
|
||||
import no.nordicsemi.android.kotlin.ble.profile.hts.data.HTSData
|
||||
|
||||
internal data class HTSServiceData(
|
||||
val data: HTSData = HTSData(),
|
||||
val batteryLevel: Int? = null,
|
||||
val connectionState: GattConnectionState? = null
|
||||
val connectionState: GattConnectionState? = null,
|
||||
val temperatureUnit: TemperatureUnit = TemperatureUnit.CELSIUS,
|
||||
val deviceName: String? = null
|
||||
)
|
||||
|
||||
@@ -40,6 +40,7 @@ import kotlinx.coroutines.flow.map
|
||||
import no.nordicsemi.android.common.core.simpleSharedFlow
|
||||
import no.nordicsemi.android.common.logger.NordicLogger
|
||||
import no.nordicsemi.android.hts.data.HTSServiceData
|
||||
import no.nordicsemi.android.hts.view.TemperatureUnit
|
||||
import no.nordicsemi.android.kotlin.ble.core.ServerDevice
|
||||
import no.nordicsemi.android.kotlin.ble.core.data.GattConnectionState
|
||||
import no.nordicsemi.android.kotlin.ble.profile.hts.data.HTSData
|
||||
@@ -68,6 +69,14 @@ class HTSRepository @Inject constructor(
|
||||
serviceManager.startService(HTSService::class.java, device)
|
||||
}
|
||||
|
||||
fun onInitComplete(device: ServerDevice) {
|
||||
_data.value = _data.value.copy(deviceName = device.name)
|
||||
}
|
||||
|
||||
internal fun setTemperatureUnit(temperatureUnit: TemperatureUnit) {
|
||||
_data.value = _data.value.copy(temperatureUnit = temperatureUnit)
|
||||
}
|
||||
|
||||
fun onConnectionStateChanged(connectionState: GattConnectionState?) {
|
||||
_data.value = _data.value.copy(connectionState = connectionState)
|
||||
}
|
||||
|
||||
@@ -91,11 +91,11 @@ internal class HTSService : 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 htsService = services.findService(HTS_SERVICE_UUID)!!
|
||||
val htsMeasurementCharacteristic = htsService.findCharacteristic(HTS_MEASUREMENT_CHARACTERISTIC_UUID)!!
|
||||
val batteryService = services.findService(BATTERY_SERVICE_UUID)!!
|
||||
@@ -110,6 +110,8 @@ internal class HTSService : NotificationService() {
|
||||
.mapNotNull { HTSDataParser.parse(it) }
|
||||
.onEach { repository.onHTSDataChanged(it) }
|
||||
.launchIn(lifecycleScope)
|
||||
|
||||
repository.onInitComplete(device)
|
||||
}
|
||||
|
||||
private fun stopIfDisconnected(connectionState: GattConnectionState) {
|
||||
|
||||
@@ -52,7 +52,7 @@ import no.nordicsemi.android.ui.view.ScreenSection
|
||||
import no.nordicsemi.android.ui.view.SectionTitle
|
||||
|
||||
@Composable
|
||||
internal fun HTSContentView(state: HTSServiceData, temperatureUnit: TemperatureUnit, onEvent: (HTSScreenViewEvent) -> Unit) {
|
||||
internal fun HTSContentView(state: HTSServiceData, onEvent: (HTSScreenViewEvent) -> Unit) {
|
||||
Column(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
@@ -62,7 +62,7 @@ internal fun HTSContentView(state: HTSServiceData, temperatureUnit: TemperatureU
|
||||
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
|
||||
RadioButtonGroup(viewEntity = temperatureUnit.temperatureSettingsItems()) {
|
||||
RadioButtonGroup(viewEntity = state.temperatureUnit.temperatureSettingsItems()) {
|
||||
onEvent(OnTemperatureUnitSelected(it.label.toTemperatureUnit()))
|
||||
}
|
||||
}
|
||||
@@ -76,7 +76,7 @@ internal fun HTSContentView(state: HTSServiceData, temperatureUnit: TemperatureU
|
||||
|
||||
KeyValueField(
|
||||
stringResource(id = R.string.hts_temperature),
|
||||
displayTemperature(state.data.temperature, temperatureUnit)
|
||||
displayTemperature(state.data.temperature, state.temperatureUnit)
|
||||
)
|
||||
}
|
||||
|
||||
@@ -99,5 +99,5 @@ internal fun HTSContentView(state: HTSServiceData, temperatureUnit: TemperatureU
|
||||
@Preview
|
||||
@Composable
|
||||
private fun Preview() {
|
||||
HTSContentView(state = HTSServiceData(), TemperatureUnit.CELSIUS) { }
|
||||
HTSContentView(state = HTSServiceData()) { }
|
||||
}
|
||||
|
||||
@@ -47,6 +47,7 @@ import no.nordicsemi.android.common.ui.scanner.view.DeviceConnectingView
|
||||
import no.nordicsemi.android.common.ui.scanner.view.DeviceDisconnectedView
|
||||
import no.nordicsemi.android.common.ui.scanner.view.Reason
|
||||
import no.nordicsemi.android.hts.R
|
||||
import no.nordicsemi.android.hts.data.HTSServiceData
|
||||
import no.nordicsemi.android.hts.viewmodel.HTSViewModel
|
||||
import no.nordicsemi.android.kotlin.ble.core.data.GattConnectionState
|
||||
import no.nordicsemi.android.ui.view.BackIconAppBar
|
||||
@@ -70,14 +71,15 @@ fun HTSScreen() {
|
||||
.padding(16.dp)
|
||||
.verticalScroll(rememberScrollState())
|
||||
) {
|
||||
when (state.htsManagerState) {
|
||||
NoDeviceState -> DeviceConnectingView()
|
||||
is WorkingState -> when (state.htsManagerState.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 -> HTSContentView(state.htsManagerState.result, state.temperatureUnit) { viewModel.onEvent(it) }
|
||||
GattConnectionState.STATE_CONNECTED -> HTSContentView(state) { viewModel.onEvent(it) }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -85,7 +87,7 @@ fun HTSScreen() {
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun AppBar(state: HTSViewState, navigateUp: () -> Unit, viewModel: HTSViewModel) {
|
||||
private fun AppBar(state: HTSServiceData, navigateUp: () -> Unit, viewModel: HTSViewModel) {
|
||||
if (state.deviceName?.isNotBlank() == true) {
|
||||
LoggerIconAppBar(state.deviceName, navigateUp, { viewModel.onEvent(DisconnectEvent) }) {
|
||||
viewModel.onEvent(OpenLoggerEvent)
|
||||
|
||||
@@ -1,46 +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.hts.view
|
||||
|
||||
import no.nordicsemi.android.hts.data.HTSServiceData
|
||||
|
||||
internal data class HTSViewState(
|
||||
val temperatureUnit: TemperatureUnit = TemperatureUnit.CELSIUS,
|
||||
val htsManagerState: HTSManagerState = NoDeviceState,
|
||||
val deviceName: String? = null
|
||||
)
|
||||
|
||||
internal sealed class HTSManagerState
|
||||
|
||||
internal data class WorkingState(val result: HTSServiceData) : HTSManagerState()
|
||||
|
||||
internal object NoDeviceState : HTSManagerState()
|
||||
@@ -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
|
||||
@@ -50,11 +48,9 @@ import no.nordicsemi.android.hts.repository.HTSRepository
|
||||
import no.nordicsemi.android.hts.repository.HTS_SERVICE_UUID
|
||||
import no.nordicsemi.android.hts.view.DisconnectEvent
|
||||
import no.nordicsemi.android.hts.view.HTSScreenViewEvent
|
||||
import no.nordicsemi.android.hts.view.HTSViewState
|
||||
import no.nordicsemi.android.hts.view.NavigateUp
|
||||
import no.nordicsemi.android.hts.view.OnTemperatureUnitSelected
|
||||
import no.nordicsemi.android.hts.view.OpenLoggerEvent
|
||||
import no.nordicsemi.android.hts.view.WorkingState
|
||||
import no.nordicsemi.android.kotlin.ble.core.ServerDevice
|
||||
import no.nordicsemi.android.kotlin.ble.core.data.GattConnectionState
|
||||
import no.nordicsemi.android.toolbox.scanner.ScannerDestinationId
|
||||
@@ -67,8 +63,7 @@ internal class HTSViewModel @Inject constructor(
|
||||
private val analytics: AppAnalytics
|
||||
) : ViewModel() {
|
||||
|
||||
private val _state = MutableStateFlow(HTSViewState())
|
||||
val state = _state.asStateFlow()
|
||||
val state = repository.data
|
||||
|
||||
init {
|
||||
viewModelScope.launch {
|
||||
@@ -78,8 +73,6 @@ internal class HTSViewModel @Inject constructor(
|
||||
}
|
||||
|
||||
repository.data.onEach {
|
||||
_state.value = _state.value.copy(htsManagerState = WorkingState(it))
|
||||
|
||||
if (it.connectionState == GattConnectionState.STATE_CONNECTED) {
|
||||
analytics.logEvent(ProfileConnectedEvent(Profile.HTS))
|
||||
}
|
||||
@@ -102,7 +95,6 @@ internal class HTSViewModel @Inject constructor(
|
||||
}
|
||||
|
||||
private fun onDeviceSelected(device: ServerDevice) {
|
||||
_state.value = _state.value.copy(deviceName = device.name)
|
||||
repository.launch(device)
|
||||
}
|
||||
|
||||
@@ -121,6 +113,6 @@ internal class HTSViewModel @Inject constructor(
|
||||
}
|
||||
|
||||
private fun onTemperatureUnitSelected(event: OnTemperatureUnitSelected) {
|
||||
_state.value = _state.value.copy(temperatureUnit = event.value)
|
||||
repository.setTemperatureUnit(event.value)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user