Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always observe fragmentConfig #4034

Merged
merged 1 commit into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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