From 5c7539f7806971577111d6ba69200475763f6dc4 Mon Sep 17 00:00:00 2001 From: "Bruno R. Nunes" Date: Wed, 21 Jul 2021 00:21:20 -0700 Subject: [PATCH] Always observe `fragmentConfig` --- .../paymentsheet/PaymentOptionsActivity.kt | 29 +++++++------------ .../paymentsheet/PaymentSheetActivity.kt | 26 ++++++----------- .../viewmodels/BaseSheetViewModel.kt | 2 +- .../paymentsheet/PaymentSheetViewModelTest.kt | 4 +-- 4 files changed, 23 insertions(+), 38 deletions(-) diff --git a/payments-core/src/main/java/com/stripe/android/paymentsheet/PaymentOptionsActivity.kt b/payments-core/src/main/java/com/stripe/android/paymentsheet/PaymentOptionsActivity.kt index b2cc9f26a8f..e1f39f81c7d 100644 --- a/payments-core/src/main/java/com/stripe/android/paymentsheet/PaymentOptionsActivity.kt +++ b/payments-core/src/main/java/com/stripe/android/paymentsheet/PaymentOptionsActivity.kt @@ -106,24 +106,7 @@ internal class PaymentOptionsActivity : BaseSheetActivity() } } - if (savedInstanceState == null) { - // Only fetch initial state if the activity is being created for the first time. - // Otherwise the FragmentManager will correctly restore the previous state. - fetchConfig(starterArgs) - } - - supportFragmentManager.registerFragmentLifecycleCallbacks( - object : FragmentManager.FragmentLifecycleCallbacks() { - override fun onFragmentStarted(fm: FragmentManager, fragment: Fragment) { - viewBinding.addButton.isVisible = fragment is PaymentOptionsAddPaymentMethodFragment - } - }, - false - ) - } - - private fun fetchConfig(starterArgs: PaymentOptionContract.Args) { - viewModel.fetchFragmentConfig().observe(this) { config -> + viewModel.fragmentConfig.observe(this) { config -> if (config != null) { viewModel.transitionTo( // It would be nice to see this condition move into the PaymentOptionsListFragment @@ -138,6 +121,16 @@ internal class PaymentOptionsActivity : BaseSheetActivity() ) } } + + supportFragmentManager.registerFragmentLifecycleCallbacks( + object : FragmentManager.FragmentLifecycleCallbacks() { + override fun onFragmentStarted(fm: FragmentManager, fragment: Fragment) { + viewBinding.addButton.isVisible = + fragment is PaymentOptionsAddPaymentMethodFragment + } + }, + false + ) } private fun setupAddButton(addButton: PrimaryButton) { diff --git a/payments-core/src/main/java/com/stripe/android/paymentsheet/PaymentSheetActivity.kt b/payments-core/src/main/java/com/stripe/android/paymentsheet/PaymentSheetActivity.kt index 94f340115ea..d0b91e270c8 100644 --- a/payments-core/src/main/java/com/stripe/android/paymentsheet/PaymentSheetActivity.kt +++ b/payments-core/src/main/java/com/stripe/android/paymentsheet/PaymentSheetActivity.kt @@ -147,10 +147,15 @@ internal class PaymentSheetActivity : BaseSheetActivity() { } } - if (savedInstanceState == null) { - // Only fetch initial state if the activity is being created for the first time. - // Otherwise the FragmentManager will correctly restore the previous state. - fetchConfig() + viewModel.fragmentConfig.observe(this) { config -> + if (config != null) { + val target = if (config.paymentMethods.isEmpty()) { + PaymentSheetViewModel.TransitionTarget.AddPaymentMethodSheet(config) + } else { + PaymentSheetViewModel.TransitionTarget.SelectSavedPaymentMethod(config) + } + viewModel.transitionTo(target) + } } viewModel.startConfirm.observe(this) { event -> @@ -187,19 +192,6 @@ internal class PaymentSheetActivity : BaseSheetActivity() { messageView.text = userMessage?.message } - private fun fetchConfig() { - viewModel.fetchFragmentConfig().observe(this) { config -> - if (config != null) { - val target = if (config.paymentMethods.isEmpty()) { - PaymentSheetViewModel.TransitionTarget.AddPaymentMethodSheet(config) - } else { - PaymentSheetViewModel.TransitionTarget.SelectSavedPaymentMethod(config) - } - viewModel.transitionTo(target) - } - } - } - private fun onTransitionTarget( transitionTarget: PaymentSheetViewModel.TransitionTarget, fragmentArgs: Bundle diff --git a/payments-core/src/main/java/com/stripe/android/paymentsheet/viewmodels/BaseSheetViewModel.kt b/payments-core/src/main/java/com/stripe/android/paymentsheet/viewmodels/BaseSheetViewModel.kt index fd610a20a92..c12fa441d38 100644 --- a/payments-core/src/main/java/com/stripe/android/paymentsheet/viewmodels/BaseSheetViewModel.kt +++ b/payments-core/src/main/java/com/stripe/android/paymentsheet/viewmodels/BaseSheetViewModel.kt @@ -106,7 +106,7 @@ internal abstract class BaseSheetViewModel( fetchSavedSelection() } - fun fetchFragmentConfig() = MediatorLiveData().also { configLiveData -> + val fragmentConfig = MediatorLiveData().also { configLiveData -> listOf( savedSelection, stripeIntent, diff --git a/payments-core/src/test/java/com/stripe/android/paymentsheet/PaymentSheetViewModelTest.kt b/payments-core/src/test/java/com/stripe/android/paymentsheet/PaymentSheetViewModelTest.kt index a69ddb91c5b..004b23ce630 100644 --- a/payments-core/src/test/java/com/stripe/android/paymentsheet/PaymentSheetViewModelTest.kt +++ b/payments-core/src/test/java/com/stripe/android/paymentsheet/PaymentSheetViewModelTest.kt @@ -674,12 +674,12 @@ internal class PaymentSheetViewModelTest { } @Test - fun `fetchFragmentConfig() when all data is ready should emit value`() { + fun `fragmentConfig when all data is ready should emit value`() { viewModel.fetchStripeIntent() viewModel.fetchIsGooglePayReady() val configs = mutableListOf() - viewModel.fetchFragmentConfig().observeForever { config -> + viewModel.fragmentConfig.observeForever { config -> if (config != null) { configs.add(config) }