From 9d6c44ed56f2c30e6bcbfc72fa49ec963a7a989f Mon Sep 17 00:00:00 2001 From: ozgur <6615094+ozgur00@users.noreply.github.com> Date: Fri, 18 Oct 2024 10:59:21 +0200 Subject: [PATCH] Introduce DispatcherProvider --- .../data/api/SubmitFingerprintService.kt | 4 +- .../provider/Adyen3DS2ComponentProvider.kt | 4 +- .../internal/data/api/BinLookupService.kt | 4 +- .../internal/ui/DefaultCashAppPayDelegate.kt | 4 +- .../adyen/checkout/core/DispatcherProvider.kt | 59 +++++++++++++++++++ .../checkout/core/internal/ui/ImageLoader.kt | 12 ++-- .../analytics/DefaultAnalyticsManager.kt | 4 +- .../internal/data/api/AnalyticsService.kt | 4 +- .../internal/data/api/OrderStatusService.kt | 4 +- .../internal/data/api/PublicKeyService.kt | 4 +- .../internal/data/api/StatusRepository.kt | 4 +- .../internal/service/BaseDropInService.kt | 4 +- .../dropin/internal/ui/DropInViewModel.kt | 4 +- .../test/rule/IdlingDispatcherRule.kt | 13 ++-- .../data/api/NativeRedirectService.kt | 4 +- .../internal/CheckoutSessionInitializer.kt | 4 +- .../core/internal/data/api/SessionService.kt | 4 +- .../core/internal/data/api/AddressService.kt | 4 +- .../data/api/DefaultAddressRepository.kt | 4 +- .../ui/core/internal/util/ImageSaver.kt | 4 +- 20 files changed, 103 insertions(+), 49 deletions(-) create mode 100644 checkout-core/src/main/java/com/adyen/checkout/core/DispatcherProvider.kt diff --git a/3ds2/src/main/java/com/adyen/checkout/adyen3ds2/internal/data/api/SubmitFingerprintService.kt b/3ds2/src/main/java/com/adyen/checkout/adyen3ds2/internal/data/api/SubmitFingerprintService.kt index 104047ee34..9cfd065e39 100644 --- a/3ds2/src/main/java/com/adyen/checkout/adyen3ds2/internal/data/api/SubmitFingerprintService.kt +++ b/3ds2/src/main/java/com/adyen/checkout/adyen3ds2/internal/data/api/SubmitFingerprintService.kt @@ -10,15 +10,15 @@ package com.adyen.checkout.adyen3ds2.internal.data.api import com.adyen.checkout.adyen3ds2.internal.data.model.SubmitFingerprintRequest import com.adyen.checkout.adyen3ds2.internal.data.model.SubmitFingerprintResponse +import com.adyen.checkout.core.DispatcherProvider import com.adyen.checkout.core.internal.data.api.HttpClient import com.adyen.checkout.core.internal.data.api.post import kotlinx.coroutines.CoroutineDispatcher -import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext internal class SubmitFingerprintService( private val httpClient: HttpClient, - private val coroutineDispatcher: CoroutineDispatcher = Dispatchers.IO, + private val coroutineDispatcher: CoroutineDispatcher = DispatcherProvider.IO, ) { suspend fun submitFingerprint( diff --git a/3ds2/src/main/java/com/adyen/checkout/adyen3ds2/internal/provider/Adyen3DS2ComponentProvider.kt b/3ds2/src/main/java/com/adyen/checkout/adyen3ds2/internal/provider/Adyen3DS2ComponentProvider.kt index b8aed566e4..317f4aaf88 100644 --- a/3ds2/src/main/java/com/adyen/checkout/adyen3ds2/internal/provider/Adyen3DS2ComponentProvider.kt +++ b/3ds2/src/main/java/com/adyen/checkout/adyen3ds2/internal/provider/Adyen3DS2ComponentProvider.kt @@ -39,11 +39,11 @@ import com.adyen.checkout.components.core.internal.ui.model.CommonComponentParam import com.adyen.checkout.components.core.internal.ui.model.DropInOverrideParams import com.adyen.checkout.components.core.internal.util.get import com.adyen.checkout.components.core.internal.util.viewModelFactory +import com.adyen.checkout.core.DispatcherProvider import com.adyen.checkout.core.internal.data.api.HttpClientFactory import com.adyen.checkout.core.internal.util.LocaleProvider import com.adyen.checkout.ui.core.internal.DefaultRedirectHandler import com.adyen.threeds2.ThreeDS2Service -import kotlinx.coroutines.Dispatchers class Adyen3DS2ComponentProvider @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) @@ -105,7 +105,7 @@ constructor( adyen3DS2Serializer = adyen3DS2DetailsParser, redirectHandler = redirectHandler, threeDS2Service = ThreeDS2Service.INSTANCE, - coroutineDispatcher = Dispatchers.Default, + coroutineDispatcher = DispatcherProvider.Default, application = application, analyticsManager = analyticsManager, ) diff --git a/card/src/main/java/com/adyen/checkout/card/internal/data/api/BinLookupService.kt b/card/src/main/java/com/adyen/checkout/card/internal/data/api/BinLookupService.kt index dbae165ccb..c6df098527 100644 --- a/card/src/main/java/com/adyen/checkout/card/internal/data/api/BinLookupService.kt +++ b/card/src/main/java/com/adyen/checkout/card/internal/data/api/BinLookupService.kt @@ -11,16 +11,16 @@ package com.adyen.checkout.card.internal.data.api import androidx.annotation.RestrictTo import com.adyen.checkout.card.internal.data.model.BinLookupRequest import com.adyen.checkout.card.internal.data.model.BinLookupResponse +import com.adyen.checkout.core.DispatcherProvider import com.adyen.checkout.core.internal.data.api.HttpClient import com.adyen.checkout.core.internal.data.api.post import kotlinx.coroutines.CoroutineDispatcher -import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) class BinLookupService( private val httpClient: HttpClient, - private val coroutineDispatcher: CoroutineDispatcher = Dispatchers.IO, + private val coroutineDispatcher: CoroutineDispatcher = DispatcherProvider.IO, ) { suspend fun makeBinLookup( diff --git a/cashapppay/src/main/java/com/adyen/checkout/cashapppay/internal/ui/DefaultCashAppPayDelegate.kt b/cashapppay/src/main/java/com/adyen/checkout/cashapppay/internal/ui/DefaultCashAppPayDelegate.kt index e5b178176b..639a297589 100644 --- a/cashapppay/src/main/java/com/adyen/checkout/cashapppay/internal/ui/DefaultCashAppPayDelegate.kt +++ b/cashapppay/src/main/java/com/adyen/checkout/cashapppay/internal/ui/DefaultCashAppPayDelegate.kt @@ -38,6 +38,7 @@ import com.adyen.checkout.components.core.internal.analytics.GenericEvents import com.adyen.checkout.components.core.internal.util.bufferedChannel import com.adyen.checkout.components.core.paymentmethod.CashAppPayPaymentMethod import com.adyen.checkout.core.AdyenLogLevel +import com.adyen.checkout.core.DispatcherProvider import com.adyen.checkout.core.exception.CheckoutException import com.adyen.checkout.core.exception.ComponentException import com.adyen.checkout.core.internal.util.adyenLog @@ -47,7 +48,6 @@ import com.adyen.checkout.ui.core.internal.ui.ComponentViewType import com.adyen.checkout.ui.core.internal.ui.SubmitHandler import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableStateFlow @@ -65,7 +65,7 @@ constructor( private val order: OrderRequest?, override val componentParams: CashAppPayComponentParams, private val cashAppPayFactory: CashAppPayFactory, - private val coroutineDispatcher: CoroutineDispatcher = Dispatchers.IO, + private val coroutineDispatcher: CoroutineDispatcher = DispatcherProvider.IO, ) : CashAppPayDelegate, ButtonDelegate, CashAppPayListener { private val inputData = CashAppPayInputData() diff --git a/checkout-core/src/main/java/com/adyen/checkout/core/DispatcherProvider.kt b/checkout-core/src/main/java/com/adyen/checkout/core/DispatcherProvider.kt new file mode 100644 index 0000000000..cfb77fb3be --- /dev/null +++ b/checkout-core/src/main/java/com/adyen/checkout/core/DispatcherProvider.kt @@ -0,0 +1,59 @@ +/* + * 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 ozgur on 16/10/2024. + */ + +package com.adyen.checkout.core + +import androidx.annotation.RestrictTo +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.Dispatchers + +@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) +object DispatcherProvider { + + @Suppress("ktlint:standard:property-naming") + var Main: CoroutineDispatcher = Dispatchers.Main + private set + + @Suppress("ktlint:standard:property-naming") + var IO: CoroutineDispatcher = Dispatchers.IO + private set + + @Suppress("ktlint:standard:property-naming") + var Default: CoroutineDispatcher = Dispatchers.Default + private set + + fun setMain(dispatcher: CoroutineDispatcher) { + this.Main = dispatcher + } + + fun setIO(dispatcher: CoroutineDispatcher) { + this.IO = dispatcher + } + + fun setDefault(dispatcher: CoroutineDispatcher) { + this.Default = dispatcher + } + + fun resetMain() { + Main = Dispatchers.Main + } + + fun resetIO() { + IO = Dispatchers.IO + } + + fun resetDefault() { + Default = Dispatchers.Default + } + + fun resetAll() { + resetMain() + resetIO() + resetDefault() + } +} diff --git a/checkout-core/src/main/java/com/adyen/checkout/core/internal/ui/ImageLoader.kt b/checkout-core/src/main/java/com/adyen/checkout/core/internal/ui/ImageLoader.kt index af3994244d..23a8600ba8 100644 --- a/checkout-core/src/main/java/com/adyen/checkout/core/internal/ui/ImageLoader.kt +++ b/checkout-core/src/main/java/com/adyen/checkout/core/internal/ui/ImageLoader.kt @@ -14,9 +14,9 @@ import android.content.pm.ApplicationInfo import android.graphics.Bitmap import android.graphics.BitmapFactory import androidx.annotation.RestrictTo +import com.adyen.checkout.core.DispatcherProvider import com.adyen.checkout.core.exception.HttpException import kotlinx.coroutines.CancellationException -import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import okhttp3.OkHttpClient import okhttp3.Request @@ -43,10 +43,10 @@ class DefaultImageLoader(context: Context) : ImageLoader { url: String, onSuccess: suspend (Bitmap) -> Unit, onError: suspend (Throwable) -> Unit - ) = withContext(Dispatchers.IO) { + ) = withContext(DispatcherProvider.IO) { val cachedBitmap = cache[url] if (cachedBitmap != null) { - withContext(Dispatchers.Main) { + withContext(DispatcherProvider.Main) { onSuccess(cachedBitmap) } return@withContext @@ -72,11 +72,11 @@ class DefaultImageLoader(context: Context) : ImageLoader { cache[url] = bitmap - withContext(Dispatchers.Main) { + withContext(DispatcherProvider.Main) { onSuccess(bitmap) } } else { - withContext(Dispatchers.Main) { + withContext(DispatcherProvider.Main) { onError(HttpException(response.code, response.message, null)) } } @@ -85,7 +85,7 @@ class DefaultImageLoader(context: Context) : ImageLoader { } catch (e: CancellationException) { call.cancel() } catch (e: IOException) { - withContext(Dispatchers.Main) { + withContext(DispatcherProvider.Main) { onError(e) } } diff --git a/components-core/src/main/java/com/adyen/checkout/components/core/internal/analytics/DefaultAnalyticsManager.kt b/components-core/src/main/java/com/adyen/checkout/components/core/internal/analytics/DefaultAnalyticsManager.kt index 29a3a89423..dcc1df5324 100644 --- a/components-core/src/main/java/com/adyen/checkout/components/core/internal/analytics/DefaultAnalyticsManager.kt +++ b/components-core/src/main/java/com/adyen/checkout/components/core/internal/analytics/DefaultAnalyticsManager.kt @@ -13,11 +13,11 @@ import com.adyen.checkout.components.core.internal.analytics.data.AnalyticsRepos import com.adyen.checkout.components.core.internal.ui.model.AnalyticsParams import com.adyen.checkout.components.core.internal.ui.model.AnalyticsParamsLevel import com.adyen.checkout.core.AdyenLogLevel +import com.adyen.checkout.core.DispatcherProvider import com.adyen.checkout.core.internal.util.adyenLog import com.adyen.checkout.core.internal.util.runSuspendCatching import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job import kotlinx.coroutines.delay import kotlinx.coroutines.isActive @@ -27,7 +27,7 @@ import kotlin.time.Duration.Companion.seconds internal class DefaultAnalyticsManager( private val analyticsRepository: AnalyticsRepository, private val analyticsParams: AnalyticsParams, - private val coroutineDispatcher: CoroutineDispatcher = Dispatchers.IO, + private val coroutineDispatcher: CoroutineDispatcher = DispatcherProvider.IO, ) : AnalyticsManager { private var checkoutAttemptIdState: CheckoutAttemptIdState = CheckoutAttemptIdState.NotAvailable diff --git a/components-core/src/main/java/com/adyen/checkout/components/core/internal/data/api/AnalyticsService.kt b/components-core/src/main/java/com/adyen/checkout/components/core/internal/data/api/AnalyticsService.kt index b42d84fa39..6843ce0d8e 100644 --- a/components-core/src/main/java/com/adyen/checkout/components/core/internal/data/api/AnalyticsService.kt +++ b/components-core/src/main/java/com/adyen/checkout/components/core/internal/data/api/AnalyticsService.kt @@ -12,17 +12,17 @@ import androidx.annotation.RestrictTo import com.adyen.checkout.components.core.internal.data.model.AnalyticsSetupRequest import com.adyen.checkout.components.core.internal.data.model.AnalyticsSetupResponse import com.adyen.checkout.components.core.internal.data.model.AnalyticsTrackRequest +import com.adyen.checkout.core.DispatcherProvider import com.adyen.checkout.core.internal.data.api.HttpClient import com.adyen.checkout.core.internal.data.api.post import com.adyen.checkout.core.internal.data.model.EmptyResponse import kotlinx.coroutines.CoroutineDispatcher -import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) class AnalyticsService( private val httpClient: HttpClient, - private val coroutineDispatcher: CoroutineDispatcher = Dispatchers.IO, + private val coroutineDispatcher: CoroutineDispatcher = DispatcherProvider.IO, ) { internal suspend fun setupAnalytics( diff --git a/components-core/src/main/java/com/adyen/checkout/components/core/internal/data/api/OrderStatusService.kt b/components-core/src/main/java/com/adyen/checkout/components/core/internal/data/api/OrderStatusService.kt index 5fe4a1f9de..119a39c3c2 100644 --- a/components-core/src/main/java/com/adyen/checkout/components/core/internal/data/api/OrderStatusService.kt +++ b/components-core/src/main/java/com/adyen/checkout/components/core/internal/data/api/OrderStatusService.kt @@ -11,16 +11,16 @@ package com.adyen.checkout.components.core.internal.data.api import androidx.annotation.RestrictTo import com.adyen.checkout.components.core.internal.data.model.OrderStatusRequest import com.adyen.checkout.components.core.internal.data.model.OrderStatusResponse +import com.adyen.checkout.core.DispatcherProvider import com.adyen.checkout.core.internal.data.api.HttpClient import com.adyen.checkout.core.internal.data.api.post import kotlinx.coroutines.CoroutineDispatcher -import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) class OrderStatusService( private val httpClient: HttpClient, - private val coroutineDispatcher: CoroutineDispatcher = Dispatchers.IO, + private val coroutineDispatcher: CoroutineDispatcher = DispatcherProvider.IO, ) { internal suspend fun getOrderStatus( diff --git a/components-core/src/main/java/com/adyen/checkout/components/core/internal/data/api/PublicKeyService.kt b/components-core/src/main/java/com/adyen/checkout/components/core/internal/data/api/PublicKeyService.kt index 15e4c1365e..6289f49408 100644 --- a/components-core/src/main/java/com/adyen/checkout/components/core/internal/data/api/PublicKeyService.kt +++ b/components-core/src/main/java/com/adyen/checkout/components/core/internal/data/api/PublicKeyService.kt @@ -10,16 +10,16 @@ package com.adyen.checkout.components.core.internal.data.api import androidx.annotation.RestrictTo import com.adyen.checkout.components.core.internal.data.model.PublicKeyResponse +import com.adyen.checkout.core.DispatcherProvider import com.adyen.checkout.core.internal.data.api.HttpClient import com.adyen.checkout.core.internal.data.api.get import kotlinx.coroutines.CoroutineDispatcher -import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) class PublicKeyService( private val httpClient: HttpClient, - private val coroutineDispatcher: CoroutineDispatcher = Dispatchers.IO, + private val coroutineDispatcher: CoroutineDispatcher = DispatcherProvider.IO, ) { internal suspend fun getPublicKey( diff --git a/components-core/src/main/java/com/adyen/checkout/components/core/internal/data/api/StatusRepository.kt b/components-core/src/main/java/com/adyen/checkout/components/core/internal/data/api/StatusRepository.kt index 4850b00e36..5058f08ee9 100644 --- a/components-core/src/main/java/com/adyen/checkout/components/core/internal/data/api/StatusRepository.kt +++ b/components-core/src/main/java/com/adyen/checkout/components/core/internal/data/api/StatusRepository.kt @@ -15,10 +15,10 @@ import com.adyen.checkout.components.core.internal.data.model.StatusResponse import com.adyen.checkout.components.core.internal.util.StatusResponseUtils import com.adyen.checkout.components.core.internal.util.bufferedChannel import com.adyen.checkout.core.AdyenLogLevel +import com.adyen.checkout.core.DispatcherProvider import com.adyen.checkout.core.internal.util.adyenLog import com.adyen.checkout.core.internal.util.runSuspendCatching import kotlinx.coroutines.CoroutineDispatcher -import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.FlowPreview import kotlinx.coroutines.cancel import kotlinx.coroutines.currentCoroutineContext @@ -51,7 +51,7 @@ class DefaultStatusRepository( private val statusService: StatusService, private val clientKey: String, private val timeSource: TimeSource = TimeSource.Monotonic, - private val coroutineDispatcher: CoroutineDispatcher = Dispatchers.IO, + private val coroutineDispatcher: CoroutineDispatcher = DispatcherProvider.IO, ) : StatusRepository { private var delay: Long = 0 diff --git a/drop-in/src/main/java/com/adyen/checkout/dropin/internal/service/BaseDropInService.kt b/drop-in/src/main/java/com/adyen/checkout/dropin/internal/service/BaseDropInService.kt index 27fc2b8f69..acc796fe53 100644 --- a/drop-in/src/main/java/com/adyen/checkout/dropin/internal/service/BaseDropInService.kt +++ b/drop-in/src/main/java/com/adyen/checkout/dropin/internal/service/BaseDropInService.kt @@ -22,6 +22,7 @@ import com.adyen.checkout.components.core.LookupAddress import com.adyen.checkout.components.core.StoredPaymentMethod import com.adyen.checkout.components.core.internal.util.bufferedChannel import com.adyen.checkout.core.AdyenLogLevel +import com.adyen.checkout.core.DispatcherProvider import com.adyen.checkout.core.internal.util.adyenLog import com.adyen.checkout.dropin.AddressLookupDropInServiceResult import com.adyen.checkout.dropin.BalanceDropInServiceResult @@ -31,7 +32,6 @@ import com.adyen.checkout.dropin.DropInServiceResult import com.adyen.checkout.dropin.OrderDropInServiceResult import com.adyen.checkout.dropin.RecurringDropInServiceResult import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job import kotlinx.coroutines.cancel import kotlinx.coroutines.channels.Channel @@ -46,7 +46,7 @@ abstract class BaseDropInService constructor() : Service(), CoroutineScope, BaseDropInServiceInterface, BaseDropInServiceContract { private val coroutineJob: Job = Job() - final override val coroutineContext: CoroutineContext get() = Dispatchers.Main + coroutineJob + final override val coroutineContext: CoroutineContext get() = DispatcherProvider.Main + coroutineJob @Suppress("LeakingThis") private val binder = DropInBinder(this) diff --git a/drop-in/src/main/java/com/adyen/checkout/dropin/internal/ui/DropInViewModel.kt b/drop-in/src/main/java/com/adyen/checkout/dropin/internal/ui/DropInViewModel.kt index a6e513d1f2..213eb8fea7 100644 --- a/drop-in/src/main/java/com/adyen/checkout/dropin/internal/ui/DropInViewModel.kt +++ b/drop-in/src/main/java/com/adyen/checkout/dropin/internal/ui/DropInViewModel.kt @@ -31,6 +31,7 @@ import com.adyen.checkout.components.core.internal.ui.model.DropInOverrideParams import com.adyen.checkout.components.core.internal.util.bufferedChannel import com.adyen.checkout.components.core.paymentmethod.GiftCardPaymentMethod import com.adyen.checkout.core.AdyenLogLevel +import com.adyen.checkout.core.DispatcherProvider import com.adyen.checkout.core.exception.CheckoutException import com.adyen.checkout.core.internal.util.adyenLog import com.adyen.checkout.dropin.R @@ -47,7 +48,6 @@ import com.adyen.checkout.giftcard.internal.util.GiftCardBalanceUtils import com.adyen.checkout.sessions.core.internal.data.model.SessionDetails import com.adyen.checkout.sessions.core.internal.data.model.mapToModel import kotlinx.coroutines.CoroutineDispatcher -import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.receiveAsFlow @@ -60,7 +60,7 @@ internal class DropInViewModel( internal val analyticsManager: AnalyticsManager, private val initialDropInParams: DropInParams, private val dropInConfigDataGenerator: DropInConfigDataGenerator, - private val coroutineDispatcher: CoroutineDispatcher = Dispatchers.IO, + private val coroutineDispatcher: CoroutineDispatcher = DispatcherProvider.IO, ) : ViewModel() { private val eventChannel: Channel = bufferedChannel() diff --git a/example-app/src/androidTest/java/com/adyen/checkout/test/rule/IdlingDispatcherRule.kt b/example-app/src/androidTest/java/com/adyen/checkout/test/rule/IdlingDispatcherRule.kt index ddfa72bcd6..eabf3c78bb 100644 --- a/example-app/src/androidTest/java/com/adyen/checkout/test/rule/IdlingDispatcherRule.kt +++ b/example-app/src/androidTest/java/com/adyen/checkout/test/rule/IdlingDispatcherRule.kt @@ -10,6 +10,7 @@ package com.adyen.checkout.test.rule import androidx.test.espresso.IdlingRegistry import androidx.test.espresso.idling.CountingIdlingResource +import com.adyen.checkout.core.DispatcherProvider import com.adyen.checkout.test.util.IdlingResourceDispatcher import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.Dispatchers @@ -45,6 +46,7 @@ class IdlingDispatcherRule : TestRule { unregister(defaultIdlingResource) unregister(ioIdlingResource) } + DispatcherProvider.resetAll() overrideDispatchers(defaultDispatcher, ioDispatcher) } } @@ -54,14 +56,7 @@ class IdlingDispatcherRule : TestRule { default: CoroutineDispatcher, io: CoroutineDispatcher, ) { - fun setField(name: String, value: CoroutineDispatcher) { - Dispatchers::class.java.getDeclaredField(name).apply { - isAccessible = true - set(Dispatchers, value) - } - } - - setField("Default", default) - setField("IO", io) + DispatcherProvider.setDefault(default) + DispatcherProvider.setIO(io) } } diff --git a/redirect/src/main/java/com/adyen/checkout/redirect/internal/data/api/NativeRedirectService.kt b/redirect/src/main/java/com/adyen/checkout/redirect/internal/data/api/NativeRedirectService.kt index afadbb867d..088cc831cd 100644 --- a/redirect/src/main/java/com/adyen/checkout/redirect/internal/data/api/NativeRedirectService.kt +++ b/redirect/src/main/java/com/adyen/checkout/redirect/internal/data/api/NativeRedirectService.kt @@ -8,17 +8,17 @@ package com.adyen.checkout.redirect.internal.data.api +import com.adyen.checkout.core.DispatcherProvider import com.adyen.checkout.core.internal.data.api.HttpClient import com.adyen.checkout.core.internal.data.api.post import com.adyen.checkout.redirect.internal.data.model.NativeRedirectRequest import com.adyen.checkout.redirect.internal.data.model.NativeRedirectResponse import kotlinx.coroutines.CoroutineDispatcher -import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext internal class NativeRedirectService( private val httpClient: HttpClient, - private val dispatcher: CoroutineDispatcher = Dispatchers.IO + private val dispatcher: CoroutineDispatcher = DispatcherProvider.IO ) { suspend fun makeNativeRedirect( diff --git a/sessions-core/src/main/java/com/adyen/checkout/sessions/core/internal/CheckoutSessionInitializer.kt b/sessions-core/src/main/java/com/adyen/checkout/sessions/core/internal/CheckoutSessionInitializer.kt index 7d7ee9fb8a..ff395aaf51 100644 --- a/sessions-core/src/main/java/com/adyen/checkout/sessions/core/internal/CheckoutSessionInitializer.kt +++ b/sessions-core/src/main/java/com/adyen/checkout/sessions/core/internal/CheckoutSessionInitializer.kt @@ -10,6 +10,7 @@ package com.adyen.checkout.sessions.core.internal import com.adyen.checkout.components.core.Amount import com.adyen.checkout.components.core.Order +import com.adyen.checkout.core.DispatcherProvider import com.adyen.checkout.core.Environment import com.adyen.checkout.core.exception.CheckoutException import com.adyen.checkout.core.internal.data.api.HttpClientFactory @@ -19,7 +20,6 @@ import com.adyen.checkout.sessions.core.SessionModel import com.adyen.checkout.sessions.core.internal.data.api.SessionRepository import com.adyen.checkout.sessions.core.internal.data.api.SessionService import kotlinx.coroutines.CoroutineDispatcher -import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext internal class CheckoutSessionInitializer( @@ -27,7 +27,7 @@ internal class CheckoutSessionInitializer( private val environment: Environment, private val clientKey: String, private val order: Order?, - private val coroutineDispatcher: CoroutineDispatcher = Dispatchers.IO, + private val coroutineDispatcher: CoroutineDispatcher = DispatcherProvider.IO, ) { private val httpClient = HttpClientFactory.getHttpClient(environment) private val sessionService = SessionService(httpClient) diff --git a/sessions-core/src/main/java/com/adyen/checkout/sessions/core/internal/data/api/SessionService.kt b/sessions-core/src/main/java/com/adyen/checkout/sessions/core/internal/data/api/SessionService.kt index 81e04391a9..36e2b3f3c5 100644 --- a/sessions-core/src/main/java/com/adyen/checkout/sessions/core/internal/data/api/SessionService.kt +++ b/sessions-core/src/main/java/com/adyen/checkout/sessions/core/internal/data/api/SessionService.kt @@ -9,6 +9,7 @@ package com.adyen.checkout.sessions.core.internal.data.api import androidx.annotation.RestrictTo +import com.adyen.checkout.core.DispatcherProvider import com.adyen.checkout.core.internal.data.api.HttpClient import com.adyen.checkout.core.internal.data.api.post import com.adyen.checkout.sessions.core.SessionSetupResponse @@ -26,13 +27,12 @@ import com.adyen.checkout.sessions.core.internal.data.model.SessionPaymentsReque import com.adyen.checkout.sessions.core.internal.data.model.SessionPaymentsResponse import com.adyen.checkout.sessions.core.internal.data.model.SessionSetupRequest import kotlinx.coroutines.CoroutineDispatcher -import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) class SessionService( private val httpClient: HttpClient, - private val coroutineDispatcher: CoroutineDispatcher = Dispatchers.IO, + private val coroutineDispatcher: CoroutineDispatcher = DispatcherProvider.IO, ) { suspend fun setupSession( diff --git a/ui-core/src/main/java/com/adyen/checkout/ui/core/internal/data/api/AddressService.kt b/ui-core/src/main/java/com/adyen/checkout/ui/core/internal/data/api/AddressService.kt index 0f8ac517f5..eb4404d8d6 100644 --- a/ui-core/src/main/java/com/adyen/checkout/ui/core/internal/data/api/AddressService.kt +++ b/ui-core/src/main/java/com/adyen/checkout/ui/core/internal/data/api/AddressService.kt @@ -9,17 +9,17 @@ package com.adyen.checkout.ui.core.internal.data.api import androidx.annotation.RestrictTo +import com.adyen.checkout.core.DispatcherProvider import com.adyen.checkout.core.internal.data.api.HttpClient import com.adyen.checkout.core.internal.data.api.getList import com.adyen.checkout.ui.core.internal.data.model.AddressItem import kotlinx.coroutines.CoroutineDispatcher -import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) class AddressService( private val httpClient: HttpClient, - private val coroutineDispatcher: CoroutineDispatcher = Dispatchers.IO, + private val coroutineDispatcher: CoroutineDispatcher = DispatcherProvider.IO, ) { suspend fun getCountries( shopperLocale: String diff --git a/ui-core/src/main/java/com/adyen/checkout/ui/core/internal/data/api/DefaultAddressRepository.kt b/ui-core/src/main/java/com/adyen/checkout/ui/core/internal/data/api/DefaultAddressRepository.kt index ea70733278..aa781b6693 100644 --- a/ui-core/src/main/java/com/adyen/checkout/ui/core/internal/data/api/DefaultAddressRepository.kt +++ b/ui-core/src/main/java/com/adyen/checkout/ui/core/internal/data/api/DefaultAddressRepository.kt @@ -11,13 +11,13 @@ package com.adyen.checkout.ui.core.internal.data.api import androidx.annotation.RestrictTo import com.adyen.checkout.components.core.internal.util.bufferedChannel import com.adyen.checkout.core.AdyenLogLevel +import com.adyen.checkout.core.DispatcherProvider import com.adyen.checkout.core.internal.util.adyenLog import com.adyen.checkout.core.internal.util.runSuspendCatching import com.adyen.checkout.ui.core.internal.data.model.AddressItem import com.adyen.checkout.ui.core.internal.ui.AddressSpecification import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.receiveAsFlow @@ -27,7 +27,7 @@ import java.util.Locale @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) class DefaultAddressRepository( private val addressService: AddressService, - private val coroutineDispatcher: CoroutineDispatcher = Dispatchers.IO, + private val coroutineDispatcher: CoroutineDispatcher = DispatcherProvider.IO, ) : AddressRepository { private val statesChannel: Channel> = bufferedChannel() diff --git a/ui-core/src/main/java/com/adyen/checkout/ui/core/internal/util/ImageSaver.kt b/ui-core/src/main/java/com/adyen/checkout/ui/core/internal/util/ImageSaver.kt index f6cd571e5c..111fdb6cf5 100644 --- a/ui-core/src/main/java/com/adyen/checkout/ui/core/internal/util/ImageSaver.kt +++ b/ui-core/src/main/java/com/adyen/checkout/ui/core/internal/util/ImageSaver.kt @@ -27,6 +27,7 @@ import androidx.annotation.RequiresPermission import androidx.annotation.RestrictTo import androidx.core.content.ContextCompat import com.adyen.checkout.core.AdyenLogLevel +import com.adyen.checkout.core.DispatcherProvider import com.adyen.checkout.core.exception.CheckoutException import com.adyen.checkout.core.internal.ui.PermissionHandler import com.adyen.checkout.core.internal.util.adyenLog @@ -36,7 +37,6 @@ import com.adyen.checkout.ui.core.internal.util.PermissionHandlerResult.PERMISSI import com.adyen.checkout.ui.core.internal.util.PermissionHandlerResult.PERMISSION_REQUEST_NOT_HANDLED import com.google.android.material.color.MaterialColors import kotlinx.coroutines.CoroutineDispatcher -import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import java.io.BufferedInputStream import java.io.File @@ -49,7 +49,7 @@ import com.google.android.material.R as MaterialR @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) class ImageSaver( - private val dispatcher: CoroutineDispatcher = Dispatchers.IO, + private val dispatcher: CoroutineDispatcher = DispatcherProvider.IO, ) { @Suppress("LongParameterList")