From 82443af8f7736ee4f76d7585f4a178e7175eae3d Mon Sep 17 00:00:00 2001 From: Alex Hart Date: Fri, 16 Aug 2024 16:28:06 -0300 Subject: [PATCH] Check remote subscription object to determine if a cancel is necessary. --- .../app/subscription/InAppPaymentsRepository.kt | 14 ++++++++++++-- .../RecurringInAppPaymentRepository.kt | 2 +- .../manage/ManageDonationsViewModel.kt | 3 ++- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/subscription/InAppPaymentsRepository.kt b/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/subscription/InAppPaymentsRepository.kt index 9f515c1763..3050d6a353 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/subscription/InAppPaymentsRepository.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/subscription/InAppPaymentsRepository.kt @@ -406,18 +406,28 @@ object InAppPaymentsRepository { /** * Retrieves whether or not we should force a cancel before next subscribe attempt for in app payments of the given - * type. This method will first check the database, and then fall back on the deprecated SignalStore value. + * type. This method will first check the database, and then fall back on the deprecated SignalStore value. This method + * will also access and check the current subscriber data, if it exists. */ @JvmStatic @WorkerThread fun getShouldCancelSubscriptionBeforeNextSubscribeAttempt(subscriberType: InAppPaymentSubscriberRecord.Type): Boolean { val latestSubscriber = getSubscriber(subscriberType) - return latestSubscriber?.requiresCancel ?: if (subscriberType == InAppPaymentSubscriberRecord.Type.DONATION) { + val localState = latestSubscriber?.requiresCancel ?: if (subscriberType == InAppPaymentSubscriberRecord.Type.DONATION) { SignalStore.inAppPayments.shouldCancelSubscriptionBeforeNextSubscribeAttempt } else { false } + + if (latestSubscriber != null) { + val remoteState = AppDependencies.donationsService.getSubscription(latestSubscriber.subscriberId) + val result = remoteState.result.getOrNull() ?: return localState + + return result.activeSubscription?.isCanceled ?: localState + } + + return localState } /** diff --git a/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/subscription/RecurringInAppPaymentRepository.kt b/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/subscription/RecurringInAppPaymentRepository.kt index 4714f24207..c562015691 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/subscription/RecurringInAppPaymentRepository.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/subscription/RecurringInAppPaymentRepository.kt @@ -163,7 +163,7 @@ object RecurringInAppPaymentRepository { } else { Completable.complete() } - } + }.subscribeOn(Schedulers.io()) } fun getPaymentSourceTypeOfLatestSubscription(subscriberType: InAppPaymentSubscriberRecord.Type): Single { diff --git a/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/subscription/manage/ManageDonationsViewModel.kt b/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/subscription/manage/ManageDonationsViewModel.kt index 41e8e5d8bb..fbe04eccbc 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/subscription/manage/ManageDonationsViewModel.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/subscription/manage/ManageDonationsViewModel.kt @@ -2,6 +2,7 @@ package org.thoughtcrime.securesms.components.settings.app.subscription.manage import androidx.lifecycle.LiveData import androidx.lifecycle.ViewModel +import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers import io.reactivex.rxjava3.core.Observable import io.reactivex.rxjava3.core.Single import io.reactivex.rxjava3.disposables.CompositeDisposable @@ -66,7 +67,7 @@ class ManageDonationsViewModel : ViewModel() { disposables += Single.fromCallable { InAppPaymentsRepository.getShouldCancelSubscriptionBeforeNextSubscribeAttempt(InAppPaymentSubscriberRecord.Type.DONATION) - }.subscribeBy { requiresCancel -> + }.subscribeOn(Schedulers.io()).subscribeBy { requiresCancel -> store.update { it.copy(subscriberRequiresCancel = requiresCancel) }