mirror of
https://github.com/aljazceru/Android-nRF-Toolbox.git
synced 2026-01-08 09:14:23 +01:00
Change Toolbar to display connected device
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
package no.nordicsemi.android.service
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.bluetooth.BluetoothDevice
|
||||
|
||||
sealed class BleManagerResult <T> {
|
||||
|
||||
fun isRunning(): Boolean {
|
||||
@@ -17,7 +20,11 @@ sealed class BleManagerResult <T> {
|
||||
|
||||
class IdleResult<T> : BleManagerResult<T>()
|
||||
class ConnectingResult<T> : BleManagerResult<T>()
|
||||
data class SuccessResult<T>(val data: T) : BleManagerResult<T>()
|
||||
data class SuccessResult<T>(val device: BluetoothDevice, val data: T) : BleManagerResult<T>() {
|
||||
|
||||
@SuppressLint("MissingPermission")
|
||||
fun deviceName(): String = device.name ?: device.address
|
||||
}
|
||||
|
||||
class LinkLossResult<T>(val data: T) : BleManagerResult<T>()
|
||||
class DisconnectedResult<T> : BleManagerResult<T>()
|
||||
|
||||
@@ -34,7 +34,7 @@ class ConnectionObserverAdapter<T> : ConnectionObserver {
|
||||
|
||||
override fun onDeviceReady(device: BluetoothDevice) {
|
||||
Log.d(TAG, "onDeviceReady()")
|
||||
_status.value = SuccessResult(lastValue!!)
|
||||
_status.value = SuccessResult(device, lastValue!!)
|
||||
}
|
||||
|
||||
override fun onDeviceDisconnecting(device: BluetoothDevice) {
|
||||
@@ -53,8 +53,8 @@ class ConnectionObserverAdapter<T> : ConnectionObserver {
|
||||
|
||||
fun setValue(value: T) {
|
||||
lastValue = value
|
||||
if (_status.value.isRunning()) {
|
||||
_status.value = SuccessResult(value)
|
||||
(_status.value as? SuccessResult)?.let {
|
||||
_status.value = SuccessResult(it.device, value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,7 +61,7 @@ internal class BPSManager(
|
||||
val dataHolder = ConnectionObserverAdapter<BPSData>()
|
||||
|
||||
init {
|
||||
setConnectionObserver(dataHolder)
|
||||
connectionObserver = dataHolder
|
||||
|
||||
data.onEach {
|
||||
dataHolder.setValue(it)
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package no.nordicsemi.android.bps.repository
|
||||
|
||||
import android.bluetooth.BluetoothDevice
|
||||
import android.content.Context
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import dagger.hilt.android.scopes.ViewModelScoped
|
||||
|
||||
@@ -15,7 +15,7 @@ import no.nordicsemi.android.bps.R
|
||||
import no.nordicsemi.android.bps.data.BPSData
|
||||
|
||||
@Composable
|
||||
internal fun BPSContentView(state: BPSData, onEvent: (BPSScreenViewEvent) -> Unit) {
|
||||
internal fun BPSContentView(state: BPSData, onEvent: (BPSViewEvent) -> Unit) {
|
||||
Column(
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
modifier = Modifier.padding(16.dp)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package no.nordicsemi.android.bps.view
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
@@ -9,8 +10,10 @@ import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import no.nordicsemi.android.bps.R
|
||||
import no.nordicsemi.android.bps.data.BPSData
|
||||
import no.nordicsemi.android.bps.viewmodel.BPSViewModel
|
||||
import no.nordicsemi.android.service.*
|
||||
import no.nordicsemi.android.theme.view.BackIconAppBar
|
||||
import no.nordicsemi.android.theme.view.LoggerIconAppBar
|
||||
import no.nordicsemi.android.utils.exhaustive
|
||||
import no.nordicsemi.ui.scanner.ui.DeviceConnectingView
|
||||
@@ -26,11 +29,7 @@ fun BPSScreen() {
|
||||
Column {
|
||||
val navigateUp = { viewModel.onEvent(DisconnectEvent) }
|
||||
|
||||
LoggerIconAppBar(stringResource(id = R.string.bps_title), {
|
||||
viewModel.onEvent(DisconnectEvent)
|
||||
}, { viewModel.onEvent(DisconnectEvent) }) {
|
||||
viewModel.onEvent(OpenLoggerEvent)
|
||||
}
|
||||
AppBar(state = state, navigateUp = navigateUp, viewModel = viewModel)
|
||||
|
||||
Column(modifier = Modifier.verticalScroll(rememberScrollState())) {
|
||||
when (state) {
|
||||
@@ -48,3 +47,20 @@ fun BPSScreen() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun AppBar(state: BPSViewState, navigateUp: () -> Unit, viewModel: BPSViewModel) {
|
||||
val toolbarName = (state as? WorkingState)?.let {
|
||||
(it.result as? SuccessResult<BPSData>)?.deviceName()
|
||||
}
|
||||
|
||||
if (toolbarName == null) {
|
||||
BackIconAppBar(stringResource(id = R.string.bps_title), navigateUp)
|
||||
} else {
|
||||
LoggerIconAppBar(toolbarName, {
|
||||
viewModel.onEvent(DisconnectEvent)
|
||||
}, { viewModel.onEvent(DisconnectEvent) }) {
|
||||
viewModel.onEvent(OpenLoggerEvent)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
package no.nordicsemi.android.bps.view
|
||||
|
||||
internal sealed class BPSScreenViewEvent
|
||||
|
||||
internal object DisconnectEvent : BPSScreenViewEvent()
|
||||
|
||||
internal object OpenLoggerEvent : BPSScreenViewEvent()
|
||||
@@ -0,0 +1,7 @@
|
||||
package no.nordicsemi.android.bps.view
|
||||
|
||||
internal sealed class BPSViewEvent
|
||||
|
||||
internal object DisconnectEvent : BPSViewEvent()
|
||||
|
||||
internal object OpenLoggerEvent : BPSViewEvent()
|
||||
@@ -2,6 +2,7 @@ package no.nordicsemi.android.bps.view
|
||||
|
||||
import no.nordicsemi.android.bps.data.BPSData
|
||||
import no.nordicsemi.android.service.BleManagerResult
|
||||
import no.nordicsemi.ui.scanner.DiscoveredBluetoothDevice
|
||||
|
||||
internal sealed class BPSViewState
|
||||
|
||||
|
||||
@@ -43,7 +43,7 @@ internal class BPSViewModel @Inject constructor(
|
||||
}.exhaustive
|
||||
}
|
||||
|
||||
fun onEvent(event: BPSScreenViewEvent) {
|
||||
fun onEvent(event: BPSViewEvent) {
|
||||
when (event) {
|
||||
DisconnectEvent -> navigationManager.navigateUp()
|
||||
OpenLoggerEvent -> repository.openLogger()
|
||||
|
||||
@@ -85,7 +85,7 @@ internal class CGMManager(
|
||||
val dataHolder = ConnectionObserverAdapter<CGMData>()
|
||||
|
||||
init {
|
||||
setConnectionObserver(dataHolder)
|
||||
connectionObserver = dataHolder
|
||||
|
||||
data.onEach {
|
||||
dataHolder.setValue(it)
|
||||
|
||||
@@ -9,8 +9,10 @@ import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import no.nordicsemi.android.cgms.R
|
||||
import no.nordicsemi.android.cgms.viewmodel.CGMScreenViewModel
|
||||
import no.nordicsemi.android.cgms.data.CGMData
|
||||
import no.nordicsemi.android.cgms.viewmodel.CGMViewModel
|
||||
import no.nordicsemi.android.service.*
|
||||
import no.nordicsemi.android.theme.view.BackIconAppBar
|
||||
import no.nordicsemi.android.theme.view.LoggerIconAppBar
|
||||
import no.nordicsemi.android.utils.exhaustive
|
||||
import no.nordicsemi.ui.scanner.ui.DeviceConnectingView
|
||||
@@ -20,15 +22,13 @@ import no.nordicsemi.ui.scanner.ui.Reason
|
||||
|
||||
@Composable
|
||||
fun CGMScreen() {
|
||||
val viewModel: CGMScreenViewModel = hiltViewModel()
|
||||
val viewModel: CGMViewModel = hiltViewModel()
|
||||
val state = viewModel.state.collectAsState().value
|
||||
|
||||
Column {
|
||||
val navigateUp = { viewModel.onEvent(NavigateUp) }
|
||||
|
||||
LoggerIconAppBar(stringResource(id = R.string.cgms_title), navigateUp, { viewModel.onEvent(DisconnectEvent)}) {
|
||||
viewModel.onEvent(OpenLoggerEvent)
|
||||
}
|
||||
AppBar(state, navigateUp, viewModel)
|
||||
|
||||
Column(modifier = Modifier.verticalScroll(rememberScrollState())) {
|
||||
when (state) {
|
||||
@@ -46,3 +46,18 @@ fun CGMScreen() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun AppBar(state: CGMViewState, navigateUp: () -> Unit, viewModel: CGMViewModel) {
|
||||
val toolbarName = (state as? WorkingState)?.let {
|
||||
(it.result as? SuccessResult<CGMData>)?.deviceName()
|
||||
}
|
||||
|
||||
if (toolbarName == null) {
|
||||
BackIconAppBar(stringResource(id = R.string.cgms_title), navigateUp)
|
||||
} else {
|
||||
LoggerIconAppBar(toolbarName, navigateUp, { viewModel.onEvent(DisconnectEvent) }) {
|
||||
viewModel.onEvent(OpenLoggerEvent)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package no.nordicsemi.android.cgms.view
|
||||
import no.nordicsemi.android.cgms.data.CGMData
|
||||
import no.nordicsemi.android.service.BleManagerResult
|
||||
|
||||
internal sealed class BPSViewState
|
||||
internal sealed class CGMViewState
|
||||
|
||||
internal data class WorkingState(val result: BleManagerResult<CGMData>) : BPSViewState()
|
||||
internal object NoDeviceState : BPSViewState()
|
||||
internal data class WorkingState(val result: BleManagerResult<CGMData>) : CGMViewState()
|
||||
internal object NoDeviceState : CGMViewState()
|
||||
|
||||
@@ -16,12 +16,12 @@ import no.nordicsemi.ui.scanner.ScannerDestinationId
|
||||
import javax.inject.Inject
|
||||
|
||||
@HiltViewModel
|
||||
internal class CGMScreenViewModel @Inject constructor(
|
||||
internal class CGMViewModel @Inject constructor(
|
||||
private val repository: CGMRepository,
|
||||
private val navigationManager: NavigationManager
|
||||
) : ViewModel() {
|
||||
|
||||
private val _state = MutableStateFlow<BPSViewState>(NoDeviceState)
|
||||
private val _state = MutableStateFlow<CGMViewState>(NoDeviceState)
|
||||
val state = _state.asStateFlow()
|
||||
|
||||
init {
|
||||
@@ -59,7 +59,7 @@ internal class CSCManager(
|
||||
val dataHolder = ConnectionObserverAdapter<CSCData>()
|
||||
|
||||
init {
|
||||
setConnectionObserver(dataHolder)
|
||||
connectionObserver = dataHolder
|
||||
|
||||
data.onEach {
|
||||
dataHolder.setValue(it)
|
||||
|
||||
@@ -9,8 +9,10 @@ import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import no.nordicsemi.android.csc.R
|
||||
import no.nordicsemi.android.csc.data.CSCData
|
||||
import no.nordicsemi.android.csc.viewmodel.CSCViewModel
|
||||
import no.nordicsemi.android.service.*
|
||||
import no.nordicsemi.android.theme.view.BackIconAppBar
|
||||
import no.nordicsemi.android.theme.view.LoggerIconAppBar
|
||||
import no.nordicsemi.android.utils.exhaustive
|
||||
import no.nordicsemi.ui.scanner.ui.DeviceConnectingView
|
||||
@@ -26,9 +28,7 @@ fun CSCScreen() {
|
||||
Column {
|
||||
val navigateUp = { viewModel.onEvent(NavigateUp) }
|
||||
|
||||
LoggerIconAppBar(stringResource(id = R.string.csc_title), navigateUp, { viewModel.onEvent(OnDisconnectButtonClick) }) {
|
||||
viewModel.onEvent(OpenLogger)
|
||||
}
|
||||
AppBar(state, navigateUp, viewModel)
|
||||
|
||||
Column(modifier = Modifier.verticalScroll(rememberScrollState())) {
|
||||
when (state.cscManagerState) {
|
||||
@@ -46,3 +46,18 @@ 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()
|
||||
}
|
||||
|
||||
if (toolbarName == null) {
|
||||
BackIconAppBar(stringResource(id = R.string.csc_title), navigateUp)
|
||||
} else {
|
||||
LoggerIconAppBar(toolbarName, navigateUp, { viewModel.onEvent(OnDisconnectButtonClick) }) {
|
||||
viewModel.onEvent(OpenLogger)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,7 +71,7 @@ internal class GLSManager(
|
||||
val dataHolder = ConnectionObserverAdapter<GLSData>()
|
||||
|
||||
init {
|
||||
setConnectionObserver(dataHolder)
|
||||
connectionObserver = dataHolder
|
||||
|
||||
data.onEach {
|
||||
dataHolder.setValue(it)
|
||||
|
||||
@@ -9,8 +9,10 @@ import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import no.nordicsemi.android.gls.R
|
||||
import no.nordicsemi.android.gls.data.GLSData
|
||||
import no.nordicsemi.android.gls.main.viewmodel.GLSViewModel
|
||||
import no.nordicsemi.android.service.*
|
||||
import no.nordicsemi.android.theme.view.BackIconAppBar
|
||||
import no.nordicsemi.android.theme.view.LoggerIconAppBar
|
||||
import no.nordicsemi.android.utils.exhaustive
|
||||
import no.nordicsemi.ui.scanner.ui.DeviceConnectingView
|
||||
@@ -26,11 +28,7 @@ fun GLSScreen() {
|
||||
Column {
|
||||
val navigateUp = { viewModel.onEvent(DisconnectEvent) }
|
||||
|
||||
LoggerIconAppBar(stringResource(id = R.string.gls_title), {
|
||||
viewModel.onEvent(DisconnectEvent)
|
||||
}, { viewModel.onEvent(DisconnectEvent) }) {
|
||||
viewModel.onEvent(OpenLoggerEvent)
|
||||
}
|
||||
AppBar(state, navigateUp, viewModel)
|
||||
|
||||
Column(modifier = Modifier.verticalScroll(rememberScrollState())) {
|
||||
when (state) {
|
||||
@@ -48,3 +46,20 @@ fun GLSScreen() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun AppBar(state: GLSViewState, navigateUp: () -> Unit, viewModel: GLSViewModel) {
|
||||
val toolbarName = (state as? WorkingState)?.let {
|
||||
(it.result as? SuccessResult<GLSData>)?.deviceName()
|
||||
}
|
||||
|
||||
if (toolbarName == null) {
|
||||
BackIconAppBar(stringResource(id = R.string.gls_title), navigateUp)
|
||||
} else {
|
||||
LoggerIconAppBar(toolbarName, {
|
||||
viewModel.onEvent(DisconnectEvent)
|
||||
}, { viewModel.onEvent(DisconnectEvent) }) {
|
||||
viewModel.onEvent(OpenLoggerEvent)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package no.nordicsemi.android.gls.main.view
|
||||
import no.nordicsemi.android.gls.data.GLSData
|
||||
import no.nordicsemi.android.service.BleManagerResult
|
||||
|
||||
internal sealed class BPSViewState
|
||||
internal sealed class GLSViewState
|
||||
|
||||
internal data class WorkingState(val result: BleManagerResult<GLSData>) : BPSViewState()
|
||||
internal object NoDeviceState : BPSViewState()
|
||||
internal data class WorkingState(val result: BleManagerResult<GLSData>) : GLSViewState()
|
||||
internal object NoDeviceState : GLSViewState()
|
||||
|
||||
@@ -21,7 +21,7 @@ internal class GLSViewModel @Inject constructor(
|
||||
private val navigationManager: NavigationManager
|
||||
) : ViewModel() {
|
||||
|
||||
private val _state = MutableStateFlow<BPSViewState>(NoDeviceState)
|
||||
private val _state = MutableStateFlow<GLSViewState>(NoDeviceState)
|
||||
val state = _state.asStateFlow()
|
||||
|
||||
init {
|
||||
|
||||
@@ -61,7 +61,7 @@ internal class HRSManager(
|
||||
val dataHolder = ConnectionObserverAdapter<HRSData>()
|
||||
|
||||
init {
|
||||
setConnectionObserver(dataHolder)
|
||||
connectionObserver = dataHolder
|
||||
|
||||
data.onEach {
|
||||
dataHolder.setValue(it)
|
||||
|
||||
@@ -9,8 +9,10 @@ import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import no.nordicsemi.android.hrs.R
|
||||
import no.nordicsemi.android.hrs.data.HRSData
|
||||
import no.nordicsemi.android.hrs.viewmodel.HRSViewModel
|
||||
import no.nordicsemi.android.service.*
|
||||
import no.nordicsemi.android.theme.view.BackIconAppBar
|
||||
import no.nordicsemi.android.theme.view.LoggerIconAppBar
|
||||
import no.nordicsemi.android.utils.exhaustive
|
||||
import no.nordicsemi.ui.scanner.ui.DeviceConnectingView
|
||||
@@ -26,9 +28,7 @@ fun HRSScreen() {
|
||||
Column {
|
||||
val navigateUp = { viewModel.onEvent(NavigateUpEvent) }
|
||||
|
||||
LoggerIconAppBar(stringResource(id = R.string.hrs_title), navigateUp, { viewModel.onEvent(DisconnectEvent) }) {
|
||||
viewModel.onEvent(OpenLoggerEvent)
|
||||
}
|
||||
AppBar(state, navigateUp, viewModel)
|
||||
|
||||
Column(modifier = Modifier.verticalScroll(rememberScrollState())) {
|
||||
when (state) {
|
||||
@@ -46,3 +46,18 @@ fun HRSScreen() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun AppBar(state: HRSViewState, navigateUp: () -> Unit, viewModel: HRSViewModel) {
|
||||
val toolbarName = (state as? WorkingState)?.let {
|
||||
(it.result as? SuccessResult<HRSData>)?.deviceName()
|
||||
}
|
||||
|
||||
if (toolbarName == null) {
|
||||
BackIconAppBar(stringResource(id = R.string.hrs_title), navigateUp)
|
||||
} else {
|
||||
LoggerIconAppBar(toolbarName, navigateUp, { viewModel.onEvent(DisconnectEvent) }) {
|
||||
viewModel.onEvent(OpenLoggerEvent)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ internal class HTSManager internal constructor(
|
||||
val dataHolder = ConnectionObserverAdapter<HTSData>()
|
||||
|
||||
init {
|
||||
setConnectionObserver(dataHolder)
|
||||
connectionObserver = dataHolder
|
||||
|
||||
data.onEach {
|
||||
dataHolder.setValue(it)
|
||||
|
||||
@@ -9,8 +9,10 @@ import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import no.nordicsemi.android.hts.R
|
||||
import no.nordicsemi.android.hts.data.HTSData
|
||||
import no.nordicsemi.android.hts.viewmodel.HTSViewModel
|
||||
import no.nordicsemi.android.service.*
|
||||
import no.nordicsemi.android.theme.view.BackIconAppBar
|
||||
import no.nordicsemi.android.theme.view.LoggerIconAppBar
|
||||
import no.nordicsemi.android.utils.exhaustive
|
||||
import no.nordicsemi.ui.scanner.ui.DeviceConnectingView
|
||||
@@ -26,9 +28,7 @@ fun HTSScreen() {
|
||||
Column {
|
||||
val navigateUp = { viewModel.onEvent(NavigateUp) }
|
||||
|
||||
LoggerIconAppBar(stringResource(id = R.string.hts_title), navigateUp, { viewModel.onEvent(DisconnectEvent) }) {
|
||||
viewModel.onEvent(OpenLoggerEvent)
|
||||
}
|
||||
AppBar(state, navigateUp, viewModel)
|
||||
|
||||
Column(modifier = Modifier.verticalScroll(rememberScrollState())) {
|
||||
when (state.htsManagerState) {
|
||||
@@ -46,3 +46,19 @@ 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()
|
||||
}
|
||||
|
||||
if (toolbarName == null) {
|
||||
BackIconAppBar(stringResource(id = R.string.hts_title), navigateUp)
|
||||
} else {
|
||||
LoggerIconAppBar(toolbarName, navigateUp, { viewModel.onEvent(DisconnectEvent) }) {
|
||||
viewModel.onEvent(OpenLoggerEvent)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -68,7 +68,7 @@ internal class PRXManager(
|
||||
val dataHolder = ConnectionObserverAdapter<PRXData>()
|
||||
|
||||
init {
|
||||
setConnectionObserver(dataHolder)
|
||||
connectionObserver = dataHolder
|
||||
|
||||
data.onEach {
|
||||
dataHolder.setValue(it)
|
||||
|
||||
@@ -10,8 +10,10 @@ import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import no.nordicsemi.android.prx.R
|
||||
import no.nordicsemi.android.prx.data.PRXData
|
||||
import no.nordicsemi.android.prx.viewmodel.PRXViewModel
|
||||
import no.nordicsemi.android.service.*
|
||||
import no.nordicsemi.android.theme.view.BackIconAppBar
|
||||
import no.nordicsemi.android.theme.view.LoggerIconAppBar
|
||||
import no.nordicsemi.android.utils.exhaustive
|
||||
import no.nordicsemi.ui.scanner.ui.DeviceConnectingView
|
||||
@@ -27,9 +29,7 @@ fun PRXScreen() {
|
||||
Column(horizontalAlignment = Alignment.CenterHorizontally) {
|
||||
val navigateUp = { viewModel.onEvent(NavigateUpEvent) }
|
||||
|
||||
LoggerIconAppBar(stringResource(id = R.string.prx_title), navigateUp, { viewModel.onEvent(DisconnectEvent) }) {
|
||||
viewModel.onEvent(OpenLoggerEvent)
|
||||
}
|
||||
AppBar(state, navigateUp, viewModel)
|
||||
|
||||
Column(modifier = Modifier.verticalScroll(rememberScrollState())) {
|
||||
when (state) {
|
||||
@@ -47,3 +47,18 @@ fun PRXScreen() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun AppBar(state: PRXViewState, navigateUp: () -> Unit, viewModel: PRXViewModel) {
|
||||
val toolbarName = (state as? WorkingState)?.let {
|
||||
(it.result as? SuccessResult<PRXData>)?.deviceName()
|
||||
}
|
||||
|
||||
if (toolbarName == null) {
|
||||
BackIconAppBar(stringResource(id = R.string.prx_title), navigateUp)
|
||||
} else {
|
||||
LoggerIconAppBar(toolbarName, navigateUp, { viewModel.onEvent(DisconnectEvent) }) {
|
||||
viewModel.onEvent(OpenLoggerEvent)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ internal class RSCSManager internal constructor(
|
||||
val dataHolder = ConnectionObserverAdapter<RSCSData>()
|
||||
|
||||
init {
|
||||
setConnectionObserver(dataHolder)
|
||||
connectionObserver = dataHolder
|
||||
|
||||
data.onEach {
|
||||
dataHolder.setValue(it)
|
||||
|
||||
@@ -9,8 +9,10 @@ import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import no.nordicsemi.android.rscs.R
|
||||
import no.nordicsemi.android.rscs.data.RSCSData
|
||||
import no.nordicsemi.android.rscs.viewmodel.RSCSViewModel
|
||||
import no.nordicsemi.android.service.*
|
||||
import no.nordicsemi.android.theme.view.BackIconAppBar
|
||||
import no.nordicsemi.android.theme.view.LoggerIconAppBar
|
||||
import no.nordicsemi.android.utils.exhaustive
|
||||
import no.nordicsemi.ui.scanner.ui.DeviceConnectingView
|
||||
@@ -26,9 +28,7 @@ fun RSCSScreen() {
|
||||
Column {
|
||||
val navigateUp = { viewModel.onEvent(NavigateUpEvent) }
|
||||
|
||||
LoggerIconAppBar(stringResource(id = R.string.rscs_title), navigateUp, { viewModel.onEvent(DisconnectEvent) }) {
|
||||
viewModel.onEvent(OpenLoggerEvent)
|
||||
}
|
||||
AppBar(state, navigateUp, viewModel)
|
||||
|
||||
Column(modifier = Modifier.verticalScroll(rememberScrollState())) {
|
||||
when (state) {
|
||||
@@ -46,3 +46,18 @@ fun RSCSScreen() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun AppBar(state: RSCSViewState, navigateUp: () -> Unit, viewModel: RSCSViewModel) {
|
||||
val toolbarName = (state as? WorkingState)?.let {
|
||||
(it.result as? SuccessResult<RSCSData>)?.deviceName()
|
||||
}
|
||||
|
||||
if (toolbarName == null) {
|
||||
BackIconAppBar(stringResource(id = R.string.rscs_title), navigateUp)
|
||||
} else {
|
||||
LoggerIconAppBar(toolbarName, navigateUp, { viewModel.onEvent(DisconnectEvent) }) {
|
||||
viewModel.onEvent(OpenLoggerEvent)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ internal class UARTManager(
|
||||
val dataHolder = ConnectionObserverAdapter<UARTData>()
|
||||
|
||||
init {
|
||||
setConnectionObserver(dataHolder)
|
||||
connectionObserver = dataHolder
|
||||
|
||||
data.onEach {
|
||||
dataHolder.setValue(it)
|
||||
|
||||
@@ -32,8 +32,6 @@ class UARTRepository @Inject internal constructor(
|
||||
private val _data = MutableStateFlow<BleManagerResult<UARTData>>(IdleResult())
|
||||
internal val data = _data.asStateFlow()
|
||||
|
||||
var device: DiscoveredBluetoothDevice? = null
|
||||
|
||||
val isRunning = data.map { it.isRunning() }
|
||||
val hasBeenDisconnected = data.map { it.hasBeenDisconnected() }
|
||||
|
||||
@@ -44,7 +42,6 @@ class UARTRepository @Inject internal constructor(
|
||||
}
|
||||
|
||||
fun start(device: DiscoveredBluetoothDevice, scope: CoroutineScope) {
|
||||
this.device = device
|
||||
val createdLogger = toolboxLoggerFactory.create("UART", device.address()).also {
|
||||
logger = it
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package no.nordicsemi.android.uart.view
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
@@ -34,44 +33,33 @@ fun UARTScreen() {
|
||||
|
||||
AppBar(state = state, navigateUp = navigateUp) { viewModel.onEvent(it) }
|
||||
|
||||
Column(modifier = Modifier) {
|
||||
when (state.uartManagerState) {
|
||||
NoDeviceState -> NoDeviceView()
|
||||
is WorkingState -> when (state.uartManagerState.result) {
|
||||
is IdleResult,
|
||||
is ConnectingResult -> Scroll { DeviceConnectingView { viewModel.onEvent(DisconnectEvent) } }
|
||||
is DisconnectedResult -> Scroll { DeviceDisconnectedView(Reason.USER, navigateUp) }
|
||||
is LinkLossResult -> Scroll { DeviceDisconnectedView(Reason.LINK_LOSS, navigateUp) }
|
||||
is MissingServiceResult -> Scroll { DeviceDisconnectedView(Reason.MISSING_SERVICE, navigateUp) }
|
||||
is UnknownErrorResult -> Scroll { DeviceDisconnectedView(Reason.UNKNOWN, navigateUp) }
|
||||
is SuccessResult -> SuccessScreen(state.uartManagerState.result.data, state, viewModel)
|
||||
}
|
||||
}.exhaustive
|
||||
}
|
||||
when (state.uartManagerState) {
|
||||
NoDeviceState -> NoDeviceView()
|
||||
is WorkingState -> when (state.uartManagerState.result) {
|
||||
is IdleResult,
|
||||
is ConnectingResult -> Scroll { DeviceConnectingView { viewModel.onEvent(DisconnectEvent) } }
|
||||
is DisconnectedResult -> Scroll { DeviceDisconnectedView(Reason.USER, navigateUp) }
|
||||
is LinkLossResult -> Scroll { DeviceDisconnectedView(Reason.LINK_LOSS, navigateUp) }
|
||||
is MissingServiceResult -> Scroll { DeviceDisconnectedView(Reason.MISSING_SERVICE, navigateUp) }
|
||||
is UnknownErrorResult -> Scroll { DeviceDisconnectedView(Reason.UNKNOWN, navigateUp) }
|
||||
is SuccessResult -> SuccessScreen(state.uartManagerState.result.data, state, viewModel)
|
||||
}
|
||||
}.exhaustive
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressLint("MissingPermission")
|
||||
@Composable
|
||||
private fun AppBar(state: UARTViewState, navigateUp: () -> Unit, onEvent: (UARTViewEvent) -> Unit) {
|
||||
Column(modifier = Modifier) {
|
||||
when (state.uartManagerState) {
|
||||
NoDeviceState -> BackIconAppBar(stringResource(id = R.string.uart_title), navigateUp)
|
||||
is WorkingState -> when (state.uartManagerState.result) {
|
||||
is IdleResult,
|
||||
is DisconnectedResult,
|
||||
is LinkLossResult,
|
||||
is MissingServiceResult,
|
||||
is UnknownErrorResult -> BackIconAppBar(stringResource(id = R.string.uart_title), navigateUp)
|
||||
is ConnectingResult,
|
||||
is SuccessResult -> {
|
||||
val text = state.uartManagerState.device.nameOrAddress()
|
||||
LoggerIconAppBar(text, navigateUp, { onEvent(DisconnectEvent) }) {
|
||||
onEvent(OpenLogger)
|
||||
}
|
||||
}
|
||||
}
|
||||
}.exhaustive
|
||||
val toolbarName = (state.uartManagerState as? WorkingState)?.let {
|
||||
(it.result as? SuccessResult<UARTData>)?.deviceName()
|
||||
}
|
||||
|
||||
if (toolbarName == null) {
|
||||
BackIconAppBar(stringResource(id = R.string.uart_title), navigateUp)
|
||||
} else {
|
||||
LoggerIconAppBar(toolbarName, navigateUp, { onEvent(DisconnectEvent) }) {
|
||||
onEvent(OpenLogger)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
package no.nordicsemi.android.uart.view
|
||||
|
||||
import android.bluetooth.BluetoothDevice
|
||||
import no.nordicsemi.android.service.BleManagerResult
|
||||
import no.nordicsemi.android.uart.data.UARTConfiguration
|
||||
import no.nordicsemi.android.uart.data.UARTData
|
||||
import no.nordicsemi.android.uart.data.UARTMacro
|
||||
import no.nordicsemi.ui.scanner.DiscoveredBluetoothDevice
|
||||
|
||||
internal data class UARTViewState(
|
||||
val editedPosition: Int? = null,
|
||||
@@ -29,7 +27,6 @@ internal data class UARTViewState(
|
||||
internal sealed class HTSManagerState
|
||||
|
||||
internal data class WorkingState(
|
||||
val device: DiscoveredBluetoothDevice,
|
||||
val result: BleManagerResult<UARTData>
|
||||
) : HTSManagerState()
|
||||
|
||||
|
||||
@@ -40,7 +40,7 @@ internal class UARTViewModel @Inject constructor(
|
||||
if (it is IdleResult) {
|
||||
return@onEach
|
||||
}
|
||||
_state.value = _state.value.copy(uartManagerState = WorkingState(repository.device!!, it))
|
||||
_state.value = _state.value.copy(uartManagerState = WorkingState(it))
|
||||
}.launchIn(viewModelScope)
|
||||
|
||||
dataSource.getConfigurations().onEach {
|
||||
|
||||
Reference in New Issue
Block a user