Skip to content

Commit

Permalink
Merge pull request #1896 from Adyen/fix/google-pay-activity-result
Browse files Browse the repository at this point in the history
Move loading state to GooglePayView
  • Loading branch information
OscarSpruit authored Nov 21, 2024
2 parents afc18b0 + 179237a commit 4ffbf7f
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,22 @@ internal class DefaultGooglePayDelegate(

private fun updateOutputData(
isButtonVisible: Boolean = this.outputData.isButtonVisible,
isLoading: Boolean = this.outputData.isLoading,
paymentData: PaymentData? = this.outputData.paymentData,
) {
val newOutputData = createOutputData(isButtonVisible, paymentData)
val newOutputData = createOutputData(isButtonVisible, isLoading, paymentData)
_outputDataFlow.tryEmit(newOutputData)
updateComponentState(newOutputData)
}

private fun createOutputData(
isButtonVisible: Boolean = componentParams.isSubmitButtonVisible,
isLoading: Boolean = !isButtonVisible,
paymentData: PaymentData? = null,
): GooglePayOutputData {
return GooglePayOutputData(
isButtonVisible = isButtonVisible,
isLoading = isLoading,
paymentData = paymentData,
)
}
Expand Down Expand Up @@ -204,7 +207,7 @@ internal class DefaultGooglePayDelegate(
override fun onSubmit() {
adyenLog(AdyenLogLevel.DEBUG) { "onSubmit" }

_viewFlow.tryEmit(PaymentInProgressViewType)
updateOutputData(isButtonVisible = false, isLoading = true)

val paymentDataRequest = GooglePayUtils.createPaymentDataRequest(componentParams)
val paymentDataTask = paymentsClient.loadPaymentData(paymentDataRequest)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import androidx.core.view.isVisible
import com.adyen.checkout.components.core.internal.ui.ComponentDelegate
import com.adyen.checkout.googlepay.databinding.ViewGooglePayBinding
import com.adyen.checkout.googlepay.internal.ui.model.GooglePayOutputData
import com.adyen.checkout.ui.core.internal.ui.ComponentView
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach

internal class GooglePayView
@JvmOverloads
Expand All @@ -41,12 +45,24 @@ internal constructor(
require(delegate is GooglePayDelegate) { "Unsupported delegate type" }
this.delegate = delegate
initializeFragment(delegate)

observeDelegate(delegate, coroutineScope)
}

private fun initializeFragment(delegate: GooglePayDelegate) {
binding.fragmentContainer.getFragment<GooglePayFragment?>()?.initialize(delegate)
}

private fun observeDelegate(delegate: GooglePayDelegate, coroutineScope: CoroutineScope) {
delegate.outputDataFlow
.onEach { outputDataChanged(it) }
.launchIn(coroutineScope)
}

private fun outputDataChanged(outputData: GooglePayOutputData) {
binding.processingPaymentView.isVisible = outputData.isLoading
}

override fun highlightValidationErrors() = Unit

override fun getView(): View = this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import com.adyen.checkout.ui.core.internal.ui.ComponentView
import com.adyen.checkout.ui.core.internal.ui.ComponentViewType
import com.adyen.checkout.ui.core.internal.ui.ViewProvider
import com.adyen.checkout.ui.core.internal.ui.view.PayButton
import com.adyen.checkout.ui.core.internal.ui.view.ProcessingPaymentView

internal class GooglePayViewProvider : ViewProvider {

Expand All @@ -25,7 +24,6 @@ internal class GooglePayViewProvider : ViewProvider {
context: Context,
): ComponentView = when (viewType) {
GooglePayComponentViewType -> GooglePayView(context)
PaymentInProgressViewType -> ProcessingPaymentView(context)
else -> throw IllegalArgumentException("Unsupported view type")
}

Expand All @@ -34,7 +32,6 @@ internal class GooglePayViewProvider : ViewProvider {
layoutInflater: LayoutInflater
): ComponentView = when (viewType) {
GooglePayComponentViewType -> GooglePayView(layoutInflater)
PaymentInProgressViewType -> ProcessingPaymentView(layoutInflater.context)
else -> throw IllegalArgumentException("Unsupported view type")
}
}
Expand All @@ -50,8 +47,3 @@ internal object GooglePayComponentViewType : ButtonComponentViewType {

override val buttonTextResId: Int = ButtonComponentViewType.DEFAULT_BUTTON_TEXT_RES_ID
}

internal object PaymentInProgressViewType : ComponentViewType {

override val viewProvider: ViewProvider get() = GooglePayViewProvider()
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ import com.google.android.gms.wallet.PaymentData

internal data class GooglePayOutputData(
val isButtonVisible: Boolean,
val isLoading: Boolean,
val paymentData: PaymentData?,
)
8 changes: 8 additions & 0 deletions googlepay/src/main/res/layout/view_google_pay.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,12 @@
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<!-- ProcessingPaymentView has a RestrictTo annotation, but is actually available -->
<!--suppress AndroidUnresolvableTag -->
<com.adyen.checkout.ui.core.internal.ui.view.ProcessingPaymentView
android:id="@+id/processingPaymentView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" />

</merge>
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,18 @@ internal class DefaultGooglePayDelegateTest(
assertEquals(task, payEventFlow.latestValue)
}

@Test
fun `when onSubmit is called, then button is hidden and loading state is shown`() = runTest {
val outputDataFlow = delegate.outputDataFlow.test(testScheduler)
delegate.initialize(CoroutineScope(UnconfinedTestDispatcher()))

delegate.onSubmit()

val latestOutputData = outputDataFlow.latestValue
assertFalse(latestOutputData.isButtonVisible)
assertTrue(latestOutputData.isLoading)
}

@ParameterizedTest
@MethodSource("amountSource")
fun `when input data is valid then amount is propagated in component state if set`(
Expand Down Expand Up @@ -353,8 +365,9 @@ internal class DefaultGooglePayDelegateTest(

private fun createOutputData(
isButtonVisible: Boolean = false,
isLoading: Boolean = !isButtonVisible,
paymentData: PaymentData? = null,
) = GooglePayOutputData(isButtonVisible, paymentData)
) = GooglePayOutputData(isButtonVisible, isLoading, paymentData)

companion object {
private val TEST_ORDER = OrderRequest("PSP", "ORDER_DATA")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import com.adyen.checkout.ui.core.internal.util.setLocalizedTextFromStyle
import kotlinx.coroutines.CoroutineScope

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
class ProcessingPaymentView @JvmOverloads constructor(
class ProcessingPaymentView
@JvmOverloads
constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0,
Expand Down

0 comments on commit 4ffbf7f

Please sign in to comment.