improvement: Better initial select of the account for Add item screen

This commit is contained in:
Artem Chepurnoy 2024-03-31 19:04:45 +03:00
parent 12e7382400
commit 06d2ea6005
No known key found for this signature in database
GPG Key ID: FAC37D0CF674043E
1 changed files with 15 additions and 5 deletions

View File

@ -1902,7 +1902,10 @@ private suspend fun RememberStateFlowScope.produceOwnershipFlow(
// default account.
val accountId = ioEffect {
val accountIds = getProfiles().toIO().bind()
.map { it.accountId() }
.asSequence()
.map { profile ->
profile.accountId()
}
.toSet()
fun String.takeIfAccountIdExists() = this
@ -1918,10 +1921,17 @@ private suspend fun RememberStateFlowScope.produceOwnershipFlow(
val ciphers = getCiphers().toIO().bind()
ciphers
.asSequence()
.filter { it.organizationId != null }
.groupBy { it.accountId }
// the one that has the most ciphers
.maxByOrNull { entry -> entry.value.size }
.map {
val groupKey = it.accountId
val score = 1.0 +
(if (it.organizationId == null) 0.8 else 0.0) +
(if (it.folderId != null) 0.2 else 0.0)
groupKey to score
}
.groupBy { it.first }
.mapValues { it.value.sumOf { it.second } }
// the one that has the highest score
.maxByOrNull { entry -> entry.value }
// account id
?.key
?.takeIfAccountIdExists()