From 85900e5bffd3e4ca3c08efdfaf8beb610bcc395e Mon Sep 17 00:00:00 2001 From: darken Date: Mon, 3 Apr 2023 14:50:58 +0200 Subject: [PATCH] Improve final headphones checks and fix IllegalStateException if data changes between the final two checks. --- .../ui/TroubleShooterFragmentVM.kt | 22 +++++++------------ 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/eu/darken/capod/troubleshooter/ui/TroubleShooterFragmentVM.kt b/app/src/main/java/eu/darken/capod/troubleshooter/ui/TroubleShooterFragmentVM.kt index 021673a4..3dbe8cb0 100644 --- a/app/src/main/java/eu/darken/capod/troubleshooter/ui/TroubleShooterFragmentVM.kt +++ b/app/src/main/java/eu/darken/capod/troubleshooter/ui/TroubleShooterFragmentVM.kt @@ -174,8 +174,14 @@ class TroubleShooterFragmentVM @Inject constructor( run { progress("Checking all closeby headphones.") - var otherDevices = withTimeoutOrNull(STEP_TIME) { - podMonitor.devices.take(10).toList().flatten() + val otherDevices = withTimeoutOrNull(STEP_TIME) { + val start = System.currentTimeMillis() + podMonitor.devices + .take(10) + .takeWhile { System.currentTimeMillis() - start < STEP_TIME - 1000 } + .toList() + .flatten() + .distinctBy { it.address } } ?: emptyList() if (otherDevices.isEmpty()) { @@ -189,18 +195,6 @@ class TroubleShooterFragmentVM @Inject constructor( generalSettings.mainDeviceAddress.value = null generalSettings.minimumSignalQuality.value = 0.25f - otherDevices = withTimeoutOrNull(STEP_TIME) { - withTimeoutOrNull(STEP_TIME) { - val start = System.currentTimeMillis() - podMonitor.devices - .take(10) - .takeWhile { System.currentTimeMillis() - start < STEP_TIME - 1000 } - .toList() - .flatten() - .distinctBy { it.address } - } ?: emptyList() - } ?: emptyList() - progress("Setting headphone with strongest signal as yours.") generalSettings.mainDeviceModel.value = otherDevices.maxBy { it.signalQuality }.model