Fix manager in progress indicator

This commit is contained in:
Sylwester Zieliński
2022-02-15 09:46:45 +01:00
parent a6f0f58448
commit 1ae259fc65
44 changed files with 195 additions and 224 deletions

View File

@@ -4,10 +4,7 @@ import android.bluetooth.BluetoothDevice
import android.content.Context
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import no.nordicsemi.android.ble.ktx.suspend
import no.nordicsemi.android.prx.repository.AlarmHandler
@@ -35,8 +32,8 @@ class PRXRepository @Inject internal constructor(
private val _data = MutableStateFlow<BleManagerResult<PRXData>>(ConnectingResult())
internal val data = _data.asStateFlow()
private val _isRunning = MutableStateFlow(false)
val isRunning = _isRunning.asStateFlow()
val isRunning = data.map { it.isRunning() }
val hasBeenDisconnected = data.map { it.hasBeenDisconnected() }
fun launch(device: BluetoothDevice) {
serviceManager.startService(PRXService::class.java, device)
@@ -64,7 +61,6 @@ class PRXRepository @Inject internal constructor(
.useAutoConnect(false)
.retry(3, 100)
.suspend()
_isRunning.value = true
} catch (e: Exception) {
e.printStackTrace()
}
@@ -89,9 +85,7 @@ class PRXRepository @Inject internal constructor(
}
fun release() {
serviceManager.stopService(PRXService::class.java)
manager?.disconnect()?.enqueue()
manager = null
_isRunning.value = false
}
}

View File

@@ -4,6 +4,8 @@ import android.bluetooth.BluetoothDevice
import android.content.Intent
import androidx.lifecycle.lifecycleScope
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import no.nordicsemi.android.prx.data.PRXRepository
import no.nordicsemi.android.service.DEVICE_DATA
import no.nordicsemi.android.service.NotificationService
@@ -22,6 +24,10 @@ internal class PRXService : NotificationService() {
repository.start(device, lifecycleScope)
repository.hasBeenDisconnected.onEach {
if (it) stopSelf()
}.launchIn(lifecycleScope)
return START_REDELIVER_INTENT
}
}

View File

@@ -33,11 +33,11 @@ fun PRXScreen() {
when (state) {
NoDeviceState -> NoDeviceView()
is WorkingState -> when (state.result) {
is ConnectingResult,
is ReadyResult -> DeviceConnectingView { viewModel.onEvent(DisconnectEvent) }
is ConnectingResult -> DeviceConnectingView { viewModel.onEvent(DisconnectEvent) }
is DisconnectedResult -> DeviceDisconnectedView(Reason.USER, navigateUp)
is LinkLossResult -> DeviceOutOfRangeView { viewModel.onEvent(DisconnectEvent) }
is MissingServiceResult -> DeviceDisconnectedView(Reason.MISSING_SERVICE, navigateUp)
is UnknownErrorResult -> DeviceDisconnectedView(Reason.UNKNOWN, navigateUp)
is SuccessResult -> ContentView(state.result.data) { viewModel.onEvent(it) }
}
}.exhaustive

View File

@@ -3,10 +3,8 @@ package no.nordicsemi.android.prx.viewmodel
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import no.nordicsemi.android.navigation.*
import no.nordicsemi.android.prx.data.PRXRepository
import no.nordicsemi.android.prx.repository.PRX_SERVICE_UUID
@@ -26,8 +24,10 @@ internal class PRXViewModel @Inject constructor(
val state = _state.asStateFlow()
init {
if (!repository.isRunning.value) {
requestBluetoothDevice()
viewModelScope.launch {
if (repository.isRunning.firstOrNull() == false) {
requestBluetoothDevice()
}
}
repository.data.onEach {

View File

@@ -5,7 +5,7 @@
<string name="prx_silent_me">SILENT ME</string>
<string name="prx_find_me">FIND ME</string>
<string name="prx_records">Records</string>
<string name="prx_records">Data</string>
<string name="prx_settings">Settings</string>
<string name="prx_alarm_level_none">none</string>