diff --git a/app/src/main/java/org/thoughtcrime/securesms/onboarding/OnboardingBackPressAlertDialog.kt b/app/src/main/java/org/thoughtcrime/securesms/onboarding/OnboardingBackPressAlertDialog.kt new file mode 100644 index 0000000000..3e6ac4a0c9 --- /dev/null +++ b/app/src/main/java/org/thoughtcrime/securesms/onboarding/OnboardingBackPressAlertDialog.kt @@ -0,0 +1,31 @@ +package org.thoughtcrime.securesms.onboarding + +import androidx.compose.runtime.Composable +import androidx.compose.ui.res.stringResource +import network.loki.messenger.R +import org.thoughtcrime.securesms.ui.AlertDialog +import org.thoughtcrime.securesms.ui.DialogButtonModel +import org.thoughtcrime.securesms.ui.GetString +import org.thoughtcrime.securesms.ui.color.LocalColors + +@Composable +fun OnboardingBackPressAlertDialog( + dismissDialog: () -> Unit, + quit: () -> Unit +) { + AlertDialog( + onDismissRequest = dismissDialog, + title = stringResource(R.string.warning), + text = stringResource(R.string.you_cannot_go_back_further_in_order_to_stop_loading_your_account_session_needs_to_quit), + buttons = listOf( + DialogButtonModel( + GetString(stringResource(R.string.quit)), + color = LocalColors.current.danger, + onClick = quit + ), + DialogButtonModel( + GetString(stringResource(R.string.cancel)) + ) + ) + ) +} diff --git a/app/src/main/java/org/thoughtcrime/securesms/onboarding/messagenotifications/MessageNotifications.kt b/app/src/main/java/org/thoughtcrime/securesms/onboarding/messagenotifications/MessageNotifications.kt index f609afd1f4..dcd3975c45 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/onboarding/messagenotifications/MessageNotifications.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/onboarding/messagenotifications/MessageNotifications.kt @@ -19,6 +19,7 @@ import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.PreviewParameter import androidx.compose.ui.unit.dp import network.loki.messenger.R +import org.thoughtcrime.securesms.onboarding.OnboardingBackPressAlertDialog import org.thoughtcrime.securesms.onboarding.messagenotifications.MessageNotificationsViewModel.UiState import org.thoughtcrime.securesms.onboarding.ui.ContinuePrimaryOutlineButton import org.thoughtcrime.securesms.ui.AlertDialog @@ -57,21 +58,7 @@ internal fun MessageNotificationsScreen( return } - if (state.showDialog) AlertDialog( - onDismissRequest = dismissDialog, - title = stringResource(R.string.warning), - text = stringResource(R.string.you_cannot_go_back_further_in_order_to_stop_loading_your_account_session_needs_to_quit), - buttons = listOf( - DialogButtonModel( - GetString(stringResource(R.string.quit)), - color = LocalColors.current.danger, - onClick = quit - ), - DialogButtonModel( - GetString(stringResource(R.string.cancel)) - ) - ) - ) + if (state.showDialog) OnboardingBackPressAlertDialog(dismissDialog, quit) Column { Spacer(Modifier.weight(1f)) diff --git a/app/src/main/java/org/thoughtcrime/securesms/onboarding/pickname/PickDisplayName.kt b/app/src/main/java/org/thoughtcrime/securesms/onboarding/pickname/PickDisplayName.kt index b6b4b017aa..0a3a50370a 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/onboarding/pickname/PickDisplayName.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/onboarding/pickname/PickDisplayName.kt @@ -15,6 +15,7 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource import androidx.compose.ui.tooling.preview.Preview import network.loki.messenger.R +import org.thoughtcrime.securesms.onboarding.OnboardingBackPressAlertDialog import org.thoughtcrime.securesms.onboarding.ui.ContinuePrimaryOutlineButton import org.thoughtcrime.securesms.ui.LocalDimensions import org.thoughtcrime.securesms.ui.PreviewTheme @@ -24,14 +25,23 @@ import org.thoughtcrime.securesms.ui.h4 @Preview @Composable -private fun PreviewDisplayName() { +private fun PreviewPickDisplayName() { PreviewTheme { - DisplayName(State()) + PickDisplayName(State()) } } @Composable -internal fun DisplayName(state: State, onChange: (String) -> Unit = {}, onContinue: () -> Unit = {}) { +internal fun PickDisplayName( + state: State, + onChange: (String) -> Unit = {}, + onContinue: () -> Unit = {}, + dismissDialog: () -> Unit = {}, + quit: () -> Unit = {} +) { + + if (state.showDialog) OnboardingBackPressAlertDialog(dismissDialog, quit) + Column( modifier = Modifier .fillMaxSize() diff --git a/app/src/main/java/org/thoughtcrime/securesms/onboarding/pickname/PickDisplayNameActivity.kt b/app/src/main/java/org/thoughtcrime/securesms/onboarding/pickname/PickDisplayNameActivity.kt index c727cfd111..da0c80fcb6 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/onboarding/pickname/PickDisplayNameActivity.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/onboarding/pickname/PickDisplayNameActivity.kt @@ -53,8 +53,19 @@ class PickDisplayNameActivity : BaseActionBarActivity() { @Composable private fun DisplayNameScreen(viewModel: PickDisplayNameViewModel) { - val state = viewModel.states.collectAsState() - DisplayName(state.value, viewModel::onChange) { viewModel.onContinue() } + PickDisplayName( + viewModel.states.collectAsState().value, + viewModel::onChange, + viewModel::onContinue, + viewModel::dismissDialog + ) { viewModel.dismissDialog(); finish() } + } + + override fun onBackPressed() { + if (viewModel.onBackPressed()) return + + @Suppress("DEPRECATION") + super.onBackPressed() } } diff --git a/app/src/main/java/org/thoughtcrime/securesms/onboarding/pickname/PickDisplayNameViewModel.kt b/app/src/main/java/org/thoughtcrime/securesms/onboarding/pickname/PickDisplayNameViewModel.kt index 7e59fb459a..99b697037d 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/onboarding/pickname/PickDisplayNameViewModel.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/onboarding/pickname/PickDisplayNameViewModel.kt @@ -16,13 +16,17 @@ import kotlinx.coroutines.launch import network.loki.messenger.R import org.session.libsession.utilities.SSKEnvironment.ProfileManagerProtocol.Companion.NAME_PADDED_LENGTH import org.session.libsession.utilities.TextSecurePreferences +import org.thoughtcrime.securesms.ApplicationContext import org.thoughtcrime.securesms.dependencies.ConfigFactory +import org.thoughtcrime.securesms.onboarding.messagenotifications.MessageNotificationsViewModel internal class PickDisplayNameViewModel( private val loadFailed: Boolean, private val prefs: TextSecurePreferences, private val configFactory: ConfigFactory ): ViewModel() { + private val isCreateAccount = !loadFailed + private val _states = MutableStateFlow(if (loadFailed) pickNewNameState() else State()) val states = _states.asStateFlow() @@ -63,6 +67,17 @@ internal class PickDisplayNameViewModel( } } + /** + * @return [true] if the back press was handled. + */ + fun onBackPressed(): Boolean = isCreateAccount.also { + if (it) _states.update { it.copy(showDialog = true) } + } + + fun dismissDialog() { + _states.update { it.copy(showDialog = false) } + } + @dagger.assisted.AssistedFactory interface AssistedFactory { fun create(loadFailed: Boolean): Factory @@ -84,6 +99,7 @@ internal class PickDisplayNameViewModel( data class State( @StringRes val title: Int = R.string.displayNamePick, @StringRes val description: Int = R.string.displayNameDescription, + val showDialog: Boolean = false, val isTextErrorColor: Boolean = false, @StringRes val error: Int? = null, val displayName: String = ""