From 06d2ea60057f9f46f945eb2952b6b856e2725334 Mon Sep 17 00:00:00 2001 From: Artem Chepurnoy Date: Sun, 31 Mar 2024 19:04:45 +0300 Subject: [PATCH] improvement: Better initial select of the account for Add item screen --- .../home/vault/add/AddStateProducer.kt | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/common/src/commonMain/kotlin/com/artemchep/keyguard/feature/home/vault/add/AddStateProducer.kt b/common/src/commonMain/kotlin/com/artemchep/keyguard/feature/home/vault/add/AddStateProducer.kt index 272f1a6..78f610d 100644 --- a/common/src/commonMain/kotlin/com/artemchep/keyguard/feature/home/vault/add/AddStateProducer.kt +++ b/common/src/commonMain/kotlin/com/artemchep/keyguard/feature/home/vault/add/AddStateProducer.kt @@ -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()