From e1c71cf6b6b3114d593eb7e01b9def67e49867bf Mon Sep 17 00:00:00 2001 From: darken Date: Wed, 26 Jan 2022 00:08:51 +0100 Subject: [PATCH] Improve sorting --- .../main/ui/overview/OverviewFragmentVM.kt | 59 +++++++++---------- .../darken/capod/monitor/core/PodMonitor.kt | 49 +++++++-------- .../eu/darken/capod/monitor/core/PodSorter.kt | 9 +++ 3 files changed, 59 insertions(+), 58 deletions(-) create mode 100644 app/src/main/java/eu/darken/capod/monitor/core/PodSorter.kt diff --git a/app/src/main/java/eu/darken/capod/main/ui/overview/OverviewFragmentVM.kt b/app/src/main/java/eu/darken/capod/main/ui/overview/OverviewFragmentVM.kt index 7fe05466..dda0a21d 100644 --- a/app/src/main/java/eu/darken/capod/main/ui/overview/OverviewFragmentVM.kt +++ b/app/src/main/java/eu/darken/capod/main/ui/overview/OverviewFragmentVM.kt @@ -129,38 +129,35 @@ class OverviewFragmentVM @Inject constructor( } if (permissions.isEmpty() && isBluetoothEnabled) { - pods - .map { - val now = Instant.now() - when (it) { - is DualApplePods -> DualApplePodsCardVH.Item( - now = now, - device = it, - showDebug = isDebugMode, - isMainPod = it == mainPod, - ) - is SingleApplePods -> SingleApplePodsCardVH.Item( - now = now, - device = it, - showDebug = isDebugMode, - isMainPod = it == mainPod, - ) - is BasicSingleApplePods -> BasicSingleApplePodsCardVH.Item( - now = now, - device = it, - showDebug = isDebugMode, - isMainPod = it == mainPod, - ) - else -> UnknownPodDeviceCardVH.Item( - now = now, - device = it, - showDebug = isDebugMode, - isMainPod = it == mainPod, - ) - } + pods.map { + val now = Instant.now() + when (it) { + is DualApplePods -> DualApplePodsCardVH.Item( + now = now, + device = it, + showDebug = isDebugMode, + isMainPod = it == mainPod, + ) + is SingleApplePods -> SingleApplePodsCardVH.Item( + now = now, + device = it, + showDebug = isDebugMode, + isMainPod = it == mainPod, + ) + is BasicSingleApplePods -> BasicSingleApplePodsCardVH.Item( + now = now, + device = it, + showDebug = isDebugMode, + isMainPod = it == mainPod, + ) + else -> UnknownPodDeviceCardVH.Item( + now = now, + device = it, + showDebug = isDebugMode, + isMainPod = it == mainPod, + ) } - .sortedByDescending { it.isMainPod } - .run { items.addAll(this) } + }.run { items.addAll(this) } } items 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 47d55e7a..226c3d19 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 @@ -77,28 +77,22 @@ class PodMonitor @Inject constructor( } } - - val aboveThreshold = mutableListOf() - val belowThreshold = mutableListOf() - val minimumSignalQuality = generalSettings.minimumSignalQuality.value - pods.values.forEach { device -> - if (device.signalQuality > minimumSignalQuality) aboveThreshold.add(device) - else belowThreshold.add(device) - } val now = Instant.now() - aboveThreshold.sortedWith(compareBy { - Duration.between( - it.lastSeenAt, - now + val minimumSignalQuality = generalSettings.minimumSignalQuality.value + + val aboveTresholdSorted = pods.values + .filter { it.signalQuality > minimumSignalQuality } + .sortedWith( + compareBy { Duration.between(it.lastSeenAt, now) }.thenByDescending { it.rssi } ) - }.thenByDescending { it.rssi }) - .plus(belowThreshold.sortedWith(compareBy { - Duration.between( - it.lastSeenAt, - now - ) - }.thenByDescending { it.rssi })) -// pods.values.sortedByDescending { it.rssi } + val belowThresholdSorted = pods.values + .filter { it.signalQuality <= minimumSignalQuality } + .sortedWith( + compareBy { Duration.between(it.lastSeenAt, now) }.thenByDescending { it.rssi } + ) + + val main = pods.values.determineMainDevice() + (aboveTresholdSorted + belowThresholdSorted).sortedByDescending { it == main } } .onStart { emit(emptyList()) } .catch { @@ -107,16 +101,17 @@ class PodMonitor @Inject constructor( .replayingShare(appScope) val mainDevice: Flow - get() = devices.map { devices -> - devices.maxByOrNull { it.rssi }?.let filter@{ device -> - val minimumSignalQuality = generalSettings.minimumSignalQuality.value + get() = devices.map { it.determineMainDevice() } - generalSettings.mainDeviceModel.value.let { - if (device.model != it && it != PodDevice.Model.UNKNOWN) return@filter null - } + private fun Collection.determineMainDevice(): PodDevice? = + maxByOrNull { it.rssi }?.let filter@{ device -> + val minimumSignalQuality = generalSettings.minimumSignalQuality.value - if (device.signalQuality > minimumSignalQuality) device else null + generalSettings.mainDeviceModel.value.let { + if (device.model != it && it != PodDevice.Model.UNKNOWN) return@filter null } + + if (device.signalQuality > minimumSignalQuality) device else null } companion object { diff --git a/app/src/main/java/eu/darken/capod/monitor/core/PodSorter.kt b/app/src/main/java/eu/darken/capod/monitor/core/PodSorter.kt new file mode 100644 index 00000000..4722c106 --- /dev/null +++ b/app/src/main/java/eu/darken/capod/monitor/core/PodSorter.kt @@ -0,0 +1,9 @@ +package eu.darken.capod.monitor.core + +import dagger.Reusable +import javax.inject.Inject + +@Reusable +class PodSorter @Inject constructor( + +) \ No newline at end of file