From aad8d66670e9fbacb816a4e9418f7b8e2ab6b680 Mon Sep 17 00:00:00 2001 From: hiar Date: Fri, 8 Aug 2025 15:04:18 +0200 Subject: [PATCH] Changed to stateflow --- .../no/nordicsemi/android/service/profile/ProfileService.kt | 5 +++-- .../android/service/profile/ProfileServiceManager.kt | 2 +- .../java/no/nordicsemi/android/service/profile/ServiceApi.kt | 5 +++-- .../android/toolbox/profile/viewmodel/ProfileViewModel.kt | 1 + 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/lib_service/src/main/java/no/nordicsemi/android/service/profile/ProfileService.kt b/lib_service/src/main/java/no/nordicsemi/android/service/profile/ProfileService.kt index fec9c4e2..fc472858 100644 --- a/lib_service/src/main/java/no/nordicsemi/android/service/profile/ProfileService.kt +++ b/lib_service/src/main/java/no/nordicsemi/android/service/profile/ProfileService.kt @@ -8,6 +8,7 @@ import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.Job import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asSharedFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.filter @@ -87,7 +88,7 @@ internal class ProfileService : NotificationService() { override val isMissingServices: Flow get() = _isMissingServices.asStateFlow() - override val disconnectionReason: Flow + override val disconnectionReason: StateFlow get() = _disconnectionReason.asStateFlow() override suspend fun getMaxWriteValue(address: String, writeType: WriteType): Int? { @@ -134,7 +135,7 @@ internal class ProfileService : NotificationService() { } } - override fun getConnectionState(address: String): Flow? { + override fun getConnectionState(address: String): StateFlow? { val peripheral = getPeripheralById(address) ?: return null return peripheral.state.also { stateFlow -> connectionJobs[address]?.cancel() diff --git a/lib_service/src/main/java/no/nordicsemi/android/service/profile/ProfileServiceManager.kt b/lib_service/src/main/java/no/nordicsemi/android/service/profile/ProfileServiceManager.kt index 21e26eec..e65cb4c4 100644 --- a/lib_service/src/main/java/no/nordicsemi/android/service/profile/ProfileServiceManager.kt +++ b/lib_service/src/main/java/no/nordicsemi/android/service/profile/ProfileServiceManager.kt @@ -19,7 +19,7 @@ sealed interface ProfileServiceManager { } internal class ProfileServiceManagerImp @Inject constructor( - @ApplicationContext private val context: Context, + @param:ApplicationContext private val context: Context, ) : ProfileServiceManager { private var serviceConnection: ServiceConnection? = null private var api: ServiceApi? = null diff --git a/lib_service/src/main/java/no/nordicsemi/android/service/profile/ServiceApi.kt b/lib_service/src/main/java/no/nordicsemi/android/service/profile/ServiceApi.kt index 93cbcfc3..59ce672a 100644 --- a/lib_service/src/main/java/no/nordicsemi/android/service/profile/ServiceApi.kt +++ b/lib_service/src/main/java/no/nordicsemi/android/service/profile/ServiceApi.kt @@ -1,6 +1,7 @@ package no.nordicsemi.android.service.profile import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.StateFlow import no.nordicsemi.android.toolbox.profile.manager.ServiceManager import no.nordicsemi.android.ui.view.internal.DisconnectReason import no.nordicsemi.kotlin.ble.client.android.Peripheral @@ -44,14 +45,14 @@ interface ServiceApi { * * @return the connection state flow. */ - fun getConnectionState(address: String): Flow? + fun getConnectionState(address: String): StateFlow? /** * Get the disconnection reason of the device with the given address. * * @return the disconnection reason flow. */ - val disconnectionReason: Flow + val disconnectionReason: StateFlow /** * Request maximum write value length. diff --git a/profile/src/main/java/no/nordicsemi/android/toolbox/profile/viewmodel/ProfileViewModel.kt b/profile/src/main/java/no/nordicsemi/android/toolbox/profile/viewmodel/ProfileViewModel.kt index be6c9529..9e1bf637 100644 --- a/profile/src/main/java/no/nordicsemi/android/toolbox/profile/viewmodel/ProfileViewModel.kt +++ b/profile/src/main/java/no/nordicsemi/android/toolbox/profile/viewmodel/ProfileViewModel.kt @@ -140,6 +140,7 @@ internal class ProfileViewModel @Inject constructor( * Update the service data, including connection state and peripheral data. * @param api the service API. * @param deviceAddress the address of the connected device. + * @param isAlreadyConnected true if the device is already connected, false otherwise. */ private fun updateConnectionState( api: ServiceApi,