Updated on 2026-08-14

This commit is contained in:
Tangem 2022-05-03 16:39:21 +03:00
parent 097a377af3
commit a085c2998c
3 changed files with 44 additions and 47 deletions

View file

@ -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<AddCustomTokenState>("AddCustomT
private val hubState: AddCustomTokenState
get() = domainStore.state.addCustomTokensState
override fun getReducer(): ReStoreReducer<AddCustomTokenState> = 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<AddCustomTokenState>("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<AddCustomTokenState> {
override fun reduceAction(action: Action, state: AddCustomTokenState): AddCustomTokenState {
return when (action) {
@ -679,10 +686,4 @@ internal class AddCustomTokenHub : BaseStoreHub<AddCustomTokenState>("AddCustomT
return state.copy(form = Form(state.form.fieldList))
}
@Throws
private fun throwUnAppropriateInitialization(objName: String) {
throw AddCustomTokenException.UnAppropriateInitializationException(
"AddCustomTokenHub", "$objName must be not NULL"
)
}
}

View file

@ -17,14 +17,13 @@ import java.util.concurrent.Executors
* a state behavior.
* All ReStoreHub's must be marked as internal
*/
internal interface ReStoreHub<StoreState, State> : HubMiddleware<StoreState>, HubReducer<StoreState>
internal interface HubMiddleware<StoreState> {
internal interface ReStoreHub<StoreState, State> {
fun getMiddleware(): Middleware<StoreState>
fun reduce(action: Action, domainState: StoreState): StoreState
}
internal interface HubReducer<StoreState> {
fun reduce(action: Action, storeState: StoreState): StoreState
internal interface ReStoreReducer<State> {
fun reduceAction(action: Action, state: State): State
}
/**
@ -87,13 +86,13 @@ internal abstract class BaseStoreHub<State>(
* 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<State>(
}
protected abstract suspend fun handleAction(action: Action, storeState: DomainState, cancel: ValueCallback<Action>)
@Deprecated(
replaceWith = ReplaceWith("ReStoreReducer<T>"),
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<State>
protected abstract fun getReducer(): ReStoreReducer<State>
protected abstract fun getHubState(storeState: DomainState): State
protected abstract fun updateStoreState(storeState: DomainState, newHubState: State): DomainState
}
internal interface ReStoreReducer<State> {
fun reduceAction(action: Action, state: State): State
}
internal suspend inline fun ReStoreHub<*, *>.dispatchOnMain(vararg actions: Action) {
withMainContext { actions.forEach { domainStore.dispatch(it) } }
}

View file

@ -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<DomainGlobalState>("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<DomainGlobalState> = DomainGlobalReducer()
}
private class DomainGlobalReducer : ReStoreReducer<DomainGlobalState> {
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
}
}