mirror of
https://github.com/sudoxnym/habitica-android.git
synced 2026-07-13 17:51:57 +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>) {
|
private fun processPurchases(result: BillingResult, purchases: MutableList<Purchase>) {
|
||||||
when (result.responseCode) {
|
when (result.responseCode) {
|
||||||
BillingClient.BillingResponseCode.OK -> {
|
BillingClient.BillingResponseCode.OK -> {
|
||||||
|
val mostRecentSub = findMostRecentSubscription(purchases)
|
||||||
|
val plan = userViewModel.user.value?.purchased?.plan
|
||||||
for (purchase in purchases) {
|
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)
|
handle(purchase)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -231,14 +241,6 @@ class PurchaseHandler(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
PurchaseTypes.allSubscriptionTypes.contains(sku) -> {
|
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)
|
val validationRequest = buildValidationRequest(purchase)
|
||||||
apiClient.validateSubscription(validationRequest).subscribe({
|
apiClient.validateSubscription(validationRequest).subscribe({
|
||||||
processedPurchase(purchase)
|
processedPurchase(purchase)
|
||||||
|
|
@ -310,14 +312,20 @@ class PurchaseHandler(
|
||||||
}
|
}
|
||||||
var fallback: Purchase? = null
|
var fallback: Purchase? = null
|
||||||
if (result.billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
|
if (result.billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
|
||||||
val purchases = result.purchasesList.filter { it.isAcknowledged }.sortedByDescending { it.purchaseTime }
|
return findMostRecentSubscription(result.purchasesList)
|
||||||
// If there is a subscription that is still active, prioritise that. Otherwise return the most recent one.
|
}
|
||||||
for (purchase in purchases) {
|
return fallback
|
||||||
if (purchase.isAutoRenewing) {
|
}
|
||||||
return purchase
|
|
||||||
} else if (!purchase.isAutoRenewing && fallback == null) {
|
private fun findMostRecentSubscription(purchasesList: List<Purchase>): Purchase? {
|
||||||
fallback = 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
|
return fallback
|
||||||
|
|
|
||||||
|
|
@ -29,11 +29,11 @@ import com.habitrpg.android.habitica.ui.fragments.PromoInfoFragment
|
||||||
import com.habitrpg.common.habitica.extensions.loadImage
|
import com.habitrpg.common.habitica.extensions.loadImage
|
||||||
import com.habitrpg.android.habitica.ui.views.dialogs.HabiticaAlertDialog
|
import com.habitrpg.android.habitica.ui.views.dialogs.HabiticaAlertDialog
|
||||||
import com.habitrpg.android.habitica.ui.views.subscriptions.SubscriptionOptionView
|
import com.habitrpg.android.habitica.ui.views.subscriptions.SubscriptionOptionView
|
||||||
import javax.inject.Inject
|
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
class SubscriptionFragment : BaseFragment<FragmentSubscriptionBinding>() {
|
class SubscriptionFragment : BaseFragment<FragmentSubscriptionBinding>() {
|
||||||
|
|
||||||
|
|
@ -232,7 +232,7 @@ class SubscriptionFragment : BaseFragment<FragmentSubscriptionBinding>() {
|
||||||
if (user?.purchased?.plan?.paymentMethod == "Google" &&
|
if (user?.purchased?.plan?.paymentMethod == "Google" &&
|
||||||
user?.purchased?.plan?.isActive == true &&
|
user?.purchased?.plan?.isActive == true &&
|
||||||
user?.purchased?.plan?.dateTerminated == null &&
|
user?.purchased?.plan?.dateTerminated == null &&
|
||||||
(newestSubscription?.isAutoRenewing == false)
|
(newestSubscription?.isAutoRenewing != true)
|
||||||
) {
|
) {
|
||||||
compositeSubscription.add(
|
compositeSubscription.add(
|
||||||
purchaseHandler.cancelSubscription()
|
purchaseHandler.cancelSubscription()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue