diff --git a/app/src/main/java/com/tangem/tap/common/utils/SafeStoreSubscriber.kt b/app/src/main/java/com/tangem/tap/common/utils/SafeStoreSubscriber.kt new file mode 100644 index 0000000000..ac430a6ea5 --- /dev/null +++ b/app/src/main/java/com/tangem/tap/common/utils/SafeStoreSubscriber.kt @@ -0,0 +1,36 @@ +package com.tangem.tap.common.utils + +import android.os.Handler +import android.os.Looper +import org.rekotlin.StoreSubscriber + +private val mainLooper by lazy { Looper.getMainLooper() } +private val mainHandler by lazy { Handler(mainLooper) } + +/** + * A subscriber interface for safely subscribing to state changes in a Store. + * + * @param State the type of the state in the Store + */ +interface SafeStoreSubscriber : StoreSubscriber { + + /** + * A function that will be called when the state in the Store changes. + * + * @param state the new state in the Store + */ + override fun newState(state: State) { + if (Thread.currentThread() != mainLooper.thread) { + mainHandler.post { newStateOnMain(state) } + } else { + newStateOnMain(state) + } + } + + /** + * A function that will be called on the main thread when the state in the Store changes. + * + * @param state the new state in the Store + */ + fun newStateOnMain(state: State) +} \ No newline at end of file diff --git a/app/src/main/java/com/tangem/tap/features/wallet/ui/WalletDetailsFragment.kt b/app/src/main/java/com/tangem/tap/features/wallet/ui/WalletDetailsFragment.kt index 9952836ff9..7da258be29 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/ui/WalletDetailsFragment.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/ui/WalletDetailsFragment.kt @@ -41,6 +41,7 @@ import com.tangem.tap.common.extensions.show import com.tangem.tap.common.extensions.toQrCode import com.tangem.tap.common.recyclerView.SpaceItemDecoration import com.tangem.tap.common.redux.navigation.NavigationAction +import com.tangem.tap.common.utils.SafeStoreSubscriber import com.tangem.tap.domain.model.WalletDataModel import com.tangem.tap.features.wallet.models.Currency import com.tangem.tap.features.wallet.models.PendingTransaction @@ -72,7 +73,6 @@ import com.tangem.wallet.databinding.FragmentWalletDetailsBinding import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch -import org.rekotlin.StoreSubscriber import timber.log.Timber import javax.inject.Inject @@ -80,7 +80,7 @@ import javax.inject.Inject @AndroidEntryPoint class WalletDetailsFragment : Fragment(R.layout.fragment_wallet_details), - StoreSubscriber { + SafeStoreSubscriber { @Inject lateinit var swapInteractor: SwapInteractor @@ -227,7 +227,7 @@ class WalletDetailsFragment : } } - override fun newState(state: WalletState) { + override fun newStateOnMain(state: WalletState) { if (activity == null || view == null) return if (state.selectedWalletData == null) return walletStateWatcher.invoke(state) diff --git a/app/src/main/java/com/tangem/tap/features/wallet/ui/WalletFragment.kt b/app/src/main/java/com/tangem/tap/features/wallet/ui/WalletFragment.kt index ca1149d987..3bf4cd8b80 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/ui/WalletFragment.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/ui/WalletFragment.kt @@ -28,6 +28,7 @@ import com.tangem.tap.common.recyclerView.SpaceItemDecoration import com.tangem.tap.common.redux.global.GlobalAction import com.tangem.tap.common.redux.navigation.AppScreen import com.tangem.tap.common.redux.navigation.NavigationAction +import com.tangem.tap.common.utils.SafeStoreSubscriber import com.tangem.tap.domain.configurable.warningMessage.WarningMessage import com.tangem.tap.domain.statePrinter.printScanResponseState import com.tangem.tap.domain.statePrinter.printWalletState @@ -46,11 +47,10 @@ import com.tangem.wallet.BuildConfig import com.tangem.wallet.R import com.tangem.wallet.databinding.FragmentWalletBinding import dagger.hilt.android.AndroidEntryPoint -import org.rekotlin.StoreSubscriber import javax.inject.Inject @AndroidEntryPoint -class WalletFragment : Fragment(R.layout.fragment_wallet), StoreSubscriber { +class WalletFragment : Fragment(R.layout.fragment_wallet), SafeStoreSubscriber { @Inject lateinit var swapInteractor: SwapInteractor @@ -148,7 +148,7 @@ class WalletFragment : Fragment(R.layout.fragment_wallet), StoreSubscriber