Skip to content

Commit 7e8b60e

Browse files
committed
Handle lock timeout = zero correctly
1 parent 69bae91 commit 7e8b60e

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

app/src/main/java/com/aidinhut/simpletextcrypt/ui/screen/MainScreen.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ fun MainScreen(
8787
when (event) {
8888
Lifecycle.Event.ON_RESUME -> {
8989
// Check timeout and lock if expired (handles brief pauses)
90-
viewModel.checkLockTimeout(context)
90+
viewModel.lockIfTimedOut(context)
9191
}
9292
Lifecycle.Event.ON_STOP -> {
9393
// Record the time when app goes to background

app/src/main/java/com/aidinhut/simpletextcrypt/viewmodel/MainViewModel.kt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,26 @@ class MainViewModel : ViewModel() {
103103
* Checks if the lock timeout has expired. Called on lifecycle resume/pause events.
104104
* Returns true if the app should lock (navigate back to lock screen).
105105
*/
106-
fun checkLockTimeout(context: Context): Boolean {
106+
fun lockIfTimedOut(context: Context): Boolean {
107107
val timeout = SettingsManager.instance.getLockTimeout(context)
108108
val currentTime = System.currentTimeMillis() / 1000
109+
110+
// When Main Screen resumes, it calls this function, even after unlocking the screen. It would cause the
111+
// lock screen to re-appear immediately after unlock. So, if user unlocked less than a second ago, we ignore it.
112+
if (currentTime - lastActivity <= 1) {
113+
return false
114+
}
115+
116+
// If time out is zero, the screen would lock immediately.
117+
// Also, there's a bug that sometimes when the app opens, it's in a bad state: Settings
118+
// aren't loaded but the app is in the Main screen. This will workaround that (because
119+
// timeout is not loaded from the settings and it's zero.)
120+
if (timeout == 0) {
121+
_uiState.value = _uiState.value.copy(shouldLock = true)
122+
return true
123+
}
124+
125+
109126
if (timeout != 0 && currentTime - lastActivity >= timeout * 60L) {
110127
_uiState.value = _uiState.value.copy(shouldLock = true)
111128
return true

0 commit comments

Comments
 (0)