Changed to stateflow

This commit is contained in:
hiar
2025-08-08 15:04:18 +02:00
committed by Himali Aryal
parent f3333c737c
commit aad8d66670
4 changed files with 8 additions and 5 deletions

View File

@@ -8,6 +8,7 @@ import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.filter
@@ -87,7 +88,7 @@ internal class ProfileService : NotificationService() {
override val isMissingServices: Flow<Boolean> override val isMissingServices: Flow<Boolean>
get() = _isMissingServices.asStateFlow() get() = _isMissingServices.asStateFlow()
override val disconnectionReason: Flow<DeviceDisconnectionReason?> override val disconnectionReason: StateFlow<DeviceDisconnectionReason?>
get() = _disconnectionReason.asStateFlow() get() = _disconnectionReason.asStateFlow()
override suspend fun getMaxWriteValue(address: String, writeType: WriteType): Int? { override suspend fun getMaxWriteValue(address: String, writeType: WriteType): Int? {
@@ -134,7 +135,7 @@ internal class ProfileService : NotificationService() {
} }
} }
override fun getConnectionState(address: String): Flow<ConnectionState>? { override fun getConnectionState(address: String): StateFlow<ConnectionState>? {
val peripheral = getPeripheralById(address) ?: return null val peripheral = getPeripheralById(address) ?: return null
return peripheral.state.also { stateFlow -> return peripheral.state.also { stateFlow ->
connectionJobs[address]?.cancel() connectionJobs[address]?.cancel()

View File

@@ -19,7 +19,7 @@ sealed interface ProfileServiceManager {
} }
internal class ProfileServiceManagerImp @Inject constructor( internal class ProfileServiceManagerImp @Inject constructor(
@ApplicationContext private val context: Context, @param:ApplicationContext private val context: Context,
) : ProfileServiceManager { ) : ProfileServiceManager {
private var serviceConnection: ServiceConnection? = null private var serviceConnection: ServiceConnection? = null
private var api: ServiceApi? = null private var api: ServiceApi? = null

View File

@@ -1,6 +1,7 @@
package no.nordicsemi.android.service.profile package no.nordicsemi.android.service.profile
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow
import no.nordicsemi.android.toolbox.profile.manager.ServiceManager import no.nordicsemi.android.toolbox.profile.manager.ServiceManager
import no.nordicsemi.android.ui.view.internal.DisconnectReason import no.nordicsemi.android.ui.view.internal.DisconnectReason
import no.nordicsemi.kotlin.ble.client.android.Peripheral import no.nordicsemi.kotlin.ble.client.android.Peripheral
@@ -44,14 +45,14 @@ interface ServiceApi {
* *
* @return the connection state flow. * @return the connection state flow.
*/ */
fun getConnectionState(address: String): Flow<ConnectionState>? fun getConnectionState(address: String): StateFlow<ConnectionState>?
/** /**
* Get the disconnection reason of the device with the given address. * Get the disconnection reason of the device with the given address.
* *
* @return the disconnection reason flow. * @return the disconnection reason flow.
*/ */
val disconnectionReason: Flow<DeviceDisconnectionReason?> val disconnectionReason: StateFlow<DeviceDisconnectionReason?>
/** /**
* Request maximum write value length. * Request maximum write value length.

View File

@@ -140,6 +140,7 @@ internal class ProfileViewModel @Inject constructor(
* Update the service data, including connection state and peripheral data. * Update the service data, including connection state and peripheral data.
* @param api the service API. * @param api the service API.
* @param deviceAddress the address of the connected device. * @param deviceAddress the address of the connected device.
* @param isAlreadyConnected true if the device is already connected, false otherwise.
*/ */
private fun updateConnectionState( private fun updateConnectionState(
api: ServiceApi, api: ServiceApi,