Prevent ISE during code entry in registration.

This commit is contained in:
Nicholas Tinsley
2024-08-08 11:47:56 -04:00
committed by mtang-signal
parent d45acd0e24
commit e83c6dc7c2
3 changed files with 64 additions and 11 deletions

View File

@@ -11,6 +11,7 @@ import android.view.View
import android.widget.Toast
import androidx.activity.OnBackPressedCallback
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.fragment.findNavController
import com.google.android.material.dialog.MaterialAlertDialogBuilder
@@ -47,6 +48,7 @@ class EnterCodeFragment : LoggingFragment(R.layout.fragment_registration_enter_c
private val TAG = Log.tag(EnterCodeFragment::class.java)
private val sharedViewModel by activityViewModels<RegistrationViewModel>()
private val fragmentViewModel by viewModels<EnterCodeViewModel>()
private val bottomSheet = ContactSupportBottomSheetFragment()
private val binding: FragmentRegistrationEnterCodeBinding by ViewBinderDelegate(FragmentRegistrationEnterCodeBinding::bind)
@@ -130,6 +132,20 @@ class EnterCodeFragment : LoggingFragment(R.layout.fragment_registration_enter_c
binding.keyboard.displayKeyboard()
}
}
fragmentViewModel.uiState.observe(viewLifecycleOwner) {
if (it.resetRequiredAfterFailure) {
binding.callMeCountDown.visibility = View.VISIBLE
binding.resendSmsCountDown.visibility = View.VISIBLE
binding.wrongNumber.visibility = View.VISIBLE
binding.code.clear()
binding.keyboard.displayKeyboard()
fragmentViewModel.allViewsResetCompleted()
} else if (it.showKeyboard) {
binding.keyboard.displayKeyboard()
fragmentViewModel.keyboardShown()
}
}
}
override fun onResume() {
@@ -190,11 +206,7 @@ class EnterCodeFragment : LoggingFragment(R.layout.fragment_registration_enter_c
setTitle(R.string.RegistrationActivity_too_many_attempts)
setMessage(R.string.RegistrationActivity_you_have_made_too_many_attempts_please_try_again_later)
setPositiveButton(android.R.string.ok) { _: DialogInterface?, _: Int ->
binding.callMeCountDown.visibility = View.VISIBLE
binding.resendSmsCountDown.visibility = View.VISIBLE
binding.wrongNumber.visibility = View.VISIBLE
binding.code.clear()
binding.keyboard.displayKeyboard()
fragmentViewModel.resetAllViews()
}
show()
}
@@ -210,11 +222,7 @@ class EnterCodeFragment : LoggingFragment(R.layout.fragment_registration_enter_c
binding.keyboard.displayFailure().addListener(object : AssertedSuccessListener<Boolean?>() {
override fun onSuccess(result: Boolean?) {
binding.callMeCountDown.visibility = View.VISIBLE
binding.resendSmsCountDown.visibility = View.VISIBLE
binding.wrongNumber.visibility = View.VISIBLE
binding.code.clear()
binding.keyboard.displayKeyboard()
fragmentViewModel.resetAllViews()
}
})
}
@@ -229,7 +237,7 @@ class EnterCodeFragment : LoggingFragment(R.layout.fragment_registration_enter_c
setTitle(it)
}
setMessage(getString(R.string.RegistrationActivity_error_connecting_to_service))
setPositiveButton(android.R.string.ok) { _, _ -> binding.keyboard.displayKeyboard() }
setPositiveButton(android.R.string.ok) { _, _ -> fragmentViewModel.showKeyboard() }
show()
}
}

View File

@@ -0,0 +1,8 @@
/*
* Copyright 2024 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
package org.thoughtcrime.securesms.registration.ui.entercode
data class EnterCodeState(val resetRequiredAfterFailure: Boolean = false, val showKeyboard: Boolean = false)

View File

@@ -0,0 +1,37 @@
/*
* Copyright 2024 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
package org.thoughtcrime.securesms.registration.ui.entercode
import androidx.lifecycle.ViewModel
import androidx.lifecycle.asLiveData
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.update
class EnterCodeViewModel : ViewModel() {
private val store = MutableStateFlow(EnterCodeState())
val uiState = store.asLiveData()
fun resetAllViews() {
store.update { it.copy(resetRequiredAfterFailure = true) }
}
fun allViewsResetCompleted() {
store.update {
it.copy(
resetRequiredAfterFailure = false,
showKeyboard = false
)
}
}
fun showKeyboard() {
store.update { it.copy(showKeyboard = true) }
}
fun keyboardShown() {
store.update { it.copy(showKeyboard = false) }
}
}