From 90fc2fac245f4287ede4c4113a2262130e105569 Mon Sep 17 00:00:00 2001 From: darken Date: Thu, 27 Jan 2022 00:46:06 +0100 Subject: [PATCH] Improve sorting --- .../darken/capod/monitor/core/PodMonitor.kt | 34 ++++++++++++------- .../darken/capod/pods/core/apple/ApplePods.kt | 2 +- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/eu/darken/capod/monitor/core/PodMonitor.kt b/app/src/main/java/eu/darken/capod/monitor/core/PodMonitor.kt index 1d50a45f..b1d40adb 100644 --- a/app/src/main/java/eu/darken/capod/monitor/core/PodMonitor.kt +++ b/app/src/main/java/eu/darken/capod/monitor/core/PodMonitor.kt @@ -4,6 +4,7 @@ import eu.darken.capod.common.bluetooth.BleScanner import eu.darken.capod.common.bluetooth.BluetoothManager2 import eu.darken.capod.common.coroutine.AppScope import eu.darken.capod.common.debug.logging.Logging.Priority.* +import eu.darken.capod.common.debug.logging.asLog import eu.darken.capod.common.debug.logging.log import eu.darken.capod.common.debug.logging.logTag import eu.darken.capod.common.flow.replayingShare @@ -83,24 +84,31 @@ class PodMonitor @Inject constructor( val now = Instant.now() val minimumSignalQuality = generalSettings.minimumSignalQuality.value + val mainDeviceModel = generalSettings.mainDeviceModel.value val aboveTresholdSorted = pods.values .filter { it.signalQuality > minimumSignalQuality } .sortedWith( - compareBy { Duration.between(it.lastSeenAt, now) }.thenByDescending { it.rssi } + compareByDescending { it.model == mainDeviceModel && it.model != PodDevice.Model.UNKNOWN } + .thenBy { + val age = Duration.between(it.lastSeenAt, now).seconds + if (age < 3) 0L else age + } + .thenByDescending { it.signalQuality } ) val belowThresholdSorted = pods.values .filter { it.signalQuality <= minimumSignalQuality } .sortedWith( - compareBy { Duration.between(it.lastSeenAt, now) }.thenByDescending { it.rssi } + compareBy { Duration.between(it.lastSeenAt, now) }.thenByDescending { it.signalQuality } ) - val main = pods.values.determineMainDevice() - (aboveTresholdSorted + belowThresholdSorted).sortedByDescending { it == main } + val presorted = (aboveTresholdSorted + belowThresholdSorted) + val main = presorted.determineMainDevice() + presorted.sortedByDescending { it == main } } .onStart { emit(emptyList()) } .retryWhen { cause, attempt -> - log(TAG, WARN) { "PodMonitor failed (attempt=$attempt), will retry: $cause" } + log(TAG, WARN) { "PodMonitor failed (attempt=$attempt), will retry: ${cause.asLog()}" } delay(3000) true } @@ -111,15 +119,15 @@ class PodMonitor @Inject constructor( .map { it.determineMainDevice() } .replayingShare(appScope) - private fun Collection.determineMainDevice(): PodDevice? = - firstOrNull()?.let filter@{ device -> - val minimumSignalQuality = generalSettings.minimumSignalQuality.value - - generalSettings.mainDeviceModel.value.let { - if (device.model != it && it != PodDevice.Model.UNKNOWN) return@filter null + private fun List.determineMainDevice(): PodDevice? = + this.firstOrNull()?.let { + val mainDeviceModel = generalSettings.mainDeviceModel.value + when { + it.model == PodDevice.Model.UNKNOWN -> null + mainDeviceModel != PodDevice.Model.UNKNOWN && it.model != mainDeviceModel -> null + it.signalQuality <= generalSettings.minimumSignalQuality.value -> null + else -> it } - - if (device.signalQuality > minimumSignalQuality) device else null } companion object { diff --git a/app/src/main/java/eu/darken/capod/pods/core/apple/ApplePods.kt b/app/src/main/java/eu/darken/capod/pods/core/apple/ApplePods.kt index defe14ab..7aaeef20 100644 --- a/app/src/main/java/eu/darken/capod/pods/core/apple/ApplePods.kt +++ b/app/src/main/java/eu/darken/capod/pods/core/apple/ApplePods.kt @@ -119,7 +119,7 @@ interface ApplePods : PodDevice { rssiHistory = recognizedDevice.rssiHistory .toMutableList() .let { - if (it.size > 2) it.removeAt(0) + if (it.size > 3) it.removeAt(0) it.plus(scanResult.rssi) } )