Skip to content

Commit

Permalink
Avoid public API changes
Browse files Browse the repository at this point in the history
Introduce `FinancialConnectionsSheetInternalResult` and a `toPublicResult` method
  • Loading branch information
tillh-stripe committed Nov 22, 2024
1 parent 9a40c09 commit 455f21a
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 40 deletions.
24 changes: 24 additions & 0 deletions financial-connections/api/financial-connections.api
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,30 @@ public final class com/stripe/android/financialconnections/FinancialConnectionsS
public synthetic fun newArray (I)[Ljava/lang/Object;
}

public final class com/stripe/android/financialconnections/FinancialConnectionsSheetInternalResult$Canceled$Creator : android/os/Parcelable$Creator {
public fun <init> ()V
public final fun createFromParcel (Landroid/os/Parcel;)Lcom/stripe/android/financialconnections/FinancialConnectionsSheetInternalResult$Canceled;
public synthetic fun createFromParcel (Landroid/os/Parcel;)Ljava/lang/Object;
public final fun newArray (I)[Lcom/stripe/android/financialconnections/FinancialConnectionsSheetInternalResult$Canceled;
public synthetic fun newArray (I)[Ljava/lang/Object;
}

public final class com/stripe/android/financialconnections/FinancialConnectionsSheetInternalResult$Completed$Creator : android/os/Parcelable$Creator {
public fun <init> ()V
public final fun createFromParcel (Landroid/os/Parcel;)Lcom/stripe/android/financialconnections/FinancialConnectionsSheetInternalResult$Completed;
public synthetic fun createFromParcel (Landroid/os/Parcel;)Ljava/lang/Object;
public final fun newArray (I)[Lcom/stripe/android/financialconnections/FinancialConnectionsSheetInternalResult$Completed;
public synthetic fun newArray (I)[Ljava/lang/Object;
}

public final class com/stripe/android/financialconnections/FinancialConnectionsSheetInternalResult$Failed$Creator : android/os/Parcelable$Creator {
public fun <init> ()V
public final fun createFromParcel (Landroid/os/Parcel;)Lcom/stripe/android/financialconnections/FinancialConnectionsSheetInternalResult$Failed;
public synthetic fun createFromParcel (Landroid/os/Parcel;)Ljava/lang/Object;
public final fun newArray (I)[Lcom/stripe/android/financialconnections/FinancialConnectionsSheetInternalResult$Failed;
public synthetic fun newArray (I)[Ljava/lang/Object;
}

public final class com/stripe/android/financialconnections/FinancialConnectionsSheetRedirectActivity : androidx/appcompat/app/AppCompatActivity {
public static final field $stable I
public fun <init> ()V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ class FinancialConnectionsSheet internal constructor(
callback: FinancialConnectionsSheetResultCallback
): FinancialConnectionsSheet {
return FinancialConnectionsSheet(
FinancialConnectionsSheetForDataLauncher(activity, callback)
FinancialConnectionsSheetForDataLauncher(activity) {
callback.onFinancialConnectionsSheetResult(it.toPublicResult())
}
)
}

Expand All @@ -145,7 +147,9 @@ class FinancialConnectionsSheet internal constructor(
callback: FinancialConnectionsSheetResultCallback
): FinancialConnectionsSheet {
return FinancialConnectionsSheet(
FinancialConnectionsSheetForDataLauncher(fragment, callback)
FinancialConnectionsSheetForDataLauncher(fragment) {
callback.onFinancialConnectionsSheetResult(it.toPublicResult())
}
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fun rememberFinancialConnectionsSheet(
): FinancialConnectionsSheet {
val activityResultLauncher = rememberLauncherForActivityResult(
FinancialConnectionsSheetForDataContract()
) { callback(it) }
) { callback(it.toPublicResult()) }
return remember {
FinancialConnectionsSheet(
FinancialConnectionsSheetForDataLauncher(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.stripe.android.financialconnections

import android.os.Parcelable
import androidx.annotation.RestrictTo
import com.stripe.android.financialconnections.FinancialConnectionsSheetInternalResult.Canceled
import com.stripe.android.financialconnections.FinancialConnectionsSheetInternalResult.Completed
import com.stripe.android.financialconnections.FinancialConnectionsSheetInternalResult.Failed
import com.stripe.android.financialconnections.model.FinancialConnectionsSession
import kotlinx.parcelize.Parcelize

Expand All @@ -15,7 +19,6 @@ sealed class FinancialConnectionsSheetResult : Parcelable {
@Parcelize
data class Completed(
val financialConnectionsSession: FinancialConnectionsSession,
val manualEntryUsesMicrodeposits: Boolean,
) : FinancialConnectionsSheetResult()

/**
Expand All @@ -33,3 +36,33 @@ sealed class FinancialConnectionsSheetResult : Parcelable {
val error: Throwable
) : FinancialConnectionsSheetResult()
}

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
sealed class FinancialConnectionsSheetInternalResult : Parcelable {

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
@Parcelize
data class Completed(
val financialConnectionsSession: FinancialConnectionsSession,
val manualEntryUsesMicrodeposits: Boolean,
) : FinancialConnectionsSheetInternalResult()

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
@Parcelize
data object Canceled : FinancialConnectionsSheetInternalResult()

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
@Parcelize
data class Failed(
val error: Throwable
) : FinancialConnectionsSheetInternalResult()
}

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
fun FinancialConnectionsSheetInternalResult.toPublicResult(): FinancialConnectionsSheetResult {
return when (this) {
is Canceled -> FinancialConnectionsSheetResult.Canceled
is Failed -> FinancialConnectionsSheetResult.Failed(error)
is Completed -> FinancialConnectionsSheetResult.Completed(financialConnectionsSession)
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package com.stripe.android.financialconnections

import androidx.annotation.RestrictTo

/**
* Callback that is invoked when a [FinancialConnectionsSheetResult] is available.
*/
fun interface FinancialConnectionsSheetResultCallback {
fun onFinancialConnectionsSheetResult(financialConnectionsSheetResult: FinancialConnectionsSheetResult)
}

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
fun interface FinancialConnectionsSheetInternalResultCallback {
fun onFinancialConnectionsSheetResult(financialConnectionsSheetResult: FinancialConnectionsSheetInternalResult)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import android.content.Context
import android.content.Intent
import androidx.activity.result.contract.ActivityResultContract
import com.stripe.android.financialconnections.FinancialConnectionsSheetActivity
import com.stripe.android.financialconnections.FinancialConnectionsSheetResult
import com.stripe.android.financialconnections.FinancialConnectionsSheetInternalResult
import com.stripe.android.financialconnections.launcher.FinancialConnectionsSheetActivityResult.Companion.EXTRA_RESULT

internal class FinancialConnectionsSheetForDataContract :
ActivityResultContract<FinancialConnectionsSheetActivityArgs.ForData, FinancialConnectionsSheetResult>() {
ActivityResultContract<FinancialConnectionsSheetActivityArgs.ForData, FinancialConnectionsSheetInternalResult>() {

override fun createIntent(
context: Context,
Expand All @@ -23,29 +23,29 @@ internal class FinancialConnectionsSheetForDataContract :
override fun parseResult(
resultCode: Int,
intent: Intent?
): FinancialConnectionsSheetResult {
): FinancialConnectionsSheetInternalResult {
return intent
?.getParcelableExtra<FinancialConnectionsSheetActivityResult>(EXTRA_RESULT)
?.toExposedResult()
?: FinancialConnectionsSheetResult.Failed(
?.toResult()
?: FinancialConnectionsSheetInternalResult.Failed(
IllegalArgumentException("Failed to retrieve a ConnectionsSheetResult.")
)
}

private fun FinancialConnectionsSheetActivityResult.toExposedResult(): FinancialConnectionsSheetResult =
private fun FinancialConnectionsSheetActivityResult.toResult(): FinancialConnectionsSheetInternalResult =
when (this) {
is FinancialConnectionsSheetActivityResult.Canceled -> FinancialConnectionsSheetResult.Canceled
is FinancialConnectionsSheetActivityResult.Failed -> FinancialConnectionsSheetResult.Failed(
is FinancialConnectionsSheetActivityResult.Canceled -> FinancialConnectionsSheetInternalResult.Canceled
is FinancialConnectionsSheetActivityResult.Failed -> FinancialConnectionsSheetInternalResult.Failed(
error
)

is FinancialConnectionsSheetActivityResult.Completed ->
when (financialConnectionsSession) {
null -> FinancialConnectionsSheetResult.Failed(
null -> FinancialConnectionsSheetInternalResult.Failed(
IllegalArgumentException("financialConnectionsSession not set.")
)

else -> FinancialConnectionsSheetResult.Completed(
else -> FinancialConnectionsSheetInternalResult.Completed(
financialConnectionsSession = financialConnectionsSession,
manualEntryUsesMicrodeposits = manualEntryUsesMicrodeposits,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import androidx.activity.result.ActivityResultRegistry
import androidx.annotation.RestrictTo
import androidx.fragment.app.Fragment
import com.stripe.android.financialconnections.FinancialConnectionsSheet
import com.stripe.android.financialconnections.FinancialConnectionsSheetResultCallback
import com.stripe.android.financialconnections.FinancialConnectionsSheetInternalResultCallback
import org.jetbrains.annotations.TestOnly

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
Expand All @@ -16,7 +16,7 @@ class FinancialConnectionsSheetForDataLauncher(

constructor(
activity: ComponentActivity,
callback: FinancialConnectionsSheetResultCallback
callback: FinancialConnectionsSheetInternalResultCallback
) : this(
activity.registerForActivityResult(
FinancialConnectionsSheetForDataContract()
Expand All @@ -27,7 +27,7 @@ class FinancialConnectionsSheetForDataLauncher(

constructor(
fragment: Fragment,
callback: FinancialConnectionsSheetResultCallback
callback: FinancialConnectionsSheetInternalResultCallback
) : this(
fragment.registerForActivityResult(
FinancialConnectionsSheetForDataContract()
Expand All @@ -40,7 +40,7 @@ class FinancialConnectionsSheetForDataLauncher(
constructor(
fragment: Fragment,
registry: ActivityResultRegistry,
callback: FinancialConnectionsSheetResultCallback
callback: FinancialConnectionsSheetInternalResultCallback
) : this(
fragment.registerForActivityResult(
FinancialConnectionsSheetForDataContract(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class FinancialConnectionsSheetForDataContractTest {
@Test
fun `parseResult() with missing data should return failed result`() {
assertThat(FinancialConnectionsSheetForDataContract().parseResult(0, Intent()))
.isInstanceOf(FinancialConnectionsSheetResult.Failed::class.java)
.isInstanceOf(FinancialConnectionsSheetInternalResult.Failed::class.java)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.google.common.truth.Truth.assertThat
import com.stripe.android.financialconnections.FinancialConnectionsSheet
import com.stripe.android.financialconnections.FinancialConnectionsSheetResult
import com.stripe.android.financialconnections.financialConnectionsSessionWithNoMoreAccounts
import com.stripe.android.financialconnections.toPublicResult
import com.stripe.android.financialconnections.utils.FakeActivityResultRegistry
import com.stripe.android.financialconnections.utils.TestFragment
import org.junit.Test
Expand All @@ -21,8 +22,7 @@ class FinancialConnectionsSheetForDataLauncherTest {
fun `create and present should return expected ConnectionsSheetResult#Completed`() {
val testRegistry = FakeActivityResultRegistry(
FinancialConnectionsSheetResult.Completed(
financialConnectionsSession = financialConnectionsSessionWithNoMoreAccounts,
manualEntryUsesMicrodeposits = false,
financialConnectionsSession = financialConnectionsSessionWithNoMoreAccounts
)
)

Expand All @@ -35,16 +35,15 @@ class FinancialConnectionsSheetForDataLauncherTest {
fragment,
testRegistry
) {
results.add(it)
results.add(it.toPublicResult())
}

moveToState(Lifecycle.State.RESUMED)
launcher.present(configuration)
assertThat(results)
.containsExactly(
FinancialConnectionsSheetResult.Completed(
financialConnectionsSessionWithNoMoreAccounts,
manualEntryUsesMicrodeposits = false,
financialConnectionsSessionWithNoMoreAccounts
)
)
}
Expand All @@ -64,7 +63,7 @@ class FinancialConnectionsSheetForDataLauncherTest {
fragment,
testRegistry
) {
results.add(it)
results.add(it.toPublicResult())
}

moveToState(Lifecycle.State.RESUMED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import androidx.lifecycle.viewmodel.CreationExtras
import com.stripe.android.core.Logger
import com.stripe.android.core.utils.requireApplication
import com.stripe.android.financialconnections.FinancialConnectionsSheet.ElementsSessionContext
import com.stripe.android.financialconnections.FinancialConnectionsSheetResult
import com.stripe.android.financialconnections.FinancialConnectionsSheetInternalResult
import com.stripe.android.financialconnections.launcher.FinancialConnectionsSheetInstantDebitsResult
import com.stripe.android.financialconnections.model.FinancialConnectionsSession
import com.stripe.android.model.PaymentMethod
Expand Down Expand Up @@ -135,15 +135,15 @@ internal class CollectBankAccountViewModel @Inject constructor(
.onFailure { finishWithError(it) }
}

fun onConnectionsForACHResult(result: FinancialConnectionsSheetResult) {
fun onConnectionsForACHResult(result: FinancialConnectionsSheetInternalResult) {
hasLaunched = false
viewModelScope.launch {
when (result) {
is FinancialConnectionsSheetResult.Canceled -> finishWithResult(Cancelled)
is FinancialConnectionsSheetInternalResult.Canceled -> finishWithResult(Cancelled)

is FinancialConnectionsSheetResult.Failed -> finishWithError(result.error)
is FinancialConnectionsSheetInternalResult.Failed -> finishWithError(result.error)

is FinancialConnectionsSheetResult.Completed -> when {
is FinancialConnectionsSheetInternalResult.Completed -> when {
args.attachToIntent -> attachSessionToIntent(
financialConnectionsSession = result.financialConnectionsSession,
usesMicrodeposits = result.manualEntryUsesMicrodeposits,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import androidx.appcompat.app.AppCompatActivity
import com.stripe.android.BuildConfig
import com.stripe.android.financialconnections.FinancialConnectionsSheet
import com.stripe.android.financialconnections.FinancialConnectionsSheet.ElementsSessionContext
import com.stripe.android.financialconnections.FinancialConnectionsSheetResult
import com.stripe.android.financialconnections.FinancialConnectionsSheetInternalResult
import com.stripe.android.financialconnections.launcher.FinancialConnectionsSheetForDataLauncher
import com.stripe.android.financialconnections.launcher.FinancialConnectionsSheetForInstantDebitsLauncher
import com.stripe.android.financialconnections.launcher.FinancialConnectionsSheetInstantDebitsResult
Expand Down Expand Up @@ -46,7 +46,7 @@ internal interface FinancialConnectionsPaymentsProxy {

fun createForACH(
activity: AppCompatActivity,
onComplete: (FinancialConnectionsSheetResult) -> Unit,
onComplete: (FinancialConnectionsSheetInternalResult) -> Unit,
provider: () -> FinancialConnectionsPaymentsProxy = {
FinancialConnectionsLauncherProxy(
FinancialConnectionsSheetForDataLauncher(
Expand Down
Loading

0 comments on commit 455f21a

Please sign in to comment.