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

[Woo POS] POS avalability check optimisation #12759

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
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
Loading