From 954b03c704e8147e609772cfb9257ee60636f273 Mon Sep 17 00:00:00 2001 From: jqwout Date: Wed, 28 Dec 2022 23:56:05 +0300 Subject: [PATCH 1/2] MC-7844 - scopes refactoring --- .../tinkoff/acquiring/sdk/TinkoffAcquiring.kt | 28 ++++++++----------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/ui/src/main/java/ru/tinkoff/acquiring/sdk/TinkoffAcquiring.kt b/ui/src/main/java/ru/tinkoff/acquiring/sdk/TinkoffAcquiring.kt index 8e8452f4..482c8ceb 100644 --- a/ui/src/main/java/ru/tinkoff/acquiring/sdk/TinkoffAcquiring.kt +++ b/ui/src/main/java/ru/tinkoff/acquiring/sdk/TinkoffAcquiring.kt @@ -21,9 +21,7 @@ import android.app.PendingIntent import android.content.Context import android.content.Intent import androidx.fragment.app.Fragment -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.launch +import kotlinx.coroutines.* import ru.tinkoff.acquiring.sdk.localization.LocalizationSource import ru.tinkoff.acquiring.sdk.models.* import ru.tinkoff.acquiring.sdk.models.options.FeaturesOptions @@ -33,6 +31,7 @@ import ru.tinkoff.acquiring.sdk.models.paysources.CardData import ru.tinkoff.acquiring.sdk.models.paysources.GooglePay import ru.tinkoff.acquiring.sdk.payment.PaymentProcess import ru.tinkoff.acquiring.sdk.requests.performSuspendRequest +import ru.tinkoff.acquiring.sdk.responses.GetTerminalPayMethodsResponse import ru.tinkoff.acquiring.sdk.responses.TerminalInfo import ru.tinkoff.acquiring.sdk.responses.TinkoffPayStatusResponse import ru.tinkoff.acquiring.sdk.threeds.ThreeDsHelper @@ -209,11 +208,11 @@ class TinkoffAcquiring( onFailure: ((Throwable) -> Unit)? = null ) { CoroutineScope(Dispatchers.IO).launch { - sdk.tinkoffPayStatus().execute({ - launch(Dispatchers.Main) { onSuccess(it) } - }, { - launch(Dispatchers.Main) { onFailure?.invoke(it) } - }) + val result = sdk.tinkoffPayStatus().performSuspendRequest() + withContext(Dispatchers.Main) { + result.fold(onSuccess = onSuccess, onFailure = { onFailure?.invoke(it) }) + cancel() + } } } @@ -221,18 +220,15 @@ class TinkoffAcquiring( * Проверка доступных спосбов оплаты */ fun checkTerminalInfo(onSuccess: (TerminalInfo?) -> Unit, - onFailure: ((Throwable) -> Unit)? = null) { - - val onFailureOrThrow = onFailure ?: { throw it } - + onFailure: ((Throwable) -> Unit)? = null + ) { CoroutineScope(Dispatchers.IO).launch { - val result = sdk.getTerminalPayMethods() .performSuspendRequest() .map { it.terminalInfo } - - launch(Dispatchers.Main) { - result.fold(onSuccess, onFailureOrThrow) + withContext(Dispatchers.Main) { + result.fold(onSuccess = onSuccess, onFailure = { onFailure?.invoke(it) }) + cancel() } } } From 3b04f323f067011ec4e9c4c44ffcd93475799a00 Mon Sep 17 00:00:00 2001 From: jqwout Date: Thu, 29 Dec 2022 13:01:24 +0300 Subject: [PATCH 2/2] MC-7844 - scopes refactoring --- .../main/java/ru/tinkoff/acquiring/sdk/TinkoffAcquiring.kt | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/ui/src/main/java/ru/tinkoff/acquiring/sdk/TinkoffAcquiring.kt b/ui/src/main/java/ru/tinkoff/acquiring/sdk/TinkoffAcquiring.kt index 482c8ceb..ec30f37e 100644 --- a/ui/src/main/java/ru/tinkoff/acquiring/sdk/TinkoffAcquiring.kt +++ b/ui/src/main/java/ru/tinkoff/acquiring/sdk/TinkoffAcquiring.kt @@ -42,6 +42,7 @@ import ru.tinkoff.acquiring.sdk.ui.activities.NotificationPaymentActivity import ru.tinkoff.acquiring.sdk.ui.activities.PaymentActivity import ru.tinkoff.acquiring.sdk.ui.activities.QrCodeActivity import ru.tinkoff.acquiring.sdk.ui.activities.SavedCardsActivity +import kotlin.coroutines.suspendCoroutine /** * Точка входа для взаимодействия с Acquiring SDK @@ -208,10 +209,11 @@ class TinkoffAcquiring( onFailure: ((Throwable) -> Unit)? = null ) { CoroutineScope(Dispatchers.IO).launch { + val mainScope = this val result = sdk.tinkoffPayStatus().performSuspendRequest() withContext(Dispatchers.Main) { result.fold(onSuccess = onSuccess, onFailure = { onFailure?.invoke(it) }) - cancel() + mainScope.cancel() } } } @@ -223,12 +225,13 @@ class TinkoffAcquiring( onFailure: ((Throwable) -> Unit)? = null ) { CoroutineScope(Dispatchers.IO).launch { + val mainScope = this val result = sdk.getTerminalPayMethods() .performSuspendRequest() .map { it.terminalInfo } withContext(Dispatchers.Main) { result.fold(onSuccess = onSuccess, onFailure = { onFailure?.invoke(it) }) - cancel() + mainScope.cancel() } } }