Skip to content

Commit

Permalink
Fix process death handling in success screen
Browse files Browse the repository at this point in the history
  • Loading branch information
tillh-stripe committed Apr 3, 2024
1 parent be55b43 commit fa0b09d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.stripe.android.financialconnections.di

import android.app.Application
import androidx.lifecycle.SavedStateHandle
import com.stripe.android.core.ApiVersion
import com.stripe.android.core.Logger
import com.stripe.android.core.networking.ApiRequest
Expand Down Expand Up @@ -127,12 +128,14 @@ internal interface FinancialConnectionsSheetNativeModule {
requestExecutor: FinancialConnectionsRequestExecutor,
apiOptions: ApiRequest.Options,
apiRequestFactory: ApiRequest.Factory,
logger: Logger
logger: Logger,
savedStateHandle: SavedStateHandle,
) = FinancialConnectionsAccountsRepository(
requestExecutor = requestExecutor,
apiRequestFactory = apiRequestFactory,
apiOptions = apiOptions,
logger = logger
logger = logger,
savedStateHandle = savedStateHandle,
)

@Singleton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ internal class GetCachedAccounts @Inject constructor(
) {

suspend operator fun invoke(): List<PartnerAccount> {
return requireNotNull(repository.getCachedAccounts())
return repository.getCachedAccounts()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal class UpdateCachedAccounts @Inject constructor(
) {

suspend operator fun invoke(
block: (List<PartnerAccount>?) -> List<PartnerAccount>?
block: (List<PartnerAccount>) -> List<PartnerAccount>
) {
val updatedAccounts = block(repository.getCachedAccounts())
repository.updateCachedAccounts(updatedAccounts)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.stripe.android.financialconnections.repository

import androidx.lifecycle.SavedStateHandle
import com.stripe.android.core.Logger
import com.stripe.android.core.networking.ApiRequest
import com.stripe.android.financialconnections.model.FinancialConnectionsSessionManifest
Expand All @@ -24,9 +25,9 @@ import kotlinx.coroutines.sync.withLock
*/
internal interface FinancialConnectionsAccountsRepository {

suspend fun getCachedAccounts(): List<PartnerAccount>?
suspend fun getCachedAccounts(): List<PartnerAccount>

suspend fun updateCachedAccounts(partnerAccountsList: List<PartnerAccount>?)
suspend fun updateCachedAccounts(partnerAccountsList: List<PartnerAccount>)

suspend fun postAuthorizationSessionAccounts(
clientSecret: String,
Expand Down Expand Up @@ -67,36 +68,43 @@ internal interface FinancialConnectionsAccountsRepository {
requestExecutor: FinancialConnectionsRequestExecutor,
apiRequestFactory: ApiRequest.Factory,
apiOptions: ApiRequest.Options,
logger: Logger
logger: Logger,
savedStateHandle: SavedStateHandle,
): FinancialConnectionsAccountsRepository =
FinancialConnectionsAccountsRepositoryImpl(
requestExecutor,
apiRequestFactory,
apiOptions,
logger
logger,
savedStateHandle,
)
}
}

private class FinancialConnectionsAccountsRepositoryImpl(
val requestExecutor: FinancialConnectionsRequestExecutor,
val apiRequestFactory: ApiRequest.Factory,
val apiOptions: ApiRequest.Options,
val logger: Logger
private val requestExecutor: FinancialConnectionsRequestExecutor,
private val apiRequestFactory: ApiRequest.Factory,
private val apiOptions: ApiRequest.Options,
private val logger: Logger,
private val savedStateHandle: SavedStateHandle,
) : FinancialConnectionsAccountsRepository {

/**
* Ensures that [cachedAccounts] accesses via [getCachedAccounts] suspend until
* current writes are running.
*/
val mutex = Mutex()
private var cachedAccounts: List<PartnerAccount>? = null

override suspend fun getCachedAccounts(): List<PartnerAccount>? =
mutex.withLock { cachedAccounts }
override suspend fun getCachedAccounts(): List<PartnerAccount> {
return mutex.withLock { savedStateHandle[KeyCachedAccounts] ?: emptyList() }
}

override suspend fun updateCachedAccounts(partnerAccountsList: List<PartnerAccount>) =
mutex.withLock { setCachedAccounts(partnerAccountsList) }

override suspend fun updateCachedAccounts(partnerAccountsList: List<PartnerAccount>?) =
mutex.withLock { cachedAccounts = partnerAccountsList }
private fun setCachedAccounts(partnerAccountsList: List<PartnerAccount>) {
savedStateHandle[KeyCachedAccounts] = partnerAccountsList
}

override suspend fun postAuthorizationSessionAccounts(
clientSecret: String,
Expand Down Expand Up @@ -213,10 +221,12 @@ private class FinancialConnectionsAccountsRepositoryImpl(
accounts: List<PartnerAccount>
) {
logger.debug("updating local partner accounts from $source")
cachedAccounts = accounts
setCachedAccounts(accounts)
}

companion object {
private const val KeyCachedAccounts = "KeyCachedAccounts"

internal const val accountsSessionUrl: String =
"${ApiRequest.API_HOST}/v1/connections/auth_sessions/accounts"

Expand Down

0 comments on commit fa0b09d

Please sign in to comment.