From 0eae1a0620dc7a30307b4ba84df9c585edaaab78 Mon Sep 17 00:00:00 2001 From: Jonatan Rhodin Date: Mon, 8 Apr 2024 11:29:37 +0200 Subject: [PATCH] Show loading states in purchasing flow an extra amount of time --- .../mullvadvpn/usecase/PaymentUseCase.kt | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/usecase/PaymentUseCase.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/usecase/PaymentUseCase.kt index 5a50e8040151..82efd5d72206 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/usecase/PaymentUseCase.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/usecase/PaymentUseCase.kt @@ -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 @@ -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() { @@ -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 {