Skip to content

Commit

Permalink
Merge pull request #1851 from Adyen/chore/fix-dispatchers-in-example
Browse files Browse the repository at this point in the history
Replace usage of Dispatchers with DispatcherProvider in example app
  • Loading branch information
OscarSpruit authored Oct 29, 2024
2 parents 036226b + a4e7e7a commit abda40d
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
package com.adyen.checkout.example.repositories

import android.util.Log
import com.adyen.checkout.core.DispatcherProvider
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

@Suppress("TooGenericExceptionCaught")
internal suspend fun <T> safeApiCall(call: suspend () -> T): T? = withContext(Dispatchers.IO) {
@Suppress("TooGenericExceptionCaught", "RestrictedApi")
internal suspend fun <T> safeApiCall(call: suspend () -> T): T? = withContext(DispatcherProvider.IO) {
return@withContext try {
call()
} catch (e: CancellationException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.adyen.checkout.components.core.PaymentComponentState
import com.adyen.checkout.components.core.StoredPaymentMethod
import com.adyen.checkout.components.core.action.Action
import com.adyen.checkout.components.core.paymentmethod.PaymentMethodDetails
import com.adyen.checkout.core.DispatcherProvider
import com.adyen.checkout.core.exception.ModelSerializationException
import com.adyen.checkout.dropin.AddressLookupDropInServiceResult
import com.adyen.checkout.dropin.BalanceDropInServiceResult
Expand All @@ -39,7 +40,6 @@ import com.adyen.checkout.example.repositories.AddressLookupRepository
import com.adyen.checkout.example.repositories.PaymentsRepository
import com.adyen.checkout.redirect.RedirectComponent
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -80,6 +80,7 @@ class ExampleAdvancedDropInService : DropInService() {
is AddressLookupCompletionState.Address -> {
AddressLookupDropInServiceResult.LookupComplete(it.lookupAddress)
}

is AddressLookupCompletionState.Error -> AddressLookupDropInServiceResult.Error(
errorDialog = ErrorDialog(
message = it.message,
Expand All @@ -90,10 +91,11 @@ class ExampleAdvancedDropInService : DropInService() {
}.launchIn(this)
}

@Suppress("RestrictedApi")
override fun onSubmit(
state: PaymentComponentState<*>,
) {
launch(Dispatchers.IO) {
launch(DispatcherProvider.IO) {
Log.d(TAG, "onPaymentsCallRequested")

checkPaymentState(state)
Expand Down Expand Up @@ -139,8 +141,9 @@ class ExampleAdvancedDropInService : DropInService() {
// read bundle and handle it
}

@Suppress("RestrictedApi")
override fun onAdditionalDetails(actionComponentData: ActionComponentData) {
launch(Dispatchers.IO) {
launch(DispatcherProvider.IO) {
Log.d(TAG, "onDetailsCallRequested")

val actionComponentJson = ActionComponentData.SERIALIZER.serialize(actionComponentData)
Expand Down Expand Up @@ -212,9 +215,10 @@ class ExampleAdvancedDropInService : DropInService() {
return OrderResponse.SERIALIZER.deserialize(orderJSON)
}

@Suppress("RestrictedApi")
private fun fetchPaymentMethods(orderResponse: OrderResponse? = null) {
Log.d(TAG, "fetchPaymentMethods")
launch(Dispatchers.IO) {
launch(DispatcherProvider.IO) {
val order = orderResponse?.let {
Order(
pspReference = it.pspReference,
Expand All @@ -241,8 +245,9 @@ class ExampleAdvancedDropInService : DropInService() {
}
}

@Suppress("RestrictedApi")
override fun onBalanceCheck(paymentComponentState: PaymentComponentState<*>) {
launch(Dispatchers.IO) {
launch(DispatcherProvider.IO) {
Log.d(TAG, "checkBalance")
val amount = paymentComponentState.data.amount
val paymentMethod = paymentComponentState.data.paymentMethod
Expand Down Expand Up @@ -295,8 +300,9 @@ class ExampleAdvancedDropInService : DropInService() {
}
}

@Suppress("RestrictedApi")
override fun onOrderRequest() {
launch(Dispatchers.IO) {
launch(DispatcherProvider.IO) {
Log.d(TAG, "createOrder")

val paymentRequest = createOrderRequest(
Expand Down Expand Up @@ -326,8 +332,9 @@ class ExampleAdvancedDropInService : DropInService() {
}
}

@Suppress("RestrictedApi")
override fun onOrderCancel(order: Order, shouldUpdatePaymentMethods: Boolean) {
launch(Dispatchers.IO) {
launch(DispatcherProvider.IO) {
Log.d(TAG, "cancelOrder")
val orderJson = Order.SERIALIZER.serialize(order)
val request = createCancelOrderRequest(
Expand Down Expand Up @@ -364,10 +371,11 @@ class ExampleAdvancedDropInService : DropInService() {
}
}

@Suppress("RestrictedApi")
override fun onRemoveStoredPaymentMethod(
storedPaymentMethod: StoredPaymentMethod,
) {
launch(Dispatchers.IO) {
launch(DispatcherProvider.IO) {
val storedPaymentMethodId = storedPaymentMethod.id.orEmpty()
val isSuccessfullyRemoved = paymentsRepository.removeStoredPaymentMethod(
storedPaymentMethodId = storedPaymentMethodId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.adyen.checkout.components.core.PaymentComponentData
import com.adyen.checkout.components.core.PaymentComponentState
import com.adyen.checkout.components.core.StoredPaymentMethod
import com.adyen.checkout.components.core.action.Action
import com.adyen.checkout.core.DispatcherProvider
import com.adyen.checkout.dropin.DropInService
import com.adyen.checkout.dropin.DropInServiceResult
import com.adyen.checkout.dropin.ErrorDialog
Expand All @@ -25,7 +26,6 @@ import com.adyen.checkout.example.extensions.toStringPretty
import com.adyen.checkout.example.repositories.PaymentsRepository
import com.adyen.checkout.redirect.RedirectComponent
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.json.JSONObject
import javax.inject.Inject
Expand All @@ -43,10 +43,11 @@ class ExampleDropInService : DropInService() {
@Inject
lateinit var keyValueStorage: KeyValueStorage

@Suppress("RestrictedApi")
override fun onSubmit(
state: PaymentComponentState<*>
) {
launch(Dispatchers.IO) {
launch(DispatcherProvider.IO) {
Log.d(TAG, "onPaymentsCallRequested")

checkPaymentState(state)
Expand All @@ -61,7 +62,7 @@ class ExampleDropInService : DropInService() {
merchantAccount = keyValueStorage.getMerchantAccount(),
redirectUrl = RedirectComponent.getReturnUrl(applicationContext),
threeDSMode = keyValueStorage.getThreeDSMode(),
shopperEmail = keyValueStorage.getShopperEmail()
shopperEmail = keyValueStorage.getShopperEmail(),
)

Log.v(TAG, "paymentComponentJson - ${paymentComponentJson.toStringPretty()}")
Expand All @@ -82,8 +83,9 @@ class ExampleDropInService : DropInService() {
}
}

@Suppress("RestrictedApi")
override fun onAdditionalDetails(actionComponentData: ActionComponentData) {
launch(Dispatchers.IO) {
launch(DispatcherProvider.IO) {
Log.d(TAG, "onDetailsCallRequested")

val actionComponentJson = ActionComponentData.SERIALIZER.serialize(actionComponentData)
Expand Down Expand Up @@ -126,10 +128,11 @@ class ExampleDropInService : DropInService() {
return jsonResponse.has("action")
}

@Suppress("RestrictedApi")
override fun onRemoveStoredPaymentMethod(
storedPaymentMethod: StoredPaymentMethod,
) {
launch(Dispatchers.IO) {
launch(DispatcherProvider.IO) {
val storedPaymentMethodId = storedPaymentMethod.id.orEmpty()
val isSuccessfullyRemoved = paymentsRepository.removeStoredPaymentMethod(
storedPaymentMethodId = storedPaymentMethodId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.adyen.checkout.components.core.ActionComponentData
import com.adyen.checkout.components.core.PaymentComponentData
import com.adyen.checkout.components.core.PaymentComponentState
import com.adyen.checkout.components.core.action.Action
import com.adyen.checkout.core.DispatcherProvider
import com.adyen.checkout.dropin.DropInServiceResult
import com.adyen.checkout.dropin.ErrorDialog
import com.adyen.checkout.dropin.SessionDropInService
Expand All @@ -24,7 +25,6 @@ import com.adyen.checkout.example.extensions.toStringPretty
import com.adyen.checkout.example.repositories.PaymentsRepository
import com.adyen.checkout.redirect.RedirectComponent
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.json.JSONObject
import javax.inject.Inject
Expand All @@ -38,14 +38,15 @@ class ExampleSessionsDropInService : SessionDropInService() {
@Inject
lateinit var keyValueStorage: KeyValueStorage

@Suppress("RestrictedApi")
override fun onSubmit(
state: PaymentComponentState<*>,
): Boolean {
return if (
state is BlikComponentState ||
state is CardComponentState
) {
launch(Dispatchers.IO) {
launch(DispatcherProvider.IO) {
Log.d(TAG, "onPaymentsCallRequested")

// Check out the documentation of this method on the parent DropInService class
Expand Down Expand Up @@ -73,11 +74,12 @@ class ExampleSessionsDropInService : SessionDropInService() {
}
}

@Suppress("RestrictedApi")
override fun onAdditionalDetails(
actionComponentData: ActionComponentData,
): Boolean {
return if (isFlowTakenOver) {
launch(Dispatchers.IO) {
launch(DispatcherProvider.IO) {
Log.d(TAG, "onDetailsCallRequested")

val response = paymentsRepository.makeDetailsRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ import com.adyen.checkout.components.core.ComponentCallback
import com.adyen.checkout.components.core.ComponentError
import com.adyen.checkout.components.core.PaymentComponentData
import com.adyen.checkout.components.core.action.Action
import com.adyen.checkout.core.DispatcherProvider
import com.adyen.checkout.example.R
import com.adyen.checkout.example.data.storage.KeyValueStorage
import com.adyen.checkout.example.repositories.PaymentsRepository
import com.adyen.checkout.example.service.createPaymentRequest
import com.adyen.checkout.example.service.getPaymentMethodRequest
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
Expand Down Expand Up @@ -57,7 +57,8 @@ internal class BacsViewModel @Inject constructor(
viewModelScope.launch { fetchPaymentMethods() }
}

private suspend fun fetchPaymentMethods() = withContext(Dispatchers.IO) {
@Suppress("RestrictedApi")
private suspend fun fetchPaymentMethods() = withContext(DispatcherProvider.IO) {
val validationError = if (keyValueStorage.getAmount().currency != CheckoutCurrency.GBP.name) {
BacsViewState.Error(R.string.currency_code_error, CheckoutCurrency.GBP.name)
} else if (keyValueStorage.getCountry() != Locale.UK.country) {
Expand All @@ -79,7 +80,7 @@ internal class BacsViewModel @Inject constructor(
countryCode = keyValueStorage.getCountry(),
shopperLocale = keyValueStorage.getShopperLocale(),
splitCardFundingSources = keyValueStorage.isSplitCardFundingSources(),
)
),
)

val paymentMethod = paymentMethodResponse
Expand All @@ -92,8 +93,8 @@ internal class BacsViewModel @Inject constructor(
_bacsComponentDataFlow.emit(
BacsComponentData(
paymentMethod,
this@BacsViewModel
)
this@BacsViewModel,
),
)
_viewState.emit(BacsViewState.ShowComponent)
}
Expand All @@ -115,8 +116,9 @@ internal class BacsViewModel @Inject constructor(
viewModelScope.launch { _events.emit(BacsEvent.PaymentResult("Failed: ${error.errorMessage}")) }
}

@Suppress("RestrictedApi")
private fun sendPaymentDetails(actionComponentData: ActionComponentData) {
viewModelScope.launch(Dispatchers.IO) {
viewModelScope.launch(DispatcherProvider.IO) {
val json = ActionComponentData.SERIALIZER.serialize(actionComponentData)
handlePaymentResponse(paymentsRepository.makeDetailsRequest(json))
}
Expand Down Expand Up @@ -145,7 +147,8 @@ internal class BacsViewModel @Inject constructor(

val paymentComponentData = PaymentComponentData.SERIALIZER.serialize(data)

viewModelScope.launch(Dispatchers.IO) {
@Suppress("RestrictedApi")
viewModelScope.launch(DispatcherProvider.IO) {
val paymentRequest = createPaymentRequest(
paymentComponentData = paymentComponentData,
shopperReference = keyValueStorage.getShopperReference(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ import com.adyen.checkout.components.core.ComponentError
import com.adyen.checkout.components.core.PaymentComponentData
import com.adyen.checkout.components.core.action.Action
import com.adyen.checkout.components.core.paymentmethod.BlikPaymentMethod
import com.adyen.checkout.core.DispatcherProvider
import com.adyen.checkout.example.R
import com.adyen.checkout.example.data.storage.KeyValueStorage
import com.adyen.checkout.example.repositories.PaymentsRepository
import com.adyen.checkout.example.service.createPaymentRequest
import com.adyen.checkout.example.service.getPaymentMethodRequest
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
Expand Down Expand Up @@ -55,7 +55,8 @@ class BlikViewModel @Inject constructor(
viewModelScope.launch { _blikViewState.emit(fetchPaymentMethods()) }
}

private suspend fun fetchPaymentMethods(): BlikViewState = withContext(Dispatchers.IO) {
@Suppress("RestrictedApi")
private suspend fun fetchPaymentMethods(): BlikViewState = withContext(DispatcherProvider.IO) {
if (keyValueStorage.getAmount().currency != CheckoutCurrency.PLN.name) {
return@withContext BlikViewState.Error(R.string.currency_code_error, CheckoutCurrency.PLN.name)
} else if (keyValueStorage.getCountry() != POLAND_COUNTRY_CODE) {
Expand All @@ -70,7 +71,7 @@ class BlikViewModel @Inject constructor(
countryCode = keyValueStorage.getCountry(),
shopperLocale = keyValueStorage.getShopperLocale(),
splitCardFundingSources = keyValueStorage.isSplitCardFundingSources(),
)
),
)

val blikPaymentMethod = paymentMethodResponse
Expand Down Expand Up @@ -132,13 +133,15 @@ class BlikViewModel @Inject constructor(
val action = Action.SERIALIZER.deserialize(json.getJSONObject("action"))
_blikViewState.value = BlikViewState.Action(action)
}

else -> _events.emit(BlikEvent.PaymentResult("Finished: ${json.optString("resultCode")}"))
}
} ?: _events.emit(BlikEvent.PaymentResult("Failed"))
}

@Suppress("RestrictedApi")
private fun sendPaymentDetails(actionComponentData: ActionComponentData) {
viewModelScope.launch(Dispatchers.IO) {
viewModelScope.launch(DispatcherProvider.IO) {
val json = ActionComponentData.SERIALIZER.serialize(actionComponentData)
handlePaymentResponse(paymentsRepository.makeDetailsRequest(json))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import com.adyen.checkout.components.core.ComponentError
import com.adyen.checkout.components.core.LookupAddress
import com.adyen.checkout.components.core.PaymentComponentData
import com.adyen.checkout.components.core.action.Action
import com.adyen.checkout.core.DispatcherProvider
import com.adyen.checkout.example.data.storage.KeyValueStorage
import com.adyen.checkout.example.repositories.AddressLookupCompletionState
import com.adyen.checkout.example.repositories.AddressLookupRepository
import com.adyen.checkout.example.repositories.PaymentsRepository
import com.adyen.checkout.example.service.createPaymentRequest
import com.adyen.checkout.example.service.getPaymentMethodRequest
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
Expand Down Expand Up @@ -60,7 +60,8 @@ internal class CardViewModel @Inject constructor(
}.launchIn(viewModelScope)
}

private suspend fun fetchPaymentMethods() = withContext(Dispatchers.IO) {
@Suppress("RestrictedApi")
private suspend fun fetchPaymentMethods() = withContext(DispatcherProvider.IO) {
val paymentMethodResponse = paymentsRepository.getPaymentMethods(
getPaymentMethodRequest(
merchantAccount = keyValueStorage.getMerchantAccount(),
Expand Down Expand Up @@ -135,7 +136,8 @@ internal class CardViewModel @Inject constructor(

val paymentComponentData = PaymentComponentData.SERIALIZER.serialize(data)

viewModelScope.launch(Dispatchers.IO) {
@Suppress("RestrictedApi")
viewModelScope.launch(DispatcherProvider.IO) {
val paymentRequest = createPaymentRequest(
paymentComponentData = paymentComponentData,
shopperReference = keyValueStorage.getShopperReference(),
Expand Down Expand Up @@ -169,8 +171,9 @@ internal class CardViewModel @Inject constructor(
_events.emit(CardEvent.AdditionalAction(action))
}

@Suppress("RestrictedApi")
private fun sendPaymentDetails(actionComponentData: ActionComponentData) {
viewModelScope.launch(Dispatchers.IO) {
viewModelScope.launch(DispatcherProvider.IO) {
val json = ActionComponentData.SERIALIZER.serialize(actionComponentData)
handlePaymentResponse(paymentsRepository.makeDetailsRequest(json))
}
Expand Down
Loading

0 comments on commit abda40d

Please sign in to comment.