Skip to content
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 @@ -5,20 +5,26 @@ import android.os.Parcelable
import androidx.activity.ComponentActivity
import androidx.annotation.RestrictTo
import androidx.lifecycle.SavedStateHandle
import com.stripe.android.checkout.injection.DaggerCheckoutControllerComponent
import com.stripe.android.core.injection.ViewModelScope
import com.stripe.android.paymentelement.CheckoutSessionPreview
import dev.drewhamilton.poko.Poko
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.parcelize.Parcelize
import javax.inject.Inject
import javax.inject.Singleton

@Singleton

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

why is this a singleton? I thought we could have multiple checkout controller instances for different checkout sessions: https://stripe.slack.com/archives/C0B9H95FK6F/p1783625928531009?thread_ts=1783616873.322879&cid=C0B9H95FK6F

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Singleton in the dagger graph. No an actual singleton concept. We'll still support multiple controller instances with this.

@CheckoutSessionPreview
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
@Suppress("TooManyFunctions", "UnusedParameter")
class CheckoutController(
application: Application,
savedStateHandle: SavedStateHandle,
class CheckoutController @Inject internal constructor(
resultCallback: ResultCallback,
@ViewModelScope private val viewModelScope: CoroutineScope,
) {
private val _checkoutSession = MutableStateFlow<CheckoutSession?>(null)
val checkoutSession: StateFlow<CheckoutSession?> = _checkoutSession.asStateFlow()
Expand Down Expand Up @@ -75,13 +81,31 @@ class CheckoutController(
}

fun destroy() {
TODO("Not yet implemented")
viewModelScope.cancel()
}

fun clearPaymentOption() {
TODO("Not yet implemented")
}

@CheckoutSessionPreview
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
class Builder(
private val application: Application,
private val savedStateHandle: SavedStateHandle,
private val resultCallback: ResultCallback,
) {
fun build(): CheckoutController {
val component = DaggerCheckoutControllerComponent.factory().create(
application = application,
savedStateHandle = savedStateHandle,
resultCallback = resultCallback,
)

return component.checkoutController
}
}

@CheckoutSessionPreview
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
class Configuration {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
@file:OptIn(CheckoutSessionPreview::class)

package com.stripe.android.checkout.injection

import android.app.Application
import androidx.lifecycle.SavedStateHandle
import com.stripe.android.checkout.CheckoutController
import com.stripe.android.core.injection.ViewModelScope
import com.stripe.android.paymentelement.CheckoutSessionPreview
import dagger.BindsInstance
import dagger.Component
import dagger.Module
import dagger.Provides
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import javax.inject.Singleton

@Singleton
@Component(
modules = [
CheckoutControllerModule::class,
],
)
internal interface CheckoutControllerComponent {
val checkoutController: CheckoutController

@Component.Factory
interface Factory {
fun create(
@BindsInstance application: Application,
@BindsInstance savedStateHandle: SavedStateHandle,
@BindsInstance resultCallback: CheckoutController.ResultCallback,
): CheckoutControllerComponent
}
}

@Module
internal object CheckoutControllerModule {
@Provides
@Singleton
@ViewModelScope
fun provideViewModelScope(): CoroutineScope {
return CoroutineScope(Dispatchers.Main)
}
}
Loading