mirror of
https://github.com/aljazceru/Android-nRF-Toolbox.git
synced 2025-12-19 07:24:22 +01:00
Fix new Toolbar
This commit is contained in:
@@ -3,7 +3,7 @@ package no.nordicsemi.android.service
|
||||
import android.annotation.SuppressLint
|
||||
import android.bluetooth.BluetoothDevice
|
||||
|
||||
sealed class BleManagerResult <T> {
|
||||
sealed interface BleManagerResult<T> {
|
||||
|
||||
fun isRunning(): Boolean {
|
||||
return this is SuccessResult
|
||||
@@ -18,15 +18,18 @@ sealed class BleManagerResult <T> {
|
||||
}
|
||||
}
|
||||
|
||||
class IdleResult<T> : BleManagerResult<T>()
|
||||
class ConnectingResult<T> : BleManagerResult<T>()
|
||||
data class SuccessResult<T>(val device: BluetoothDevice, val data: T) : BleManagerResult<T>() {
|
||||
sealed class DeviceHolder(val device: BluetoothDevice) {
|
||||
|
||||
@SuppressLint("MissingPermission")
|
||||
fun deviceName(): String = device.name ?: device.address
|
||||
|
||||
}
|
||||
|
||||
class LinkLossResult<T>(val data: T) : BleManagerResult<T>()
|
||||
class DisconnectedResult<T> : BleManagerResult<T>()
|
||||
class UnknownErrorResult<T> : BleManagerResult<T>()
|
||||
class MissingServiceResult<T> : BleManagerResult<T>()
|
||||
class IdleResult<T> : BleManagerResult<T>
|
||||
class ConnectingResult<T>(device: BluetoothDevice) : DeviceHolder(device), BleManagerResult<T>
|
||||
class SuccessResult<T>(device: BluetoothDevice, val data: T) : DeviceHolder(device), BleManagerResult<T>
|
||||
|
||||
class LinkLossResult<T>(device: BluetoothDevice, val data: T) : DeviceHolder(device), BleManagerResult<T>
|
||||
class DisconnectedResult<T>(device: BluetoothDevice) : DeviceHolder(device), BleManagerResult<T>
|
||||
class UnknownErrorResult<T>(device: BluetoothDevice) : DeviceHolder(device), BleManagerResult<T>
|
||||
class MissingServiceResult<T>(device: BluetoothDevice) : DeviceHolder(device), BleManagerResult<T>
|
||||
|
||||
@@ -10,7 +10,7 @@ class ConnectionObserverAdapter<T> : ConnectionObserver {
|
||||
|
||||
private val TAG = "BLE-CONNECTION"
|
||||
|
||||
private val _status = MutableStateFlow<BleManagerResult<T>>(ConnectingResult())
|
||||
private val _status = MutableStateFlow<BleManagerResult<T>>(IdleResult())
|
||||
val status = _status.asStateFlow()
|
||||
|
||||
private var lastValue: T? = null
|
||||
@@ -21,6 +21,7 @@ class ConnectionObserverAdapter<T> : ConnectionObserver {
|
||||
|
||||
override fun onDeviceConnecting(device: BluetoothDevice) {
|
||||
Log.d(TAG, "onDeviceConnecting()")
|
||||
_status.value = ConnectingResult(device)
|
||||
}
|
||||
|
||||
override fun onDeviceConnected(device: BluetoothDevice) {
|
||||
@@ -29,7 +30,7 @@ class ConnectionObserverAdapter<T> : ConnectionObserver {
|
||||
|
||||
override fun onDeviceFailedToConnect(device: BluetoothDevice, reason: Int) {
|
||||
Log.d(TAG, "onDeviceFailedToConnect(), reason: $reason")
|
||||
_status.value = MissingServiceResult()
|
||||
_status.value = MissingServiceResult(device)
|
||||
}
|
||||
|
||||
override fun onDeviceReady(device: BluetoothDevice) {
|
||||
@@ -44,10 +45,10 @@ class ConnectionObserverAdapter<T> : ConnectionObserver {
|
||||
override fun onDeviceDisconnected(device: BluetoothDevice, reason: Int) {
|
||||
Log.d(TAG, "onDeviceDisconnected(), reason: $reason")
|
||||
_status.value = when (reason) {
|
||||
ConnectionObserver.REASON_NOT_SUPPORTED -> MissingServiceResult()
|
||||
ConnectionObserver.REASON_LINK_LOSS -> LinkLossResult(getData()!!)
|
||||
ConnectionObserver.REASON_SUCCESS -> DisconnectedResult()
|
||||
else -> UnknownErrorResult()
|
||||
ConnectionObserver.REASON_NOT_SUPPORTED -> MissingServiceResult(device)
|
||||
ConnectionObserver.REASON_LINK_LOSS -> LinkLossResult(device, getData()!!)
|
||||
ConnectionObserver.REASON_SUCCESS -> DisconnectedResult(device)
|
||||
else -> UnknownErrorResult(device)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ fun BPSScreen() {
|
||||
@Composable
|
||||
private fun AppBar(state: BPSViewState, navigateUp: () -> Unit, viewModel: BPSViewModel) {
|
||||
val toolbarName = (state as? WorkingState)?.let {
|
||||
(it.result as? SuccessResult<BPSData>)?.deviceName()
|
||||
(it.result as? DeviceHolder)?.deviceName()
|
||||
}
|
||||
|
||||
if (toolbarName == null) {
|
||||
|
||||
@@ -13,6 +13,7 @@ import no.nordicsemi.android.logger.ToolboxLogger
|
||||
import no.nordicsemi.android.logger.ToolboxLoggerFactory
|
||||
import no.nordicsemi.android.service.BleManagerResult
|
||||
import no.nordicsemi.android.service.ConnectingResult
|
||||
import no.nordicsemi.android.service.IdleResult
|
||||
import no.nordicsemi.android.service.ServiceManager
|
||||
import no.nordicsemi.ui.scanner.DiscoveredBluetoothDevice
|
||||
import javax.inject.Inject
|
||||
@@ -28,7 +29,7 @@ class CGMRepository @Inject constructor(
|
||||
private var manager: CGMManager? = null
|
||||
private var logger: ToolboxLogger? = null
|
||||
|
||||
private val _data = MutableStateFlow<BleManagerResult<CGMData>>(ConnectingResult())
|
||||
private val _data = MutableStateFlow<BleManagerResult<CGMData>>(IdleResult())
|
||||
internal val data = _data.asStateFlow()
|
||||
|
||||
val isRunning = data.map { it.isRunning() }
|
||||
|
||||
@@ -50,7 +50,7 @@ fun CGMScreen() {
|
||||
@Composable
|
||||
private fun AppBar(state: CGMViewState, navigateUp: () -> Unit, viewModel: CGMViewModel) {
|
||||
val toolbarName = (state as? WorkingState)?.let {
|
||||
(it.result as? SuccessResult<CGMData>)?.deviceName()
|
||||
(it.result as? DeviceHolder)?.deviceName()
|
||||
}
|
||||
|
||||
if (toolbarName == null) {
|
||||
|
||||
@@ -14,6 +14,7 @@ import no.nordicsemi.android.logger.ToolboxLogger
|
||||
import no.nordicsemi.android.logger.ToolboxLoggerFactory
|
||||
import no.nordicsemi.android.service.BleManagerResult
|
||||
import no.nordicsemi.android.service.ConnectingResult
|
||||
import no.nordicsemi.android.service.IdleResult
|
||||
import no.nordicsemi.android.service.ServiceManager
|
||||
import no.nordicsemi.ui.scanner.DiscoveredBluetoothDevice
|
||||
import javax.inject.Inject
|
||||
@@ -29,7 +30,7 @@ class CSCRepository @Inject constructor(
|
||||
private var manager: CSCManager? = null
|
||||
private var logger: ToolboxLogger? = null
|
||||
|
||||
private val _data = MutableStateFlow<BleManagerResult<CSCData>>(ConnectingResult())
|
||||
private val _data = MutableStateFlow<BleManagerResult<CSCData>>(IdleResult())
|
||||
internal val data = _data.asStateFlow()
|
||||
|
||||
val isRunning = data.map { it.isRunning() }
|
||||
|
||||
@@ -50,7 +50,7 @@ fun CSCScreen() {
|
||||
@Composable
|
||||
private fun AppBar(state: CSCViewState, navigateUp: () -> Unit, viewModel: CSCViewModel) {
|
||||
val toolbarName = (state.cscManagerState as? WorkingState)?.let {
|
||||
(it.result as? SuccessResult<CSCData>)?.deviceName()
|
||||
(it.result as? DeviceHolder)?.deviceName()
|
||||
}
|
||||
|
||||
if (toolbarName == null) {
|
||||
|
||||
@@ -50,7 +50,7 @@ fun GLSScreen() {
|
||||
@Composable
|
||||
private fun AppBar(state: GLSViewState, navigateUp: () -> Unit, viewModel: GLSViewModel) {
|
||||
val toolbarName = (state as? WorkingState)?.let {
|
||||
(it.result as? SuccessResult<GLSData>)?.deviceName()
|
||||
(it.result as? DeviceHolder)?.deviceName()
|
||||
}
|
||||
|
||||
if (toolbarName == null) {
|
||||
|
||||
@@ -13,6 +13,7 @@ import no.nordicsemi.android.logger.ToolboxLogger
|
||||
import no.nordicsemi.android.logger.ToolboxLoggerFactory
|
||||
import no.nordicsemi.android.service.BleManagerResult
|
||||
import no.nordicsemi.android.service.ConnectingResult
|
||||
import no.nordicsemi.android.service.IdleResult
|
||||
import no.nordicsemi.android.service.ServiceManager
|
||||
import no.nordicsemi.ui.scanner.DiscoveredBluetoothDevice
|
||||
import javax.inject.Inject
|
||||
@@ -28,7 +29,7 @@ class HRSRepository @Inject constructor(
|
||||
private var manager: HRSManager? = null
|
||||
private var logger: ToolboxLogger? = null
|
||||
|
||||
private val _data = MutableStateFlow<BleManagerResult<HRSData>>(ConnectingResult())
|
||||
private val _data = MutableStateFlow<BleManagerResult<HRSData>>(IdleResult())
|
||||
internal val data = _data.asStateFlow()
|
||||
|
||||
val isRunning = data.map { it.isRunning() }
|
||||
|
||||
@@ -50,7 +50,7 @@ fun HRSScreen() {
|
||||
@Composable
|
||||
private fun AppBar(state: HRSViewState, navigateUp: () -> Unit, viewModel: HRSViewModel) {
|
||||
val toolbarName = (state as? WorkingState)?.let {
|
||||
(it.result as? SuccessResult<HRSData>)?.deviceName()
|
||||
(it.result as? DeviceHolder)?.deviceName()
|
||||
}
|
||||
|
||||
if (toolbarName == null) {
|
||||
|
||||
@@ -13,6 +13,7 @@ import no.nordicsemi.android.logger.ToolboxLogger
|
||||
import no.nordicsemi.android.logger.ToolboxLoggerFactory
|
||||
import no.nordicsemi.android.service.BleManagerResult
|
||||
import no.nordicsemi.android.service.ConnectingResult
|
||||
import no.nordicsemi.android.service.IdleResult
|
||||
import no.nordicsemi.android.service.ServiceManager
|
||||
import no.nordicsemi.ui.scanner.DiscoveredBluetoothDevice
|
||||
import javax.inject.Inject
|
||||
@@ -28,7 +29,7 @@ class HTSRepository @Inject constructor(
|
||||
private var manager: HTSManager? = null
|
||||
private var logger: ToolboxLogger? = null
|
||||
|
||||
private val _data = MutableStateFlow<BleManagerResult<HTSData>>(ConnectingResult())
|
||||
private val _data = MutableStateFlow<BleManagerResult<HTSData>>(IdleResult())
|
||||
internal val data = _data.asStateFlow()
|
||||
|
||||
val isRunning = data.map { it.isRunning() }
|
||||
|
||||
@@ -50,7 +50,7 @@ fun HTSScreen() {
|
||||
@Composable
|
||||
private fun AppBar(state: HTSViewState, navigateUp: () -> Unit, viewModel: HTSViewModel) {
|
||||
val toolbarName = (state.htsManagerState as? WorkingState)?.let {
|
||||
(it.result as? SuccessResult<HTSData>)?.deviceName()
|
||||
(it.result as? DeviceHolder)?.deviceName()
|
||||
}
|
||||
|
||||
if (toolbarName == null) {
|
||||
|
||||
@@ -29,7 +29,7 @@ class PRXRepository @Inject internal constructor(
|
||||
private var manager: PRXManager? = null
|
||||
private var logger: ToolboxLogger? = null
|
||||
|
||||
private val _data = MutableStateFlow<BleManagerResult<PRXData>>(ConnectingResult())
|
||||
private val _data = MutableStateFlow<BleManagerResult<PRXData>>(IdleResult())
|
||||
internal val data = _data.asStateFlow()
|
||||
|
||||
val isRunning = data.map { it.isRunning() }
|
||||
|
||||
@@ -51,7 +51,7 @@ fun PRXScreen() {
|
||||
@Composable
|
||||
private fun AppBar(state: PRXViewState, navigateUp: () -> Unit, viewModel: PRXViewModel) {
|
||||
val toolbarName = (state as? WorkingState)?.let {
|
||||
(it.result as? SuccessResult<PRXData>)?.deviceName()
|
||||
(it.result as? DeviceHolder)?.deviceName()
|
||||
}
|
||||
|
||||
if (toolbarName == null) {
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package no.nordicsemi.android.rscs.repository
|
||||
|
||||
import android.bluetooth.BluetoothDevice
|
||||
import android.content.Context
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
@@ -12,7 +11,7 @@ import no.nordicsemi.android.logger.ToolboxLoggerFactory
|
||||
import no.nordicsemi.android.rscs.data.RSCSData
|
||||
import no.nordicsemi.android.rscs.data.RSCSManager
|
||||
import no.nordicsemi.android.service.BleManagerResult
|
||||
import no.nordicsemi.android.service.ConnectingResult
|
||||
import no.nordicsemi.android.service.IdleResult
|
||||
import no.nordicsemi.android.service.ServiceManager
|
||||
import no.nordicsemi.ui.scanner.DiscoveredBluetoothDevice
|
||||
import javax.inject.Inject
|
||||
@@ -28,7 +27,7 @@ class RSCSRepository @Inject constructor(
|
||||
private var manager: RSCSManager? = null
|
||||
private var logger: ToolboxLogger? = null
|
||||
|
||||
private val _data = MutableStateFlow<BleManagerResult<RSCSData>>(ConnectingResult())
|
||||
private val _data = MutableStateFlow<BleManagerResult<RSCSData>>(IdleResult())
|
||||
internal val data = _data.asStateFlow()
|
||||
|
||||
val isRunning = data.map { it.isRunning() }
|
||||
|
||||
@@ -50,7 +50,7 @@ fun RSCSScreen() {
|
||||
@Composable
|
||||
private fun AppBar(state: RSCSViewState, navigateUp: () -> Unit, viewModel: RSCSViewModel) {
|
||||
val toolbarName = (state as? WorkingState)?.let {
|
||||
(it.result as? SuccessResult<RSCSData>)?.deviceName()
|
||||
(it.result as? DeviceHolder)?.deviceName()
|
||||
}
|
||||
|
||||
if (toolbarName == null) {
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package no.nordicsemi.android.uart.repository
|
||||
|
||||
import android.bluetooth.BluetoothDevice
|
||||
import android.content.Context
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
|
||||
@@ -51,7 +51,7 @@ fun UARTScreen() {
|
||||
@Composable
|
||||
private fun AppBar(state: UARTViewState, navigateUp: () -> Unit, onEvent: (UARTViewEvent) -> Unit) {
|
||||
val toolbarName = (state.uartManagerState as? WorkingState)?.let {
|
||||
(it.result as? SuccessResult<UARTData>)?.deviceName()
|
||||
(it.result as? DeviceHolder)?.deviceName()
|
||||
}
|
||||
|
||||
if (toolbarName == null) {
|
||||
|
||||
Reference in New Issue
Block a user