Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.ViewCompositionStrategy
import androidx.fragment.app.viewModels
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.processout.sdk.api.dispatcher.PODefaultEventDispatchers
import com.processout.sdk.api.model.response.POCard
import com.processout.sdk.core.ProcessOutActivityResult
import com.processout.sdk.core.ProcessOutResult
Expand Down Expand Up @@ -51,8 +50,7 @@ internal class CardTokenizationBottomSheet : BaseBottomSheetDialogFragment<POCar
private val viewModel: CardTokenizationViewModel by viewModels {
CardTokenizationViewModel.Factory(
app = requireActivity().application,
configuration = configuration,
legacyEventDispatcher = PODefaultEventDispatchers.defaultCardTokenization
configuration = configuration
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import androidx.compose.ui.text.TextRange
import androidx.compose.ui.text.input.TextFieldValue
import com.processout.sdk.R
import com.processout.sdk.api.dispatcher.POEventDispatcher
import com.processout.sdk.api.dispatcher.card.tokenization.PODefaultCardTokenizationEventDispatcher
import com.processout.sdk.api.model.event.POCardTokenizationEvent
import com.processout.sdk.api.model.event.POCardTokenizationEvent.*
import com.processout.sdk.api.model.request.*
Expand Down Expand Up @@ -61,7 +60,6 @@ internal class CardTokenizationInteractor(
private val cardsRepository: POCardsRepository,
private val cardSchemeProvider: CardSchemeProvider,
private val addressSpecificationProvider: AddressSpecificationProvider,
private val legacyEventDispatcher: PODefaultCardTokenizationEventDispatcher?, // TODO: remove before next major release.
private val eventDispatcher: POEventDispatcher = POEventDispatcher
) : BaseInteractor() {

Expand Down Expand Up @@ -90,6 +88,7 @@ internal class CardTokenizationInteractor(

private var issuerInformationJob: Job? = null

private var latestProcessingRequest: POCardTokenizationProcessingRequest? = null
private var latestEligibilityRequest: CardTokenizationEligibilityRequest? = null
private var latestPreferredSchemeRequest: POCardTokenizationPreferredSchemeRequest? = null
private var latestShouldContinueRequest: POCardTokenizationShouldContinueRequest? = null
Expand Down Expand Up @@ -134,6 +133,7 @@ internal class CardTokenizationInteractor(
private fun cancelProcessing() {
interactorScope.coroutineContext.cancelChildren()
issuerInformationJob = null
latestProcessingRequest = null
latestEligibilityRequest = null
latestPreferredSchemeRequest = null
latestShouldContinueRequest = null
Expand Down Expand Up @@ -509,35 +509,22 @@ internal class CardTokenizationInteractor(
interactorScope.launch {
val request = POCardTokenizationPreferredSchemeRequest(issuerInformation)
latestPreferredSchemeRequest = request
if (legacyEventDispatcher?.subscribedForPreferredSchemeRequest() == true) {
legacyEventDispatcher.send(request)
} else {
eventDispatcher.send(request)
}
eventDispatcher.send(request)
POLogger.info("Requested to choose preferred scheme by issuer information: %s", issuerInformation)
}
}

private fun collectPreferredScheme() {
interactorScope.launch {
legacyEventDispatcher?.preferredSchemeResponse?.collect { response ->
handlePreferredScheme(response)
}
}
eventDispatcher.subscribeForResponse<POCardTokenizationPreferredSchemeResponse>(
coroutineScope = interactorScope
) { response ->
handlePreferredScheme(response)
}
}

private fun handlePreferredScheme(response: POCardTokenizationPreferredSchemeResponse) {
if (response.uuid == latestPreferredSchemeRequest?.uuid) {
latestPreferredSchemeRequest = null
updatePreferredScheme(scheme = response.preferredScheme)
if (_state.value.pendingSubmit) {
_state.update { it.copy(pendingSubmit = false) }
submit()
if (response.uuid == latestPreferredSchemeRequest?.uuid) {
latestPreferredSchemeRequest = null
updatePreferredScheme(scheme = response.preferredScheme)
if (_state.value.pendingSubmit) {
_state.update { it.copy(pendingSubmit = false) }
submit()
}
}
}
}
Expand Down Expand Up @@ -839,52 +826,31 @@ internal class CardTokenizationInteractor(
}
}

@Suppress("DEPRECATION")
private suspend fun requestToProcessTokenizedCard(card: POCard) {
val request = POCardTokenizationProcessingRequest(
card = card,
saveCard = _state.value.saveCardField.value.text.toBooleanStrictOrNull() ?: false
)
if (legacyEventDispatcher?.subscribedForProcessTokenizedCard() == true) {
legacyEventDispatcher.processTokenizedCard(card)
} else if (legacyEventDispatcher?.subscribedForProcessTokenizedCardRequest() == true) {
legacyEventDispatcher.processTokenizedCardRequest(request)
} else {
eventDispatcher.send(request)
}
latestProcessingRequest = request
eventDispatcher.send(request)
POLogger.info(
message = "Requested to process tokenized card.",
attributes = mapOf(POLogAttribute.CARD_ID to card.id)
)
}

private fun handleCompletion() {
interactorScope.launch {
legacyEventDispatcher?.completion?.collect { result ->
result.onSuccess {
_state.value.tokenizedCard?.let { card ->
complete(Success(card))
}.orElse {
val failure = ProcessOutResult.Failure(
code = Generic(),
message = "Completion is called with Success via dispatcher before card is tokenized."
)
requestIfShouldContinue(failure)
}
}.onFailure {
requestIfShouldContinue(failure = it)
}
}
}
eventDispatcher.subscribeForResponse<POCardTokenizationProcessingResponse>(
coroutineScope = interactorScope
) { response ->
response.result
.onSuccess { card ->
complete(Success(card))
}.onFailure {
requestIfShouldContinue(failure = it)
}
if (response.uuid == latestProcessingRequest?.uuid) {
response.result
.onSuccess { card ->
complete(Success(card))
}.onFailure {
requestIfShouldContinue(failure = it)
}
}
}
}

Expand All @@ -901,36 +867,23 @@ internal class CardTokenizationInteractor(
interactorScope.launch {
val request = POCardTokenizationShouldContinueRequest(failure)
latestShouldContinueRequest = request
if (legacyEventDispatcher?.subscribedForShouldContinueRequest() == true) {
legacyEventDispatcher.send(request)
} else {
eventDispatcher.send(request)
}
eventDispatcher.send(request)
POLogger.info("Requested to decide whether the flow should continue or complete after the failure: %s", failure)
}
}

private fun shouldContinueOnFailure() {
interactorScope.launch {
legacyEventDispatcher?.shouldContinueResponse?.collect { response ->
handleShouldContinue(response)
}
}
eventDispatcher.subscribeForResponse<POCardTokenizationShouldContinueResponse>(
coroutineScope = interactorScope
) { response ->
handleShouldContinue(response)
}
}

private fun handleShouldContinue(response: POCardTokenizationShouldContinueResponse) {
if (response.uuid == latestShouldContinueRequest?.uuid) {
latestShouldContinueRequest = null
if (response.shouldContinue) {
handle(response.failure)
} else {
POLogger.info("Completed after the failure: %s", response.failure)
_completion.update { Failure(response.failure) }
if (response.uuid == latestShouldContinueRequest?.uuid) {
latestShouldContinueRequest = null
if (response.shouldContinue) {
handle(response.failure)
} else {
POLogger.info("Completed after the failure: %s", response.failure)
_completion.update { Failure(response.failure) }
}
}
}
}
Expand Down Expand Up @@ -1086,7 +1039,6 @@ internal class CardTokenizationInteractor(

private fun dispatch(event: POCardTokenizationEvent) {
interactorScope.launch {
legacyEventDispatcher?.send(event)
eventDispatcher.send(event)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import com.processout.sdk.R
import com.processout.sdk.api.ProcessOut
import com.processout.sdk.api.dispatcher.card.tokenization.PODefaultCardTokenizationEventDispatcher
import com.processout.sdk.ui.card.tokenization.CardTokenizationInteractorState.*
import com.processout.sdk.ui.card.tokenization.CardTokenizationViewModelState.*
import com.processout.sdk.ui.core.shared.image.PODrawableImage
Expand Down Expand Up @@ -41,8 +40,7 @@ internal class CardTokenizationViewModel private constructor(

class Factory(
private val app: Application,
private val configuration: POCardTokenizationConfiguration,
private val legacyEventDispatcher: PODefaultCardTokenizationEventDispatcher?
private val configuration: POCardTokenizationConfiguration
) : ViewModelProvider.Factory {
@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel> create(modelClass: Class<T>): T =
Expand All @@ -54,8 +52,7 @@ internal class CardTokenizationViewModel private constructor(
configuration = configuration,
cardsRepository = ProcessOut.instance.cards,
cardSchemeProvider = CardSchemeProvider(),
addressSpecificationProvider = AddressSpecificationProvider(app),
legacyEventDispatcher = legacyEventDispatcher
addressSpecificationProvider = AddressSpecificationProvider(app)
)
) as T
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("unused")

package com.processout.sdk.ui.card.tokenization.component

import android.content.Context
Expand Down Expand Up @@ -79,8 +81,7 @@ class POCardTokenizationViewComponent private constructor(
val viewModel: CardTokenizationViewModel by from.viewModels {
CardTokenizationViewModel.Factory(
app = from.requireActivity().application,
configuration = configuration,
legacyEventDispatcher = null
configuration = configuration
)
}
return POCardTokenizationViewComponent(
Expand Down Expand Up @@ -117,8 +118,7 @@ class POCardTokenizationViewComponent private constructor(
val viewModel: CardTokenizationViewModel by from.viewModels {
CardTokenizationViewModel.Factory(
app = from.application,
configuration = configuration,
legacyEventDispatcher = null
configuration = configuration
)
}
return POCardTokenizationViewComponent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ internal fun CardUpdateScreen(
.verticalScroll(rememberScrollState())
.padding(spacing.extraLarge)
) {
val verticalSpacingPx = (spacing.extraLarge * 4 + 10.dp).dpToPx()
val verticalSpacingPx = (spacing.extraLarge * 4 + 15.dp).dpToPx()
Column(
modifier = Modifier.onGloballyPositioned {
val contentHeight = it.size.height + topBarHeight + bottomBarHeight + verticalSpacingPx
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import androidx.lifecycle.viewModelScope
import com.processout.sdk.R
import com.processout.sdk.api.ProcessOut
import com.processout.sdk.api.dispatcher.POEventDispatcher
import com.processout.sdk.api.dispatcher.card.update.PODefaultCardUpdateEventDispatcher
import com.processout.sdk.api.model.event.POCardUpdateEvent
import com.processout.sdk.api.model.event.POCardUpdateEvent.*
import com.processout.sdk.api.model.request.POCardUpdateRequest
Expand Down Expand Up @@ -45,7 +44,6 @@ internal class CardUpdateViewModel private constructor(
private val app: Application,
private val configuration: POCardUpdateConfiguration,
private val cardsRepository: POCardsRepository,
private val legacyEventDispatcher: PODefaultCardUpdateEventDispatcher, // TODO: remove before next major release.
private val eventDispatcher: POEventDispatcher,
private val logAttributes: Map<String, String>
) : ViewModel() {
Expand All @@ -60,7 +58,6 @@ internal class CardUpdateViewModel private constructor(
app = app,
configuration = configuration,
cardsRepository = ProcessOut.instance.cards,
legacyEventDispatcher = PODefaultCardUpdateEventDispatcher,
eventDispatcher = POEventDispatcher,
logAttributes = mapOf(POLogAttribute.CARD_ID to configuration.cardId)
) as T
Expand Down Expand Up @@ -171,14 +168,14 @@ internal class CardUpdateViewModel private constructor(
message = "Attempt to resolve card scheme.",
attributes = logAttributes
)
val iin = this?.iin ?: this?.maskedNumber?.let { iin(it) }
iin?.let {
val iin = this?.iin ?: this?.maskedNumber?.let { iin(maskedNumber = it) }
iin?.let { iin ->
viewModelScope.launch {
cardsRepository.fetchIssuerInformation(it)
cardsRepository.fetchIssuerInformation(iin)
.onSuccess { updateScheme(it.scheme) }
.onFailure {
.onFailure { failure ->
POLogger.info(
message = "Failed to resolve card scheme: %s", it,
message = "Failed to resolve card scheme: %s", failure,
attributes = logAttributes
)
}
Expand Down Expand Up @@ -362,11 +359,7 @@ internal class CardUpdateViewModel private constructor(
failure = failure
)
latestShouldContinueRequest = request
if (legacyEventDispatcher.subscribedForShouldContinueRequest()) {
legacyEventDispatcher.send(request)
} else {
eventDispatcher.send(request)
}
eventDispatcher.send(request)
POLogger.info(
message = "Requested to decide whether the flow should continue or complete after the failure: %s", failure,
attributes = logAttributes
Expand All @@ -375,29 +368,20 @@ internal class CardUpdateViewModel private constructor(
}

private fun shouldContinueOnFailure() {
viewModelScope.launch {
legacyEventDispatcher.shouldContinueResponse.collect { response ->
handleShouldContinue(response)
}
}
eventDispatcher.subscribeForResponse<POCardUpdateShouldContinueResponse>(
coroutineScope = viewModelScope
) { response ->
handleShouldContinue(response)
}
}

private fun handleShouldContinue(response: POCardUpdateShouldContinueResponse) {
if (response.uuid == latestShouldContinueRequest?.uuid) {
latestShouldContinueRequest = null
if (response.shouldContinue) {
handle(response.failure)
} else {
POLogger.info(
message = "Completed after the failure: %s", response.failure,
attributes = logAttributes
)
_completion.update { Failure(response.failure) }
if (response.uuid == latestShouldContinueRequest?.uuid) {
latestShouldContinueRequest = null
if (response.shouldContinue) {
handle(response.failure)
} else {
POLogger.info(
message = "Completed after the failure: %s", response.failure,
attributes = logAttributes
)
_completion.update { Failure(response.failure) }
}
}
}
}
Expand Down Expand Up @@ -462,7 +446,6 @@ internal class CardUpdateViewModel private constructor(

private fun dispatch(event: POCardUpdateEvent) {
viewModelScope.launch {
legacyEventDispatcher.send(event)
eventDispatcher.send(event)
}
}
Expand Down
Loading