Updated on 2026-08-14

This commit is contained in:
Tangem 2022-04-17 17:15:20 +03:00
parent dff86fd4dd
commit 5d29cfb181
10 changed files with 32 additions and 31 deletions

View file

@ -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<AppState> = { 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)
}
}

View file

@ -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)
}

View file

@ -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<String, MutableList<Any>>()
val contractAddresses = currencies.mapNotNull { currency ->

View file

@ -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,
))
}

View file

@ -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<AddCustomTokenState>("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) {

View file

@ -18,7 +18,7 @@ data class AddCustomTokenState(
val tokenId: String? = null,
val warnings: Set<AddCustomTokenWarning> = emptySet(),
val screenState: ScreenState = createInitialScreenState(),
val tangemTechServiceManager: TangemTechServiceManager? = null
val tangemTechServiceManager: AddCustomTokenService? = null
) : StateType {
inline fun <reified T> getField(id: FieldId): T = form.getField(id) as T

View file

@ -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()
}

View file

@ -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<DomainGlobalState>("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 -> {

View file

@ -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()
)

View file

@ -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,
))