Skip to content

Commit

Permalink
Make EmbeddedContent have real content. (#9768)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaynewstrom-stripe authored Dec 9, 2024
1 parent 6749823 commit 0c4cce6
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.os.Parcelable
import androidx.activity.result.ActivityResultCaller
import androidx.annotation.RestrictTo
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.text.AnnotatedString
import androidx.lifecycle.LifecycleOwner
Expand All @@ -18,12 +19,12 @@ import com.stripe.android.model.CardBrand
import com.stripe.android.model.PaymentIntent
import com.stripe.android.model.SetupIntent
import com.stripe.android.paymentelement.embedded.EmbeddedConfirmationHelper
import com.stripe.android.paymentelement.embedded.EmbeddedContent
import com.stripe.android.paymentelement.embedded.SharedPaymentElementViewModel
import com.stripe.android.paymentsheet.ExternalPaymentMethodConfirmHandler
import com.stripe.android.paymentsheet.PaymentSheet
import com.stripe.android.paymentsheet.addresselement.AddressDetails
import com.stripe.android.uicore.image.rememberDrawablePainter
import com.stripe.android.uicore.utils.collectAsState
import dev.drewhamilton.poko.Poko
import kotlinx.coroutines.flow.StateFlow
import kotlinx.parcelize.Parcelize
Expand Down Expand Up @@ -61,7 +62,8 @@ class EmbeddedPaymentElement private constructor(
*/
@Composable
fun Content() {
EmbeddedContent()
val embeddedContent by sharedViewModel.embeddedContent.collectAsState()
embeddedContent?.Content()
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,73 @@
package com.stripe.android.paymentelement.embedded

import androidx.compose.material.Text
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.dimensionResource
import androidx.compose.ui.unit.dp
import com.stripe.android.core.strings.ResolvableString
import com.stripe.android.paymentsheet.R
import com.stripe.android.paymentsheet.ui.ErrorMessage
import com.stripe.android.paymentsheet.ui.Mandate
import com.stripe.android.paymentsheet.verticalmode.PaymentMethodVerticalLayoutInteractor
import com.stripe.android.paymentsheet.verticalmode.PaymentMethodVerticalLayoutUI
import com.stripe.android.uicore.strings.resolve

@Composable
internal fun EmbeddedContent() {
Text("Hello World!")
@Immutable
internal data class EmbeddedContent(
private val interactor: PaymentMethodVerticalLayoutInteractor,
private val error: ResolvableString? = null,
private val mandate: ResolvableString? = null,
) {
@Composable
fun Content() {
val horizontalPadding = dimensionResource(R.dimen.stripe_paymentsheet_outer_spacing_horizontal)
Column(
modifier = Modifier
.padding(horizontal = horizontalPadding)
.padding(top = 8.dp)
) {
EmbeddedVerticalList()
EmbeddedError()
EmbeddedMandate()
}
}

@Composable
private fun EmbeddedVerticalList() {
PaymentMethodVerticalLayoutUI(
interactor = interactor,
modifier = Modifier.padding(bottom = 8.dp),
)
}

@Composable
private fun EmbeddedError() {
error?.let {
ErrorMessage(
error = it.resolve(),
modifier = Modifier
.padding(top = 2.dp, bottom = 8.dp)
.testTag(EMBEDDED_ERROR_TEXT_TEST_TAG),
)
}
}

@Composable
private fun EmbeddedMandate() {
Mandate(
mandateText = mandate?.resolve(),
modifier = Modifier
.padding(bottom = 8.dp)
.testTag(EMBEDDED_MANDATE_TEXT_TEST_TAG),
)
}

companion object {
const val EMBEDDED_ERROR_TEXT_TEST_TAG = "EMBEDDED_ERROR"
const val EMBEDDED_MANDATE_TEXT_TEST_TAG = "EMBEDDED_MANDATE"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ internal class SharedPaymentElementViewModel @Inject constructor(

val confirmationHandler = confirmationHandlerFactory.create(viewModelScope + ioContext)

private val _embeddedContent = MutableStateFlow<EmbeddedContent?>(null)
val embeddedContent: StateFlow<EmbeddedContent?> = _embeddedContent.asStateFlow()

@Volatile
var confirmationState: EmbeddedConfirmationHelper.State? = null

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.stripe.android.paymentsheet.navigation

import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.Dp
Expand Down Expand Up @@ -365,7 +366,7 @@ internal sealed interface PaymentSheetScreen {

@Composable
override fun Content(modifier: Modifier) {
PaymentMethodVerticalLayoutUI(interactor, modifier)
PaymentMethodVerticalLayoutUI(interactor, modifier.padding(horizontal = 20.dp))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ internal fun PaymentMethodVerticalLayoutUI(
},
imageLoader = imageLoader,
modifier = modifier
.padding(horizontal = 20.dp)
.testTag(TEST_TAG_PAYMENT_METHOD_VERTICAL_LAYOUT)
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.stripe.android.paymentelement.embedded

import com.stripe.android.core.strings.resolvableString
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodMetadataFactory
import com.stripe.android.paymentsheet.verticalmode.FakePaymentMethodVerticalLayoutInteractor
import com.stripe.android.screenshottesting.PaparazziRule
import org.junit.Rule
import kotlin.test.Test

internal class EmbeddedContentScreenshotTest {
@get:Rule
val paparazziRule = PaparazziRule()

@Test
fun displaysVerticalModeList() {
val metadata = PaymentMethodMetadataFactory.create()
val interactor = FakePaymentMethodVerticalLayoutInteractor.create(metadata)
val content = EmbeddedContent(interactor)
paparazziRule.snapshot {
content.Content()
}
}

@Test
fun displaysVerticalModeListWithError() {
val metadata = PaymentMethodMetadataFactory.create()
val interactor = FakePaymentMethodVerticalLayoutInteractor.create(metadata)
val content = EmbeddedContent(interactor, error = "Some error".resolvableString)
paparazziRule.snapshot {
content.Content()
}
}

@Test
fun displaysVerticalModeListWithMandate() {
val metadata = PaymentMethodMetadataFactory.create()
val interactor = FakePaymentMethodVerticalLayoutInteractor.create(metadata)
val content = EmbeddedContent(interactor, mandate = "Some mandate".resolvableString)
paparazziRule.snapshot {
content.Content()
}
}

@Test
fun displaysVerticalModeListWithErrorAndMandate() {
val metadata = PaymentMethodMetadataFactory.create()
val interactor = FakePaymentMethodVerticalLayoutInteractor.create(metadata)
val content = EmbeddedContent(
interactor = interactor,
error = "Some error".resolvableString,
mandate = "Some mandate".resolvableString
)
paparazziRule.snapshot {
content.Content()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.stripe.android.paymentsheet.verticalmode

import android.os.Build
import androidx.compose.foundation.layout.padding
import androidx.compose.ui.Modifier
import androidx.compose.ui.test.assert
import androidx.compose.ui.test.assertAll
import androidx.compose.ui.test.isSelected
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onChildren
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performClick
import androidx.compose.ui.unit.dp
import com.google.common.truth.Truth.assertThat
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodMetadataFactory
import com.stripe.android.lpmfoundations.paymentmethod.definitions.CardDefinition
Expand Down Expand Up @@ -291,7 +294,7 @@ internal class PaymentMethodVerticalLayoutUITest {
)

composeRule.setContent {
PaymentMethodVerticalLayoutUI(interactor)
PaymentMethodVerticalLayoutUI(interactor, Modifier.padding(horizontal = 20.dp))
}

Scenario(viewActionRecorder).apply(block)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0c4cce6

Please sign in to comment.