Skip to content

Commit

Permalink
Merge pull request #1665 from novasamatech/fix/swipe-gov-update-refer…
Browse files Browse the repository at this point in the history
…enda-state

Awaite while all voted referenda update their state to voted
  • Loading branch information
antonijzelinskij authored Sep 19, 2024
2 parents 4ff73cd + 997dadf commit fccb326
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ interface PartialRetriableMixin : Retriable {

interface Presentation : PartialRetriableMixin {

fun <T> handleMultiResult(
suspend fun <T> handleMultiResult(
multiResult: RetriableMultiResult<T>,
onSuccess: (List<T>) -> Unit,
onSuccess: suspend (List<T>) -> Unit,
progressConsumer: ProgressConsumer? = null,
onRetryCancelled: () -> Unit
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ private class PartialRetriableMixinImpl(
coroutineScope: CoroutineScope
) : PartialRetriableMixin.Presentation, CoroutineScope by coroutineScope {

override fun <T> handleMultiResult(
override suspend fun <T> handleMultiResult(
multiResult: RetriableMultiResult<T>,
onSuccess: (List<T>) -> Unit,
onSuccess: suspend (List<T>) -> Unit,
progressConsumer: ProgressConsumer?,
onRetryCancelled: () -> Unit
) {
Expand All @@ -44,7 +44,7 @@ private class PartialRetriableMixinImpl(

private fun <T> submissionFailed(
failure: RetriableMultiResult.RetriableFailure<T>,
onSuccess: (List<T>) -> Unit,
onSuccess: suspend (List<T>) -> Unit,
progressConsumer: ProgressConsumer?,
onRetryCancelled: () -> Unit,
) {
Expand All @@ -62,7 +62,7 @@ private class PartialRetriableMixinImpl(

private fun <T> retrySubmission(
failure: RetriableMultiResult.RetriableFailure<T>,
onSuccess: (List<T>) -> Unit,
onSuccess: suspend (List<T>) -> Unit,
progressConsumer: ProgressConsumer?,
onRetryCancelled: () -> Unit
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class RetriableMultiResult<T>(val succeeded: List<T>, val failed: RetriableFailu

fun <T> RetriableMultiResult.Companion.allFailed(failed: RetriableFailure<T>) = RetriableMultiResult(emptyList(), failed)

inline fun <T> RetriableMultiResult<T>.onFullSuccess(action: (successResults: List<T>) -> Unit): RetriableMultiResult<T> {
suspend inline fun <T> RetriableMultiResult<T>.onFullSuccess(action: suspend (successResults: List<T>) -> Unit): RetriableMultiResult<T> {
if (failed == null) {
action(succeeded)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,6 @@ interface TinderGovInteractor {
suspend fun clearBasket()

suspend fun getBasketItemsToRemove(coroutineScope: CoroutineScope): List<TinderGovBasketItem>

suspend fun awaitAllItemsVoted(coroutineScope: CoroutineScope, basket: List<TinderGovBasketItem>)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -180,6 +181,19 @@ class RealTinderGovInteractor(
return basket.filter { it.isItemNotAvailableToVote(availableToVoteReferenda, asset) }
}

override suspend fun awaitAllItemsVoted(coroutineScope: CoroutineScope, basket: List<TinderGovBasketItem>) {
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<ReferendumId>,
asset: Asset
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<TinderGovBasketItem>) {
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<TinderGovBasketItem>) {
tinderGovInteractor.awaitAllItemsVoted(coroutineScope, basket)
}

private suspend fun getValidationPayload(): VoteTinderGovValidationPayload {
Expand Down

0 comments on commit fccb326

Please sign in to comment.