Skip to content

Commit

Permalink
MOB-513 Clean up force update logic (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruixhuang authored May 21, 2024
1 parent 9047bca commit 32279c6
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ interface DydxRouter {
Default,
}

val initialized: StateFlow<Boolean>

fun presentation(route: String): Presentation
fun isParentChild(parent: String, child: String): Boolean

Expand All @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<Boolean> = _initialized

override val destinationFlow: MutableStateFlow<Destination?> = MutableStateFlow(null)

override fun handleIntent(intent: Intent) {
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,49 @@ 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

override fun start() {
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)
Expand All @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ fun timerFlow(period: Duration, initialDelay: Duration = Duration.ZERO) = flow {
delay(period)
}
}

fun delayFlow(duration: Duration) = flow {
delay(duration)
emit(Unit)
}

0 comments on commit 32279c6

Please sign in to comment.