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

Fix issue with enter address manually button #8596

Merged
merged 5 commits into from
Jun 10, 2024
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## XX.XX.XX - 20XX-XX-XX

### AddressElement
* [FIXED][8596](https://github.com/stripe/stripe-android/pull/8596) Fixed issue where "Enter address manually" button only worked the first time it was clicked.

### Payments
* [FIXED][8590](https://github.com/stripe/stripe-android/pull/8590) Fix an issue where cancelling payment for Amazon Pay and Cash App Pay would show an error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ internal class AddressElementNavigator @Inject constructor() {
}
}
}

companion object {
internal const val FORCE_EXPANDED_FORM_KEY = "force_expanded_form"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import com.stripe.android.paymentsheet.PaymentSheet
import com.stripe.android.paymentsheet.addresselement.AddressElementNavigator.Companion.FORCE_EXPANDED_FORM_KEY
import com.stripe.android.paymentsheet.addresselement.analytics.AddressLauncherEventReporter
import com.stripe.android.paymentsheet.injection.AutocompleteViewModelSubcomponent
import com.stripe.android.ui.core.elements.autocomplete.PlacesClientProxy
Expand Down Expand Up @@ -150,6 +151,7 @@ internal class AutocompleteViewModel @Inject constructor(
}

fun onEnterAddressManually() {
navigator.setResult(FORCE_EXPANDED_FORM_KEY, true)
setResultAndGoBack(
AddressDetails(
address = PaymentSheet.Address(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.stripe.android.paymentsheet.injection.InputAddressViewModelSubcompone
import com.stripe.android.ui.core.elements.LayoutSpec
import com.stripe.android.uicore.elements.IdentifierSpec
import com.stripe.android.uicore.forms.FormFieldEntry
import com.stripe.android.uicore.utils.combineAsStateFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
Expand All @@ -25,6 +26,9 @@ internal class InputAddressViewModel @Inject constructor(
private val _collectedAddress = MutableStateFlow(args.config?.address)
val collectedAddress: StateFlow<AddressDetails?> = _collectedAddress

private val _forceExpandedForm = MutableStateFlow<Boolean?>(false)
private val forceExpandedForm: StateFlow<Boolean?> = _forceExpandedForm

private val _formController = MutableStateFlow<FormController?>(null)
val formController: StateFlow<FormController?> = _formController

Expand All @@ -50,7 +54,21 @@ internal class InputAddressViewModel @Inject constructor(
}

viewModelScope.launch {
collectedAddress.collect { addressDetails ->
navigator.getResultFlow<Boolean?>(
AddressElementNavigator.FORCE_EXPANDED_FORM_KEY
)?.collect {
_forceExpandedForm.emit(it)
}
}

viewModelScope.launch {
combineAsStateFlow(collectedAddress, forceExpandedForm) { collectedAddress, forceExpandedForm ->
Pair(
collectedAddress,
forceExpandedForm
)
}.collect { (addressDetails, forceExpandedFormNullable) ->
val forceExpandedForm = forceExpandedFormNullable ?: false
val initialValues: Map<IdentifierSpec, String?> = addressDetails
?.toIdentifierMap()
?: emptyMap()
Expand All @@ -59,7 +77,11 @@ internal class InputAddressViewModel @Inject constructor(
.stripeIntent(null)
.merchantName("")
.shippingValues(null)
.formSpec(buildFormSpec(addressDetails?.address?.line1 == null))
.formSpec(
buildFormSpec(
condensedForm = !forceExpandedForm && addressDetails?.address?.line1 == null
)
)
.initialValues(initialValues)
.build().formController
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class InputAddressViewModelTest {
fun `autocomplete address passed is collected to start`() = runTest(UnconfinedTestDispatcher()) {
val expectedAddress = AddressDetails(name = "skyler", address = PaymentSheet.Address(country = "US"))
val flow = MutableStateFlow<AddressDetails?>(expectedAddress)
whenever(navigator.getResultFlow<AddressDetails?>(any())).thenReturn(flow)
whenever(navigator.getResultFlow<AddressDetails?>(AddressDetails.KEY)).thenReturn(flow)

val viewModel = createViewModel()
assertThat(viewModel.collectedAddress.value).isEqualTo(expectedAddress)
Expand All @@ -89,7 +89,7 @@ class InputAddressViewModelTest {
fun `takes only fields in new address`() = runTest(UnconfinedTestDispatcher()) {
val usAddress = AddressDetails(name = "skyler", address = PaymentSheet.Address(country = "US"))
val flow = MutableStateFlow<AddressDetails?>(usAddress)
whenever(navigator.getResultFlow<AddressDetails?>(any())).thenReturn(flow)
whenever(navigator.getResultFlow<AddressDetails?>(AddressDetails.KEY)).thenReturn(flow)

val viewModel = createViewModel()
assertThat(viewModel.collectedAddress.value).isEqualTo(usAddress)
Expand Down
Loading