Skip to content

Commit

Permalink
Fix app lock issue (#958)
Browse files Browse the repository at this point in the history
* Fix app lock issue

Fixes #954

Add app lock feature when the app is not in the foreground.

* **Settings Fragment**
  - Add a new setting to specify the time duration after which the app will lock when not in the foreground.
  - Update the UI to include this new setting.

* **Dashboard Activity**
  - Implement logic to lock the app when it goes to the background.
  - Use the new setting to determine the time duration after which the app will lock.
  - Add handlers to manage the lock timeout when the app is paused and resumed.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/yogeshpaliyal/KeyPass/issues/954?shareId=XXXX-XXXX-XXXX-XXXX).

* feat: minor improvements

* feat: minor improvements

* feat: spotless fixes
  • Loading branch information
yogeshpaliyal authored Sep 14, 2024
1 parent c1822fd commit 7082edd
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,17 @@ fun AuthScreen(state: AuthState) {
mutableStateOf<Int?>(null)
}

val (passwordHint, setPasswordHint) = remember(state) {
mutableStateOf("")
}

BackHandler(state is AuthState.ConfirmPassword) {
dispatchAction(NavigationAction(AuthState.CreatePassword, true))
}

LaunchedEffect(key1 = userSettings.keyPassPassword, block = {
if (userSettings.isDefault) {
return@LaunchedEffect
}
val mPassword = userSettings.keyPassPassword
if (mPassword == null) {
dispatchAction(NavigationAction(AuthState.CreatePassword, true))
} else {
dispatchAction(NavigationAction(AuthState.Login, true))
}
})

Expand Down Expand Up @@ -107,7 +104,7 @@ fun AuthScreen(state: AuthState) {
hint = if (state is AuthState.Login && userSettings.passwordHint != null) userSettings.passwordHint else null
)

ButtonBar(state, password, setPasswordError, passwordHint) {
ButtonBar(state, password, setPasswordError) {
dispatchAction(it)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import com.yogeshpaliyal.common.utils.setKeyPassPassword
import com.yogeshpaliyal.common.utils.setPasswordHint
import com.yogeshpaliyal.keypass.R
import com.yogeshpaliyal.keypass.ui.nav.LocalUserSettings
import com.yogeshpaliyal.keypass.ui.redux.KeyPassRedux
import com.yogeshpaliyal.keypass.ui.redux.actions.Action
import com.yogeshpaliyal.keypass.ui.redux.actions.GoBackAction
import com.yogeshpaliyal.keypass.ui.redux.actions.NavigationAction
import com.yogeshpaliyal.keypass.ui.redux.states.AuthState
import com.yogeshpaliyal.keypass.ui.redux.states.HomeState
Expand All @@ -28,8 +30,7 @@ fun ButtonBar(
state: AuthState,
password: String,
setPasswordError: (Int?) -> Unit,
passwordHint: String, // New parameter for password hint
dispatchAction: (NavigationAction) -> Unit
dispatchAction: (Action) -> Unit
) {
val coroutineScope = rememberCoroutineScope()
val context = LocalContext.current
Expand Down Expand Up @@ -71,7 +72,6 @@ fun ButtonBar(
if (state.password == password) {
coroutineScope.launch {
context.setKeyPassPassword(password)
context.setPasswordHint(passwordHint) // Save the password hint
dispatchAction(NavigationAction(HomeState(), true))
}
} else {
Expand All @@ -83,7 +83,9 @@ fun ButtonBar(
coroutineScope.launch {
val savedPassword = userSettings.keyPassPassword
if (savedPassword == password) {
dispatchAction(NavigationAction(HomeState(), true))
KeyPassRedux.getLastScreen()?.let {
dispatchAction(GoBackAction)
} ?: dispatchAction(NavigationAction(HomeState(), true))
} else {
setPasswordError(R.string.incorrect_password)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import androidx.compose.runtime.compositionLocalOf
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.compose.LifecycleEventEffect
import com.yogeshpaliyal.common.data.UserSettings
import com.yogeshpaliyal.common.utils.getUserSettings
import com.yogeshpaliyal.common.utils.getUserSettingsFlow
Expand All @@ -38,6 +40,7 @@ import com.yogeshpaliyal.keypass.ui.nav.components.KeyPassBottomBar
import com.yogeshpaliyal.keypass.ui.passwordHint.PasswordHintScreen
import com.yogeshpaliyal.keypass.ui.redux.KeyPassRedux
import com.yogeshpaliyal.keypass.ui.redux.actions.GoBackAction
import com.yogeshpaliyal.keypass.ui.redux.actions.NavigationAction
import com.yogeshpaliyal.keypass.ui.redux.actions.UpdateContextAction
import com.yogeshpaliyal.keypass.ui.redux.states.AboutState
import com.yogeshpaliyal.keypass.ui.redux.states.AccountDetailState
Expand Down Expand Up @@ -74,7 +77,7 @@ class DashboardComposeActivity : AppCompatActivity() {
}

setContent {
val localUserSettings by getUserSettingsFlow().collectAsState(initial = UserSettings())
val localUserSettings by getUserSettingsFlow().collectAsState(initial = UserSettings(true))

CompositionLocalProvider(LocalUserSettings provides localUserSettings) {
KeyPassTheme {
Expand All @@ -90,15 +93,16 @@ class DashboardComposeActivity : AppCompatActivity() {
val buildConfigVersion = BuildConfig.VERSION_CODE
val currentAppVersion = userSettings.currentAppVersion
if (buildConfigVersion != currentAppVersion) {
applicationContext.setUserSettings(userSettings.copy(lastAppVersion = currentAppVersion, currentAppVersion = buildConfigVersion))
applicationContext.setUserSettings(
userSettings.copy(
lastAppVersion = currentAppVersion,
currentAppVersion = buildConfigVersion
)
)
}
})
}
}

override fun onDestroy() {
super.onDestroy()
}
}

@Composable
Expand All @@ -112,6 +116,11 @@ fun Dashboard() {
dispatch(GoBackAction)
}

// Call this like any other SideEffect in your composable
LifecycleEventEffect(Lifecycle.Event.ON_PAUSE) {
dispatch(NavigationAction(AuthState.Login))
}

LaunchedEffect(key1 = systemBackPress, block = {
if (systemBackPress) {
(context as? ComponentActivity)?.finishAffinity()
Expand Down Expand Up @@ -174,7 +183,7 @@ fun CurrentPage() {
is BackupImporterState -> BackupImporter(state = it)
is AboutState -> AboutScreen()
is PasswordGeneratorState -> GeneratePasswordScreen()
ChangeAppHintState -> PasswordHintScreen()
is ChangeAppHintState -> PasswordHintScreen()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ object KeyPassRedux {

private var arrPages = mutableListOf<ScreenState>()

fun getLastScreen() = arrPages.lastOrNull()

private val reducer: Reducer<KeyPassState> = { state, action ->
when (action) {
is NavigationAction -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const val DEFAULT_PASSWORD_LENGTH = 10f
@Keep
@Serializable
data class UserSettings(
val isDefault: Boolean = false,
val keyPassPassword: String? = null,
val dbPassword: String? = null,
@Deprecated("Use passwordConfig instead")
Expand Down

0 comments on commit 7082edd

Please sign in to comment.