From 43ea775356a18d71921812c8f253be3ac7a820f2 Mon Sep 17 00:00:00 2001 From: Oscar Spruit Date: Thu, 14 Nov 2024 15:38:00 +0100 Subject: [PATCH] Centralize IO dispatcher usage in example app --- .../extensions/DispatcherExtensions.kt | 15 ++++++++++++ .../example/repositories/RepositoryUtils.kt | 6 ++--- .../service/ExampleAdvancedDropInService.kt | 23 +++++++------------ .../example/service/ExampleDropInService.kt | 11 ++++----- .../service/ExampleSessionsDropInService.kt | 8 +++---- .../checkout/example/ui/bacs/BacsViewModel.kt | 11 ++++----- .../checkout/example/ui/blik/BlikViewModel.kt | 8 +++---- .../checkout/example/ui/card/CardViewModel.kt | 11 ++++----- .../ui/card/SessionsCardTakenOverViewModel.kt | 10 +++----- .../example/ui/giftcard/GiftCardViewModel.kt | 17 +++++--------- .../ui/googlepay/GooglePayViewModel.kt | 11 ++++----- .../compose/SessionsGooglePayViewModel.kt | 5 ++-- .../example/ui/instant/InstantViewModel.kt | 11 ++++----- 13 files changed, 63 insertions(+), 84 deletions(-) create mode 100644 example-app/src/main/java/com/adyen/checkout/example/extensions/DispatcherExtensions.kt diff --git a/example-app/src/main/java/com/adyen/checkout/example/extensions/DispatcherExtensions.kt b/example-app/src/main/java/com/adyen/checkout/example/extensions/DispatcherExtensions.kt new file mode 100644 index 0000000000..7ed4fbec46 --- /dev/null +++ b/example-app/src/main/java/com/adyen/checkout/example/extensions/DispatcherExtensions.kt @@ -0,0 +1,15 @@ +/* + * Copyright (c) 2024 Adyen N.V. + * + * This file is open source and available under the MIT license. See the LICENSE file for more info. + * + * Created by oscars on 14/11/2024. + */ + +package com.adyen.checkout.example.extensions + +import com.adyen.checkout.core.DispatcherProvider +import kotlinx.coroutines.CoroutineDispatcher + +@Suppress("RestrictedApi") +fun ioDispatcher(): CoroutineDispatcher = DispatcherProvider.IO diff --git a/example-app/src/main/java/com/adyen/checkout/example/repositories/RepositoryUtils.kt b/example-app/src/main/java/com/adyen/checkout/example/repositories/RepositoryUtils.kt index 0141a64236..4f5ad18df9 100644 --- a/example-app/src/main/java/com/adyen/checkout/example/repositories/RepositoryUtils.kt +++ b/example-app/src/main/java/com/adyen/checkout/example/repositories/RepositoryUtils.kt @@ -9,12 +9,12 @@ package com.adyen.checkout.example.repositories import android.util.Log -import com.adyen.checkout.core.DispatcherProvider +import com.adyen.checkout.example.extensions.ioDispatcher import kotlinx.coroutines.CancellationException import kotlinx.coroutines.withContext -@Suppress("TooGenericExceptionCaught", "RestrictedApi") -internal suspend fun safeApiCall(call: suspend () -> T): T? = withContext(DispatcherProvider.IO) { +@Suppress("TooGenericExceptionCaught") +internal suspend fun safeApiCall(call: suspend () -> T): T? = withContext(ioDispatcher()) { return@withContext try { call() } catch (e: CancellationException) { diff --git a/example-app/src/main/java/com/adyen/checkout/example/service/ExampleAdvancedDropInService.kt b/example-app/src/main/java/com/adyen/checkout/example/service/ExampleAdvancedDropInService.kt index 1c7e359f9f..c801ba5b55 100644 --- a/example-app/src/main/java/com/adyen/checkout/example/service/ExampleAdvancedDropInService.kt +++ b/example-app/src/main/java/com/adyen/checkout/example/service/ExampleAdvancedDropInService.kt @@ -22,7 +22,6 @@ 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 @@ -34,6 +33,7 @@ import com.adyen.checkout.dropin.OrderDropInServiceResult import com.adyen.checkout.dropin.RecurringDropInServiceResult import com.adyen.checkout.example.data.storage.KeyValueStorage import com.adyen.checkout.example.extensions.getLogTag +import com.adyen.checkout.example.extensions.ioDispatcher import com.adyen.checkout.example.extensions.toStringPretty import com.adyen.checkout.example.repositories.AddressLookupCompletionResult import com.adyen.checkout.example.repositories.AddressLookupRepository @@ -75,11 +75,10 @@ class ExampleAdvancedDropInService : DropInService() { }.launchIn(this) } - @Suppress("RestrictedApi") override fun onSubmit( state: PaymentComponentState<*>, ) { - launch(DispatcherProvider.IO) { + launch(ioDispatcher()) { Log.d(TAG, "onPaymentsCallRequested") checkPaymentState(state) @@ -125,9 +124,8 @@ class ExampleAdvancedDropInService : DropInService() { // read bundle and handle it } - @Suppress("RestrictedApi") override fun onAdditionalDetails(actionComponentData: ActionComponentData) { - launch(DispatcherProvider.IO) { + launch(ioDispatcher()) { Log.d(TAG, "onDetailsCallRequested") val actionComponentJson = ActionComponentData.SERIALIZER.serialize(actionComponentData) @@ -199,10 +197,9 @@ class ExampleAdvancedDropInService : DropInService() { return OrderResponse.SERIALIZER.deserialize(orderJSON) } - @Suppress("RestrictedApi") private fun fetchPaymentMethods(orderResponse: OrderResponse? = null) { Log.d(TAG, "fetchPaymentMethods") - launch(DispatcherProvider.IO) { + launch(ioDispatcher()) { val order = orderResponse?.let { Order( pspReference = it.pspReference, @@ -229,9 +226,8 @@ class ExampleAdvancedDropInService : DropInService() { } } - @Suppress("RestrictedApi") override fun onBalanceCheck(paymentComponentState: PaymentComponentState<*>) { - launch(DispatcherProvider.IO) { + launch(ioDispatcher()) { Log.d(TAG, "checkBalance") val amount = paymentComponentState.data.amount val paymentMethod = paymentComponentState.data.paymentMethod @@ -284,9 +280,8 @@ class ExampleAdvancedDropInService : DropInService() { } } - @Suppress("RestrictedApi") override fun onOrderRequest() { - launch(DispatcherProvider.IO) { + launch(ioDispatcher()) { Log.d(TAG, "createOrder") val paymentRequest = createOrderRequest( @@ -316,9 +311,8 @@ class ExampleAdvancedDropInService : DropInService() { } } - @Suppress("RestrictedApi") override fun onOrderCancel(order: Order, shouldUpdatePaymentMethods: Boolean) { - launch(DispatcherProvider.IO) { + launch(ioDispatcher()) { Log.d(TAG, "cancelOrder") val orderJson = Order.SERIALIZER.serialize(order) val request = createCancelOrderRequest( @@ -355,11 +349,10 @@ class ExampleAdvancedDropInService : DropInService() { } } - @Suppress("RestrictedApi") override fun onRemoveStoredPaymentMethod( storedPaymentMethod: StoredPaymentMethod, ) { - launch(DispatcherProvider.IO) { + launch(ioDispatcher()) { val storedPaymentMethodId = storedPaymentMethod.id.orEmpty() val isSuccessfullyRemoved = paymentsRepository.removeStoredPaymentMethod( storedPaymentMethodId = storedPaymentMethodId, diff --git a/example-app/src/main/java/com/adyen/checkout/example/service/ExampleDropInService.kt b/example-app/src/main/java/com/adyen/checkout/example/service/ExampleDropInService.kt index d5bcd49371..00e00c0954 100644 --- a/example-app/src/main/java/com/adyen/checkout/example/service/ExampleDropInService.kt +++ b/example-app/src/main/java/com/adyen/checkout/example/service/ExampleDropInService.kt @@ -15,13 +15,13 @@ 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 import com.adyen.checkout.dropin.RecurringDropInServiceResult import com.adyen.checkout.example.data.storage.KeyValueStorage import com.adyen.checkout.example.extensions.getLogTag +import com.adyen.checkout.example.extensions.ioDispatcher import com.adyen.checkout.example.extensions.toStringPretty import com.adyen.checkout.example.repositories.PaymentsRepository import com.adyen.checkout.redirect.RedirectComponent @@ -43,11 +43,10 @@ class ExampleDropInService : DropInService() { @Inject lateinit var keyValueStorage: KeyValueStorage - @Suppress("RestrictedApi") override fun onSubmit( state: PaymentComponentState<*> ) { - launch(DispatcherProvider.IO) { + launch(ioDispatcher()) { Log.d(TAG, "onPaymentsCallRequested") checkPaymentState(state) @@ -83,9 +82,8 @@ class ExampleDropInService : DropInService() { } } - @Suppress("RestrictedApi") override fun onAdditionalDetails(actionComponentData: ActionComponentData) { - launch(DispatcherProvider.IO) { + launch(ioDispatcher()) { Log.d(TAG, "onDetailsCallRequested") val actionComponentJson = ActionComponentData.SERIALIZER.serialize(actionComponentData) @@ -128,11 +126,10 @@ class ExampleDropInService : DropInService() { return jsonResponse.has("action") } - @Suppress("RestrictedApi") override fun onRemoveStoredPaymentMethod( storedPaymentMethod: StoredPaymentMethod, ) { - launch(DispatcherProvider.IO) { + launch(ioDispatcher()) { val storedPaymentMethodId = storedPaymentMethod.id.orEmpty() val isSuccessfullyRemoved = paymentsRepository.removeStoredPaymentMethod( storedPaymentMethodId = storedPaymentMethodId, diff --git a/example-app/src/main/java/com/adyen/checkout/example/service/ExampleSessionsDropInService.kt b/example-app/src/main/java/com/adyen/checkout/example/service/ExampleSessionsDropInService.kt index 268db5b20a..5ebc20d130 100644 --- a/example-app/src/main/java/com/adyen/checkout/example/service/ExampleSessionsDropInService.kt +++ b/example-app/src/main/java/com/adyen/checkout/example/service/ExampleSessionsDropInService.kt @@ -15,12 +15,12 @@ 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 import com.adyen.checkout.example.data.storage.KeyValueStorage import com.adyen.checkout.example.extensions.getLogTag +import com.adyen.checkout.example.extensions.ioDispatcher import com.adyen.checkout.example.extensions.toStringPretty import com.adyen.checkout.example.repositories.PaymentsRepository import com.adyen.checkout.redirect.RedirectComponent @@ -38,7 +38,6 @@ class ExampleSessionsDropInService : SessionDropInService() { @Inject lateinit var keyValueStorage: KeyValueStorage - @Suppress("RestrictedApi") override fun onSubmit( state: PaymentComponentState<*>, ): Boolean { @@ -46,7 +45,7 @@ class ExampleSessionsDropInService : SessionDropInService() { state is BlikComponentState || state is CardComponentState ) { - launch(DispatcherProvider.IO) { + launch(ioDispatcher()) { Log.d(TAG, "onPaymentsCallRequested") // Check out the documentation of this method on the parent DropInService class @@ -74,12 +73,11 @@ class ExampleSessionsDropInService : SessionDropInService() { } } - @Suppress("RestrictedApi") override fun onAdditionalDetails( actionComponentData: ActionComponentData, ): Boolean { return if (isFlowTakenOver) { - launch(DispatcherProvider.IO) { + launch(ioDispatcher()) { Log.d(TAG, "onDetailsCallRequested") val response = paymentsRepository.makeDetailsRequest( diff --git a/example-app/src/main/java/com/adyen/checkout/example/ui/bacs/BacsViewModel.kt b/example-app/src/main/java/com/adyen/checkout/example/ui/bacs/BacsViewModel.kt index 45ed525d8b..25d5c6f459 100644 --- a/example-app/src/main/java/com/adyen/checkout/example/ui/bacs/BacsViewModel.kt +++ b/example-app/src/main/java/com/adyen/checkout/example/ui/bacs/BacsViewModel.kt @@ -19,9 +19,9 @@ 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.extensions.ioDispatcher import com.adyen.checkout.example.repositories.PaymentsRepository import com.adyen.checkout.example.service.createPaymentRequest import com.adyen.checkout.example.service.getPaymentMethodRequest @@ -57,8 +57,7 @@ internal class BacsViewModel @Inject constructor( viewModelScope.launch { fetchPaymentMethods() } } - @Suppress("RestrictedApi") - private suspend fun fetchPaymentMethods() = withContext(DispatcherProvider.IO) { + private suspend fun fetchPaymentMethods() = withContext(ioDispatcher()) { 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) { @@ -116,9 +115,8 @@ internal class BacsViewModel @Inject constructor( viewModelScope.launch { _events.emit(BacsEvent.PaymentResult("Failed: ${error.errorMessage}")) } } - @Suppress("RestrictedApi") private fun sendPaymentDetails(actionComponentData: ActionComponentData) { - viewModelScope.launch(DispatcherProvider.IO) { + viewModelScope.launch(ioDispatcher()) { val json = ActionComponentData.SERIALIZER.serialize(actionComponentData) handlePaymentResponse(paymentsRepository.makeDetailsRequest(json)) } @@ -147,8 +145,7 @@ internal class BacsViewModel @Inject constructor( val paymentComponentData = PaymentComponentData.SERIALIZER.serialize(data) - @Suppress("RestrictedApi") - viewModelScope.launch(DispatcherProvider.IO) { + viewModelScope.launch(ioDispatcher()) { val paymentRequest = createPaymentRequest( paymentComponentData = paymentComponentData, shopperReference = keyValueStorage.getShopperReference(), diff --git a/example-app/src/main/java/com/adyen/checkout/example/ui/blik/BlikViewModel.kt b/example-app/src/main/java/com/adyen/checkout/example/ui/blik/BlikViewModel.kt index 7a18bea928..c6b08dc51e 100644 --- a/example-app/src/main/java/com/adyen/checkout/example/ui/blik/BlikViewModel.kt +++ b/example-app/src/main/java/com/adyen/checkout/example/ui/blik/BlikViewModel.kt @@ -20,9 +20,9 @@ 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.extensions.ioDispatcher import com.adyen.checkout.example.repositories.PaymentsRepository import com.adyen.checkout.example.service.createPaymentRequest import com.adyen.checkout.example.service.getPaymentMethodRequest @@ -55,8 +55,7 @@ class BlikViewModel @Inject constructor( viewModelScope.launch { _blikViewState.emit(fetchPaymentMethods()) } } - @Suppress("RestrictedApi") - private suspend fun fetchPaymentMethods(): BlikViewState = withContext(DispatcherProvider.IO) { + private suspend fun fetchPaymentMethods(): BlikViewState = withContext(ioDispatcher()) { 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) { @@ -139,9 +138,8 @@ class BlikViewModel @Inject constructor( } ?: _events.emit(BlikEvent.PaymentResult("Failed")) } - @Suppress("RestrictedApi") private fun sendPaymentDetails(actionComponentData: ActionComponentData) { - viewModelScope.launch(DispatcherProvider.IO) { + viewModelScope.launch(ioDispatcher()) { val json = ActionComponentData.SERIALIZER.serialize(actionComponentData) handlePaymentResponse(paymentsRepository.makeDetailsRequest(json)) } diff --git a/example-app/src/main/java/com/adyen/checkout/example/ui/card/CardViewModel.kt b/example-app/src/main/java/com/adyen/checkout/example/ui/card/CardViewModel.kt index 1670b5a611..787f92b038 100644 --- a/example-app/src/main/java/com/adyen/checkout/example/ui/card/CardViewModel.kt +++ b/example-app/src/main/java/com/adyen/checkout/example/ui/card/CardViewModel.kt @@ -11,8 +11,8 @@ 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.extensions.ioDispatcher import com.adyen.checkout.example.repositories.AddressLookupCompletionResult import com.adyen.checkout.example.repositories.AddressLookupRepository import com.adyen.checkout.example.repositories.PaymentsRepository @@ -56,8 +56,7 @@ internal class CardViewModel @Inject constructor( }.launchIn(viewModelScope) } - @Suppress("RestrictedApi") - private suspend fun fetchPaymentMethods() = withContext(DispatcherProvider.IO) { + private suspend fun fetchPaymentMethods() = withContext(ioDispatcher()) { val paymentMethodResponse = paymentsRepository.getPaymentMethods( getPaymentMethodRequest( merchantAccount = keyValueStorage.getMerchantAccount(), @@ -129,8 +128,7 @@ internal class CardViewModel @Inject constructor( val paymentComponentData = PaymentComponentData.SERIALIZER.serialize(data) - @Suppress("RestrictedApi") - viewModelScope.launch(DispatcherProvider.IO) { + viewModelScope.launch(ioDispatcher()) { val paymentRequest = createPaymentRequest( paymentComponentData = paymentComponentData, shopperReference = keyValueStorage.getShopperReference(), @@ -164,9 +162,8 @@ internal class CardViewModel @Inject constructor( _events.emit(CardEvent.AdditionalAction(action)) } - @Suppress("RestrictedApi") private fun sendPaymentDetails(actionComponentData: ActionComponentData) { - viewModelScope.launch(DispatcherProvider.IO) { + viewModelScope.launch(ioDispatcher()) { val json = ActionComponentData.SERIALIZER.serialize(actionComponentData) handlePaymentResponse(paymentsRepository.makeDetailsRequest(json)) } diff --git a/example-app/src/main/java/com/adyen/checkout/example/ui/card/SessionsCardTakenOverViewModel.kt b/example-app/src/main/java/com/adyen/checkout/example/ui/card/SessionsCardTakenOverViewModel.kt index 2d99c29244..65878e1a23 100644 --- a/example-app/src/main/java/com/adyen/checkout/example/ui/card/SessionsCardTakenOverViewModel.kt +++ b/example-app/src/main/java/com/adyen/checkout/example/ui/card/SessionsCardTakenOverViewModel.kt @@ -22,9 +22,9 @@ import com.adyen.checkout.components.core.LookupAddress import com.adyen.checkout.components.core.PaymentComponentData import com.adyen.checkout.components.core.PaymentMethodTypes 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.extensions.getLogTag +import com.adyen.checkout.example.extensions.ioDispatcher import com.adyen.checkout.example.repositories.AddressLookupCompletionResult import com.adyen.checkout.example.repositories.AddressLookupRepository import com.adyen.checkout.example.repositories.PaymentsRepository @@ -39,7 +39,6 @@ import com.adyen.checkout.sessions.core.SessionComponentCallback import com.adyen.checkout.sessions.core.SessionModel import com.adyen.checkout.sessions.core.SessionPaymentResult import dagger.hilt.android.lifecycle.HiltViewModel -import kotlinx.coroutines.FlowPreview import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableStateFlow @@ -50,7 +49,6 @@ import kotlinx.coroutines.launch import org.json.JSONObject import javax.inject.Inject -@OptIn(FlowPreview::class) @Suppress("TooManyFunctions") @HiltViewModel internal class SessionsCardTakenOverViewModel @Inject constructor( @@ -174,13 +172,12 @@ internal class SessionsCardTakenOverViewModel @Inject constructor( return isFlowTakenOver } - @Suppress("RestrictedApi") private fun makePayment(data: PaymentComponentData<*>) { _cardViewState.value = CardViewState.Loading val paymentComponentData = PaymentComponentData.SERIALIZER.serialize(data) - viewModelScope.launch(DispatcherProvider.IO) { + viewModelScope.launch(ioDispatcher()) { val paymentRequest = createPaymentRequest( paymentComponentData = paymentComponentData, shopperReference = keyValueStorage.getShopperReference(), @@ -214,9 +211,8 @@ internal class SessionsCardTakenOverViewModel @Inject constructor( _events.emit(CardEvent.AdditionalAction(action)) } - @Suppress("RestrictedApi") private fun sendPaymentDetails(actionComponentData: ActionComponentData) { - viewModelScope.launch(DispatcherProvider.IO) { + viewModelScope.launch(ioDispatcher()) { val json = ActionComponentData.SERIALIZER.serialize(actionComponentData) handlePaymentResponse(paymentsRepository.makeDetailsRequest(json)) } diff --git a/example-app/src/main/java/com/adyen/checkout/example/ui/giftcard/GiftCardViewModel.kt b/example-app/src/main/java/com/adyen/checkout/example/ui/giftcard/GiftCardViewModel.kt index b223ef1f1d..ef7a007522 100644 --- a/example-app/src/main/java/com/adyen/checkout/example/ui/giftcard/GiftCardViewModel.kt +++ b/example-app/src/main/java/com/adyen/checkout/example/ui/giftcard/GiftCardViewModel.kt @@ -23,10 +23,10 @@ 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.components.core.paymentmethod.PaymentMethodDetails -import com.adyen.checkout.core.DispatcherProvider import com.adyen.checkout.core.exception.ModelSerializationException import com.adyen.checkout.example.data.storage.KeyValueStorage import com.adyen.checkout.example.extensions.getLogTag +import com.adyen.checkout.example.extensions.ioDispatcher import com.adyen.checkout.example.repositories.PaymentsRepository import com.adyen.checkout.example.service.createBalanceRequest import com.adyen.checkout.example.service.createOrderRequest @@ -68,8 +68,7 @@ internal class GiftCardViewModel @Inject constructor( viewModelScope.launch { fetchPaymentMethods() } } - @Suppress("RestrictedApi") - private suspend fun fetchPaymentMethods() = withContext(DispatcherProvider.IO) { + private suspend fun fetchPaymentMethods() = withContext(ioDispatcher()) { val paymentMethodResponse = paymentsRepository.getPaymentMethods( getPaymentMethodRequest( merchantAccount = keyValueStorage.getMerchantAccount(), @@ -113,9 +112,8 @@ internal class GiftCardViewModel @Inject constructor( // no ops override fun onStateChanged(state: GiftCardComponentState) = Unit - @Suppress("RestrictedApi") override fun onBalanceCheck(paymentComponentState: PaymentComponentState<*>) { - viewModelScope.launch(DispatcherProvider.IO) { + viewModelScope.launch(ioDispatcher()) { Log.d(TAG, "checkBalance") val amount = paymentComponentState.data.amount @@ -177,9 +175,8 @@ internal class GiftCardViewModel @Inject constructor( } } - @Suppress("RestrictedApi") override fun onRequestOrder() { - viewModelScope.launch(DispatcherProvider.IO) { + viewModelScope.launch(ioDispatcher()) { Log.d(TAG, "createOrder") val paymentRequest = createOrderRequest( @@ -213,13 +210,12 @@ internal class GiftCardViewModel @Inject constructor( } } - @Suppress("RestrictedApi") private fun makePayment(data: PaymentComponentData<*>) { _giftCardViewStateFlow.value = GiftCardViewState.Loading val paymentComponentData = PaymentComponentData.SERIALIZER.serialize(data) - viewModelScope.launch(DispatcherProvider.IO) { + viewModelScope.launch(ioDispatcher()) { val paymentRequest = createPaymentRequest( paymentComponentData = paymentComponentData, shopperReference = keyValueStorage.getShopperReference(), @@ -284,9 +280,8 @@ internal class GiftCardViewModel @Inject constructor( viewModelScope.launch { _events.emit(GiftCardEvent.AdditionalAction(action)) } } - @Suppress("RestrictedApi") private fun sendPaymentDetails(actionComponentData: ActionComponentData) { - viewModelScope.launch(DispatcherProvider.IO) { + viewModelScope.launch(ioDispatcher()) { val json = ActionComponentData.SERIALIZER.serialize(actionComponentData) handlePaymentResponse(paymentsRepository.makeDetailsRequest(json)) } diff --git a/example-app/src/main/java/com/adyen/checkout/example/ui/googlepay/GooglePayViewModel.kt b/example-app/src/main/java/com/adyen/checkout/example/ui/googlepay/GooglePayViewModel.kt index 93d280b836..813335f627 100644 --- a/example-app/src/main/java/com/adyen/checkout/example/ui/googlepay/GooglePayViewModel.kt +++ b/example-app/src/main/java/com/adyen/checkout/example/ui/googlepay/GooglePayViewModel.kt @@ -20,9 +20,9 @@ import com.adyen.checkout.components.core.ComponentError import com.adyen.checkout.components.core.PaymentComponentData import com.adyen.checkout.components.core.PaymentMethod 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.extensions.ioDispatcher import com.adyen.checkout.example.repositories.PaymentsRepository import com.adyen.checkout.example.service.createPaymentRequest import com.adyen.checkout.example.service.getPaymentMethodRequest @@ -67,8 +67,7 @@ internal class GooglePayViewModel @Inject constructor( viewModelScope.launch { fetchPaymentMethods() } } - @Suppress("RestrictedApi") - private suspend fun fetchPaymentMethods() = withContext(DispatcherProvider.IO) { + private suspend fun fetchPaymentMethods() = withContext(ioDispatcher()) { val paymentMethodResponse = paymentsRepository.getPaymentMethods( getPaymentMethodRequest( merchantAccount = keyValueStorage.getMerchantAccount(), @@ -138,9 +137,8 @@ internal class GooglePayViewModel @Inject constructor( viewModelScope.launch { _events.emit(GooglePayEvent.PaymentResult("Failed: ${error.errorMessage}")) } } - @Suppress("RestrictedApi") private fun sendPaymentDetails(actionComponentData: ActionComponentData) { - viewModelScope.launch(DispatcherProvider.IO) { + viewModelScope.launch(ioDispatcher()) { val json = ActionComponentData.SERIALIZER.serialize(actionComponentData) handlePaymentResponse(paymentsRepository.makeDetailsRequest(json)) } @@ -169,8 +167,7 @@ internal class GooglePayViewModel @Inject constructor( val paymentComponentData = PaymentComponentData.SERIALIZER.serialize(data) - @Suppress("RestrictedApi") - viewModelScope.launch(DispatcherProvider.IO) { + viewModelScope.launch(ioDispatcher()) { val paymentRequest = createPaymentRequest( paymentComponentData = paymentComponentData, shopperReference = keyValueStorage.getShopperReference(), diff --git a/example-app/src/main/java/com/adyen/checkout/example/ui/googlepay/compose/SessionsGooglePayViewModel.kt b/example-app/src/main/java/com/adyen/checkout/example/ui/googlepay/compose/SessionsGooglePayViewModel.kt index ea6a4c54ed..57bd22722b 100644 --- a/example-app/src/main/java/com/adyen/checkout/example/ui/googlepay/compose/SessionsGooglePayViewModel.kt +++ b/example-app/src/main/java/com/adyen/checkout/example/ui/googlepay/compose/SessionsGooglePayViewModel.kt @@ -20,9 +20,9 @@ import com.adyen.checkout.components.core.ComponentError import com.adyen.checkout.components.core.PaymentMethod import com.adyen.checkout.components.core.PaymentMethodTypes 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.extensions.getLogTag +import com.adyen.checkout.example.extensions.ioDispatcher import com.adyen.checkout.example.repositories.PaymentsRepository import com.adyen.checkout.example.service.getSessionRequest import com.adyen.checkout.example.service.getSettingsInstallmentOptionsMode @@ -71,8 +71,7 @@ internal class SessionsGooglePayViewModel @Inject constructor( viewModelScope.launch { fetchSession() } } - @Suppress("RestrictedApi") - private suspend fun fetchSession() = withContext(DispatcherProvider.IO) { + private suspend fun fetchSession() = withContext(ioDispatcher()) { val paymentMethodType = PaymentMethodTypes.GOOGLE_PAY val checkoutSession = getSession(paymentMethodType) if (checkoutSession == null) { diff --git a/example-app/src/main/java/com/adyen/checkout/example/ui/instant/InstantViewModel.kt b/example-app/src/main/java/com/adyen/checkout/example/ui/instant/InstantViewModel.kt index 26fd56a436..3bda1291af 100644 --- a/example-app/src/main/java/com/adyen/checkout/example/ui/instant/InstantViewModel.kt +++ b/example-app/src/main/java/com/adyen/checkout/example/ui/instant/InstantViewModel.kt @@ -16,9 +16,9 @@ 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.core.PermissionHandlerCallback import com.adyen.checkout.example.data.storage.KeyValueStorage +import com.adyen.checkout.example.extensions.ioDispatcher import com.adyen.checkout.example.repositories.PaymentsRepository import com.adyen.checkout.example.service.createPaymentRequest import com.adyen.checkout.example.service.getPaymentMethodRequest @@ -55,8 +55,7 @@ internal class InstantViewModel @Inject constructor( viewModelScope.launch { fetchPaymentMethods() } } - @Suppress("RestrictedApi") - private suspend fun fetchPaymentMethods() = withContext(DispatcherProvider.IO) { + private suspend fun fetchPaymentMethods() = withContext(ioDispatcher()) { val paymentMethodResponse = paymentsRepository.getPaymentMethods( getPaymentMethodRequest( merchantAccount = keyValueStorage.getMerchantAccount(), @@ -119,12 +118,11 @@ internal class InstantViewModel @Inject constructor( permissionCallback = null } - @Suppress("RestrictedApi") private fun makePayment(data: PaymentComponentData<*>) { _instantViewState.tryEmit(InstantViewState.Loading) val paymentComponentData = PaymentComponentData.SERIALIZER.serialize(data) - viewModelScope.launch(DispatcherProvider.IO) { + viewModelScope.launch(ioDispatcher()) { val paymentRequest = createPaymentRequest( paymentComponentData = paymentComponentData, shopperReference = keyValueStorage.getShopperReference(), @@ -155,9 +153,8 @@ internal class InstantViewModel @Inject constructor( } ?: _events.emit(InstantEvent.PaymentResult("Failed")) } - @Suppress("RestrictedApi") private fun sendPaymentDetails(actionComponentData: ActionComponentData) { - viewModelScope.launch(DispatcherProvider.IO) { + viewModelScope.launch(ioDispatcher()) { val json = ActionComponentData.SERIALIZER.serialize(actionComponentData) handlePaymentResponse(paymentsRepository.makeDetailsRequest(json)) }