Skip to content

Commit

Permalink
More clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
toluo-stripe committed Jan 4, 2025
1 parent 9493e3e commit 169b022
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 58 deletions.
21 changes: 8 additions & 13 deletions paymentsheet/src/main/java/com/stripe/android/link/LinkActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.annotation.VisibleForTesting
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.ModalBottomSheetValue
Expand All @@ -21,8 +20,6 @@ import androidx.compose.runtime.setValue
import androidx.core.os.bundleOf
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.stripe.android.core.Logger
Expand All @@ -33,9 +30,7 @@ import com.stripe.android.uicore.utils.collectAsState
import kotlinx.coroutines.launch

internal class LinkActivity : ComponentActivity() {
internal val viewModel: LinkActivityViewModel by viewModels {
LinkActivityViewModel.factory()
}
internal var viewModel: LinkActivityViewModel? = null

@VisibleForTesting
internal lateinit var navController: NavHostController
Expand All @@ -44,13 +39,13 @@ internal class LinkActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

// try {
// viewModel = ViewModelProvider(this, LinkActivityViewModel.factory())[LinkActivityViewModel::class.java]
// } catch (e: NoArgsException) {
// Logger.getInstance(BuildConfig.DEBUG).error("Failed to create LinkActivityViewModel", e)
// setResult(Activity.RESULT_CANCELED)
// finish()
// }
try {
viewModel = ViewModelProvider(this, LinkActivityViewModel.factory())[LinkActivityViewModel::class.java]
} catch (e: NoArgsException) {
Logger.getInstance(BuildConfig.DEBUG).error("Failed to create LinkActivityViewModel", e)
setResult(Activity.RESULT_CANCELED)
finish()
}

val vm = viewModel ?: return
vm.registerFromActivity(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.stripe.android.link

import android.app.Application
import android.util.Log
import androidx.activity.result.ActivityResultCaller
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
Expand Down Expand Up @@ -54,7 +53,6 @@ internal class LinkActivityViewModel @Inject constructor(
var dismissWithResult: ((LinkActivityResult) -> Unit)? = null

init {
Log.d("TOLUWANI", "create new vm [${navController?.currentDestination?.route}]")
confirmationHandler = confirmationHandlerFactory.create(viewModelScope)
viewModelScope.launch {

Expand All @@ -70,15 +68,10 @@ internal class LinkActivityViewModel @Inject constructor(
}
}

/**
* Used to set up any dependencies that require a reference to the current Activity.
* Must be called from the Activity's `onCreate`.
*/
fun registerFromActivity(
activityResultCaller: ActivityResultCaller,
lifecycleOwner: LifecycleOwner,
) {
Log.d("TOLUWANI", "register")
confirmationHandler.register(activityResultCaller, lifecycleOwner)
}

Expand Down Expand Up @@ -142,7 +135,6 @@ internal class LinkActivityViewModel @Inject constructor(
val handle: SavedStateHandle = savedStateHandle ?: createSavedStateHandle()
val app = this[APPLICATION_KEY] as Application
val args: NativeLinkArgs = getArgs(handle) ?: throw NoArgsException()
Log.d("TOLUWANI", "creating new component")

DaggerNativeLinkComponent
.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.stripe.android.link.LinkActivityResult.PaymentMethodObtained
import com.stripe.android.link.account.LinkStore
import com.stripe.android.link.injection.LinkAnalyticsComponent
import javax.inject.Inject
import javax.inject.Singleton

/**
* Launcher for an Activity that will confirm a payment using Link.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal class LinkAnalyticsHelper @Inject internal constructor(
is LinkActivityResult.Failed -> {
linkEventsReporter.onPopupError(linkActivityResult.error)
}
LinkActivityResult.Completed -> TODO()
LinkActivityResult.Completed -> Unit
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import dagger.Module
import dagger.Provides

@Module
internal object ViewModelModule {
internal object LinkViewModelModule {
@Provides
@NativeLinkScope // Use your custom scope instead of @Singleton
@NativeLinkScope
fun provideLinkActivityViewModel(
component: NativeLinkComponent,
defaultConfirmationHandlerFactory: DefaultConfirmationHandler.Factory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,11 @@ import androidx.lifecycle.SavedStateHandle
import com.stripe.android.core.Logger
import com.stripe.android.core.injection.PUBLISHABLE_KEY
import com.stripe.android.core.injection.STRIPE_ACCOUNT_ID
import com.stripe.android.customersheet.injection.CustomerSheetViewModelComponent.Builder
import com.stripe.android.link.LinkActivityViewModel
import com.stripe.android.link.LinkConfiguration
import com.stripe.android.link.account.LinkAccountManager
import com.stripe.android.link.analytics.LinkEventsReporter
import com.stripe.android.paymentelement.confirmation.bacs.BacsConfirmationModule
import com.stripe.android.paymentelement.confirmation.epms.ExternalPaymentMethodConfirmationModule
import com.stripe.android.paymentelement.confirmation.gpay.GooglePayConfirmationModule
import com.stripe.android.paymentelement.confirmation.injection.DefaultConfirmationModule
import com.stripe.android.paymentelement.confirmation.link.LinkConfirmationModule
import com.stripe.android.payments.core.injection.STATUS_BAR_COLOR
import dagger.BindsInstance
import dagger.Component
Expand All @@ -29,11 +24,8 @@ internal annotation class NativeLinkScope
@Component(
modules = [
NativeLinkModule::class,
ViewModelModule::class,
LinkViewModelModule::class,
DefaultConfirmationModule::class,
// LinkConfirmationModule::class,
// BacsConfirmationModule::class,
// ExternalPaymentMethodConfirmationModule::class,
]
)
internal interface NativeLinkComponent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ import com.stripe.android.link.repositories.LinkRepository
import com.stripe.android.networking.StripeApiRepository
import com.stripe.android.networking.StripeRepository
import com.stripe.android.paymentelement.confirmation.ALLOWS_MANUAL_CONFIRMATION
import com.stripe.android.paymentelement.confirmation.ConfirmationHandler
import com.stripe.android.paymentelement.confirmation.DefaultConfirmationHandler
import com.stripe.android.payments.core.analytics.ErrorReporter
import com.stripe.android.payments.core.analytics.RealErrorReporter
import com.stripe.android.payments.core.injection.PRODUCT_USAGE
Expand All @@ -37,11 +35,9 @@ import com.stripe.android.repository.ConsumersApiServiceImpl
import dagger.Binds
import dagger.Module
import dagger.Provides
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.plus
import javax.inject.Named
import javax.inject.Singleton
import kotlin.coroutines.CoroutineContext

@Module
Expand All @@ -66,11 +62,6 @@ internal interface NativeLinkModule {
@NativeLinkScope
fun stripeRepository(stripeRepository: StripeApiRepository): StripeRepository

// @Binds
// fun bindsConfirmationHandlerFactory(
// defaultConfirmationHandlerFactory: DefaultConfirmationHandler.Factory
// ): ConfirmationHandler.Factory

companion object {
@Provides
@NativeLinkScope
Expand Down Expand Up @@ -137,18 +128,6 @@ internal interface NativeLinkModule {
@NativeLinkScope
fun providesEnableLogging(): Boolean = BuildConfig.DEBUG

// @Provides
// @NativeLinkScope
// fun providesConfirmationHandler(
// confirmationHandlerFactory: ConfirmationHandler.Factory,
// scope: CoroutineScope,
// @IOContext workContext: CoroutineContext,
// ): ConfirmationHandler {
// return confirmationHandlerFactory.create(
// scope = scope.plus(workContext)
// )
// }

@Provides
@NativeLinkScope
fun providePaymentConfiguration(appContext: Context): PaymentConfiguration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import com.stripe.android.common.exception.stripeErrorMessage
import com.stripe.android.core.Logger
import com.stripe.android.core.strings.resolvableString
import com.stripe.android.link.LinkActivityResult
import com.stripe.android.link.LinkActivityViewModel
import com.stripe.android.link.LinkConfiguration
import com.stripe.android.link.LinkScreen
import com.stripe.android.link.account.LinkAccountManager
Expand Down Expand Up @@ -120,7 +118,9 @@ internal class WalletViewModel @Inject constructor(
shouldSave = false
),
appearance = PaymentSheet.Appearance(),
initializationMode = PaymentElementLoader.InitializationMode.PaymentIntent(configuration.stripeIntent.clientSecret ?: ""),
initializationMode = PaymentElementLoader.InitializationMode.PaymentIntent(
clientSecret = configuration.stripeIntent.clientSecret ?: ""
),
shippingDetails = null
)
)
Expand Down

0 comments on commit 169b022

Please sign in to comment.