From aaba1d60dbecf42ab541802c06f21beab7473727 Mon Sep 17 00:00:00 2001 From: Rui Date: Mon, 20 May 2024 13:48:53 -0700 Subject: [PATCH] MOB-513 Clean up force update logic --- .../trading/common/navigation/DydxRouter.kt | 3 +++ .../dydx/trading/core/DydxRouterImpl.kt | 15 +++++++++++- .../feature/workers/DydxGlobalWorkers.kt | 2 +- .../workers/globalworkers/DydxUpdateWorker.kt | 24 ++++++++++++++++--- .../dydx/utilities/utils/TimerFlow.kt | 5 ++++ 5 files changed, 44 insertions(+), 5 deletions(-) diff --git a/v4/common/src/main/java/exchange/dydx/trading/common/navigation/DydxRouter.kt b/v4/common/src/main/java/exchange/dydx/trading/common/navigation/DydxRouter.kt index 2ad0937e..d49f879f 100644 --- a/v4/common/src/main/java/exchange/dydx/trading/common/navigation/DydxRouter.kt +++ b/v4/common/src/main/java/exchange/dydx/trading/common/navigation/DydxRouter.kt @@ -23,6 +23,8 @@ interface DydxRouter { Default, } + val initialized: StateFlow + fun presentation(route: String): Presentation fun isParentChild(parent: String, child: String): Boolean @@ -31,6 +33,7 @@ interface DydxRouter { fun tabTo(route: String, restoreState: Boolean = true) fun navigateBack() + fun navigateToRoot(excludeRoot: Boolean) fun requireNavController(): NavHostController fun initialize(navHostController: NavHostController) diff --git a/v4/core/src/main/java/exchange/dydx/trading/core/DydxRouterImpl.kt b/v4/core/src/main/java/exchange/dydx/trading/core/DydxRouterImpl.kt index 4aa11977..80576661 100644 --- a/v4/core/src/main/java/exchange/dydx/trading/core/DydxRouterImpl.kt +++ b/v4/core/src/main/java/exchange/dydx/trading/core/DydxRouterImpl.kt @@ -18,6 +18,7 @@ import exchange.dydx.trading.common.navigation.MarketRoutes import exchange.dydx.trading.integration.analytics.tracking.Tracking import exchange.dydx.utilities.utils.Logging import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow import javax.inject.Inject private const val TAG = "DydxRouterImpl" @@ -115,8 +116,12 @@ class DydxRouterImpl @Inject constructor( logger.d(TAG, "DydxRouter initialized") this.navHostController = navHostController navHostController.addOnDestinationChangedListener(destinationChangedListener) + _initialized.value = true } + private val _initialized = MutableStateFlow(false) + override val initialized: StateFlow = _initialized + override val destinationFlow: MutableStateFlow = MutableStateFlow(null) override fun handleIntent(intent: Intent) { @@ -166,7 +171,15 @@ class DydxRouterImpl @Inject constructor( override fun navigateBack() { routeQueue.removeLast() - navHostController?.popBackStack() + navHostController.popBackStack() + } + + override fun navigateToRoot(excludeRoot: Boolean) { + routeQueue.clear() + navHostController.popBackStack( + destinationId = navHostController.graph.findStartDestination().id, + inclusive = excludeRoot, + ) } override fun requireNavController(): NavHostController { diff --git a/v4/feature/workers/src/main/java/exchange/dydx/trading/feature/workers/DydxGlobalWorkers.kt b/v4/feature/workers/src/main/java/exchange/dydx/trading/feature/workers/DydxGlobalWorkers.kt index 8a3b1629..344fee9a 100644 --- a/v4/feature/workers/src/main/java/exchange/dydx/trading/feature/workers/DydxGlobalWorkers.kt +++ b/v4/feature/workers/src/main/java/exchange/dydx/trading/feature/workers/DydxGlobalWorkers.kt @@ -39,7 +39,7 @@ class DydxGlobalWorkers( ) : WorkerProtocol { private val workers = listOf( - DydxUpdateWorker(scope, abacusStateManager, router, context), + DydxUpdateWorker(scope, abacusStateManager, router, context, logger), DydxAlertsWorker(scope, abacusStateManager, localizer, router, platformInfo, preferencesStore), DydxApiStatusWorker(scope, abacusStateManager, localizer, platformInfo), DydxRestrictionsWorker(scope, abacusStateManager, localizer, platformInfo), diff --git a/v4/feature/workers/src/main/java/exchange/dydx/trading/feature/workers/globalworkers/DydxUpdateWorker.kt b/v4/feature/workers/src/main/java/exchange/dydx/trading/feature/workers/globalworkers/DydxUpdateWorker.kt index b23fdfff..f39e3332 100644 --- a/v4/feature/workers/src/main/java/exchange/dydx/trading/feature/workers/globalworkers/DydxUpdateWorker.kt +++ b/v4/feature/workers/src/main/java/exchange/dydx/trading/feature/workers/globalworkers/DydxUpdateWorker.kt @@ -5,19 +5,27 @@ import exchange.dydx.abacus.state.manager.V4Environment import exchange.dydx.dydxstatemanager.AbacusStateManagerProtocol import exchange.dydx.trading.common.navigation.DydxRouter import exchange.dydx.trading.common.navigation.ProfileRoutes +import exchange.dydx.utilities.utils.Logging import exchange.dydx.utilities.utils.VersionUtils import exchange.dydx.utilities.utils.WorkerProtocol +import exchange.dydx.utilities.utils.delayFlow import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.flow.distinctUntilChanged +import kotlinx.coroutines.flow.filter +import kotlinx.coroutines.flow.flatMapLatest import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.mapNotNull import kotlinx.coroutines.flow.onEach +import kotlin.time.Duration.Companion.seconds + +private const val TAG = "DydxUpdateWorker" class DydxUpdateWorker( override val scope: CoroutineScope, private val abacusStateManager: AbacusStateManagerProtocol, private val router: DydxRouter, private val context: Context, + private val logger: Logging, ) : WorkerProtocol { override var isStarted = false @@ -25,12 +33,21 @@ class DydxUpdateWorker( if (!isStarted) { isStarted = true - abacusStateManager.currentEnvironmentId + router.initialized + .filter { it } + .flatMapLatest { + // Wait 2 seconds for the root view to load first. Otherwise, "/portfolio/" would take + // place after "/update" + delayFlow(duration = 2.seconds) + } + .flatMapLatest { abacusStateManager.currentEnvironmentId } .mapNotNull { it } .distinctUntilChanged() .onEach { abacusStateManager.environment?.let { update(it) + } ?: run { + logger.e(TAG, "Environment is null") } } .launchIn(scope) @@ -43,10 +60,11 @@ class DydxUpdateWorker( } } - private fun update(environment: V4Environment?) { - val desired = environment?.apps?.android?.build ?: return + private fun update(environment: V4Environment) { + val desired = environment.apps?.android?.build ?: return val mine = VersionUtils.versionCode(context) ?: return if (desired > mine) { + router.navigateToRoot(excludeRoot = true) router.navigateTo(ProfileRoutes.update) } } diff --git a/v4/utilities/src/main/java/exchange/dydx/utilities/utils/TimerFlow.kt b/v4/utilities/src/main/java/exchange/dydx/utilities/utils/TimerFlow.kt index 77636c53..41c5531f 100644 --- a/v4/utilities/src/main/java/exchange/dydx/utilities/utils/TimerFlow.kt +++ b/v4/utilities/src/main/java/exchange/dydx/utilities/utils/TimerFlow.kt @@ -11,3 +11,8 @@ fun timerFlow(period: Duration, initialDelay: Duration = Duration.ZERO) = flow { delay(period) } } + +fun delayFlow(duration: Duration) = flow { + delay(duration) + emit(Unit) +}