-
Notifications
You must be signed in to change notification settings - Fork 657
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Save confirmation state to SavedStateHandle. (#9794)
- Loading branch information
1 parent
b1d4116
commit 0f90bbe
Showing
8 changed files
with
192 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...c/main/java/com/stripe/android/paymentelement/embedded/EmbeddedConfirmationStateHolder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.stripe.android.paymentelement.embedded | ||
|
||
import android.os.Parcelable | ||
import androidx.lifecycle.SavedStateHandle | ||
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodMetadata | ||
import com.stripe.android.paymentelement.EmbeddedPaymentElement | ||
import com.stripe.android.paymentelement.ExperimentalEmbeddedPaymentElementApi | ||
import com.stripe.android.paymentsheet.model.PaymentSelection | ||
import com.stripe.android.paymentsheet.state.PaymentElementLoader | ||
import dagger.assisted.Assisted | ||
import dagger.assisted.AssistedFactory | ||
import dagger.assisted.AssistedInject | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.launch | ||
import kotlinx.parcelize.Parcelize | ||
|
||
@AssistedFactory | ||
@ExperimentalEmbeddedPaymentElementApi | ||
internal fun interface EmbeddedConfirmationStateHolderFactory { | ||
fun create(coroutineScope: CoroutineScope): EmbeddedConfirmationStateHolder | ||
} | ||
|
||
@ExperimentalEmbeddedPaymentElementApi | ||
internal class EmbeddedConfirmationStateHolder @AssistedInject constructor( | ||
private val savedStateHandle: SavedStateHandle, | ||
private val selectionHolder: EmbeddedSelectionHolder, | ||
@Assisted coroutineScope: CoroutineScope, | ||
) { | ||
var state: State? | ||
get() = savedStateHandle[CONFIRMATION_STATE_KEY] | ||
set(value) { | ||
savedStateHandle[CONFIRMATION_STATE_KEY] = value | ||
} | ||
|
||
init { | ||
coroutineScope.launch { | ||
selectionHolder.selection.collect { selection -> | ||
state = state?.copy(selection = selection) | ||
} | ||
} | ||
} | ||
|
||
@Parcelize | ||
data class State( | ||
val paymentMethodMetadata: PaymentMethodMetadata, | ||
val selection: PaymentSelection?, | ||
val initializationMode: PaymentElementLoader.InitializationMode, | ||
val configuration: EmbeddedPaymentElement.Configuration, | ||
) : Parcelable | ||
|
||
companion object { | ||
const val CONFIRMATION_STATE_KEY = "CONFIRMATION_STATE_KEY" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...st/java/com/stripe/android/paymentelement/embedded/EmbeddedConfirmationStateHolderTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
@file:OptIn(ExperimentalEmbeddedPaymentElementApi::class) | ||
|
||
package com.stripe.android.paymentelement.embedded | ||
|
||
import androidx.lifecycle.SavedStateHandle | ||
import com.google.common.truth.Truth.assertThat | ||
import com.stripe.android.lpmfoundations.paymentmethod.PaymentMethodMetadataFactory | ||
import com.stripe.android.paymentelement.EmbeddedPaymentElement | ||
import com.stripe.android.paymentelement.ExperimentalEmbeddedPaymentElementApi | ||
import com.stripe.android.paymentelement.embedded.EmbeddedConfirmationStateHolder.Companion.CONFIRMATION_STATE_KEY | ||
import com.stripe.android.paymentelement.embedded.EmbeddedConfirmationStateHolder.State | ||
import com.stripe.android.paymentsheet.PaymentSheet | ||
import com.stripe.android.paymentsheet.model.PaymentSelection | ||
import com.stripe.android.paymentsheet.model.paymentMethodType | ||
import com.stripe.android.paymentsheet.state.PaymentElementLoader | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.test.UnconfinedTestDispatcher | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.Test | ||
|
||
internal class EmbeddedConfirmationStateHolderTest { | ||
@Test | ||
fun `setting state updates savedStateHandle`() = testScenario { | ||
assertThat(savedStateHandle.get<State?>(CONFIRMATION_STATE_KEY)).isNull() | ||
val state = defaultState() | ||
confirmationStateHolder.state = state | ||
assertThat(savedStateHandle.get<State?>(CONFIRMATION_STATE_KEY)).isEqualTo(state) | ||
} | ||
|
||
@Test | ||
fun `initializing with state in savedStateHandle sets initial value`() { | ||
val state = defaultState() | ||
testScenario( | ||
setup = { | ||
set(CONFIRMATION_STATE_KEY, state) | ||
}, | ||
) { | ||
assertThat(savedStateHandle.get<State?>(CONFIRMATION_STATE_KEY)).isEqualTo(state) | ||
confirmationStateHolder.state = null | ||
assertThat(savedStateHandle.get<State?>(CONFIRMATION_STATE_KEY)).isNull() | ||
} | ||
} | ||
|
||
@Test | ||
fun `updating selection updates state with selection`() = testScenario { | ||
confirmationStateHolder.state = defaultState() | ||
assertThat(confirmationStateHolder.state?.selection?.paymentMethodType).isNull() | ||
selectionHolder.set(PaymentSelection.GooglePay) | ||
assertThat(confirmationStateHolder.state?.selection?.paymentMethodType).isEqualTo("google_pay") | ||
} | ||
|
||
private fun defaultState(): State = State( | ||
paymentMethodMetadata = PaymentMethodMetadataFactory.create(), | ||
selection = null, | ||
initializationMode = PaymentElementLoader.InitializationMode.DeferredIntent( | ||
intentConfiguration = PaymentSheet.IntentConfiguration( | ||
mode = PaymentSheet.IntentConfiguration.Mode.Payment(amount = 5000, currency = "USD"), | ||
) | ||
), | ||
configuration = EmbeddedPaymentElement.Configuration.Builder("Example, Inc.").build() | ||
) | ||
|
||
private class Scenario( | ||
val selectionHolder: EmbeddedSelectionHolder, | ||
val confirmationStateHolder: EmbeddedConfirmationStateHolder, | ||
val savedStateHandle: SavedStateHandle, | ||
) | ||
|
||
private fun testScenario( | ||
setup: SavedStateHandle.() -> Unit = {}, | ||
block: suspend Scenario.() -> Unit, | ||
) = runTest { | ||
val savedStateHandle = SavedStateHandle() | ||
setup(savedStateHandle) | ||
val selectionHolder = EmbeddedSelectionHolder(savedStateHandle) | ||
val confirmationStateHolder = EmbeddedConfirmationStateHolder( | ||
savedStateHandle = savedStateHandle, | ||
selectionHolder = selectionHolder, | ||
coroutineScope = CoroutineScope(UnconfinedTestDispatcher()), | ||
) | ||
Scenario( | ||
selectionHolder = selectionHolder, | ||
confirmationStateHolder = confirmationStateHolder, | ||
savedStateHandle = savedStateHandle, | ||
).block() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters