diff --git a/app/src/main/java/com/yogeshpaliyal/keypass/ui/auth/AuthScreen.kt b/app/src/main/java/com/yogeshpaliyal/keypass/ui/auth/AuthScreen.kt index a4476f22..ce1838fd 100644 --- a/app/src/main/java/com/yogeshpaliyal/keypass/ui/auth/AuthScreen.kt +++ b/app/src/main/java/com/yogeshpaliyal/keypass/ui/auth/AuthScreen.kt @@ -48,20 +48,17 @@ fun AuthScreen(state: AuthState) { mutableStateOf(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)) } }) @@ -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) } } diff --git a/app/src/main/java/com/yogeshpaliyal/keypass/ui/auth/components/ButtonBar.kt b/app/src/main/java/com/yogeshpaliyal/keypass/ui/auth/components/ButtonBar.kt index dc0be4c4..cc54ed9e 100644 --- a/app/src/main/java/com/yogeshpaliyal/keypass/ui/auth/components/ButtonBar.kt +++ b/app/src/main/java/com/yogeshpaliyal/keypass/ui/auth/components/ButtonBar.kt @@ -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 @@ -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 @@ -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 { @@ -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) } diff --git a/app/src/main/java/com/yogeshpaliyal/keypass/ui/nav/DashboardComposeActivity.kt b/app/src/main/java/com/yogeshpaliyal/keypass/ui/nav/DashboardComposeActivity.kt index 1bac5f09..ab367954 100644 --- a/app/src/main/java/com/yogeshpaliyal/keypass/ui/nav/DashboardComposeActivity.kt +++ b/app/src/main/java/com/yogeshpaliyal/keypass/ui/nav/DashboardComposeActivity.kt @@ -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 @@ -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 @@ -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 { @@ -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 @@ -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() @@ -174,7 +183,7 @@ fun CurrentPage() { is BackupImporterState -> BackupImporter(state = it) is AboutState -> AboutScreen() is PasswordGeneratorState -> GeneratePasswordScreen() - ChangeAppHintState -> PasswordHintScreen() + is ChangeAppHintState -> PasswordHintScreen() } } } diff --git a/app/src/main/java/com/yogeshpaliyal/keypass/ui/redux/KeyPassRedux.kt b/app/src/main/java/com/yogeshpaliyal/keypass/ui/redux/KeyPassRedux.kt index 1cdf184c..cb85eb53 100644 --- a/app/src/main/java/com/yogeshpaliyal/keypass/ui/redux/KeyPassRedux.kt +++ b/app/src/main/java/com/yogeshpaliyal/keypass/ui/redux/KeyPassRedux.kt @@ -26,6 +26,8 @@ object KeyPassRedux { private var arrPages = mutableListOf() + fun getLastScreen() = arrPages.lastOrNull() + private val reducer: Reducer = { state, action -> when (action) { is NavigationAction -> { diff --git a/common/src/main/java/com/yogeshpaliyal/common/data/UserSettings.kt b/common/src/main/java/com/yogeshpaliyal/common/data/UserSettings.kt index 32e74926..ae6ecdb9 100644 --- a/common/src/main/java/com/yogeshpaliyal/common/data/UserSettings.kt +++ b/common/src/main/java/com/yogeshpaliyal/common/data/UserSettings.kt @@ -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")