mirror of
https://github.com/sudoxnym/habitica-android.git
synced 2026-05-21 05:09:00 +00:00
fix oscilating sub issue
# Conflicts: # Habitica/src/main/java/com/habitrpg/android/habitica/helpers/PurchaseHandler.kt
This commit is contained in:
parent
d5d9f6f39e
commit
e214fc8f96
2 changed files with 26 additions and 18 deletions
|
|
@ -70,7 +70,17 @@ class PurchaseHandler(
|
|||
private fun processPurchases(result: BillingResult, purchases: MutableList<Purchase>) {
|
||||
when (result.responseCode) {
|
||||
BillingClient.BillingResponseCode.OK -> {
|
||||
val mostRecentSub = findMostRecentSubscription(purchases)
|
||||
val plan = userViewModel.user.value?.purchased?.plan
|
||||
for (purchase in purchases) {
|
||||
if (plan?.isActive == true) {
|
||||
if ((plan.additionalData?.data?.orderId == purchase.orderId &&
|
||||
((plan.dateTerminated != null) == purchase.isAutoRenewing)) ||
|
||||
mostRecentSub?.orderId != purchase.orderId
|
||||
) {
|
||||
return
|
||||
}
|
||||
}
|
||||
handle(purchase)
|
||||
}
|
||||
}
|
||||
|
|
@ -231,14 +241,6 @@ class PurchaseHandler(
|
|||
}
|
||||
}
|
||||
PurchaseTypes.allSubscriptionTypes.contains(sku) -> {
|
||||
val plan = userViewModel.user.value?.purchased?.plan
|
||||
if (plan?.isActive == true) {
|
||||
if (plan.additionalData?.data?.orderId == purchase.orderId &&
|
||||
((plan.dateTerminated != null) == purchase.isAutoRenewing)
|
||||
) {
|
||||
return
|
||||
}
|
||||
}
|
||||
val validationRequest = buildValidationRequest(purchase)
|
||||
apiClient.validateSubscription(validationRequest).subscribe({
|
||||
processedPurchase(purchase)
|
||||
|
|
@ -310,14 +312,20 @@ class PurchaseHandler(
|
|||
}
|
||||
var fallback: Purchase? = null
|
||||
if (result.billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
|
||||
val purchases = result.purchasesList.filter { it.isAcknowledged }.sortedByDescending { it.purchaseTime }
|
||||
// If there is a subscription that is still active, prioritise that. Otherwise return the most recent one.
|
||||
for (purchase in purchases) {
|
||||
if (purchase.isAutoRenewing) {
|
||||
return purchase
|
||||
} else if (!purchase.isAutoRenewing && fallback == null) {
|
||||
fallback = purchase
|
||||
}
|
||||
return findMostRecentSubscription(result.purchasesList)
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
private fun findMostRecentSubscription(purchasesList: List<Purchase>): Purchase? {
|
||||
val purchases = purchasesList.filter { it.isAcknowledged }.sortedByDescending { it.purchaseTime }
|
||||
var fallback: Purchase? = null
|
||||
// If there is a subscription that is still active, prioritise that. Otherwise return the most recent one.
|
||||
for (purchase in purchases) {
|
||||
if (purchase.isAutoRenewing) {
|
||||
return purchase
|
||||
} else if (!purchase.isAutoRenewing && fallback == null) {
|
||||
fallback = purchase
|
||||
}
|
||||
}
|
||||
return fallback
|
||||
|
|
|
|||
|
|
@ -29,11 +29,11 @@ import com.habitrpg.android.habitica.ui.fragments.PromoInfoFragment
|
|||
import com.habitrpg.common.habitica.extensions.loadImage
|
||||
import com.habitrpg.android.habitica.ui.views.dialogs.HabiticaAlertDialog
|
||||
import com.habitrpg.android.habitica.ui.views.subscriptions.SubscriptionOptionView
|
||||
import javax.inject.Inject
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import javax.inject.Inject
|
||||
|
||||
class SubscriptionFragment : BaseFragment<FragmentSubscriptionBinding>() {
|
||||
|
||||
|
|
@ -232,7 +232,7 @@ class SubscriptionFragment : BaseFragment<FragmentSubscriptionBinding>() {
|
|||
if (user?.purchased?.plan?.paymentMethod == "Google" &&
|
||||
user?.purchased?.plan?.isActive == true &&
|
||||
user?.purchased?.plan?.dateTerminated == null &&
|
||||
(newestSubscription?.isAutoRenewing == false)
|
||||
(newestSubscription?.isAutoRenewing != true)
|
||||
) {
|
||||
compositeSubscription.add(
|
||||
purchaseHandler.cancelSubscription()
|
||||
|
|
|
|||
Loading…
Reference in a new issue