Skip to content

Commit c73f009

Browse files
Properly propagade states for viewmodel based profiles.
1 parent 57a9723 commit c73f009

File tree

6 files changed

+42
-23
lines changed

6 files changed

+42
-23
lines changed

profile_bps/src/main/java/no/nordicsemi/android/bps/repository/BPSRepository.kt

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ package no.nordicsemi.android.bps.repository
33
import android.content.Context
44
import dagger.hilt.android.qualifiers.ApplicationContext
55
import dagger.hilt.android.scopes.ViewModelScoped
6+
import kotlinx.coroutines.CoroutineScope
67
import kotlinx.coroutines.channels.awaitClose
78
import kotlinx.coroutines.flow.Flow
89
import kotlinx.coroutines.flow.callbackFlow
910
import kotlinx.coroutines.flow.launchIn
1011
import kotlinx.coroutines.flow.onEach
12+
import kotlinx.coroutines.launch
13+
import no.nordicsemi.android.ble.ktx.suspend
1114
import no.nordicsemi.android.bps.data.BPSData
1215
import no.nordicsemi.android.bps.data.BPSManager
1316
import no.nordicsemi.android.logger.ToolboxLogger
@@ -25,8 +28,7 @@ internal class BPSRepository @Inject constructor(
2528

2629
private var logger: ToolboxLogger? = null
2730

28-
fun downloadData(device: DiscoveredBluetoothDevice): Flow<BleManagerResult<BPSData>> = callbackFlow {
29-
val scope = this
31+
fun downloadData(scope: CoroutineScope, device: DiscoveredBluetoothDevice): Flow<BleManagerResult<BPSData>> = callbackFlow {
3032
val createdLogger = toolboxLoggerFactory.create("BPS", device.address()).also {
3133
logger = it
3234
}
@@ -36,17 +38,27 @@ internal class BPSRepository @Inject constructor(
3638
trySend(it)
3739
}.launchIn(scope)
3840

39-
manager.connect(device.device)
40-
.useAutoConnect(false)
41-
.retry(3, 100)
42-
.enqueue()
41+
scope.launch {
42+
manager.start(device)
43+
}
4344

4445
awaitClose {
4546
manager.disconnect().enqueue()
4647
logger = null
4748
}
4849
}
4950

51+
private suspend fun BPSManager.start(device: DiscoveredBluetoothDevice) {
52+
try {
53+
connect(device.device)
54+
.useAutoConnect(false)
55+
.retry(3, 100)
56+
.suspend()
57+
} catch (e: Exception) {
58+
e.printStackTrace()
59+
}
60+
}
61+
5062
fun openLogger() {
5163
logger?.openLogger()
5264
}

profile_bps/src/main/java/no/nordicsemi/android/bps/viewmodel/BPSViewModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ internal class BPSViewModel @Inject constructor(
5656
}
5757

5858
private fun connectDevice(device: DiscoveredBluetoothDevice) {
59-
repository.downloadData(device).onEach {
59+
repository.downloadData(viewModelScope, device).onEach {
6060
_state.value = WorkingState(it)
6161

6262
(it as? SuccessResult)?.let {

profile_csc/src/main/java/no/nordicsemi/android/csc/repository/CSCService.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package no.nordicsemi.android.csc.repository
22

3-
import android.bluetooth.BluetoothDevice
43
import android.content.Intent
54
import androidx.lifecycle.lifecycleScope
65
import dagger.hilt.android.AndroidEntryPoint

profile_gls/src/main/java/no/nordicsemi/android/gls/main/view/GLSScreen.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package no.nordicsemi.android.gls.main.view
22

3+
import android.util.Log
34
import androidx.compose.foundation.layout.Column
45
import androidx.compose.foundation.rememberScrollState
56
import androidx.compose.foundation.verticalScroll

profile_gls/src/main/java/no/nordicsemi/android/gls/main/viewmodel/GLSViewModel.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package no.nordicsemi.android.gls.main.viewmodel
22

3+
import android.util.Log
34
import androidx.lifecycle.ViewModel
45
import androidx.lifecycle.viewModelScope
56
import dagger.hilt.android.lifecycle.HiltViewModel
@@ -57,7 +58,7 @@ internal class GLSViewModel @Inject constructor(
5758
}
5859

5960
private fun connectDevice(device: DiscoveredBluetoothDevice) {
60-
repository.downloadData(device).onEach {
61+
repository.downloadData(viewModelScope, device).onEach {
6162
_state.value = WorkingState(it)
6263

6364
(it as? SuccessResult)?.let {

profile_gls/src/main/java/no/nordicsemi/android/gls/repository/GLSRepository.kt

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package no.nordicsemi.android.gls.repository
22

3-
import android.bluetooth.BluetoothDevice
43
import android.content.Context
4+
import android.util.Log
55
import dagger.hilt.android.qualifiers.ApplicationContext
66
import dagger.hilt.android.scopes.ViewModelScoped
7+
import kotlinx.coroutines.CoroutineScope
78
import kotlinx.coroutines.channels.awaitClose
89
import kotlinx.coroutines.flow.Flow
910
import kotlinx.coroutines.flow.callbackFlow
@@ -31,27 +32,21 @@ internal class GLSRepository @Inject constructor(
3132
private var manager: GLSManager? = null
3233
private var logger: ToolboxLogger? = null
3334

34-
fun downloadData(device: DiscoveredBluetoothDevice): Flow<BleManagerResult<GLSData>> = callbackFlow {
35-
val scope = this
35+
fun downloadData(scope: CoroutineScope, device: DiscoveredBluetoothDevice): Flow<BleManagerResult<GLSData>> = callbackFlow {
3636
val createdLogger = toolboxLoggerFactory.create("GLS", device.address()).also {
3737
logger = it
3838
}
39-
val managerInstance = manager ?: GLSManager(context, scope, createdLogger).apply {
40-
try {
41-
connect(device.device)
42-
.useAutoConnect(false)
43-
.retry(3, 100)
44-
.suspend()
45-
} catch (e: Exception) {
46-
e.printStackTrace()
47-
}
48-
}
39+
val managerInstance = manager ?: GLSManager(context, scope, createdLogger)
4940
manager = managerInstance
5041

5142
managerInstance.dataHolder.status.onEach {
52-
trySend(it)
43+
send(it)
5344
}.launchIn(scope)
5445

46+
scope.launch {
47+
managerInstance.start(device)
48+
}
49+
5550
awaitClose {
5651
launch {
5752
manager?.disconnect()?.suspend()
@@ -61,6 +56,17 @@ internal class GLSRepository @Inject constructor(
6156
}
6257
}
6358

59+
private suspend fun GLSManager.start(device: DiscoveredBluetoothDevice) {
60+
try {
61+
connect(device.device)
62+
.useAutoConnect(false)
63+
.retry(3, 100)
64+
.suspend()
65+
} catch (e: Exception) {
66+
e.printStackTrace()
67+
}
68+
}
69+
6470
fun openLogger() {
6571
logger?.openLogger()
6672
}

0 commit comments

Comments
 (0)