mirror of
https://github.com/d4rken-org/capod.git
synced 2026-09-14 18:26:11 -04:00
Improve sorting
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -77,28 +77,22 @@ class PodMonitor @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
val aboveThreshold = mutableListOf<PodDevice>()
|
||||
val belowThreshold = mutableListOf<PodDevice>()
|
||||
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<PodDevice> {
|
||||
Duration.between(
|
||||
it.lastSeenAt,
|
||||
now
|
||||
val minimumSignalQuality = generalSettings.minimumSignalQuality.value
|
||||
|
||||
val aboveTresholdSorted = pods.values
|
||||
.filter { it.signalQuality > minimumSignalQuality }
|
||||
.sortedWith(
|
||||
compareBy<PodDevice> { Duration.between(it.lastSeenAt, now) }.thenByDescending { it.rssi }
|
||||
)
|
||||
}.thenByDescending { it.rssi })
|
||||
.plus(belowThreshold.sortedWith(compareBy<PodDevice> {
|
||||
Duration.between(
|
||||
it.lastSeenAt,
|
||||
now
|
||||
)
|
||||
}.thenByDescending { it.rssi }))
|
||||
// pods.values.sortedByDescending { it.rssi }
|
||||
val belowThresholdSorted = pods.values
|
||||
.filter { it.signalQuality <= minimumSignalQuality }
|
||||
.sortedWith(
|
||||
compareBy<PodDevice> { 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<PodDevice?>
|
||||
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<PodDevice>.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 {
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package eu.darken.capod.monitor.core
|
||||
|
||||
import dagger.Reusable
|
||||
import javax.inject.Inject
|
||||
|
||||
@Reusable
|
||||
class PodSorter @Inject constructor(
|
||||
|
||||
)
|
||||
Reference in New Issue
Block a user