mirror of
https://github.com/aljazceru/Android-nRF-Toolbox.git
synced 2025-12-19 15:34:26 +01:00
Properly propagade states for viewmodel based profiles.
This commit is contained in:
@@ -3,11 +3,14 @@ package no.nordicsemi.android.bps.repository
|
||||
import android.content.Context
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import dagger.hilt.android.scopes.ViewModelScoped
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.channels.awaitClose
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.callbackFlow
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import kotlinx.coroutines.launch
|
||||
import no.nordicsemi.android.ble.ktx.suspend
|
||||
import no.nordicsemi.android.bps.data.BPSData
|
||||
import no.nordicsemi.android.bps.data.BPSManager
|
||||
import no.nordicsemi.android.logger.ToolboxLogger
|
||||
@@ -25,8 +28,7 @@ internal class BPSRepository @Inject constructor(
|
||||
|
||||
private var logger: ToolboxLogger? = null
|
||||
|
||||
fun downloadData(device: DiscoveredBluetoothDevice): Flow<BleManagerResult<BPSData>> = callbackFlow {
|
||||
val scope = this
|
||||
fun downloadData(scope: CoroutineScope, device: DiscoveredBluetoothDevice): Flow<BleManagerResult<BPSData>> = callbackFlow {
|
||||
val createdLogger = toolboxLoggerFactory.create("BPS", device.address()).also {
|
||||
logger = it
|
||||
}
|
||||
@@ -36,10 +38,9 @@ internal class BPSRepository @Inject constructor(
|
||||
trySend(it)
|
||||
}.launchIn(scope)
|
||||
|
||||
manager.connect(device.device)
|
||||
.useAutoConnect(false)
|
||||
.retry(3, 100)
|
||||
.enqueue()
|
||||
scope.launch {
|
||||
manager.start(device)
|
||||
}
|
||||
|
||||
awaitClose {
|
||||
manager.disconnect().enqueue()
|
||||
@@ -47,6 +48,17 @@ internal class BPSRepository @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun BPSManager.start(device: DiscoveredBluetoothDevice) {
|
||||
try {
|
||||
connect(device.device)
|
||||
.useAutoConnect(false)
|
||||
.retry(3, 100)
|
||||
.suspend()
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
fun openLogger() {
|
||||
logger?.openLogger()
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ internal class BPSViewModel @Inject constructor(
|
||||
}
|
||||
|
||||
private fun connectDevice(device: DiscoveredBluetoothDevice) {
|
||||
repository.downloadData(device).onEach {
|
||||
repository.downloadData(viewModelScope, device).onEach {
|
||||
_state.value = WorkingState(it)
|
||||
|
||||
(it as? SuccessResult)?.let {
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package no.nordicsemi.android.csc.repository
|
||||
|
||||
import android.bluetooth.BluetoothDevice
|
||||
import android.content.Intent
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package no.nordicsemi.android.gls.main.view
|
||||
|
||||
import android.util.Log
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package no.nordicsemi.android.gls.main.viewmodel
|
||||
|
||||
import android.util.Log
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
@@ -57,7 +58,7 @@ internal class GLSViewModel @Inject constructor(
|
||||
}
|
||||
|
||||
private fun connectDevice(device: DiscoveredBluetoothDevice) {
|
||||
repository.downloadData(device).onEach {
|
||||
repository.downloadData(viewModelScope, device).onEach {
|
||||
_state.value = WorkingState(it)
|
||||
|
||||
(it as? SuccessResult)?.let {
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
package no.nordicsemi.android.gls.repository
|
||||
|
||||
import android.bluetooth.BluetoothDevice
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import dagger.hilt.android.scopes.ViewModelScoped
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.channels.awaitClose
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.callbackFlow
|
||||
@@ -31,27 +32,21 @@ internal class GLSRepository @Inject constructor(
|
||||
private var manager: GLSManager? = null
|
||||
private var logger: ToolboxLogger? = null
|
||||
|
||||
fun downloadData(device: DiscoveredBluetoothDevice): Flow<BleManagerResult<GLSData>> = callbackFlow {
|
||||
val scope = this
|
||||
fun downloadData(scope: CoroutineScope, device: DiscoveredBluetoothDevice): Flow<BleManagerResult<GLSData>> = callbackFlow {
|
||||
val createdLogger = toolboxLoggerFactory.create("GLS", device.address()).also {
|
||||
logger = it
|
||||
}
|
||||
val managerInstance = manager ?: GLSManager(context, scope, createdLogger).apply {
|
||||
try {
|
||||
connect(device.device)
|
||||
.useAutoConnect(false)
|
||||
.retry(3, 100)
|
||||
.suspend()
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
val managerInstance = manager ?: GLSManager(context, scope, createdLogger)
|
||||
manager = managerInstance
|
||||
|
||||
managerInstance.dataHolder.status.onEach {
|
||||
trySend(it)
|
||||
send(it)
|
||||
}.launchIn(scope)
|
||||
|
||||
scope.launch {
|
||||
managerInstance.start(device)
|
||||
}
|
||||
|
||||
awaitClose {
|
||||
launch {
|
||||
manager?.disconnect()?.suspend()
|
||||
@@ -61,6 +56,17 @@ internal class GLSRepository @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun GLSManager.start(device: DiscoveredBluetoothDevice) {
|
||||
try {
|
||||
connect(device.device)
|
||||
.useAutoConnect(false)
|
||||
.retry(3, 100)
|
||||
.suspend()
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
fun openLogger() {
|
||||
logger?.openLogger()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user