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. // default account.
val accountId = ioEffect { val accountId = ioEffect {
val accountIds = getProfiles().toIO().bind() val accountIds = getProfiles().toIO().bind()
.map { it.accountId() } .asSequence()
.map { profile ->
profile.accountId()
}
.toSet() .toSet()
fun String.takeIfAccountIdExists() = this fun String.takeIfAccountIdExists() = this
@ -1918,10 +1921,17 @@ private suspend fun RememberStateFlowScope.produceOwnershipFlow(
val ciphers = getCiphers().toIO().bind() val ciphers = getCiphers().toIO().bind()
ciphers ciphers
.asSequence() .asSequence()
.filter { it.organizationId != null } .map {
.groupBy { it.accountId } val groupKey = it.accountId
// the one that has the most ciphers val score = 1.0 +
.maxByOrNull { entry -> entry.value.size } (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 // account id
?.key ?.key
?.takeIfAccountIdExists() ?.takeIfAccountIdExists()