From 9ea35a034e2a8d28e39d8bda92d2709061b3b973 Mon Sep 17 00:00:00 2001 From: Andrey Date: Thu, 3 Oct 2024 15:27:26 +0200 Subject: [PATCH 1/4] Run onboarding and payment account check in parallel --- .../android/ui/woopos/WooPosIsEnabled.kt | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/WooPosIsEnabled.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/WooPosIsEnabled.kt index 3dcc927374a..4c2532b8eb0 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/WooPosIsEnabled.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/WooPosIsEnabled.kt @@ -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 @@ -29,21 +31,27 @@ class WooPosIsEnabled @Inject constructor( private var paymentAccountCache: HashMap = hashMapOf() @Suppress("ReturnCount") - suspend operator fun invoke(): Boolean { - val selectedSite = selectedSite.getOrNull() ?: return false + suspend operator fun invoke(): Boolean = coroutineScope { + val startTime = System.currentTimeMillis() + val selectedSite = selectedSite.getOrNull() ?: return@coroutineScope false - if (!isRemoteFeatureFlagEnabled(WOO_POS)) return false - if (!isScreenSizeAllowed()) return false - if (!isWooCoreSupportsOrderAutoDraftsAndExtraPaymentsProps()) return false + if (!isRemoteFeatureFlagEnabled(WOO_POS)) return@coroutineScope false + if (!isScreenSizeAllowed()) return@coroutineScope false + if (!isWooCoreSupportsOrderAutoDraftsAndExtraPaymentsProps()) return@coroutineScope false - val onboardingStatus = cardReaderOnboardingChecker.getOnboardingState() + val onboardingStatusDeferred = async { cardReaderOnboardingChecker.getOnboardingState() } + val paymentAccountDeferred = async { getOrFetchPaymentAccount(selectedSite, WOOCOMMERCE_PAYMENTS) } - 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 true + + println("WooPosIsEnabled: ${System.currentTimeMillis() - startTime}ms") + return@coroutineScope true } private suspend fun getOrFetchPaymentAccount( From d321805302c37aa024f86f3032d7962747991a7f Mon Sep 17 00:00:00 2001 From: Andrey Date: Thu, 3 Oct 2024 15:30:40 +0200 Subject: [PATCH 2/4] Started the requests a bit earlier --- .../com/woocommerce/android/ui/woopos/WooPosIsEnabled.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/WooPosIsEnabled.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/WooPosIsEnabled.kt index 4c2532b8eb0..a3675fb38f6 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/WooPosIsEnabled.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/WooPosIsEnabled.kt @@ -35,13 +35,13 @@ class WooPosIsEnabled @Inject constructor( val startTime = System.currentTimeMillis() val selectedSite = selectedSite.getOrNull() ?: return@coroutineScope false + val onboardingStatusDeferred = async { cardReaderOnboardingChecker.getOnboardingState() } + val paymentAccountDeferred = async { getOrFetchPaymentAccount(selectedSite, WOOCOMMERCE_PAYMENTS) } + if (!isRemoteFeatureFlagEnabled(WOO_POS)) return@coroutineScope false if (!isScreenSizeAllowed()) return@coroutineScope false if (!isWooCoreSupportsOrderAutoDraftsAndExtraPaymentsProps()) return@coroutineScope false - val onboardingStatusDeferred = async { cardReaderOnboardingChecker.getOnboardingState() } - val paymentAccountDeferred = async { getOrFetchPaymentAccount(selectedSite, WOOCOMMERCE_PAYMENTS) } - val onboardingStatus = onboardingStatusDeferred.await() if (onboardingStatus.preferredPlugin != PluginType.WOOCOMMERCE_PAYMENTS) return@coroutineScope false if (!isIPPOnboardingCompleted(onboardingStatus)) return@coroutineScope false From 6c30b6dab2befd49e71c10ec834c55ed3bd2c2fa Mon Sep 17 00:00:00 2001 From: Andrey Date: Thu, 3 Oct 2024 16:41:09 +0200 Subject: [PATCH 3/4] Fixed formatting --- .../kotlin/com/woocommerce/android/ui/woopos/WooPosIsEnabled.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/WooPosIsEnabled.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/WooPosIsEnabled.kt index a3675fb38f6..e09f6f2b428 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/WooPosIsEnabled.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/WooPosIsEnabled.kt @@ -35,7 +35,7 @@ class WooPosIsEnabled @Inject constructor( val startTime = System.currentTimeMillis() val selectedSite = selectedSite.getOrNull() ?: return@coroutineScope false - val onboardingStatusDeferred = async { cardReaderOnboardingChecker.getOnboardingState() } + val onboardingStatusDeferred = async { cardReaderOnboardingChecker.getOnboardingState() } val paymentAccountDeferred = async { getOrFetchPaymentAccount(selectedSite, WOOCOMMERCE_PAYMENTS) } if (!isRemoteFeatureFlagEnabled(WOO_POS)) return@coroutineScope false From 8583d9befd70586382832b5725f51403af5362b1 Mon Sep 17 00:00:00 2001 From: Andrey Date: Fri, 4 Oct 2024 11:11:46 +0200 Subject: [PATCH 4/4] Removed logging. Fix the condition --- .../com/woocommerce/android/ui/woopos/WooPosIsEnabled.kt | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/WooPosIsEnabled.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/WooPosIsEnabled.kt index e09f6f2b428..757738316bc 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/WooPosIsEnabled.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/WooPosIsEnabled.kt @@ -32,7 +32,6 @@ class WooPosIsEnabled @Inject constructor( @Suppress("ReturnCount") suspend operator fun invoke(): Boolean = coroutineScope { - val startTime = System.currentTimeMillis() val selectedSite = selectedSite.getOrNull() ?: return@coroutineScope false val onboardingStatusDeferred = async { cardReaderOnboardingChecker.getOnboardingState() } @@ -48,9 +47,8 @@ class WooPosIsEnabled @Inject constructor( val paymentAccount = paymentAccountDeferred.await() ?: return@coroutineScope false if (paymentAccount.country.lowercase() != "us") return@coroutineScope false - if (paymentAccount.storeCurrencies.default.lowercase() != "usd") return@coroutineScope true + if (paymentAccount.storeCurrencies.default.lowercase() != "usd") return@coroutineScope false - println("WooPosIsEnabled: ${System.currentTimeMillis() - startTime}ms") return@coroutineScope true }