From c3f998434626e5711a7d75b383164501c07b60b1 Mon Sep 17 00:00:00 2001 From: Alex Hart Date: Mon, 12 Dec 2022 11:54:25 -0400 Subject: [PATCH] Update error handling to include customized action when user cancels PayPal flow. --- .../flow/GiftFlowConfirmationFragment.kt | 31 ++----------------- .../donate/DonateToSignalFragment.kt | 4 +++ .../donate/DonationCheckoutDelegate.kt | 18 ++++++++--- .../donate/card/CreditCardFragment.kt | 2 +- 4 files changed, 21 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/badges/gifts/flow/GiftFlowConfirmationFragment.kt b/app/src/main/java/org/thoughtcrime/securesms/badges/gifts/flow/GiftFlowConfirmationFragment.kt index d46b38881..56a732ece 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/badges/gifts/flow/GiftFlowConfirmationFragment.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/badges/gifts/flow/GiftFlowConfirmationFragment.kt @@ -1,6 +1,5 @@ package org.thoughtcrime.securesms.badges.gifts.flow -import android.content.DialogInterface import android.view.KeyEvent import android.widget.FrameLayout import android.widget.ImageView @@ -26,8 +25,6 @@ import org.thoughtcrime.securesms.components.settings.app.subscription.donate.Do import org.thoughtcrime.securesms.components.settings.app.subscription.donate.DonationCheckoutDelegate import org.thoughtcrime.securesms.components.settings.app.subscription.donate.DonationProcessorAction import org.thoughtcrime.securesms.components.settings.app.subscription.donate.gateway.GatewayRequest -import org.thoughtcrime.securesms.components.settings.app.subscription.errors.DonationError -import org.thoughtcrime.securesms.components.settings.app.subscription.errors.DonationErrorDialogs import org.thoughtcrime.securesms.components.settings.app.subscription.errors.DonationErrorSource import org.thoughtcrime.securesms.components.settings.configure import org.thoughtcrime.securesms.components.settings.conversation.preferences.RecipientPreference @@ -72,7 +69,6 @@ class GiftFlowConfirmationFragment : private lateinit var emojiKeyboard: MediaKeyboard private val lifecycleDisposable = LifecycleDisposable() - private var errorDialog: DialogInterface? = null private var donationCheckoutDelegate: DonationCheckoutDelegate? = null private lateinit var processingDonationPaymentDialog: AlertDialog private lateinit var verifyingRecipientDonationPaymentDialog: AlertDialog @@ -192,12 +188,6 @@ class GiftFlowConfirmationFragment : } lifecycleDisposable.bindTo(viewLifecycleOwner) - lifecycleDisposable += DonationError - .getErrorsForSource(DonationErrorSource.GIFT) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe { donationError -> - onPaymentError(donationError) - } } override fun onDestroyView() { @@ -236,24 +226,6 @@ class GiftFlowConfirmationFragment : } } - private fun onPaymentError(throwable: Throwable?) { - Log.w(TAG, "onPaymentError", throwable, true) - - if (errorDialog != null) { - Log.i(TAG, "Already displaying an error dialog. Skipping.") - return - } - - errorDialog = DonationErrorDialogs.show( - requireContext(), throwable, - object : DonationErrorDialogs.DialogCallback() { - override fun onDialogDismissed() { - requireActivity().finish() - } - } - ) - } - override fun onToolbarNavigationClicked() { findNavController().popBackStack() } @@ -301,4 +273,7 @@ class GiftFlowConfirmationFragment : } override fun onProcessorActionProcessed() = Unit + override fun onUserCancelledPaymentFlow() { + findNavController().popBackStack(R.id.giftFlowConfirmationFragment, false) + } } diff --git a/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/subscription/donate/DonateToSignalFragment.kt b/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/subscription/donate/DonateToSignalFragment.kt index 4fc643821..a886d9c00 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/subscription/donate/DonateToSignalFragment.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/subscription/donate/DonateToSignalFragment.kt @@ -424,4 +424,8 @@ class DonateToSignalFragment : override fun onProcessorActionProcessed() { viewModel.refreshActiveSubscription() } + + override fun onUserCancelledPaymentFlow() { + findNavController().popBackStack(R.id.donateToSignalFragment, false) + } } diff --git a/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/subscription/donate/DonationCheckoutDelegate.kt b/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/subscription/donate/DonationCheckoutDelegate.kt index 8b3bac840..5c9aad74d 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/subscription/donate/DonationCheckoutDelegate.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/subscription/donate/DonationCheckoutDelegate.kt @@ -64,7 +64,7 @@ class DonationCheckoutDelegate( init { fragment.viewLifecycleOwner.lifecycle.addObserver(this) - ErrorHandler().attach(fragment, errorSource, *additionalSources) + ErrorHandler().attach(fragment, callback, errorSource, *additionalSources) } override fun onCreate(owner: LifecycleOwner) { @@ -203,9 +203,12 @@ class DonationCheckoutDelegate( private var fragment: Fragment? = null private var errorDialog: DialogInterface? = null + private var userCancelledFlowCallback: UserCancelledFlowCallback? = null - fun attach(fragment: Fragment, errorSource: DonationErrorSource, vararg additionalSources: DonationErrorSource) { + fun attach(fragment: Fragment, userCancelledFlowCallback: UserCancelledFlowCallback?, errorSource: DonationErrorSource, vararg additionalSources: DonationErrorSource) { this.fragment = fragment + this.userCancelledFlowCallback = userCancelledFlowCallback + val disposables = LifecycleDisposable() fragment.viewLifecycleOwner.lifecycle.addObserver(this) @@ -219,6 +222,7 @@ class DonationCheckoutDelegate( override fun onDestroy(owner: LifecycleOwner) { errorDialog?.dismiss() fragment = null + userCancelledFlowCallback = null } private fun registerErrorSource(errorSource: DonationErrorSource): Disposable { @@ -237,7 +241,7 @@ class DonationCheckoutDelegate( if (throwable is DonationError.UserCancelledPaymentError) { Log.d(TAG, "User cancelled out of payment flow.", true) - fragment?.findNavController()?.popBackStack(R.id.donateToSignalFragment, false) + return } @@ -247,7 +251,7 @@ class DonationCheckoutDelegate( object : DonationErrorDialogs.DialogCallback() { var tryCCAgain = false - override fun onTryCreditCardAgain(context: Context): DonationErrorParams.ErrorAction? { + override fun onTryCreditCardAgain(context: Context): DonationErrorParams.ErrorAction { return DonationErrorParams.ErrorAction( label = R.string.DeclineCode__try, action = { @@ -267,7 +271,11 @@ class DonationCheckoutDelegate( } } - interface Callback { + interface UserCancelledFlowCallback { + fun onUserCancelledPaymentFlow() + } + + interface Callback : UserCancelledFlowCallback { fun navigateToStripePaymentInProgress(gatewayRequest: GatewayRequest) fun navigateToPayPalPaymentInProgress(gatewayRequest: GatewayRequest) fun navigateToCreditCardForm(gatewayRequest: GatewayRequest) diff --git a/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/subscription/donate/card/CreditCardFragment.kt b/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/subscription/donate/card/CreditCardFragment.kt index 89fc0279a..eeb6eac6a 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/subscription/donate/card/CreditCardFragment.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/subscription/donate/card/CreditCardFragment.kt @@ -53,7 +53,7 @@ class CreditCardFragment : Fragment(R.layout.credit_card_fragment) { DonateToSignalType.GIFT -> DonationErrorSource.GIFT } - DonationCheckoutDelegate.ErrorHandler().attach(this, errorSource) + DonationCheckoutDelegate.ErrorHandler().attach(this, null, errorSource) setFragmentResultListener(StripePaymentInProgressFragment.REQUEST_KEY) { _, bundle -> val result: DonationProcessorActionResult = bundle.getParcelable(StripePaymentInProgressFragment.REQUEST_KEY)!!