mirror of
https://github.com/openlibrecommunity/olcng.git
synced 2026-07-03 14:05:17 +02:00
refactor(MainViewModel): extract group sorting logic into dedicated method
This commit is contained in:
@@ -459,26 +459,47 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
|
||||
sortByTestResultsForSub(guid)
|
||||
}
|
||||
} else if (subscriptionId.startsWith("group_")) {
|
||||
val allSubs = MmkvManager.decodeSubscriptions()
|
||||
val groupSubs = when (subscriptionId) {
|
||||
"group_white" -> allSubs.filter {
|
||||
it.subscription.remarks.startsWith("БЕЛЫЕ", ignoreCase = true) ||
|
||||
it.subscription.remarks.startsWith("WHITE", ignoreCase = true)
|
||||
}
|
||||
"group_black" -> allSubs.filter {
|
||||
it.subscription.remarks.startsWith("ЧЕРНЫЕ", ignoreCase = true) ||
|
||||
it.subscription.remarks.startsWith("BLACK", ignoreCase = true)
|
||||
}
|
||||
else -> emptyList()
|
||||
}
|
||||
groupSubs.forEach { sub ->
|
||||
sortByTestResultsForSub(sub.guid)
|
||||
}
|
||||
sortByTestResultsForGroup(subscriptionId)
|
||||
} else {
|
||||
sortByTestResultsForSub(subscriptionId)
|
||||
}
|
||||
}
|
||||
|
||||
private fun sortByTestResultsForGroup(groupId: String) {
|
||||
data class ServerDelay(var guid: String, var testDelayMillis: Long, var subId: String)
|
||||
|
||||
val allSubs = MmkvManager.decodeSubscriptions()
|
||||
val groupSubs = when (groupId) {
|
||||
"group_white" -> allSubs.filter {
|
||||
it.subscription.remarks.startsWith("БЕЛЫЕ", ignoreCase = true) ||
|
||||
it.subscription.remarks.startsWith("WHITE", ignoreCase = true)
|
||||
}
|
||||
"group_black" -> allSubs.filter {
|
||||
it.subscription.remarks.startsWith("ЧЕРНЫЕ", ignoreCase = true) ||
|
||||
it.subscription.remarks.startsWith("BLACK", ignoreCase = true)
|
||||
}
|
||||
else -> emptyList()
|
||||
}
|
||||
|
||||
val allServerDelays = mutableListOf<ServerDelay>()
|
||||
|
||||
groupSubs.forEach { sub ->
|
||||
val serverList = MmkvManager.decodeServerList(sub.guid)
|
||||
serverList.forEach { guid ->
|
||||
val delay = MmkvManager.decodeServerAffiliationInfo(guid)?.testDelayMillis ?: 0L
|
||||
allServerDelays.add(ServerDelay(guid, if (delay <= 0L) 999999 else delay, sub.guid))
|
||||
}
|
||||
}
|
||||
|
||||
allServerDelays.sortBy { it.testDelayMillis }
|
||||
|
||||
val serversBySubId = allServerDelays.groupBy { it.subId }
|
||||
serversBySubId.forEach { (subId, servers) ->
|
||||
val sortedList = servers.map { it.guid }.toMutableList()
|
||||
MmkvManager.encodeServerList(sortedList, subId)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts servers by their test results for a specific subscription.
|
||||
* @param subId The subscription ID to sort servers for.
|
||||
|
||||
Reference in New Issue
Block a user