mirror of
https://github.com/d4rken-org/capod.git
synced 2026-09-14 18:26:11 -04:00
Render device settings before BT profile state arrives
This commit is contained in:
@@ -33,6 +33,7 @@ import kotlinx.coroutines.flow.flatMapLatest
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import kotlinx.coroutines.flow.flowOf
|
||||
import kotlinx.coroutines.isActive
|
||||
import kotlinx.coroutines.flow.onStart
|
||||
import java.time.Instant
|
||||
import javax.inject.Inject
|
||||
|
||||
@@ -80,7 +81,9 @@ class DeviceSettingsViewModel @Inject constructor(
|
||||
deviceForProfile(profileId),
|
||||
upgradeRepo.upgradeInfo,
|
||||
isForceConnecting,
|
||||
bluetoothManager.connectedDevices,
|
||||
// The Bluetooth HEADSET profile lookup can be slow to produce its first value.
|
||||
// Seed this branch so the screen can render immediately after navigation.
|
||||
bluetoothManager.connectedDevices.onStart { emit(emptyList()) },
|
||||
) { _, device, upgrade, forcing, connectedDevices ->
|
||||
val connectedAddresses = connectedDevices.map { it.address }.toSet()
|
||||
State(
|
||||
|
||||
+16
@@ -21,13 +21,16 @@ import io.mockk.verify
|
||||
import kotlinx.coroutines.CompletableDeferred
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.awaitCancellation
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import kotlinx.coroutines.flow.flowOf
|
||||
import kotlinx.coroutines.test.UnconfinedTestDispatcher
|
||||
import kotlinx.coroutines.test.resetMain
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import kotlinx.coroutines.test.setMain
|
||||
import kotlinx.coroutines.withTimeout
|
||||
import org.junit.jupiter.api.AfterEach
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Test
|
||||
@@ -298,4 +301,17 @@ class DeviceSettingsViewModelTest : BaseTest() {
|
||||
|
||||
vm.state.first().isClassicallyConnected shouldBe false
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `state emits even when connected devices flow has not emitted yet`() = runTest(testDispatcher) {
|
||||
every { bluetoothManager.connectedDevices } returns flow { awaitCancellation() }
|
||||
|
||||
val vm = createViewModel()
|
||||
vm.initialize(testAddress)
|
||||
|
||||
val state = withTimeout(1_000) { vm.state.first() }
|
||||
|
||||
state.device?.address shouldBe testAddress
|
||||
state.isClassicallyConnected shouldBe false
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user