Skip to content

Commit

Permalink
Use SavedStateHandleProperty to save the actions
Browse files Browse the repository at this point in the history
COAND-857
  • Loading branch information
OscarSpruit committed May 10, 2024
1 parent 06ca574 commit 24c44f1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import com.adyen.checkout.components.core.action.Threeds2ChallengeAction
import com.adyen.checkout.components.core.internal.ActionComponentEvent
import com.adyen.checkout.components.core.internal.ActionObserverRepository
import com.adyen.checkout.components.core.internal.PermissionRequestData
import com.adyen.checkout.components.core.internal.SavedStateHandleContainer
import com.adyen.checkout.components.core.internal.SavedStateHandleProperty
import com.adyen.checkout.components.core.internal.ui.ActionDelegate
import com.adyen.checkout.components.core.internal.ui.DetailsEmittingDelegate
import com.adyen.checkout.components.core.internal.ui.IntentHandlingDelegate
Expand Down Expand Up @@ -48,12 +50,12 @@ import kotlinx.coroutines.flow.receiveAsFlow
@Suppress("TooManyFunctions")
internal class DefaultGenericActionDelegate(
private val observerRepository: ActionObserverRepository,
private val savedStateHandle: SavedStateHandle,
override val savedStateHandle: SavedStateHandle,
private val checkoutConfiguration: CheckoutConfiguration,
override val componentParams: GenericComponentParams,
private val actionDelegateProvider: ActionDelegateProvider,
private val application: Application,
) : GenericActionDelegate {
) : GenericActionDelegate, SavedStateHandleContainer {

private var _delegate: ActionDelegate? = null
override val delegate: ActionDelegate get() = requireNotNull(_delegate)
Expand All @@ -75,6 +77,8 @@ internal class DefaultGenericActionDelegate(

private var onRedirectListener: (() -> Unit)? = null

private var action: Action? by SavedStateHandleProperty(ACTION_KEY)

override fun initialize(coroutineScope: CoroutineScope) {
adyenLog(AdyenLogLevel.DEBUG) { "initialize" }
_coroutineScope = coroutineScope
Expand All @@ -83,7 +87,7 @@ internal class DefaultGenericActionDelegate(

private fun restoreState() {
adyenLog(AdyenLogLevel.DEBUG) { "Restoring state" }
val action: Action? = savedStateHandle[ACTION_KEY]
val action: Action? = action
if (_delegate == null && action != null) {
createDelegateAndObserve(action)
}
Expand Down Expand Up @@ -112,7 +116,7 @@ internal class DefaultGenericActionDelegate(
}

override fun handleAction(action: Action, activity: Activity) {
savedStateHandle[ACTION_KEY] = action
this.action = action

// This check is to support an older flow where you might need to call handleAction several times with 3DS2.
// Initially handleAction is called with a fingerprint action then with a challenge action.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import com.adyen.checkout.components.core.action.AwaitAction
import com.adyen.checkout.components.core.internal.ActionComponentEvent
import com.adyen.checkout.components.core.internal.ActionObserverRepository
import com.adyen.checkout.components.core.internal.PaymentDataRepository
import com.adyen.checkout.components.core.internal.SavedStateHandleContainer
import com.adyen.checkout.components.core.internal.SavedStateHandleProperty
import com.adyen.checkout.components.core.internal.data.api.StatusRepository
import com.adyen.checkout.components.core.internal.data.model.StatusResponse
import com.adyen.checkout.components.core.internal.ui.model.GenericComponentParams
Expand Down Expand Up @@ -47,11 +49,11 @@ import java.util.concurrent.TimeUnit
@Suppress("TooManyFunctions")
internal class DefaultAwaitDelegate(
private val observerRepository: ActionObserverRepository,
private val savedStateHandle: SavedStateHandle,
override val savedStateHandle: SavedStateHandle,
override val componentParams: GenericComponentParams,
private val statusRepository: StatusRepository,
private val paymentDataRepository: PaymentDataRepository,
) : AwaitDelegate {
) : AwaitDelegate, SavedStateHandleContainer {

private val _outputDataFlow = MutableStateFlow(createOutputData())
override val outputDataFlow: Flow<AwaitOutputData> = _outputDataFlow
Expand All @@ -74,13 +76,16 @@ internal class DefaultAwaitDelegate(

private var statusPollingJob: Job? = null

private var action: Action? by SavedStateHandleProperty(ACTION_KEY)

override fun initialize(coroutineScope: CoroutineScope) {
_coroutineScope = coroutineScope
restoreState()
}

private fun restoreState() {
val action: Action? = savedStateHandle[ACTION_KEY]
adyenLog(AdyenLogLevel.DEBUG) { "Restoring state" }
val action: Action? = action
if (action != null) {
handleAction(action)
}
Expand Down Expand Up @@ -109,7 +114,7 @@ internal class DefaultAwaitDelegate(
}

override fun handleAction(action: Action, activity: Activity) {
savedStateHandle[ACTION_KEY] = action
this.action = action
handleAction(action)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import com.adyen.checkout.components.core.internal.ActionComponentEvent
import com.adyen.checkout.components.core.internal.ActionObserverRepository
import com.adyen.checkout.components.core.internal.PaymentDataRepository
import com.adyen.checkout.components.core.internal.PermissionRequestData
import com.adyen.checkout.components.core.internal.SavedStateHandleContainer
import com.adyen.checkout.components.core.internal.SavedStateHandleProperty
import com.adyen.checkout.components.core.internal.data.api.StatusRepository
import com.adyen.checkout.components.core.internal.data.model.StatusResponse
import com.adyen.checkout.components.core.internal.ui.model.GenericComponentParams
Expand Down Expand Up @@ -62,14 +64,14 @@ import kotlin.time.Duration.Companion.seconds
@Suppress("TooManyFunctions", "LongParameterList")
internal class DefaultQRCodeDelegate(
private val observerRepository: ActionObserverRepository,
private val savedStateHandle: SavedStateHandle,
override val savedStateHandle: SavedStateHandle,
override val componentParams: GenericComponentParams,
private val statusRepository: StatusRepository,
private val statusCountDownTimer: QRCodeCountDownTimer,
private val redirectHandler: RedirectHandler,
private val paymentDataRepository: PaymentDataRepository,
private val imageSaver: ImageSaver
) : QRCodeDelegate {
) : QRCodeDelegate, SavedStateHandleContainer {

private val _outputDataFlow = MutableStateFlow(createOutputData())
override val outputDataFlow: Flow<QRCodeOutputData> = _outputDataFlow
Expand Down Expand Up @@ -101,6 +103,8 @@ internal class DefaultQRCodeDelegate(

private var maxPollingDurationMillis = DEFAULT_MAX_POLLING_DURATION

private var action: QrCodeAction? by SavedStateHandleProperty(ACTION_KEY)

private fun attachStatusTimer() {
statusCountDownTimer.attach(
millisInFuture = maxPollingDurationMillis,
Expand All @@ -121,7 +125,8 @@ internal class DefaultQRCodeDelegate(
}

private fun restoreState() {
val action: QrCodeAction? = savedStateHandle[ACTION_KEY]
adyenLog(AdyenLogLevel.DEBUG) { "Restoring state" }
val action: QrCodeAction? = action
if (action != null) {
handleAction(action)
}
Expand Down Expand Up @@ -156,7 +161,7 @@ internal class DefaultQRCodeDelegate(
return
}

savedStateHandle[ACTION_KEY] = action
this.action = action
paymentDataRepository.paymentData = action.paymentData

if (shouldLaunchRedirect(action)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ import com.adyen.checkout.components.core.action.VoucherAction
import com.adyen.checkout.components.core.internal.ActionComponentEvent
import com.adyen.checkout.components.core.internal.ActionObserverRepository
import com.adyen.checkout.components.core.internal.PermissionRequestData
import com.adyen.checkout.components.core.internal.SavedStateHandleContainer
import com.adyen.checkout.components.core.internal.SavedStateHandleProperty
import com.adyen.checkout.components.core.internal.ui.model.GenericComponentParams
import com.adyen.checkout.components.core.internal.util.DateUtils
import com.adyen.checkout.components.core.internal.util.bufferedChannel
import com.adyen.checkout.core.AdyenLogLevel
import com.adyen.checkout.core.PermissionHandlerCallback
import com.adyen.checkout.core.exception.CheckoutException
import com.adyen.checkout.core.exception.ComponentException
import com.adyen.checkout.core.internal.util.adyenLog
import com.adyen.checkout.ui.core.internal.exception.PermissionRequestException
import com.adyen.checkout.ui.core.internal.ui.ComponentViewType
import com.adyen.checkout.ui.core.internal.util.ImageSaver
Expand All @@ -44,11 +48,11 @@ import java.util.Calendar
@Suppress("TooManyFunctions")
internal class DefaultVoucherDelegate(
private val observerRepository: ActionObserverRepository,
private val savedStateHandle: SavedStateHandle,
override val savedStateHandle: SavedStateHandle,
override val componentParams: GenericComponentParams,
private val pdfOpener: PdfOpener,
private val imageSaver: ImageSaver,
) : VoucherDelegate {
) : VoucherDelegate, SavedStateHandleContainer {

private val _outputDataFlow = MutableStateFlow(createOutputData())
override val outputDataFlow: Flow<VoucherOutputData> = _outputDataFlow
Expand All @@ -70,13 +74,16 @@ internal class DefaultVoucherDelegate(
private var _coroutineScope: CoroutineScope? = null
private val coroutineScope: CoroutineScope get() = requireNotNull(_coroutineScope)

private var action: Action? by SavedStateHandleProperty(ACTION_KEY)

override fun initialize(coroutineScope: CoroutineScope) {
_coroutineScope = coroutineScope
restoreState()
}

private fun restoreState() {
val action: Action? = savedStateHandle[ACTION_KEY]
adyenLog(AdyenLogLevel.DEBUG) { "Restoring state" }
val action: Action? = action
if (action != null) {
handleAction(action)
}
Expand All @@ -102,7 +109,7 @@ internal class DefaultVoucherDelegate(
}

override fun handleAction(action: Action, activity: Activity) {
savedStateHandle[ACTION_KEY] = action
this.action = action
handleAction(action)
}

Expand Down

0 comments on commit 24c44f1

Please sign in to comment.