Skip to content

Commit

Permalink
Remove usage of onBackPressed()
Browse files Browse the repository at this point in the history
  • Loading branch information
tillh-stripe committed Jan 13, 2023
1 parent 73fc81b commit 4baed97
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.stripe.android.financialconnections
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.activity.addCallback
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
import androidx.appcompat.app.AppCompatActivity
import com.airbnb.mvrx.Mavericks
Expand Down Expand Up @@ -42,18 +43,17 @@ internal class FinancialConnectionsSheetActivity :
viewModel.onEach { postInvalidate() }
if (savedInstanceState != null) viewModel.onActivityRecreated()
}

onBackPressedDispatcher.addCallback {
finishWithResult(FinancialConnectionsSheetActivityResult.Canceled)
}
}

override fun onResume() {
super.onResume()
viewModel.onResume()
}

override fun onBackPressed() {
super.onBackPressed()
finishWithResult(FinancialConnectionsSheetActivityResult.Canceled)
}

/**
* Handles new intents in the form of the redirect from the custom tab hosted auth flow
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.graphics.Color
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.activity.addCallback
import androidx.activity.viewModels
import androidx.annotation.ColorInt
import androidx.annotation.VisibleForTesting
Expand Down Expand Up @@ -59,6 +60,14 @@ class PaymentAuthWebViewActivity : AppCompatActivity() {
setSupportActionBar(viewBinding.toolbar)
customizeToolbar()

onBackPressedDispatcher.addCallback {
if (viewBinding.webView.canGoBack()) {
viewBinding.webView.goBack()
} else {
cancelIntentSource()
}
}

val clientSecret = args.clientSecret
setResult(Activity.RESULT_OK, createResultIntent(viewModel.paymentResult))

Expand Down Expand Up @@ -148,14 +157,6 @@ class PaymentAuthWebViewActivity : AppCompatActivity() {
return super.onOptionsItemSelected(item)
}

override fun onBackPressed() {
if (viewBinding.webView.canGoBack()) {
viewBinding.webView.goBack()
} else {
cancelIntentSource()
}
}

private fun cancelIntentSource() {
setResult(Activity.RESULT_OK, viewModel.cancellationResult)
finish()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.ViewGroup
import androidx.activity.addCallback
import androidx.activity.viewModels
import androidx.viewpager.widget.ViewPager
import com.stripe.android.CustomerSession
Expand Down Expand Up @@ -78,6 +79,11 @@ class PaymentFlowActivity : StripeActivity() {
paymentFlowPagerAdapter.shippingInformation = shippingInformation
paymentFlowPagerAdapter.selectedShippingMethod = viewModel.selectedShippingMethod

val onBackPressedCallback = onBackPressedDispatcher.addCallback {
viewModel.currentPage -= 1
viewPager.currentItem = viewModel.currentPage
}

viewPager.adapter = paymentFlowPagerAdapter
viewPager.addOnPageChangeListener(
object : ViewPager.OnPageChangeListener {
Expand All @@ -89,6 +95,8 @@ class PaymentFlowActivity : StripeActivity() {
viewModel.isShippingInfoSubmitted = false
paymentFlowPagerAdapter.isShippingInfoSubmitted = false
}

onBackPressedCallback.isEnabled = hasPreviousPage()
}

override fun onPageScrollStateChanged(i: Int) {
Expand Down Expand Up @@ -246,15 +254,6 @@ class PaymentFlowActivity : StripeActivity() {
finish()
}

override fun onBackPressed() {
if (hasPreviousPage()) {
viewModel.currentPage -= 1
viewPager.currentItem = viewModel.currentPage
} else {
super.onBackPressed()
}
}

internal companion object {
internal const val PRODUCT_TOKEN: String = "PaymentFlowActivity"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.text.util.Linkify
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.activity.addCallback
import androidx.activity.viewModels
import androidx.annotation.VisibleForTesting
import androidx.appcompat.app.AppCompatActivity
Expand Down Expand Up @@ -100,6 +101,10 @@ class PaymentMethodsActivity : AppCompatActivity() {
window.addFlags(it)
}

onBackPressedDispatcher.addCallback {
finishWithResult(adapter.selectedPaymentMethod, Activity.RESULT_CANCELED)
}

viewModel.snackbarData.observe(this) { snackbarText ->
snackbarText?.let {
Snackbar.make(viewBinding.coordinator, it, Snackbar.LENGTH_SHORT).show()
Expand Down Expand Up @@ -213,10 +218,6 @@ class PaymentMethodsActivity : AppCompatActivity() {
}
}

override fun onBackPressed() {
finishWithResult(adapter.selectedPaymentMethod, Activity.RESULT_CANCELED)
}

private fun fetchCustomerPaymentMethods() {
viewModel.getPaymentMethods().observe(this) { result ->
result.fold(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ abstract class StripeActivity : AppCompatActivity() {
} else {
val handled = super.onOptionsItemSelected(item)
if (!handled) {
onBackPressed()
onBackPressedDispatcher.onBackPressed()
}
handled
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ class PaymentMethodsActivityTest {
paymentMethodsAdapter.selectedPaymentMethodId =
PaymentMethodFixtures.CARD_PAYMENT_METHODS[0].id

activity.onBackPressed()
activity.onBackPressedDispatcher.onBackPressed()

// Now it should be gone.
assertEquals(View.GONE, progressBar.visibility)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import android.view.WindowInsets
import android.view.WindowMetrics
import android.widget.ScrollView
import android.widget.TextView
import androidx.activity.addCallback
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.annotation.VisibleForTesting
Expand Down Expand Up @@ -128,9 +129,14 @@ internal abstract class BaseSheetActivity<ResultType> : AppCompatActivity() {
}
}

val onBackPressedCallback = onBackPressedDispatcher.addCallback {
viewModel.handleBackPressed()
}

viewModel.processing.launchAndCollectIn(this) { isProcessing ->
updateRootViewClickHandling(isProcessing)
toolbar.isEnabled = !isProcessing
onBackPressedCallback.isEnabled = !isProcessing
}

// Set Toolbar to act as the ActionBar so it displays the menu items.
Expand Down Expand Up @@ -190,13 +196,6 @@ internal abstract class BaseSheetActivity<ResultType> : AppCompatActivity() {
overridePendingTransition(AnimationConstants.FADE_IN, AnimationConstants.FADE_OUT)
}

@Deprecated("Deprecated in Java")
override fun onBackPressed() {
if (!viewModel.processing.value) {
viewModel.handleBackPressed()
}
}

protected fun closeSheet(
result: ResultType
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.lifecycle.Lifecycle
import androidx.lifecycle.SavedStateHandle
import androidx.test.core.app.ActivityScenario
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.Espresso.pressBack
import app.cash.turbine.test
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.common.truth.Truth.assertThat
Expand Down Expand Up @@ -228,7 +229,7 @@ internal class PaymentOptionsActivityTest {
.isTrue()

// Navigate back to payment options list
activity.onBackPressed()
pressBack()
idleLooper()

assertThat(activity.viewBinding.continueButton.isVisible)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ internal class PaymentSheetActivityTest {
assertThat(activity.viewBinding.message.isVisible).isTrue()
assertThat(activity.viewBinding.message.text.toString()).isEqualTo("some error")

activity.onBackPressed()
pressBack()

assertThat(activity.viewBinding.message.isVisible).isFalse()
}
Expand Down Expand Up @@ -467,13 +467,13 @@ internal class PaymentSheetActivityTest {
assertThat(activity.toolbar.navigationContentDescription)
.isEqualTo(context.getString(R.string.back))

activity.onBackPressed()
pressBack()
idleLooper()

assertThat(activity.toolbar.navigationContentDescription)
.isEqualTo(context.getString(R.string.stripe_paymentsheet_close))

activity.onBackPressed()
pressBack()
idleLooper()
// animating out
assertThat(activity.bottomSheetBehavior.state)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.util.Size
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.activity.addCallback
import androidx.annotation.RestrictTo
import com.stripe.android.camera.CameraPreviewImage
import com.stripe.android.camera.framework.Stats
Expand Down Expand Up @@ -194,6 +195,12 @@ internal class CardScanActivity : ScanActivity(), SimpleScanStateful<CardScanSta
return
}

onBackPressedDispatcher.addCallback {
runBlocking { scanStat.trackResult("user_canceled") }
resultListener.userCanceled(CancellationReason.Back)
closeScanner()
}

viewBinding.closeButton.setOnClickListener {
userClosedScanner()
}
Expand Down Expand Up @@ -226,15 +233,6 @@ internal class CardScanActivity : ScanActivity(), SimpleScanStateful<CardScanSta
super.onDestroy()
}

/**
* Cancel the scan when the user presses back.
*/
override fun onBackPressed() {
runBlocking { scanStat.trackResult("user_canceled") }
resultListener.userCanceled(CancellationReason.Back)
closeScanner()
}

override fun onFlashSupported(supported: Boolean) {
viewBinding.torchButton.setVisible(supported)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import android.util.Size
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.activity.addCallback
import androidx.appcompat.app.AlertDialog
import com.stripe.android.camera.CameraAdapter
import com.stripe.android.camera.CameraPermissionCheckingActivity
Expand Down Expand Up @@ -80,6 +81,12 @@ internal abstract class ScanActivity : CameraPermissionCheckingActivity(), Corou

Stats.startScan()

onBackPressedDispatcher.addCallback {
runBlocking { scanStat.trackResult("user_canceled") }
resultListener.userCanceled(CancellationReason.Back)
closeScanner()
}

if (!CameraAdapter.isCameraSupported(this)) {
showCameraNotSupportedDialog()
}
Expand Down Expand Up @@ -187,15 +194,6 @@ internal abstract class ScanActivity : CameraPermissionCheckingActivity(), Corou
closeScanner()
}

/**
* Cancel the scan when the user presses back.
*/
override fun onBackPressed() {
runBlocking { scanStat.trackResult("user_canceled") }
resultListener.userCanceled(CancellationReason.Back)
closeScanner()
}

/**
* The scan has been closed by the user.
*/
Expand Down

0 comments on commit 4baed97

Please sign in to comment.