From 997dadf122509548651715f3a011349a2b0ff574 Mon Sep 17 00:00:00 2001 From: antonijzelinskij Date: Thu, 19 Sep 2024 13:04:09 +0200 Subject: [PATCH] Awaite while all voted referenda update their state to voted --- .../multiResult/PartialRetriableMixin.kt | 4 ++-- .../multiResult/PartialRetriableMixinImpl.kt | 8 ++++---- .../utils/multiResult/RetriableMultiResult.kt | 2 +- .../domain/tindergov/TinderGovInteractor.kt | 2 ++ .../tindergov/RealTinderGovInteractor.kt | 14 ++++++++++++++ .../confirm/ConfirmTinderGovVoteViewModel.kt | 19 ++++++++++++------- 6 files changed, 35 insertions(+), 14 deletions(-) diff --git a/common/src/main/java/io/novafoundation/nova/common/utils/multiResult/PartialRetriableMixin.kt b/common/src/main/java/io/novafoundation/nova/common/utils/multiResult/PartialRetriableMixin.kt index 73aef489b4..8c326aaf9c 100644 --- a/common/src/main/java/io/novafoundation/nova/common/utils/multiResult/PartialRetriableMixin.kt +++ b/common/src/main/java/io/novafoundation/nova/common/utils/multiResult/PartialRetriableMixin.kt @@ -13,9 +13,9 @@ interface PartialRetriableMixin : Retriable { interface Presentation : PartialRetriableMixin { - fun handleMultiResult( + suspend fun handleMultiResult( multiResult: RetriableMultiResult, - onSuccess: (List) -> Unit, + onSuccess: suspend (List) -> Unit, progressConsumer: ProgressConsumer? = null, onRetryCancelled: () -> Unit ) diff --git a/common/src/main/java/io/novafoundation/nova/common/utils/multiResult/PartialRetriableMixinImpl.kt b/common/src/main/java/io/novafoundation/nova/common/utils/multiResult/PartialRetriableMixinImpl.kt index 305fb83c70..1ba875ffc9 100644 --- a/common/src/main/java/io/novafoundation/nova/common/utils/multiResult/PartialRetriableMixinImpl.kt +++ b/common/src/main/java/io/novafoundation/nova/common/utils/multiResult/PartialRetriableMixinImpl.kt @@ -27,9 +27,9 @@ private class PartialRetriableMixinImpl( coroutineScope: CoroutineScope ) : PartialRetriableMixin.Presentation, CoroutineScope by coroutineScope { - override fun handleMultiResult( + override suspend fun handleMultiResult( multiResult: RetriableMultiResult, - onSuccess: (List) -> Unit, + onSuccess: suspend (List) -> Unit, progressConsumer: ProgressConsumer?, onRetryCancelled: () -> Unit ) { @@ -44,7 +44,7 @@ private class PartialRetriableMixinImpl( private fun submissionFailed( failure: RetriableMultiResult.RetriableFailure, - onSuccess: (List) -> Unit, + onSuccess: suspend (List) -> Unit, progressConsumer: ProgressConsumer?, onRetryCancelled: () -> Unit, ) { @@ -62,7 +62,7 @@ private class PartialRetriableMixinImpl( private fun retrySubmission( failure: RetriableMultiResult.RetriableFailure, - onSuccess: (List) -> Unit, + onSuccess: suspend (List) -> Unit, progressConsumer: ProgressConsumer?, onRetryCancelled: () -> Unit ) { diff --git a/common/src/main/java/io/novafoundation/nova/common/utils/multiResult/RetriableMultiResult.kt b/common/src/main/java/io/novafoundation/nova/common/utils/multiResult/RetriableMultiResult.kt index b60fe0b321..3083f98e34 100644 --- a/common/src/main/java/io/novafoundation/nova/common/utils/multiResult/RetriableMultiResult.kt +++ b/common/src/main/java/io/novafoundation/nova/common/utils/multiResult/RetriableMultiResult.kt @@ -17,7 +17,7 @@ class RetriableMultiResult(val succeeded: List, val failed: RetriableFailu fun RetriableMultiResult.Companion.allFailed(failed: RetriableFailure) = RetriableMultiResult(emptyList(), failed) -inline fun RetriableMultiResult.onFullSuccess(action: (successResults: List) -> Unit): RetriableMultiResult { +suspend inline fun RetriableMultiResult.onFullSuccess(action: suspend (successResults: List) -> Unit): RetriableMultiResult { if (failed == null) { action(succeeded) } diff --git a/feature-governance-api/src/main/java/io/novafoundation/nova/feature_governance_api/domain/tindergov/TinderGovInteractor.kt b/feature-governance-api/src/main/java/io/novafoundation/nova/feature_governance_api/domain/tindergov/TinderGovInteractor.kt index cc8972ca12..bf93269345 100644 --- a/feature-governance-api/src/main/java/io/novafoundation/nova/feature_governance_api/domain/tindergov/TinderGovInteractor.kt +++ b/feature-governance-api/src/main/java/io/novafoundation/nova/feature_governance_api/domain/tindergov/TinderGovInteractor.kt @@ -51,4 +51,6 @@ interface TinderGovInteractor { suspend fun clearBasket() suspend fun getBasketItemsToRemove(coroutineScope: CoroutineScope): List + + suspend fun awaitAllItemsVoted(coroutineScope: CoroutineScope, basket: List) } diff --git a/feature-governance-impl/src/main/java/io/novafoundation/nova/feature_governance_impl/domain/referendum/tindergov/RealTinderGovInteractor.kt b/feature-governance-impl/src/main/java/io/novafoundation/nova/feature_governance_impl/domain/referendum/tindergov/RealTinderGovInteractor.kt index b81291bea2..fc4f6b4896 100644 --- a/feature-governance-impl/src/main/java/io/novafoundation/nova/feature_governance_impl/domain/referendum/tindergov/RealTinderGovInteractor.kt +++ b/feature-governance-impl/src/main/java/io/novafoundation/nova/feature_governance_impl/domain/referendum/tindergov/RealTinderGovInteractor.kt @@ -35,6 +35,7 @@ import io.novafoundation.nova.runtime.state.selectedOption import java.math.BigInteger import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.map @@ -180,6 +181,19 @@ class RealTinderGovInteractor( return basket.filter { it.isItemNotAvailableToVote(availableToVoteReferenda, asset) } } + override suspend fun awaitAllItemsVoted(coroutineScope: CoroutineScope, basket: List) { + observeReferendaState(coroutineScope) + .filter { referendaState -> + val referenda = referendaState.referenda.associateBy { it.id } + val allBasketItemsVoted = basket.all { + val referendum = referenda[it.referendumId] + referendum?.referendumVote != null + } + + allBasketItemsVoted + }.first() + } + private fun TinderGovBasketItem.isItemNotAvailableToVote( availableToVoteReferenda: Set, asset: Asset diff --git a/feature-governance-impl/src/main/java/io/novafoundation/nova/feature_governance_impl/presentation/tindergov/confirm/ConfirmTinderGovVoteViewModel.kt b/feature-governance-impl/src/main/java/io/novafoundation/nova/feature_governance_impl/presentation/tindergov/confirm/ConfirmTinderGovVoteViewModel.kt index 9edbb97c8e..fae2d47950 100644 --- a/feature-governance-impl/src/main/java/io/novafoundation/nova/feature_governance_impl/presentation/tindergov/confirm/ConfirmTinderGovVoteViewModel.kt +++ b/feature-governance-impl/src/main/java/io/novafoundation/nova/feature_governance_impl/presentation/tindergov/confirm/ConfirmTinderGovVoteViewModel.kt @@ -10,6 +10,7 @@ import io.novafoundation.nova.common.validation.progressConsumer import io.novafoundation.nova.feature_account_api.domain.interfaces.SelectedAccountUseCase import io.novafoundation.nova.feature_account_api.presenatation.account.wallet.WalletUiUseCase import io.novafoundation.nova.feature_account_api.presenatation.actions.ExternalActions +import io.novafoundation.nova.feature_governance_api.data.model.TinderGovBasketItem import io.novafoundation.nova.feature_governance_api.data.model.accountVote import io.novafoundation.nova.feature_governance_api.domain.referendum.vote.VoteReferendumInteractor import io.novafoundation.nova.feature_governance_api.domain.tindergov.TinderGovInteractor @@ -149,19 +150,23 @@ class ConfirmTinderGovVoteViewModel( partialRetriableMixin.handleMultiResult( multiResult = result, onSuccess = { - onVoteSuccess(accountVotes.size) + onVoteSuccess(payload.basket) }, progressConsumer = _showNextProgress.progressConsumer(), onRetryCancelled = { router.back() } ) } - private fun onVoteSuccess(voteSize: Int) { - launch { - showMessage(resourceManager.getString(R.string.swipe_gov_convirm_votes_success_message, voteSize)) - tinderGovInteractor.clearBasket() - router.backToTinderGovCards() - } + private suspend fun onVoteSuccess(basket: List) { + awaitVotedReferendaStateUpdate(basket) + + showMessage(resourceManager.getString(R.string.swipe_gov_convirm_votes_success_message, basket.size)) + tinderGovInteractor.clearBasket() + router.backToTinderGovCards() + } + + private suspend fun awaitVotedReferendaStateUpdate(basket: List) { + tinderGovInteractor.awaitAllItemsVoted(coroutineScope, basket) } private suspend fun getValidationPayload(): VoteTinderGovValidationPayload {