-
Notifications
You must be signed in to change notification settings - Fork 134
[POS] Better error handling for eligibility #14463
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
Changes from all commits
e56c87a
1051e38
c138585
32b6ee9
5dfd4b9
366728e
4224b7e
e4cbb01
2fb1cdc
77750c7
ba1f40c
e6ea0f1
2e0b4b9
5f910a9
70ee69a
5c5b473
20139a7
46d5b69
798c607
60680c8
2bed5df
b57f42f
c750ee6
1ac9b92
c609905
635cf9a
42a2975
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.woocommerce.android.ui.woopos.common.util | ||
|
||
class WooPosCouldNotDetermineValueException : Exception() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package com.woocommerce.android.ui.woopos.tab | ||
|
||
import com.woocommerce.android.AppPrefs | ||
import com.woocommerce.android.extensions.semverCompareTo | ||
import com.woocommerce.android.tools.SelectedSite | ||
import com.woocommerce.android.ui.woopos.WooPOSIsRemotelyEnabled | ||
|
@@ -8,6 +9,8 @@ import com.woocommerce.android.util.FetchActiveWCPluginVersion | |
import com.woocommerce.android.util.GetWooCorePluginCachedVersion | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import org.wordpress.android.fluxc.model.SiteModel | ||
import org.wordpress.android.fluxc.model.settings.Settings | ||
import org.wordpress.android.fluxc.store.WooCommerceStore | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
@@ -19,6 +22,7 @@ import javax.inject.Singleton | |
*/ | ||
@Singleton | ||
class WooPosCanBeLaunchedInTab @Inject constructor( | ||
private val appPrefs: AppPrefs, | ||
private val selectedSite: SelectedSite, | ||
private val getWooCoreCachedVersion: GetWooCorePluginCachedVersion, | ||
private val fetchWooCoreVersion: FetchActiveWCPluginVersion, | ||
|
@@ -39,51 +43,115 @@ class WooPosCanBeLaunchedInTab @Inject constructor( | |
private suspend fun checkLaunchability(forceRefresh: Boolean = false): WooPosLaunchability { | ||
val site = selectedSite.getOrNull() | ||
?: return WooPosLaunchability.NotLaunchable( | ||
WooPosLaunchability.NonLaunchabilityReason.NoSiteSelected | ||
reason = WooPosLaunchability.NonLaunchabilityReason.NoSiteSelected | ||
) | ||
|
||
val wooCoreVersion = if (forceRefresh) { | ||
fetchWooCoreVersion() | ||
} else { | ||
getWooCoreCachedVersion() | ||
} ?: return WooPosLaunchability.NotLaunchable( | ||
WooPosLaunchability.NonLaunchabilityReason.WooCommercePluginNotFound | ||
) | ||
val cachedPositive = appPrefs.isPOSLaunchableForSite(site.id) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 I'm wondering if this should be set to false when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good question. I was considering that, and I think it's ok to keep the logic simple and use the same rationale when |
||
|
||
if (!isWooCoreSupportsOrderAutoDraftsAndExtraPaymentsProps(wooCoreVersion)) { | ||
return WooPosLaunchability.NotLaunchable( | ||
WooPosLaunchability.NonLaunchabilityReason.UnsupportedWooCommerceVersion | ||
) | ||
getNonLaunchabilityReasonFromVersionAndFeatureSwitch(forceRefresh, cachedPositive)?.let { | ||
return prepareNotLaunchableStateWithCacheUpdate(site.id, it) | ||
} | ||
|
||
if (isFeatureSwitchSupported(wooCoreVersion) && !isRemotelyEnabled(forceRefresh)) { | ||
return WooPosLaunchability.NotLaunchable( | ||
WooPosLaunchability.NonLaunchabilityReason.FeatureSwitchDisabled | ||
) | ||
getNonLaunchabilityReasonFromSiteSettingsAndCurrency(site, forceRefresh, cachedPositive)?.let { | ||
return prepareNotLaunchableStateWithCacheUpdate(site.id, it) | ||
} | ||
|
||
val siteSettings = if (forceRefresh) { | ||
wooCommerceStore.fetchSiteGeneralSettings(site).model | ||
appPrefs.setPOSLaunchableForSite(site.id) | ||
return WooPosLaunchability.Launchable | ||
} | ||
|
||
private fun prepareNotLaunchableStateWithCacheUpdate( | ||
siteId: Int, | ||
reason: WooPosLaunchability.NonLaunchabilityReason | ||
): WooPosLaunchability.NotLaunchable { | ||
if (reason != WooPosLaunchability.NonLaunchabilityReason.UnknownNoPositiveCache) { | ||
appPrefs.clearPOSLaunchableForSite(siteId) | ||
} | ||
|
||
return WooPosLaunchability.NotLaunchable(reason) | ||
} | ||
|
||
private suspend fun getNonLaunchabilityReasonFromVersionAndFeatureSwitch( | ||
forceRefresh: Boolean, | ||
cachedPositive: Boolean | ||
): WooPosLaunchability.NonLaunchabilityReason? { | ||
val wooCoreVersion = getWooCoreVersion(forceRefresh) | ||
?: return reasonIfNoPositiveCache(cachedPositive) | ||
|
||
return getNonLaunchabilityReasonFromWooCoreVersion(wooCoreVersion) | ||
?: getNonLaunchabilityReasonFromFeatureSwitch(wooCoreVersion, forceRefresh, cachedPositive) | ||
} | ||
|
||
private suspend fun getNonLaunchabilityReasonFromSiteSettingsAndCurrency( | ||
site: SiteModel, | ||
forceRefresh: Boolean, | ||
cachedPositive: Boolean | ||
): WooPosLaunchability.NonLaunchabilityReason? { | ||
val siteSettings = resolveSiteSettings(site, forceRefresh) | ||
?: return reasonIfNoPositiveCache(cachedPositive) | ||
|
||
return if (!isCountryAndCurrencySupported(siteSettings.countryCode, siteSettings.currencyCode)) { | ||
WooPosLaunchability.NonLaunchabilityReason.UnsupportedCurrency | ||
} else { | ||
wooCommerceStore.getSiteSettings(site) | ||
?: wooCommerceStore.fetchSiteGeneralSettings(site).model | ||
null | ||
} | ||
} | ||
|
||
if (siteSettings == null) { | ||
return WooPosLaunchability.NotLaunchable( | ||
WooPosLaunchability.NonLaunchabilityReason.SiteSettingsUnavailable | ||
) | ||
private fun reasonIfNoPositiveCache(hasCachedPositive: Boolean): WooPosLaunchability.NonLaunchabilityReason? = | ||
if (hasCachedPositive) { | ||
null | ||
} else { | ||
WooPosLaunchability.NonLaunchabilityReason.UnknownNoPositiveCache | ||
} | ||
|
||
return if (isCountryAndCurrencySupported(siteSettings.countryCode, siteSettings.currencyCode)) { | ||
WooPosLaunchability.Launchable | ||
private suspend fun getWooCoreVersion(forceRefresh: Boolean): String? = | ||
if (forceRefresh) fetchWooCoreVersion() else getWooCoreCachedVersion() | ||
|
||
/** | ||
* Checks the WooCommerce core version to see if it prevents POS from being launchable. | ||
* Returns the NonLaunchabilityReason if it does, or null if the version is supported. | ||
*/ | ||
private fun getNonLaunchabilityReasonFromWooCoreVersion( | ||
wooCoreVersion: String | ||
): WooPosLaunchability.NonLaunchabilityReason? = | ||
if (!isWooCoreSupportsOrderAutoDraftsAndExtraPaymentsProps(wooCoreVersion)) { | ||
WooPosLaunchability.NonLaunchabilityReason.UnsupportedWooCommerceVersion | ||
} else { | ||
WooPosLaunchability.NotLaunchable( | ||
WooPosLaunchability.NonLaunchabilityReason.UnsupportedCurrency | ||
) | ||
null | ||
} | ||
|
||
/** | ||
* Checks the feature switch to see if it prevents POS from being launchable. | ||
* Returns the NonLaunchabilityReason if it does, or null if POS might still be launchable. | ||
*/ | ||
private suspend fun getNonLaunchabilityReasonFromFeatureSwitch( | ||
wooCoreVersion: String, | ||
forceRemoteRefresh: Boolean, | ||
hasCachedLaunchableState: Boolean | ||
): WooPosLaunchability.NonLaunchabilityReason? { | ||
val result = | ||
if (!isFeatureSwitchSupported(wooCoreVersion)) { | ||
null | ||
} else { | ||
val enabled = isRemotelyEnabled(forceRemoteRefresh).getOrNull() | ||
when { | ||
enabled == true -> null | ||
enabled == false -> WooPosLaunchability.NonLaunchabilityReason.FeatureSwitchDisabled | ||
hasCachedLaunchableState -> null | ||
else -> WooPosLaunchability.NonLaunchabilityReason.UnknownNoPositiveCache | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
private suspend fun resolveSiteSettings(site: SiteModel, forceRefresh: Boolean): Settings? = | ||
if (forceRefresh) { | ||
wooCommerceStore.fetchSiteGeneralSettings(site).model | ||
} else { | ||
wooCommerceStore.getSiteSettings(site) ?: wooCommerceStore.fetchSiteGeneralSettings(site).model | ||
} | ||
|
||
private fun isCountryAndCurrencySupported(countryCode: String, currency: String) = | ||
SUPPORTED_COUNTRY_CURRENCY_PAIRS.any { | ||
it.first.equals(countryCode, true) && it.second.equals(currency, true) | ||
|
@@ -117,5 +185,6 @@ sealed class WooPosLaunchability { | |
FeatureSwitchDisabled, | ||
UnsupportedCurrency, | ||
NoSiteSelected, | ||
UnknownNoPositiveCache | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.