Skip to content

Commit

Permalink
Adds activityRetainedScope
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosmuvi-stripe committed Dec 23, 2024
1 parent 0b7c686 commit 69c825d
Show file tree
Hide file tree
Showing 19 changed files with 186 additions and 61 deletions.
1 change: 1 addition & 0 deletions financial-connections/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dependencies {
api project(":stripe-core")
api project(":stripe-ui-core")
api project(":payments-model")
implementation project(":stripe-attestation")

implementation libs.androidx.activity
implementation libs.androidx.annotation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.stripe.android.financialconnections.FinancialConnectionsSheetViewEffe
import com.stripe.android.financialconnections.FinancialConnectionsSheetViewEffect.OpenAuthFlowWithUrl
import com.stripe.android.financialconnections.FinancialConnectionsSheetViewEffect.OpenNativeAuthFlow
import com.stripe.android.financialconnections.FinancialConnectionsSheetViewModel.Companion.QUERY_PARAM_PAYMENT_METHOD
import com.stripe.android.financialconnections.analytics.FinancialConnectionsAnalyticsEvent
import com.stripe.android.financialconnections.analytics.FinancialConnectionsAnalyticsTracker
import com.stripe.android.financialconnections.analytics.FinancialConnectionsEvent.ErrorCode
import com.stripe.android.financialconnections.analytics.FinancialConnectionsEvent.Metadata
Expand All @@ -30,6 +31,7 @@ import com.stripe.android.financialconnections.analytics.logError
import com.stripe.android.financialconnections.browser.BrowserManager
import com.stripe.android.financialconnections.di.APPLICATION_ID
import com.stripe.android.financialconnections.di.DaggerFinancialConnectionsSheetComponent
import com.stripe.android.financialconnections.di.FinancialConnectionsSingletonSharedComponentHolder
import com.stripe.android.financialconnections.domain.FetchFinancialConnectionsSession
import com.stripe.android.financialconnections.domain.FetchFinancialConnectionsSessionForToken
import com.stripe.android.financialconnections.domain.GetOrFetchSync
Expand Down Expand Up @@ -57,6 +59,7 @@ import com.stripe.android.financialconnections.navigation.topappbar.TopAppBarSta
import com.stripe.android.financialconnections.presentation.FinancialConnectionsViewModel
import com.stripe.android.financialconnections.ui.FinancialConnectionsSheetNativeActivity
import com.stripe.android.financialconnections.utils.parcelable
import com.stripe.attestation.IntegrityRequestManager
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
Expand All @@ -67,6 +70,7 @@ internal class FinancialConnectionsSheetViewModel @Inject constructor(
@Named(APPLICATION_ID) private val applicationId: String,
savedStateHandle: SavedStateHandle,
private val getOrFetchSync: GetOrFetchSync,
private val integrityRequestManager: IntegrityRequestManager,
private val fetchFinancialConnectionsSession: FetchFinancialConnectionsSession,
private val fetchFinancialConnectionsSessionForToken: FetchFinancialConnectionsSessionForToken,
private val logger: Logger,
Expand Down Expand Up @@ -110,6 +114,7 @@ internal class FinancialConnectionsSheetViewModel @Inject constructor(
private fun fetchManifest() {
viewModelScope.launch {
kotlin.runCatching {
prepareStandardRequestManager()
getOrFetchSync(refetchCondition = Always)
}.onFailure {
finishWithResult(stateFlow.value, Failed(it))
Expand All @@ -119,6 +124,20 @@ internal class FinancialConnectionsSheetViewModel @Inject constructor(
}
}

private suspend fun prepareStandardRequestManager(): Boolean {
val result = runCatching { integrityRequestManager.prepare() }
result.exceptionOrNull()?.let {
analyticsTracker.track(
FinancialConnectionsAnalyticsEvent.Error(
extraMessage = "Failed to warm up the IntegrityStandardRequestManager",
pane = Pane.CONSENT,
exception = it
)
)
}
return result.isSuccess
}

/**
* Builds the ChromeCustomTab intent to launch the hosted auth flow and launches it.
*
Expand Down Expand Up @@ -529,6 +548,7 @@ internal class FinancialConnectionsSheetViewModel @Inject constructor(
.builder()
.application(app)
.savedStateHandle(savedStateHandle)
.sharedComponent(FinancialConnectionsSingletonSharedComponentHolder.getComponent(app))
.initialState(state)
.configuration(state.initialArgs.configuration)
.build().viewModel
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.stripe.android.financialconnections.di

import javax.inject.Scope

/**
* Scope annotation for bindings that should exist for the life of an activity, surviving configuration.
*
* Note: Dependencies scoped to this won't be shared across activities.
*/
@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class ActivityRetainedScope
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,18 @@ package com.stripe.android.financialconnections.di

import android.app.Application
import androidx.lifecycle.SavedStateHandle
import com.stripe.android.core.injection.CoreCommonModule
import com.stripe.android.core.injection.CoroutineContextModule
import com.stripe.android.financialconnections.FinancialConnectionsSheet
import com.stripe.android.financialconnections.FinancialConnectionsSheetState
import com.stripe.android.financialconnections.FinancialConnectionsSheetViewModel
import dagger.BindsInstance
import dagger.Component
import javax.inject.Singleton

@Singleton
@ActivityRetainedScope
@Component(
dependencies = [FinancialConnectionsSingletonSharedComponent::class],
modules = [
FinancialConnectionsSheetModule::class,
FinancialConnectionsSheetSharedModule::class,
CoroutineContextModule::class,
CoreCommonModule::class
]
)
internal interface FinancialConnectionsSheetComponent {
Expand All @@ -37,6 +33,8 @@ internal interface FinancialConnectionsSheetComponent {
@BindsInstance
fun configuration(configuration: FinancialConnectionsSheet.Configuration): Builder

fun sharedComponent(component: FinancialConnectionsSingletonSharedComponent): Builder

fun build(): FinancialConnectionsSheetComponent
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,38 @@ import com.stripe.android.financialconnections.FinancialConnectionsSheet
import dagger.Module
import dagger.Provides
import javax.inject.Named
import javax.inject.Singleton

@Module
internal object FinancialConnectionsSheetConfigurationModule {

@Provides
@Named(PUBLISHABLE_KEY)
@Singleton
@ActivityRetainedScope
fun providesPublishableKey(
configuration: FinancialConnectionsSheet.Configuration
): String = configuration.publishableKey

@Provides
@Named(STRIPE_ACCOUNT_ID)
@Singleton
@ActivityRetainedScope
fun providesStripeAccountId(
configuration: FinancialConnectionsSheet.Configuration
): String? = configuration.stripeAccountId

@Provides
@Named(ENABLE_LOGGING)
@Singleton
@ActivityRetainedScope
fun providesEnableLogging(): Boolean = BuildConfig.DEBUG

@Provides
@Singleton
@ActivityRetainedScope
@Named(APPLICATION_ID)
fun providesApplicationId(
application: Application
): String = application.packageName

@Provides
@Singleton
@ActivityRetainedScope
fun providesApiVersion(): ApiVersion = ApiVersion(
betas = setOf("financial_connections_client_api_beta=v1")
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ import com.stripe.android.financialconnections.repository.api.ProvideApiRequestO
import dagger.Module
import dagger.Provides
import java.util.Locale
import javax.inject.Singleton

@Module
internal object FinancialConnectionsSheetModule {

@Singleton
@ActivityRetainedScope
@Provides
internal fun providesProvideApiRequestOptions(
apiRequestOptions: ApiRequest.Options,
Expand All @@ -22,7 +21,7 @@ internal object FinancialConnectionsSheetModule {
return ProvideApiRequestOptions { apiRequestOptions }
}

@Singleton
@ActivityRetainedScope
@Provides
fun providesFinancialConnectionsManifestRepository(
requestExecutor: FinancialConnectionsRequestExecutor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package com.stripe.android.financialconnections.di

import android.app.Application
import androidx.lifecycle.SavedStateHandle
import com.stripe.android.core.injection.CoreCommonModule
import com.stripe.android.core.injection.CoroutineContextModule
import com.stripe.android.financialconnections.FinancialConnectionsSheet
import com.stripe.android.financialconnections.features.accountpicker.AccountPickerViewModel
import com.stripe.android.financialconnections.features.accountupdate.AccountUpdateRequiredViewModel
Expand Down Expand Up @@ -32,15 +30,13 @@ import com.stripe.android.financialconnections.ui.FinancialConnectionsSheetNativ
import dagger.BindsInstance
import dagger.Component
import javax.inject.Named
import javax.inject.Singleton

@Singleton
@ActivityRetainedScope
@Component(
dependencies = [FinancialConnectionsSingletonSharedComponent::class],
modules = [
FinancialConnectionsSheetNativeModule::class,
FinancialConnectionsSheetSharedModule::class,
CoreCommonModule::class,
CoroutineContextModule::class
FinancialConnectionsSheetSharedModule::class
]
)
internal interface FinancialConnectionsSheetNativeComponent {
Expand Down Expand Up @@ -91,6 +87,8 @@ internal interface FinancialConnectionsSheetNativeComponent {
@BindsInstance
fun configuration(configuration: FinancialConnectionsSheet.Configuration): Builder

fun sharedComponent(component: FinancialConnectionsSingletonSharedComponent): Builder

fun build(): FinancialConnectionsSheetNativeComponent
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,14 @@ import dagger.Provides
import java.util.Locale
import javax.inject.Named
import javax.inject.Provider
import javax.inject.Singleton

@Module
internal interface FinancialConnectionsSheetNativeModule {

@Binds
fun bindsPresentNoticeSheet(impl: RealPresentSheet): PresentSheet

@Singleton
@ActivityRetainedScope
@Binds
fun bindsNavigationManager(
impl: NavigationManagerImpl
Expand All @@ -63,7 +62,7 @@ internal interface FinancialConnectionsSheetNativeModule {
): HandleError

@Binds
@Singleton
@ActivityRetainedScope
fun bindsProvideApiRequestOptions(impl: RealProvideApiRequestOptions): ProvideApiRequestOptions

@Binds
Expand All @@ -78,7 +77,7 @@ internal interface FinancialConnectionsSheetNativeModule {

companion object {
@Provides
@Singleton
@ActivityRetainedScope
fun provideConsumersApiService(
apiVersion: ApiVersion,
stripeNetworkClient: StripeNetworkClient,
Expand All @@ -89,7 +88,7 @@ internal interface FinancialConnectionsSheetNativeModule {
stripeNetworkClient = stripeNetworkClient
)

@Singleton
@ActivityRetainedScope
@Provides
fun providesImageLoader(
context: Application
Expand All @@ -98,7 +97,7 @@ internal interface FinancialConnectionsSheetNativeModule {
diskCache = null,
)

@Singleton
@ActivityRetainedScope
@Provides
fun providesFinancialConnectionsManifestRepository(
requestExecutor: FinancialConnectionsRequestExecutor,
Expand All @@ -116,7 +115,7 @@ internal interface FinancialConnectionsSheetNativeModule {
initialSync = initialSynchronizeSessionResponse
)

@Singleton
@ActivityRetainedScope
@Provides
fun providesFinancialConnectionsConsumerSessionRepository(
consumersApiService: ConsumersApiService,
Expand All @@ -140,7 +139,7 @@ internal interface FinancialConnectionsSheetNativeModule {
elementsSessionContext = elementsSessionContext,
)

@Singleton
@ActivityRetainedScope
@Provides
fun providesFinancialConnectionsAccountsRepository(
requestExecutor: FinancialConnectionsRequestExecutor,
Expand All @@ -156,7 +155,7 @@ internal interface FinancialConnectionsSheetNativeModule {
savedStateHandle = savedStateHandle,
)

@Singleton
@ActivityRetainedScope
@Provides
fun providesFinancialConnectionsInstitutionsRepository(
requestExecutor: FinancialConnectionsRequestExecutor,
Expand Down
Loading

0 comments on commit 69c825d

Please sign in to comment.