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

Add FormActivityViewModel #9946

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
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
24 changes: 24 additions & 0 deletions paymentsheet/api/paymentsheet.api
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,30 @@ public final class com/stripe/android/paymentelement/embedded/EmbeddedConfirmati
public synthetic fun newArray (I)[Ljava/lang/Object;
}

public final class com/stripe/android/paymentelement/embedded/FormContract$Args$Creator : android/os/Parcelable$Creator {
public fun <init> ()V
public final fun createFromParcel (Landroid/os/Parcel;)Lcom/stripe/android/paymentelement/embedded/FormContract$Args;
public synthetic fun createFromParcel (Landroid/os/Parcel;)Ljava/lang/Object;
public final fun newArray (I)[Lcom/stripe/android/paymentelement/embedded/FormContract$Args;
public synthetic fun newArray (I)[Ljava/lang/Object;
}

public final class com/stripe/android/paymentelement/embedded/FormResult$Cancelled$Creator : android/os/Parcelable$Creator {
public fun <init> ()V
public final fun createFromParcel (Landroid/os/Parcel;)Lcom/stripe/android/paymentelement/embedded/FormResult$Cancelled;
public synthetic fun createFromParcel (Landroid/os/Parcel;)Ljava/lang/Object;
public final fun newArray (I)[Lcom/stripe/android/paymentelement/embedded/FormResult$Cancelled;
public synthetic fun newArray (I)[Ljava/lang/Object;
}

public final class com/stripe/android/paymentelement/embedded/FormResult$Complete$Creator : android/os/Parcelable$Creator {
public fun <init> ()V
public final fun createFromParcel (Landroid/os/Parcel;)Lcom/stripe/android/paymentelement/embedded/FormResult$Complete;
public synthetic fun createFromParcel (Landroid/os/Parcel;)Ljava/lang/Object;
public final fun newArray (I)[Lcom/stripe/android/paymentelement/embedded/FormResult$Complete;
public synthetic fun newArray (I)[Ljava/lang/Object;
}

public final class com/stripe/android/paymentsheet/BuildConfig {
public static final field BUILD_TYPE Ljava/lang/String;
public static final field DEBUG Z
Expand Down
3 changes: 3 additions & 0 deletions paymentsheet/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
<activity
android:name=".paymentdatacollection.cvcrecollection.CvcRecollectionActivity"
android:theme="@style/StripePaymentSheetDefaultTheme" />
<activity
android:name="com.stripe.android.paymentelement.embedded.FormActivity"
android:theme="@style/StripePaymentSheetDefaultTheme" />

<activity
android:name="com.stripe.android.link.LinkActivity"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,14 @@ import kotlinx.parcelize.Parcelize
class EmbeddedPaymentElement private constructor(
private val embeddedConfirmationHelper: EmbeddedConfirmationHelper,
private val sharedViewModel: SharedPaymentElementViewModel,
private val activityResultCaller: ActivityResultCaller,
private val lifecycleOwner: LifecycleOwner
) {

init {
sharedViewModel.initEmbeddedActivityLauncher(activityResultCaller, lifecycleOwner)
}

/**
* Contains information about the customer's selected payment option.
* Use this to display the payment option in your own UI.
Expand Down Expand Up @@ -503,6 +510,8 @@ class EmbeddedPaymentElement private constructor(
confirmationStateSupplier = { sharedViewModel.confirmationStateHolder.state },
),
sharedViewModel = sharedViewModel,
activityResultCaller = activityResultCaller,
lifecycleOwner = lifecycleOwner
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.stripe.android.paymentelement.embedded

import androidx.activity.result.ActivityResultCaller
import androidx.activity.result.ActivityResultLauncher
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodMetadata

internal interface EmbeddedActivityLauncher {
var formLauncher: ((code: String, paymentMethodMetadata: PaymentMethodMetadata?) -> Unit)?
}

internal class DefaultEmbeddedActivityLauncher(
private val activityResultCaller: ActivityResultCaller,
private val lifecycleOwner: LifecycleOwner,
private val selectionHolder: EmbeddedSelectionHolder
) : EmbeddedActivityLauncher {

private var formActivityLauncher: ActivityResultLauncher<FormContract.Args> =
activityResultCaller.registerForActivityResult(FormContract()) { result ->
if (result is FormResult.Complete) {
selectionHolder.set(result.selection)
}
}

override var formLauncher: ((code: String, paymentMethodMetadata: PaymentMethodMetadata?) -> Unit)? =
{ code, metadata ->
formActivityLauncher.launch(FormContract.Args(code, metadata))
}

init {
lifecycleOwner.lifecycle.addObserver(
object : DefaultLifecycleObserver {
override fun onDestroy(owner: LifecycleOwner) {
formActivityLauncher.unregister()
formLauncher = null
super.onDestroy(owner)
}
}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ internal interface EmbeddedContentHelper {
rowStyle: Embedded.RowStyle,
embeddedViewDisplaysMandateText: Boolean,
)

fun setFormLauncher(formLauncher: ((code: String, paymentMethodMetaData: PaymentMethodMetadata?) -> Unit)?)
}

internal fun interface EmbeddedContentHelperFactory {
Expand Down Expand Up @@ -80,6 +82,8 @@ internal class DefaultEmbeddedContentHelper @AssistedInject constructor(
private val _embeddedContent = MutableStateFlow<EmbeddedContent?>(null)
override val embeddedContent: StateFlow<EmbeddedContent?> = _embeddedContent.asStateFlow()

private var formLauncher: ((code: String, paymentMethodMetaData: PaymentMethodMetadata?) -> Unit)? = null

init {
coroutineScope.launch {
state.collect { state ->
Expand Down Expand Up @@ -119,6 +123,12 @@ internal class DefaultEmbeddedContentHelper @AssistedInject constructor(
)
}

override fun setFormLauncher(
formLauncher: ((code: String, paymentMethodMetaData: PaymentMethodMetadata?) -> Unit)?
) {
this.formLauncher = formLauncher
}

private fun createInteractor(
coroutineScope: CoroutineScope,
paymentMethodMetadata: PaymentMethodMetadata,
Expand Down Expand Up @@ -161,6 +171,7 @@ internal class DefaultEmbeddedContentHelper @AssistedInject constructor(
transitionToManageScreen = {
},
transitionToFormScreen = {
formLauncher?.invoke(it, state.value?.paymentMethodMetadata)
},
paymentMethods = customerStateHolder.paymentMethods,
mostRecentlySelectedSavedPaymentMethod = customerStateHolder.mostRecentlySelectedSavedPaymentMethod,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.stripe.android.paymentelement.embedded

import android.app.Activity
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.material.ExperimentalMaterialApi
import com.stripe.android.common.ui.ElementsBottomSheetLayout
import com.stripe.android.paymentsheet.verticalmode.VerticalModeFormUI
import com.stripe.android.uicore.StripeTheme
import com.stripe.android.uicore.elements.bottomsheet.rememberStripeBottomSheetState
import com.stripe.android.uicore.utils.fadeOut

internal class FormActivity : AppCompatActivity() {
private val formArgs: FormContract.Args? by lazy {
FormContract.Args.fromIntent(intent)
}

private val viewModel: FormActivityViewModel by viewModels {
FormActivityViewModel.Factory()
}

@OptIn(ExperimentalMaterialApi::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

if (formArgs == null) {
setFormResult(FormResult.Cancelled)
finish()
return
}

formArgs?.let { args ->
args.paymentMethodMetadata?. let { metadata ->
viewModel.initializeFormInteractor(
metadata,
args.selectedPaymentMethodCode,
)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I update to use AssistedInjection?

}
}

setContent {
StripeTheme {
val bottomSheetState = rememberStripeBottomSheetState()
ElementsBottomSheetLayout(
state = bottomSheetState,
onDismissed = {
setResult(
Activity.RESULT_OK,
FormResult.toIntent(intent, FormResult.Cancelled)
)
finish()
}
) {
VerticalModeFormUI(
viewModel.formInteractor,
false,
)
}
}
}
}

override fun finish() {
super.finish()
fadeOut()
}

private fun setFormResult(result: FormResult) {
setResult(
Activity.RESULT_OK,
FormResult.toIntent(intent, result)
)
}
}
Loading
Loading