Skip to content

Commit

Permalink
Merge pull request #12759 from woocommerce/12633-woo-pos-determinatio…
Browse files Browse the repository at this point in the history
…n-if-pos-available-takes-more-time-than-it-should

[Woo POS] POS avalability check optimisation
  • Loading branch information
samiuelson authored Oct 14, 2024
2 parents 5a9dbdd + ef8b773 commit 4592e2d
Showing 1 changed file with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import com.woocommerce.android.ui.payments.cardreader.onboarding.PluginType
import com.woocommerce.android.util.GetWooCorePluginCachedVersion
import com.woocommerce.android.util.IsRemoteFeatureFlagEnabled
import com.woocommerce.android.util.RemoteFeatureFlag.WOO_POS
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import org.wordpress.android.fluxc.model.SiteModel
import org.wordpress.android.fluxc.model.payments.inperson.WCPaymentAccountResult
import org.wordpress.android.fluxc.store.WCInPersonPaymentsStore
Expand All @@ -29,21 +31,25 @@ class WooPosIsEnabled @Inject constructor(
private var paymentAccountCache: HashMap<LocalSiteId, WCPaymentAccountResult> = hashMapOf()

@Suppress("ReturnCount")
suspend operator fun invoke(): Boolean {
val selectedSite = selectedSite.getOrNull() ?: return false
suspend operator fun invoke(): Boolean = coroutineScope {
val selectedSite = selectedSite.getOrNull() ?: return@coroutineScope false

if (!isRemoteFeatureFlagEnabled(WOO_POS)) return false
if (!isScreenSizeAllowed()) return false
if (!isWooCoreSupportsOrderAutoDraftsAndExtraPaymentsProps()) return false
val onboardingStatusDeferred = async { cardReaderOnboardingChecker.getOnboardingState() }
val paymentAccountDeferred = async { getOrFetchPaymentAccount(selectedSite, WOOCOMMERCE_PAYMENTS) }

val onboardingStatus = cardReaderOnboardingChecker.getOnboardingState()
if (!isRemoteFeatureFlagEnabled(WOO_POS)) return@coroutineScope false
if (!isScreenSizeAllowed()) return@coroutineScope false
if (!isWooCoreSupportsOrderAutoDraftsAndExtraPaymentsProps()) return@coroutineScope false

if (onboardingStatus.preferredPlugin != PluginType.WOOCOMMERCE_PAYMENTS) return false
if (!isIPPOnboardingCompleted(onboardingStatus)) return false
val onboardingStatus = onboardingStatusDeferred.await()
if (onboardingStatus.preferredPlugin != PluginType.WOOCOMMERCE_PAYMENTS) return@coroutineScope false
if (!isIPPOnboardingCompleted(onboardingStatus)) return@coroutineScope false

val paymentAccount = getOrFetchPaymentAccount(selectedSite, WOOCOMMERCE_PAYMENTS) ?: return false
if (paymentAccount.country.lowercase() != "us") return false
return paymentAccount.storeCurrencies.default.lowercase() == "usd"
val paymentAccount = paymentAccountDeferred.await() ?: return@coroutineScope false
if (paymentAccount.country.lowercase() != "us") return@coroutineScope false
if (paymentAccount.storeCurrencies.default.lowercase() != "usd") return@coroutineScope false

return@coroutineScope true
}

private suspend fun getOrFetchPaymentAccount(
Expand Down

0 comments on commit 4592e2d

Please sign in to comment.