Skip to content

Commit

Permalink
Updates async.
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosmuvi-stripe committed Mar 21, 2024
1 parent 0f39b41 commit 59a645c
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 52 deletions.
4 changes: 2 additions & 2 deletions financial-connections/detekt-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ID>ConstructorParameterNaming:FinancialConnectionsAuthorizationSession.kt$FinancialConnectionsAuthorizationSession$@SerialName(value = "is_oauth") private val _isOAuth: Boolean? = false</ID>
<ID>ConstructorParameterNaming:PartnerAccountsList.kt$PartnerAccount$@SerialName(value = "allow_selection") private val _allowSelection: Boolean? = null</ID>
<ID>LongMethod:AccountItem.kt$@Composable @Preview internal fun AccountItemPreview()</ID>
<ID>LongMethod:InstitutionPickerScreen.kt$private fun LazyListScope.searchResults( isInputEmpty: Boolean, payload: Payload, selectedInstitutionId: String?, onInstitutionSelected: (FinancialConnectionsInstitution, Boolean) -> Unit, institutions: Result&lt;InstitutionResponse>, onManualEntryClick: () -> Unit, onSearchMoreClick: () -> Unit )</ID>
<ID>LongMethod:InstitutionPickerScreen.kt$private fun LazyListScope.searchResults( isInputEmpty: Boolean, payload: Payload, selectedInstitutionId: String?, onInstitutionSelected: (FinancialConnectionsInstitution, Boolean) -> Unit, institutions: Async&lt;InstitutionResponse>, onManualEntryClick: () -> Unit, onSearchMoreClick: () -> Unit )</ID>
<ID>LongMethod:LinkAccountPickerPreviewParameterProvider.kt$LinkAccountPickerPreviewParameterProvider$private fun partnerAccountList()</ID>
<ID>MagicNumber:AccountPickerScreen.kt$3</ID>
<ID>MagicNumber:ConsentLogoHeader.kt$3</ID>
Expand All @@ -30,7 +30,7 @@
<ID>MaximumLineLength:com.stripe.android.financialconnections.presentation.FinancialConnectionsUrls.kt:41</ID>
<ID>MaximumLineLength:com.stripe.android.financialconnections.presentation.FinancialConnectionsUrls.kt:46</ID>
<ID>MaximumLineLength:com.stripe.android.financialconnections.presentation.FinancialConnectionsUrls.kt:9</ID>
<ID>NestedBlockDepth:InstitutionPickerScreen.kt$private fun LazyListScope.searchResults( isInputEmpty: Boolean, payload: Payload, selectedInstitutionId: String?, onInstitutionSelected: (FinancialConnectionsInstitution, Boolean) -> Unit, institutions: Result&lt;InstitutionResponse>, onManualEntryClick: () -> Unit, onSearchMoreClick: () -> Unit )</ID>
<ID>NestedBlockDepth:InstitutionPickerScreen.kt$private fun LazyListScope.searchResults( isInputEmpty: Boolean, payload: Payload, selectedInstitutionId: String?, onInstitutionSelected: (FinancialConnectionsInstitution, Boolean) -> Unit, institutions: Async&lt;InstitutionResponse>, onManualEntryClick: () -> Unit, onSearchMoreClick: () -> Unit )</ID>
<ID>SwallowedException:PollAttachPaymentAccount.kt$PollAttachPaymentAccount$e: StripeException</ID>
<ID>SwallowedException:PollAuthorizationSessionAccounts.kt$PollAuthorizationSessionAccounts$e: StripeException</ID>
<ID>SwallowedException:PostAuthorizationSession.kt$PostAuthorizationSession$e: StripeException</ID>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.stripe.android.financialconnections.core

import android.annotation.SuppressLint
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.collectAsState
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package com.stripe.android.financialconnections.core

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.stripe.android.financialconnections.core.Result.Fail
import com.stripe.android.financialconnections.core.Result.Loading
import com.stripe.android.financialconnections.core.Result.Success
import com.stripe.android.financialconnections.core.Async.Fail
import com.stripe.android.financialconnections.core.Async.Loading
import com.stripe.android.financialconnections.core.Async.Success
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
Expand All @@ -18,9 +18,8 @@ internal abstract class FinancialConnectionsViewModel<S>(
private val _stateFlow: MutableStateFlow<S> = MutableStateFlow(initialState)
val stateFlow: StateFlow<S> = _stateFlow


protected open fun <T : Any?> (suspend () -> T).execute(
reducer: S.(Result<T>) -> S,
reducer: S.(Async<T>) -> S,
onSuccess: (T) -> Unit = {},
onFail: (Throwable) -> Unit = {}
): Job {
Expand All @@ -44,15 +43,15 @@ internal abstract class FinancialConnectionsViewModel<S>(
protected fun setState(reducer: S.() -> S) = _stateFlow.update(reducer)
}

internal sealed class Result<out T>(
internal sealed class Async<out T>(
private val value: T?
) {
data object Uninitialized : Result<Nothing>(value = null)
data object Loading : Result<Nothing>(value = null)
data class Success<out T>(val value: T) : Result<T>(value = value) {
data object Uninitialized : Async<Nothing>(value = null)
data object Loading : Async<Nothing>(value = null)
data class Success<out T>(private val value: T) : Async<T>(value = value) {
override operator fun invoke(): T = value
}
data class Fail<out T>(val error: Throwable) : Result<T>(value = null)
data class Fail<out T>(val error: Throwable) : Async<T>(value = null)

open operator fun invoke(): T? = value
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.stripe.android.financialconnections.features.consent
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.ModalBottomSheetValue
import androidx.compose.ui.tooling.preview.PreviewParameterProvider
import com.stripe.android.financialconnections.core.Result.Success
import com.stripe.android.financialconnections.core.Async.Success
import com.stripe.android.financialconnections.model.Bullet
import com.stripe.android.financialconnections.model.ConnectedAccessNotice
import com.stripe.android.financialconnections.model.ConsentPane
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.compose.ui.unit.dp
import com.stripe.android.financialconnections.core.Result
import com.stripe.android.financialconnections.core.Result.Fail
import com.stripe.android.financialconnections.core.Result.Loading
import com.stripe.android.financialconnections.core.Result.Success
import com.stripe.android.financialconnections.core.Result.Uninitialized
import com.stripe.android.financialconnections.core.Async
import com.stripe.android.financialconnections.core.Async.Fail
import com.stripe.android.financialconnections.core.Async.Loading
import com.stripe.android.financialconnections.core.Async.Success
import com.stripe.android.financialconnections.core.Async.Uninitialized
import com.stripe.android.financialconnections.core.paneViewModel
import com.stripe.android.financialconnections.features.common.DataAccessBottomSheetContent
import com.stripe.android.financialconnections.features.common.LegalDetailsBottomSheetContent
Expand Down Expand Up @@ -148,7 +148,7 @@ private fun ConsentLoadingContent() {
@Composable
private fun ConsentMainContent(
payload: ConsentState.Payload,
acceptConsent: Result<FinancialConnectionsSessionManifest>,
acceptConsent: Async<FinancialConnectionsSessionManifest>,
onClickableTextClick: (String) -> Unit,
onContinueClick: () -> Unit,
onCloseClick: () -> Unit
Expand Down Expand Up @@ -231,7 +231,7 @@ private fun LazyListScope.consentBody(
private fun LoadedContent(
payload: ConsentState.Payload,
bottomSheetState: ModalBottomSheetState,
acceptConsent: Result<FinancialConnectionsSessionManifest>,
acceptConsent: Async<FinancialConnectionsSessionManifest>,
onContinueClick: () -> Unit,
onCloseClick: () -> Unit,
onClickableTextClick: (String) -> Unit,
Expand Down Expand Up @@ -272,7 +272,7 @@ private fun LoadedContent(
@OptIn(ExperimentalComposeUiApi::class)
@Composable
private fun ConsentFooter(
acceptConsent: Result<FinancialConnectionsSessionManifest>,
acceptConsent: Async<FinancialConnectionsSessionManifest>,
consent: ConsentPane,
onClickableTextClick: (String) -> Unit,
onContinueClick: () -> Unit,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.stripe.android.financialconnections.features.consent

import com.stripe.android.financialconnections.core.Result
import com.stripe.android.financialconnections.core.Async
import com.stripe.android.financialconnections.model.ConsentPane
import com.stripe.android.financialconnections.model.FinancialConnectionsSessionManifest

internal data class ConsentState(
val consent: Result<Payload> = Result.Uninitialized,
val consent: Async<Payload> = Async.Uninitialized,
val merchantLogos: List<String> = emptyList(),
val currentBottomSheet: BottomSheetContent = BottomSheetContent.DATA,
val acceptConsent: Result<FinancialConnectionsSessionManifest> = Result.Uninitialized,
val acceptConsent: Async<FinancialConnectionsSessionManifest> = Async.Uninitialized,
val viewEffect: ViewEffect? = null
) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.stripe.android.financialconnections.features.institutionpicker

import androidx.compose.ui.tooling.preview.PreviewParameterProvider
import com.stripe.android.financialconnections.core.Result.Fail
import com.stripe.android.financialconnections.core.Result.Loading
import com.stripe.android.financialconnections.core.Result.Success
import com.stripe.android.financialconnections.core.Result.Uninitialized
import com.stripe.android.financialconnections.core.Async.Fail
import com.stripe.android.financialconnections.core.Async.Loading
import com.stripe.android.financialconnections.core.Async.Success
import com.stripe.android.financialconnections.core.Async.Uninitialized
import com.stripe.android.financialconnections.model.FinancialConnectionsInstitution
import com.stripe.android.financialconnections.model.InstitutionResponse

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.compose.ui.unit.dp
import com.stripe.android.financialconnections.R
import com.stripe.android.financialconnections.core.Result
import com.stripe.android.financialconnections.core.Result.Fail
import com.stripe.android.financialconnections.core.Result.Loading
import com.stripe.android.financialconnections.core.Result.Success
import com.stripe.android.financialconnections.core.Result.Uninitialized
import com.stripe.android.financialconnections.core.Async
import com.stripe.android.financialconnections.core.Async.Fail
import com.stripe.android.financialconnections.core.Async.Loading
import com.stripe.android.financialconnections.core.Async.Success
import com.stripe.android.financialconnections.core.Async.Uninitialized
import com.stripe.android.financialconnections.core.paneViewModel
import com.stripe.android.financialconnections.features.common.FullScreenGenericLoading
import com.stripe.android.financialconnections.features.common.InstitutionIcon
Expand Down Expand Up @@ -114,8 +114,8 @@ internal fun InstitutionPickerScreen() {
@Composable
private fun InstitutionPickerContent(
listState: LazyListState,
payload: Result<Payload>,
institutions: Result<InstitutionResponse>,
payload: Async<Payload>,
institutions: Async<InstitutionResponse>,
previewText: String?,
selectedInstitutionId: String?,
onQueryChanged: (String) -> Unit,
Expand Down Expand Up @@ -157,7 +157,7 @@ private fun LoadedContent(
previewText: String?,
selectedInstitutionId: String?,
onQueryChanged: (String) -> Unit,
institutions: Result<InstitutionResponse>,
institutions: Async<InstitutionResponse>,
onInstitutionSelected: (FinancialConnectionsInstitution, Boolean) -> Unit,
payload: Payload,
onManualEntryClick: () -> Unit,
Expand Down Expand Up @@ -225,7 +225,7 @@ private fun LazyListScope.searchResults(
payload: Payload,
selectedInstitutionId: String?,
onInstitutionSelected: (FinancialConnectionsInstitution, Boolean) -> Unit,
institutions: Result<InstitutionResponse>,
institutions: Async<InstitutionResponse>,
onManualEntryClick: () -> Unit,
onSearchMoreClick: () -> Unit
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import com.stripe.android.financialconnections.analytics.FinancialConnectionsAna
import com.stripe.android.financialconnections.analytics.FinancialConnectionsEvent.Metadata
import com.stripe.android.financialconnections.analytics.FinancialConnectionsEvent.Name
import com.stripe.android.financialconnections.analytics.logError
import com.stripe.android.financialconnections.core.Async
import com.stripe.android.financialconnections.core.Async.Loading
import com.stripe.android.financialconnections.core.Async.Uninitialized
import com.stripe.android.financialconnections.core.FinancialConnectionsViewModel
import com.stripe.android.financialconnections.core.Result
import com.stripe.android.financialconnections.core.Result.Loading
import com.stripe.android.financialconnections.core.Result.Uninitialized
import com.stripe.android.financialconnections.di.FinancialConnectionsSheetNativeComponent
import com.stripe.android.financialconnections.domain.FeaturedInstitutions
import com.stripe.android.financialconnections.domain.GetOrFetchSync
Expand Down Expand Up @@ -248,9 +248,9 @@ internal data class InstitutionPickerState(
// This is just used to provide a text in Compose previews
val previewText: String? = null,
val selectedInstitutionId: String? = null,
val payload: Result<Payload> = Uninitialized,
val searchInstitutions: Result<InstitutionResponse> = Uninitialized,
val createSessionForInstitution: Result<Unit> = Uninitialized
val payload: Async<Payload> = Uninitialized,
val searchInstitutions: Async<InstitutionResponse> = Uninitialized,
val createSessionForInstitution: Async<Unit> = Uninitialized
) {

data class Payload(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.stripe.android.financialconnections.utils

import androidx.activity.ComponentActivity
import com.airbnb.mvrx.ActivityViewModelContext
import com.airbnb.mvrx.Async
import com.airbnb.mvrx.Fail
import com.airbnb.mvrx.InternalMavericksApi
import com.airbnb.mvrx.Loading
Expand All @@ -12,11 +11,12 @@ import com.airbnb.mvrx.MavericksViewModel
import com.airbnb.mvrx.MavericksViewModelFactory
import com.airbnb.mvrx.MavericksViewModelProvider
import com.stripe.android.core.exception.StripeException
import com.stripe.android.financialconnections.core.Result
import com.stripe.android.financialconnections.core.Async
import kotlinx.coroutines.CancellationException
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KClass
import kotlin.reflect.KProperty
import com.airbnb.mvrx.Async as MvrxAsync

/**
* Replicates [com.airbnb.mvrx.viewModel] delegate, but without using [com.airbnb.mvrx.lifecycleAwareLazy]
Expand Down Expand Up @@ -64,15 +64,15 @@ internal fun <V> argsOrNull() = object : ReadOnlyProperty<ComponentActivity, V?>
* Prevents [CancellationException] to map to [Fail] when coroutine being cancelled
* due to search query changes. In these cases, re-map the [Async] instance to [Loading]
*/
internal fun Async<*>.isCancellationError(): Boolean = when {
internal fun MvrxAsync<*>.isCancellationError(): Boolean = when {
this !is Fail -> false
error is CancellationException -> true
error is StripeException && error.cause is CancellationException -> true
else -> false
}

internal fun Result<*>.isCancellationError(): Boolean = when {
this !is Result.Fail -> false
internal fun Async<*>.isCancellationError(): Boolean = when {
this !is Async.Fail -> false
error is CancellationException -> true
error is StripeException && error.cause is CancellationException -> true
else -> false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.stripe.android.financialconnections.ApiKeyFixtures
import com.stripe.android.financialconnections.CoroutineTestRule
import com.stripe.android.financialconnections.FinancialConnectionsSheet
import com.stripe.android.financialconnections.TestFinancialConnectionsAnalyticsTracker
import com.stripe.android.financialconnections.core.Result
import com.stripe.android.financialconnections.core.Async
import com.stripe.android.financialconnections.domain.FeaturedInstitutions
import com.stripe.android.financialconnections.domain.GetOrFetchSync
import com.stripe.android.financialconnections.domain.PostAuthorizationSession
Expand Down Expand Up @@ -95,7 +95,7 @@ internal class InstitutionPickerViewModelTest {

viewModel.stateFlow.value.let { state ->
assertEquals(state.payload()!!.featuredInstitutions, institutionResponse)
assertIs<Result.Uninitialized>(state.searchInstitutions)
assertIs<Async.Uninitialized>(state.searchInstitutions)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull

class GooglePayResultTest {
class GooglePayAsyncTest {

@Test
fun fromJson_withFullBillingAddress() {
Expand Down

0 comments on commit 59a645c

Please sign in to comment.