Skip to content

Commit

Permalink
Always observe fragmentConfig (stripe#4034)
Browse files Browse the repository at this point in the history
  • Loading branch information
brnunes-stripe authored Jul 21, 2021
1 parent eee877a commit 0d88668
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,24 +106,7 @@ internal class PaymentOptionsActivity : BaseSheetActivity<PaymentOptionResult>()
}
}

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
Expand All @@ -138,6 +121,16 @@ internal class PaymentOptionsActivity : BaseSheetActivity<PaymentOptionResult>()
)
}
}

supportFragmentManager.registerFragmentLifecycleCallbacks(
object : FragmentManager.FragmentLifecycleCallbacks() {
override fun onFragmentStarted(fm: FragmentManager, fragment: Fragment) {
viewBinding.addButton.isVisible =
fragment is PaymentOptionsAddPaymentMethodFragment
}
},
false
)
}

private fun setupAddButton(addButton: PrimaryButton) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,15 @@ internal class PaymentSheetActivity : BaseSheetActivity<PaymentSheetResult>() {
}
}

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 ->
Expand Down Expand Up @@ -187,19 +192,6 @@ internal class PaymentSheetActivity : BaseSheetActivity<PaymentSheetResult>() {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ internal abstract class BaseSheetViewModel<TransitionTargetType>(
fetchSavedSelection()
}

fun fetchFragmentConfig() = MediatorLiveData<FragmentConfig?>().also { configLiveData ->
val fragmentConfig = MediatorLiveData<FragmentConfig?>().also { configLiveData ->
listOf(
savedSelection,
stripeIntent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<FragmentConfig>()
viewModel.fetchFragmentConfig().observeForever { config ->
viewModel.fragmentConfig.observeForever { config ->
if (config != null) {
configs.add(config)
}
Expand Down

0 comments on commit 0d88668

Please sign in to comment.