Updated on 2026-08-14

This commit is contained in:
Tangem 2022-12-15 10:03:20 +03:00
commit e4becad33a
4 changed files with 39 additions and 23 deletions

View file

@ -16,7 +16,7 @@ import kotlin.time.Duration
internal class LockUserWalletsTimer(
owner: LifecycleOwner,
private val duration: Duration = with(Duration) { 5.minutes },
private val duration: Duration = with(Duration) { 10.minutes },
) : LifecycleOwner by owner,
DefaultLifecycleObserver {
@ -25,8 +25,6 @@ internal class LockUserWalletsTimer(
field?.cancel()
field = value
}
private var isStopped = false
private var openWelcomeScreenWhenResumed = false
init {
lifecycle.addObserver(this)
@ -35,22 +33,22 @@ internal class LockUserWalletsTimer(
override fun onResume(owner: LifecycleOwner) {
Timber.d(
"""
Owner resumed
|- Was stopped: $isStopped
|- Need to open welcome screen: $openWelcomeScreenWhenResumed
""".trimIndent(),
Owner resumed
|- Was stopped: ${preferencesStorage.wasApplicationStopped}
|- Need to open welcome screen: ${preferencesStorage.shouldOpenWelcomeScreenOnResume}
""".trimIndent(),
)
isStopped = false
preferencesStorage.wasApplicationStopped = false
start()
if (openWelcomeScreenWhenResumed) {
if (preferencesStorage.shouldOpenWelcomeScreenOnResume) {
store.dispatchOnMain(NavigationAction.PopBackTo(AppScreen.Welcome))
openWelcomeScreenWhenResumed = false
preferencesStorage.shouldOpenWelcomeScreenOnResume = false
}
}
override fun onStop(owner: LifecycleOwner) {
Timber.d("Owner stopped")
isStopped = true
preferencesStorage.wasApplicationStopped = true
}
override fun onDestroy(owner: LifecycleOwner) {
@ -103,13 +101,13 @@ internal class LockUserWalletsTimer(
Timber.d(
"""
Finished
|- App is stopped: $isStopped
|- App is stopped: ${preferencesStorage.wasApplicationStopped}
|- Millis passed: ${currentTime - startTime}
""".trimIndent(),
)
userWalletsListManager.lock()
if (isStopped) {
openWelcomeScreenWhenResumed = true
if (preferencesStorage.wasApplicationStopped) {
preferencesStorage.shouldOpenWelcomeScreenOnResume = true
} else {
store.dispatchOnMain(NavigationAction.PopBackTo(AppScreen.Welcome))
}

View file

@ -28,15 +28,17 @@ val navigationMiddleware: Middleware<AppState> = { _, state ->
)
}
is NavigationAction.PopBackTo -> {
when (val screen = action.screen) {
AppScreen.Home,
AppScreen.Welcome,
-> {
navState?.activity?.get()?.popBackTo(screen, inclusive = true)
store.dispatchOnMain(NavigationAction.NavigateTo(screen))
}
else -> {
navState?.activity?.get()?.popBackTo(screen, action.inclusive)
if (navState?.backStack?.lastOrNull() != action.screen) {
when (val screen = action.screen) {
AppScreen.Home,
AppScreen.Welcome,
-> {
navState?.activity?.get()?.popBackTo(screen, inclusive = true)
store.dispatchOnMain(NavigationAction.NavigateTo(screen))
}
else -> {
navState?.activity?.get()?.popBackTo(screen, action.inclusive)
}
}
}
}

View file

@ -20,6 +20,8 @@ private fun internalReduce(action: Action, state: AppState): NavigationState {
navState.copy(backStack = navState.backStack + navigationAction.screen)
}
is NavigationAction.PopBackTo -> {
if (navState.backStack.lastOrNull() == navigationAction.screen) return navState
val screen = navigationAction.screen ?: navState.activity?.get()?.getPreviousScreen()
val index = navState.backStack.lastIndexOf(screen) + 1
state.navigationState.copy(backStack = navState.backStack.subList(0, index))

View file

@ -51,6 +51,18 @@ class PreferencesStorage(applicationContext: Application) {
putBoolean(SAVE_ACCESS_CODES_KEY, value)
}
var wasApplicationStopped: Boolean
get() = preferences.getBoolean(APPLICATION_STOPPED_KEY, false)
set(value) = preferences.edit {
putBoolean(APPLICATION_STOPPED_KEY, value)
}
var shouldOpenWelcomeScreenOnResume: Boolean
get() = preferences.getBoolean(OPEN_WELCOME_ON_RESUME_KEY, false)
set(value) = preferences.edit {
putBoolean(OPEN_WELCOME_ON_RESUME_KEY, value)
}
fun getCountOfLaunches(): Int = preferences.getInt(APP_LAUNCH_COUNT_KEY, 1)
fun saveTwinsOnboardingShown() {
@ -74,6 +86,8 @@ class PreferencesStorage(applicationContext: Application) {
private const val SAVE_WALLET_DIALOG_SHOWN_KEY = "saveUserWalletShown"
private const val SAVE_USER_WALLETS_KEY = "saveUserWallets"
private const val SAVE_ACCESS_CODES_KEY = "saveAccessCodes"
private const val APPLICATION_STOPPED_KEY = "applicationStopped"
private const val OPEN_WELCOME_ON_RESUME_KEY = "openWelcomeOnResume"
}
}