Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show loading states in purchasing flow an extra minimum amount of time #6084

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading