From a085c2998c4717ebd1c664054c3e9b15f6a47540 Mon Sep 17 00:00:00 2001 From: Tangem Date: Tue, 3 May 2022 16:39:21 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../addCustomToken/redux/AddCustomTokenHub.kt | 27 ++++++++------- .../com/tangem/domain/redux/ReStoreHub.kt | 34 ++++++------------- .../domain/redux/global/DomainGlobalHub.kt | 30 ++++++++++------ 3 files changed, 44 insertions(+), 47 deletions(-) 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 19e2dcc43c..73db7237db 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 @@ -18,11 +18,9 @@ import com.tangem.domain.common.form.* import com.tangem.domain.features.addCustomToken.* import com.tangem.domain.features.addCustomToken.CustomTokenFieldId.* import com.tangem.domain.features.addCustomToken.redux.AddCustomTokenAction.* -import com.tangem.domain.redux.BaseStoreHub -import com.tangem.domain.redux.DomainState -import com.tangem.domain.redux.dispatchOnMain -import com.tangem.domain.redux.domainStore +import com.tangem.domain.redux.* import com.tangem.domain.redux.global.DomainGlobalAction +import com.tangem.domain.redux.global.DomainGlobalState import com.tangem.network.api.tangemTech.CoinsResponse import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay @@ -38,6 +36,8 @@ internal class AddCustomTokenHub : BaseStoreHub("AddCustomT private val hubState: AddCustomTokenState get() = domainStore.state.addCustomTokensState + override fun getReducer(): ReStoreReducer = AddCustomTokenReducer(globalState) + override fun getHubState(storeState: DomainState): AddCustomTokenState = hubState override fun updateStoreState(storeState: DomainState, newHubState: AddCustomTokenState): DomainState { @@ -515,10 +515,17 @@ internal class AddCustomTokenHub : BaseStoreHub("AddCustomT dispatchOnMain(Warning.Replace(setOf(this), setOf(to))) } -// private suspend fun Warning.replace(replace: Boolean, to: Warning) { -// if (replace) dispatchOnMain(Warning.Replace(setOf(this), setOf(to))) -// } + @Throws + private fun throwUnAppropriateInitialization(objName: String) { + throw AddCustomTokenException.UnAppropriateInitializationException( + "AddCustomTokenHub", "$objName must be not NULL" + ) + } +} +private class AddCustomTokenReducer( + private val globalState: DomainGlobalState, +) : ReStoreReducer { override fun reduceAction(action: Action, state: AddCustomTokenState): AddCustomTokenState { return when (action) { @@ -679,10 +686,4 @@ internal class AddCustomTokenHub : BaseStoreHub("AddCustomT return state.copy(form = Form(state.form.fieldList)) } - @Throws - private fun throwUnAppropriateInitialization(objName: String) { - throw AddCustomTokenException.UnAppropriateInitializationException( - "AddCustomTokenHub", "$objName must be not NULL" - ) - } } \ No newline at end of file diff --git a/domain/src/main/java/com/tangem/domain/redux/ReStoreHub.kt b/domain/src/main/java/com/tangem/domain/redux/ReStoreHub.kt index 72aa9ca605..06775baafd 100644 --- a/domain/src/main/java/com/tangem/domain/redux/ReStoreHub.kt +++ b/domain/src/main/java/com/tangem/domain/redux/ReStoreHub.kt @@ -17,14 +17,13 @@ import java.util.concurrent.Executors * a state behavior. * All ReStoreHub's must be marked as internal */ -internal interface ReStoreHub : HubMiddleware, HubReducer - -internal interface HubMiddleware { +internal interface ReStoreHub { fun getMiddleware(): Middleware + fun reduce(action: Action, domainState: StoreState): StoreState } -internal interface HubReducer { - fun reduce(action: Action, storeState: StoreState): StoreState +internal interface ReStoreReducer { + fun reduceAction(action: Action, state: State): State } /** @@ -87,13 +86,13 @@ internal abstract class BaseStoreHub( * Reduce the action and check it. If the action hasn't updated the hubState, then it doesn't need to update * storeState */ - override fun reduce(action: Action, storeState: DomainState): DomainState { - val oldState = getHubState(storeState) - val newState = reduceAction(action, oldState) - return if (oldState === newState) { - storeState + override fun reduce(action: Action, domainState: DomainState): DomainState { + val hubOldState = getHubState(domainState) + val hubNewState = getReducer().reduceAction(action, hubOldState) + return if (hubOldState === hubNewState) { + domainState } else { - updateStoreState(storeState, newState) + updateStoreState(domainState, hubNewState) } } @@ -102,24 +101,13 @@ internal abstract class BaseStoreHub( } protected abstract suspend fun handleAction(action: Action, storeState: DomainState, cancel: ValueCallback) - - @Deprecated( - replaceWith = ReplaceWith("ReStoreReducer"), - message = "must return a Reducer instance. The reducer must be a separate component - this closes " + - "access to the states that can be obtained from ReStoreHub" - ) - abstract fun reduceAction(action: Action, state: State): State -// protected abstract fun getReducer(): ReStoreReducer + protected abstract fun getReducer(): ReStoreReducer protected abstract fun getHubState(storeState: DomainState): State protected abstract fun updateStoreState(storeState: DomainState, newHubState: State): DomainState } -internal interface ReStoreReducer { - fun reduceAction(action: Action, state: State): State -} - internal suspend inline fun ReStoreHub<*, *>.dispatchOnMain(vararg actions: Action) { withMainContext { actions.forEach { domainStore.dispatch(it) } } } 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 92fc243edd..d80b0bab73 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 @@ -4,6 +4,7 @@ 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.domain.redux.ReStoreReducer import com.tangem.network.common.CardPublicKeyHttpInterceptor import org.rekotlin.Action @@ -34,17 +35,24 @@ internal class DomainGlobalHub : BaseStoreHub("DomainGlobalHu } } - override fun reduceAction(action: Action, state: DomainGlobalState): DomainGlobalState = when (action) { - is DomainGlobalAction.SaveScanNoteResponse -> { - val cardPublicKeyHex = action.scanResponse.card.cardPublicKey.toHexString() - state.networkServices.tangemTechService.addHeaderInterceptors( - listOf(CardPublicKeyHttpInterceptor(cardPublicKeyHex)) - ) - state.copy(scanResponse = action.scanResponse) + override fun getReducer(): ReStoreReducer = DomainGlobalReducer() +} + +private class DomainGlobalReducer : ReStoreReducer { + + override fun reduceAction(action: Action, state: DomainGlobalState): DomainGlobalState { + return when (action) { + 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 -> { + state.copy(dialog = action.stateDialog) + } + else -> state } - is DomainGlobalAction.ShowDialog -> { - state.copy(dialog = action.stateDialog) - } - else -> state } } \ No newline at end of file