From eb691eded9eb2c15fb872a596b107cc97d3cd3b2 Mon Sep 17 00:00:00 2001 From: andrew Date: Mon, 16 Oct 2023 11:35:14 +1030 Subject: [PATCH] Add alternate strings for load failed in PickDisplayNameActivity --- .../securesms/onboarding/LoadingActivity.kt | 13 +++--- .../securesms/onboarding/PNModeActivity.kt | 6 ++- .../pickname/PickDisplayNameActivity.kt | 21 +++++++-- .../pickname/PickDisplayNameViewModel.kt | 43 +++++++++++++------ app/src/main/res/values/strings.xml | 2 + 5 files changed, 63 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/onboarding/LoadingActivity.kt b/app/src/main/java/org/thoughtcrime/securesms/onboarding/LoadingActivity.kt index 7fcc1fb54a..6eb4226c07 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/onboarding/LoadingActivity.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/onboarding/LoadingActivity.kt @@ -24,10 +24,9 @@ import kotlinx.coroutines.launch import org.session.libsession.utilities.TextSecurePreferences import org.thoughtcrime.securesms.BaseActionBarActivity import org.thoughtcrime.securesms.dependencies.ConfigFactory -import org.thoughtcrime.securesms.onboarding.pickname.PickDisplayNameActivity +import org.thoughtcrime.securesms.onboarding.pickname.startPickDisplayNameActivity import org.thoughtcrime.securesms.ui.AppTheme import org.thoughtcrime.securesms.ui.ProgressArc -import org.thoughtcrime.securesms.util.push import javax.inject.Inject private const val EXTRA_MNEMONIC = "mnemonic" @@ -50,9 +49,13 @@ class LoadingActivity: BaseActionBarActivity() { private fun register(skipped: Boolean) { prefs.setLastConfigurationSyncTime(System.currentTimeMillis()) - Intent(this, if (skipped) PickDisplayNameActivity::class.java else PNModeActivity::class.java) - .apply { flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK } - .also(::push) + + val flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK + + when { + skipped -> startPickDisplayNameActivity(true, flags) + else -> startPNModeActivity(flags) + } } override fun onCreate(savedInstanceState: Bundle?) { diff --git a/app/src/main/java/org/thoughtcrime/securesms/onboarding/PNModeActivity.kt b/app/src/main/java/org/thoughtcrime/securesms/onboarding/PNModeActivity.kt index aec3c1cdff..e25f329799 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/onboarding/PNModeActivity.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/onboarding/PNModeActivity.kt @@ -180,6 +180,8 @@ class PNModeActivity : BaseActionBarActivity() { // endregion } -fun Context.startPNModeActivity() { - Intent(this, PNModeActivity::class.java).also(::startActivity) +fun Context.startPNModeActivity(flags: Int = 0) { + Intent(this, PNModeActivity::class.java) + .also { it.flags = flags } + .also(::startActivity) } 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 2db88147f0..44f913f2df 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 @@ -32,6 +32,8 @@ import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.launch import network.loki.messenger.R import org.thoughtcrime.securesms.BaseActionBarActivity +import org.thoughtcrime.securesms.conversation.v2.ConversationActivityV2 +import org.thoughtcrime.securesms.conversation.v2.ConversationViewModel import org.thoughtcrime.securesms.onboarding.startPNModeActivity import org.thoughtcrime.securesms.ui.AppTheme import org.thoughtcrime.securesms.ui.OutlineButton @@ -40,10 +42,20 @@ import org.thoughtcrime.securesms.ui.base import org.thoughtcrime.securesms.ui.baseBold import org.thoughtcrime.securesms.ui.colorDestructive import org.thoughtcrime.securesms.util.setUpActionBarSessionLogo +import javax.inject.Inject + +private const val EXTRA_PICK_NEW_NAME = "extra_pick_new_name" @AndroidEntryPoint class PickDisplayNameActivity : BaseActionBarActivity() { - private val viewModel: PickDisplayNameViewModel by viewModels() + + @Inject + lateinit var viewModelFactory: PickDisplayNameViewModel.AssistedFactory + + private val viewModel: PickDisplayNameViewModel by viewModels { + val pickNewName = intent.getBooleanExtra(EXTRA_PICK_NEW_NAME, false) + viewModelFactory.create(pickNewName) + } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -132,6 +144,9 @@ class PickDisplayNameActivity : BaseActionBarActivity() { } } -fun Context.startPickDisplayNameActivity() { - Intent(this, PickDisplayNameActivity::class.java).also(::startActivity) +fun Context.startPickDisplayNameActivity(failedToLoad: Boolean = false, flags: Int = 0) { + Intent(this, PickDisplayNameActivity::class.java) + .apply { putExtra(EXTRA_PICK_NEW_NAME, failedToLoad) } + .also { it.flags = flags } + .also(::startActivity) } 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 49939c257f..9ede264ab7 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 @@ -3,8 +3,10 @@ package org.thoughtcrime.securesms.onboarding.pickname import android.content.Context import androidx.annotation.StringRes import androidx.lifecycle.ViewModel +import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.viewModelScope -import dagger.hilt.android.lifecycle.HiltViewModel +import dagger.assisted.Assisted +import dagger.assisted.AssistedInject import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow @@ -20,22 +22,18 @@ import org.session.libsignal.utilities.KeyHelper import org.session.libsignal.utilities.hexEncodedPublicKey import org.thoughtcrime.securesms.crypto.KeyPairUtilities import org.thoughtcrime.securesms.dependencies.ConfigFactory -import javax.inject.Inject -@HiltViewModel -class PickDisplayNameViewModel @Inject constructor( - private val prefs: TextSecurePreferences +class PickDisplayNameViewModel( + pickNewName: Boolean, + private val prefs: TextSecurePreferences, + private val configFactory: ConfigFactory ): ViewModel() { - - private val state = MutableStateFlow(State()) + private val state = MutableStateFlow(if (pickNewName) pickNewNameState() else State()) val stateFlow = state.asStateFlow() private val event = Channel() val eventFlow = event.receiveAsFlow() - @Inject - lateinit var configFactory: ConfigFactory - private val database: LokiAPIDatabaseProtocol get() = SnodeModule.shared.storage @@ -75,13 +73,29 @@ class PickDisplayNameViewModel @Inject constructor( } fun onChange(value: String) { - state.update { - it.copy( + state.update { state -> state.copy( displayName = value, error = value.takeIf { it.length > NAME_PADDED_LENGTH }?.let { R.string.activity_display_name_display_name_too_long_error } ) } } + + @dagger.assisted.AssistedFactory + interface AssistedFactory { + fun create(pickNewName: Boolean): Factory + } + + @Suppress("UNCHECKED_CAST") + class Factory @AssistedInject constructor( + @Assisted private val pickNewName: Boolean, + private val prefs: TextSecurePreferences, + private val configFactory: ConfigFactory + ) : ViewModelProvider.Factory { + + override fun create(modelClass: Class): T { + return PickDisplayNameViewModel(pickNewName, prefs, configFactory) as T + } + } } data class State( @@ -91,6 +105,11 @@ data class State( val displayName: String = "" ) +fun pickNewNameState() = State( + title = R.string.activity_display_name_pick_a_new_display_name, + description = R.string.activity_display_name_unable_to_load_new_name_explanation +) + sealed interface Event { object DONE: Event } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b84fb8aef2..afc37191f7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -728,7 +728,9 @@ Enter the recovery phrase that was given to you when you signed up to restore your account. Enter your recovery phrase Pick your display name + Pick a new display name It can be your real name, an alias, or anything else you like — and you can change it any time. + We were unable to load your display name. Please enter a new display name to continue. Enter a display name Please pick a display name Please pick a shorter display name