Updated on 2026-08-14

This commit is contained in:
Tangem 2020-09-01 10:21:18 +03:00
parent 12b5d855bd
commit 0b75473cb6
29 changed files with 617 additions and 75 deletions

View file

@ -1,5 +1,6 @@
package com.tangem.tap.common.redux
import com.tangem.tap.common.redux.global.globalReducer
import com.tangem.tap.common.redux.navigation.navigationReducer
import com.tangem.tap.features.wallet.redux.walletReducer
import org.rekotlin.Action
@ -8,9 +9,9 @@ fun appReducer(action: Action, state: AppState?): AppState {
requireNotNull(state)
if (action is AppAction.RestoreState) return action.state
return AppState(
navigationState = navigationReducer(action, state),
// homeState = homeReducer(action, state),
walletState = walletReducer(action, state),
navigationState = navigationReducer(action, state),
globalState = globalReducer(action, state),
walletState = walletReducer(action, state),
)
}

View file

@ -1,5 +1,6 @@
package com.tangem.tap.common.redux
import com.tangem.tap.common.redux.global.GlobalState
import com.tangem.tap.common.redux.navigation.NavigationState
import com.tangem.tap.common.redux.navigation.navigationMiddleware
import com.tangem.tap.features.home.redux.homeMiddleware
@ -9,8 +10,9 @@ import org.rekotlin.Middleware
import org.rekotlin.StateType
data class AppState(
val navigationState: NavigationState = NavigationState(),
val walletState: WalletState = WalletState()
val navigationState: NavigationState = NavigationState(),
val globalState: GlobalState = GlobalState(),
val walletState: WalletState = WalletState()
) : StateType {
companion object {

View file

@ -2,24 +2,33 @@ package com.tangem.tap.common.redux
import androidx.coordinatorlayout.widget.CoordinatorLayout
import com.google.android.material.snackbar.Snackbar
import com.tangem.TangemError
import com.tangem.tap.domain.TapError
import com.tangem.tap.notificationsHandler
import org.rekotlin.Action
import org.rekotlin.Middleware
import java.lang.ref.WeakReference
class NotificationsHandler(coordinatorLayout: CoordinatorLayout) {
private val coordinatorLayoutWeak = WeakReference(coordinatorLayout)
private val basicCoordinatorLayout = WeakReference(coordinatorLayout)
private var baseLayout = basicCoordinatorLayout
fun replaceBaseLayout(coordinatorLayout: CoordinatorLayout) {
baseLayout = WeakReference(coordinatorLayout)
}
fun returnBaseLayout() {
baseLayout = basicCoordinatorLayout
}
fun showNotification(message: String) {
coordinatorLayoutWeak.get()?.let { layout ->
baseLayout.get()?.let { layout ->
Snackbar.make(layout, message, Snackbar.LENGTH_LONG)
.also { snackbar -> snackbar.show() }
.also { snackbar -> snackbar.show() }
}
}
fun showNotification(message: Int) {
coordinatorLayoutWeak.get()?.let {
baseLayout.get()?.let {
showNotification(it.context.getString(message))
}
}
@ -32,7 +41,7 @@ val notificationsMiddleware: Middleware<AppState> = { dispatch, state ->
notificationsHandler?.showNotification(action.messageResource)
}
if (action is ErrorAction) {
notificationsHandler?.showNotification(action.error.customMessage)
notificationsHandler?.showNotification(action.error.localizedMessage)
}
next(action)
}
@ -44,5 +53,5 @@ interface NotificationAction : Action {
}
interface ErrorAction : Action {
val error: TangemError
val error: TapError
}

View file

@ -0,0 +1,11 @@
package com.tangem.tap.common.redux.global
import com.tangem.blockchain.common.WalletManager
import com.tangem.commands.Card
import org.rekotlin.Action
sealed class GlobalAction : Action {
data class LoadCard(val card: Card) : GlobalAction()
data class LoadWalletManager(val walletManager: WalletManager) : GlobalAction()
}

View file

@ -0,0 +1,19 @@
package com.tangem.tap.common.redux.global
import com.tangem.tap.common.redux.AppState
import org.rekotlin.Action
fun globalReducer(action: Action, state: AppState): GlobalState {
if (action !is GlobalAction) return state.globalState
var newState = state.globalState
when (action) {
is GlobalAction.LoadCard -> newState = newState.copy(card = action.card)
is GlobalAction.LoadWalletManager ->
newState = newState.copy(walletManager = action.walletManager)
}
return newState
}

View file

@ -0,0 +1,13 @@
package com.tangem.tap.common.redux.global
import com.tangem.blockchain.common.WalletManager
import com.tangem.commands.Card
import org.rekotlin.StateType
data class GlobalState(
val card: Card? = null,
val walletManager: WalletManager? = null,
) : StateType