Skip to content

Commit

Permalink
Show loading states in purchasing flow an extra amount of time
Browse files Browse the repository at this point in the history
  • Loading branch information
Pururun committed Apr 15, 2024
1 parent 74b67a1 commit 0eae1a0
Showing 1 changed file with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package net.mullvad.mullvadvpn.usecase

import android.app.Activity
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.transform
import net.mullvad.mullvadvpn.constant.VERIFICATION_BACK_OFF_FACTOR
import net.mullvad.mullvadvpn.constant.VERIFICATION_INITIAL_BACK_OFF_MILLISECONDS
import net.mullvad.mullvadvpn.constant.VERIFICATION_MAX_ATTEMPTS
Expand Down Expand Up @@ -35,7 +37,15 @@ class PlayPaymentUseCase(private val paymentRepository: PaymentRepository) : Pay
override val purchaseResult = _purchaseResult.asStateFlow()

override suspend fun purchaseProduct(productId: ProductId, activityProvider: () -> Activity) {
paymentRepository.purchaseProduct(productId, activityProvider).collect(_purchaseResult)
paymentRepository
.purchaseProduct(productId, activityProvider)
.transform {
emit(it)
if (it.shouldDelayLoading()) {
delay(EXTRA_LOADING_DELAY_MS)
}
}
.collect(_purchaseResult)
}

override suspend fun queryPaymentAvailability() {
Expand Down Expand Up @@ -64,6 +74,13 @@ class PlayPaymentUseCase(private val paymentRepository: PaymentRepository) : Pay
}
}
}

private fun PurchaseResult?.shouldDelayLoading() =
this is PurchaseResult.FetchingProducts || this is PurchaseResult.VerificationStarted

companion object {
const val EXTRA_LOADING_DELAY_MS = 300L
}
}

class EmptyPaymentUseCase : PaymentUseCase {
Expand Down

0 comments on commit 0eae1a0

Please sign in to comment.