diff --git a/app/src/main/java/com/tangem/tap/common/redux/global/GlobalMidlleware.kt b/app/src/main/java/com/tangem/tap/common/redux/global/GlobalMidlleware.kt index 7e1cb3f0cc..722fe8a067 100644 --- a/app/src/main/java/com/tangem/tap/common/redux/global/GlobalMidlleware.kt +++ b/app/src/main/java/com/tangem/tap/common/redux/global/GlobalMidlleware.kt @@ -4,8 +4,6 @@ import com.tangem.common.CompletionResult import com.tangem.common.core.TangemSdkError import com.tangem.common.extensions.ifNotNull import com.tangem.domain.common.extensions.withMainContext -import com.tangem.domain.redux.domainStore -import com.tangem.domain.redux.global.DomainGlobalAction import com.tangem.tap.* import com.tangem.tap.common.extensions.dispatchDialogShow import com.tangem.tap.common.extensions.dispatchOnMain @@ -102,12 +100,10 @@ private val globalMiddlewareHandler: Middleware = { dispatch, appState store.dispatch(GlobalAction.ScanFailsCounter.ChooseBehavior(result)) when (result) { is CompletionResult.Success -> { - domainStore.dispatch(DomainGlobalAction.SetScanResponse(result.data)) tangemSdkManager.changeDisplayedCardIdNumbersCount(result.data) action.onSuccess?.invoke(result.data) } is CompletionResult.Failure -> { - domainStore.dispatch(DomainGlobalAction.SetScanResponse(null)) action.onFailure?.invoke(result.error) } } diff --git a/app/src/main/java/com/tangem/tap/common/redux/global/GlobalReducer.kt b/app/src/main/java/com/tangem/tap/common/redux/global/GlobalReducer.kt index d84c43379d..67e54a6042 100644 --- a/app/src/main/java/com/tangem/tap/common/redux/global/GlobalReducer.kt +++ b/app/src/main/java/com/tangem/tap/common/redux/global/GlobalReducer.kt @@ -1,5 +1,7 @@ package com.tangem.tap.common.redux.global +import com.tangem.domain.redux.domainStore +import com.tangem.domain.redux.global.DomainGlobalAction import com.tangem.tap.common.redux.AppState import com.tangem.tap.features.onboarding.OnboardingManager import com.tangem.tap.preferencesStorage @@ -33,8 +35,10 @@ fun globalReducer(action: Action, state: AppState): GlobalState { is GlobalAction.ScanFailsCounter.Reset -> { globalState.copy(scanCardFailsCounter = 0) } - is GlobalAction.SaveScanNoteResponse -> + is GlobalAction.SaveScanNoteResponse ->{ + domainStore.dispatch(DomainGlobalAction.SaveScanNoteResponse(action.scanResponse)) globalState.copy(scanResponse = action.scanResponse) + } is GlobalAction.ChangeAppCurrency -> { globalState.copy(appCurrency = action.appCurrency) } diff --git a/app/src/main/java/com/tangem/tap/features/tokens/addCustomToken/compose/DebugActions.kt b/app/src/main/java/com/tangem/tap/features/tokens/addCustomToken/compose/DebugActions.kt index 0db334c34b..c9b231783d 100644 --- a/app/src/main/java/com/tangem/tap/features/tokens/addCustomToken/compose/DebugActions.kt +++ b/app/src/main/java/com/tangem/tap/features/tokens/addCustomToken/compose/DebugActions.kt @@ -14,7 +14,7 @@ import com.tangem.blockchain.common.Blockchain import com.tangem.common.extensions.VoidCallback import com.tangem.common.services.Result import com.tangem.domain.common.form.Field -import com.tangem.domain.features.addCustomToken.TangemTechServiceManager +import com.tangem.domain.features.addCustomToken.AddCustomTokenService import com.tangem.domain.features.addCustomToken.redux.AddCustomTokenAction import com.tangem.domain.redux.domainStore import com.tangem.network.api.tangemTech.TangemTechService @@ -147,7 +147,7 @@ private fun CustomActions() { CustomActionButton( name = "Find tokens in several networks", action = { - val manager = TangemTechServiceManager(TangemTechService()) + val manager = AddCustomTokenService(TangemTechService()) val currencies = manager.tokens() val asdfsd = mutableMapOf>() val contractAddresses = currencies.mapNotNull { currency -> diff --git a/domain/src/main/java/com/tangem/domain/features/addCustomToken/TangemTechServiceManager.kt b/domain/src/main/java/com/tangem/domain/features/addCustomToken/AddCustomTokenService.kt similarity index 78% rename from domain/src/main/java/com/tangem/domain/features/addCustomToken/TangemTechServiceManager.kt rename to domain/src/main/java/com/tangem/domain/features/addCustomToken/AddCustomTokenService.kt index 438e75e47a..464dec0abd 100644 --- a/domain/src/main/java/com/tangem/domain/features/addCustomToken/TangemTechServiceManager.kt +++ b/domain/src/main/java/com/tangem/domain/features/addCustomToken/AddCustomTokenService.kt @@ -3,12 +3,11 @@ package com.tangem.domain.features.addCustomToken import com.tangem.common.services.Result import com.tangem.network.api.tangemTech.Coins import com.tangem.network.api.tangemTech.TangemTechService -import com.tangem.network.common.AddHeaderInterceptor /** [REDACTED_AUTHOR] */ -class TangemTechServiceManager( +class AddCustomTokenService( private val tangemTechService: TangemTechService ) { @@ -44,21 +43,9 @@ class TangemTechServiceManager( return when (val result = tangemTechService.coins.tokens()) { is Result.Success -> { val tokens = result.data.tokens - tokens.filter { - it.contracts.isNullOrEmpty() - } + tokens.filter { it.contracts.isNullOrEmpty() } } is Result.Failure -> emptyList() } } - - fun attachAuthKey(authKey: String) { - tangemTechService.addHeaderInterceptors(listOf( - CardPublicKeyHttpInterceptor(authKey), - )) - } -} - -private class CardPublicKeyHttpInterceptor(cardPublicKeyHex: String) : AddHeaderInterceptor(mapOf( - "card_public_key" to cardPublicKeyHex, -)) \ No newline at end of file +} \ No newline at end of file diff --git a/domain/src/main/java/com/tangem/domain/features/addCustomToken/redux/AddCustomTokenHub.kt b/domain/src/main/java/com/tangem/domain/features/addCustomToken/redux/AddCustomTokenHub.kt index a127b43776..cfb696268a 100644 --- a/domain/src/main/java/com/tangem/domain/features/addCustomToken/redux/AddCustomTokenHub.kt +++ b/domain/src/main/java/com/tangem/domain/features/addCustomToken/redux/AddCustomTokenHub.kt @@ -4,7 +4,6 @@ import android.webkit.ValueCallback import com.tangem.blockchain.common.Blockchain import com.tangem.blockchain.common.DerivationStyle import com.tangem.common.extensions.guard -import com.tangem.common.extensions.toHexString import com.tangem.common.services.Result import com.tangem.domain.DomainDialog import com.tangem.domain.DomainException @@ -22,7 +21,6 @@ import com.tangem.domain.redux.dispatchOnMain import com.tangem.domain.redux.domainStore import com.tangem.domain.redux.global.DomainGlobalAction import com.tangem.network.api.tangemTech.Coins -import com.tangem.network.api.tangemTech.TangemTechService import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.launch @@ -476,8 +474,7 @@ internal class AddCustomTokenHub : BaseStoreHub("AddCustomT } is OnCreate -> { val card = requireNotNull(globalState.scanResponse?.card) - val tangemTechServiceManager = TangemTechServiceManager(TangemTechService()) - tangemTechServiceManager.attachAuthKey(card.cardPublicKey.toHexString()) + val tangemTechServiceManager = AddCustomTokenService(globalState.networkServices.tangemTechService) var derivationPathState = state.screenState.derivationPath derivationPathState = when (card.derivationStyle) { diff --git a/domain/src/main/java/com/tangem/domain/features/addCustomToken/redux/AddCustomTokenState.kt b/domain/src/main/java/com/tangem/domain/features/addCustomToken/redux/AddCustomTokenState.kt index d8f43f4e00..1a96e9a844 100644 --- a/domain/src/main/java/com/tangem/domain/features/addCustomToken/redux/AddCustomTokenState.kt +++ b/domain/src/main/java/com/tangem/domain/features/addCustomToken/redux/AddCustomTokenState.kt @@ -18,7 +18,7 @@ data class AddCustomTokenState( val tokenId: String? = null, val warnings: Set = emptySet(), val screenState: ScreenState = createInitialScreenState(), - val tangemTechServiceManager: TangemTechServiceManager? = null + val tangemTechServiceManager: AddCustomTokenService? = null ) : StateType { inline fun getField(id: FieldId): T = form.getField(id) as T diff --git a/domain/src/main/java/com/tangem/domain/redux/global/DomainGlobalAction.kt b/domain/src/main/java/com/tangem/domain/redux/global/DomainGlobalAction.kt index a01a16315a..d58d26a864 100644 --- a/domain/src/main/java/com/tangem/domain/redux/global/DomainGlobalAction.kt +++ b/domain/src/main/java/com/tangem/domain/redux/global/DomainGlobalAction.kt @@ -9,6 +9,6 @@ import org.rekotlin.Action */ //TODO: refactoring: is alias for the GlobalAction sealed class DomainGlobalAction : Action { - data class SetScanResponse(val scanResponse: ScanResponse?) : DomainGlobalAction() + data class SaveScanNoteResponse(val scanResponse: ScanResponse) : DomainGlobalAction() data class ShowDialog(val stateDialog: DomainDialog?) : DomainGlobalAction() } \ No newline at end of file diff --git a/domain/src/main/java/com/tangem/domain/redux/global/DomainGlobalHub.kt b/domain/src/main/java/com/tangem/domain/redux/global/DomainGlobalHub.kt index 494dc41e7e..92fc243edd 100644 --- a/domain/src/main/java/com/tangem/domain/redux/global/DomainGlobalHub.kt +++ b/domain/src/main/java/com/tangem/domain/redux/global/DomainGlobalHub.kt @@ -1,8 +1,10 @@ package com.tangem.domain.redux.global import android.webkit.ValueCallback +import com.tangem.common.extensions.toHexString import com.tangem.domain.redux.BaseStoreHub import com.tangem.domain.redux.DomainState +import com.tangem.network.common.CardPublicKeyHttpInterceptor import org.rekotlin.Action /** @@ -33,7 +35,11 @@ internal class DomainGlobalHub : BaseStoreHub("DomainGlobalHu } override fun reduceAction(action: Action, state: DomainGlobalState): DomainGlobalState = when (action) { - is DomainGlobalAction.SetScanResponse -> { + is DomainGlobalAction.SaveScanNoteResponse -> { + val cardPublicKeyHex = action.scanResponse.card.cardPublicKey.toHexString() + state.networkServices.tangemTechService.addHeaderInterceptors( + listOf(CardPublicKeyHttpInterceptor(cardPublicKeyHex)) + ) state.copy(scanResponse = action.scanResponse) } is DomainGlobalAction.ShowDialog -> { diff --git a/domain/src/main/java/com/tangem/domain/redux/global/DomainGlobalState.kt b/domain/src/main/java/com/tangem/domain/redux/global/DomainGlobalState.kt index 9bf5d7c15e..c51219beab 100644 --- a/domain/src/main/java/com/tangem/domain/redux/global/DomainGlobalState.kt +++ b/domain/src/main/java/com/tangem/domain/redux/global/DomainGlobalState.kt @@ -2,12 +2,19 @@ package com.tangem.domain.redux.global import com.tangem.domain.DomainDialog import com.tangem.domain.common.ScanResponse +import com.tangem.network.api.tangemTech.TangemTechService /** [REDACTED_AUTHOR] */ -//TODO: refactoring: is alias for the GlobalState data class DomainGlobalState( + // there is a part of mirrors from the GlobalState val scanResponse: ScanResponse? = null, + // + val networkServices: NetworkServices = NetworkServices(), val dialog: DomainDialog? = null, ) + +data class NetworkServices( + val tangemTechService: TangemTechService = TangemTechService() +) diff --git a/network/src/main/java/com/tangem/network/common/Interceptors.kt b/network/src/main/java/com/tangem/network/common/Interceptors.kt index dff259332e..bc46e5a0a0 100644 --- a/network/src/main/java/com/tangem/network/common/Interceptors.kt +++ b/network/src/main/java/com/tangem/network/common/Interceptors.kt @@ -23,4 +23,8 @@ open class AddHeaderInterceptor( class CacheControlHttpInterceptor(maxAgeSeconds: Int) : AddHeaderInterceptor(mapOf( "Cache-Control" to "max-age=$maxAgeSeconds", +)) + +class CardPublicKeyHttpInterceptor(cardPublicKeyHex: String) : AddHeaderInterceptor(mapOf( + "card_public_key" to cardPublicKeyHex, )) \ No newline at end of file