Add done button for when creating new PINs (#742)

* Add done button for when creating new PINs

* Cleanup
This commit is contained in:
Luna712 2023-11-02 17:28:25 -06:00 committed by GitHub
parent 5b0cbbf09f
commit a6786aaf98
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,18 +1,17 @@
package com.lagradost.cloudstream3.ui.account package com.lagradost.cloudstream3.ui.account
import android.content.Context import android.content.Context
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View
import android.view.inputmethod.EditorInfo import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import android.widget.TextView import android.widget.TextView
import androidx.annotation.StringRes import androidx.annotation.StringRes
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.core.view.isVisible
import androidx.core.widget.doOnTextChanged
import com.lagradost.cloudstream3.R import com.lagradost.cloudstream3.R
import com.lagradost.cloudstream3.databinding.LockPinDialogBinding import com.lagradost.cloudstream3.databinding.LockPinDialogBinding
import com.lagradost.cloudstream3.utils.UIHelper.dismissSafe import com.lagradost.cloudstream3.utils.UIHelper.dismissSafe
import com.lagradost.cloudstream3.utils.UIHelper.showInputMethod
object AccountDialog { object AccountDialog {
// TODO add account creation dialog to allow creating accounts directly from AccountSelectActivity // TODO add account creation dialog to allow creating accounts directly from AccountSelectActivity
@ -21,14 +20,18 @@ object AccountDialog {
context: Context, context: Context,
currentPin: String?, currentPin: String?,
editAccount: Boolean, editAccount: Boolean,
errorText: String? = null,
callback: (String?) -> Unit callback: (String?) -> Unit
) { ) {
fun TextView.visibleWithText(@StringRes textRes: Int) { fun TextView.visibleWithText(@StringRes textRes: Int) {
visibility = View.VISIBLE isVisible = true
setText(textRes) setText(textRes)
} }
fun View.isVisible() = visibility == View.VISIBLE fun TextView.visibleWithText(text: String?) {
isVisible = true
setText(text)
}
val binding = LockPinDialogBinding.inflate(LayoutInflater.from(context)) val binding = LockPinDialogBinding.inflate(LayoutInflater.from(context))
@ -38,7 +41,9 @@ object AccountDialog {
val titleRes = if (isEditPin) R.string.enter_current_pin else R.string.enter_pin val titleRes = if (isEditPin) R.string.enter_current_pin else R.string.enter_pin
val dialog = AlertDialog.Builder(context, R.style.AlertDialogCustom) var isPinValid = false
val builder = AlertDialog.Builder(context, R.style.AlertDialogCustom)
.setView(binding.root) .setView(binding.root)
.setTitle(titleRes) .setTitle(titleRes)
.setNegativeButton(R.string.cancel) { _, _ -> .setNegativeButton(R.string.cancel) { _, _ ->
@ -48,46 +53,59 @@ object AccountDialog {
callback.invoke(null) callback.invoke(null)
} }
.setOnDismissListener { .setOnDismissListener {
if (binding.pinEditTextError.isVisible()) { if (!isPinValid) {
callback.invoke(null) callback.invoke(null)
} }
} }
.create()
var isPinValid = false if (isNewPin) {
if (errorText != null) binding.pinEditTextError.visibleWithText(errorText)
binding.pinEditText.addTextChangedListener(object : TextWatcher { builder.setPositiveButton(R.string.setup_done) { _, _ ->
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {} if (!isPinValid) {
// If the done button is pressed and there is an error,
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { // ask again, and mention the error that caused this.
val enteredPin = s.toString() showPinInputDialog(
val isEnteredPinValid = enteredPin.length == 4 context = binding.root.context,
currentPin = null,
if (isEnteredPinValid) { editAccount = true,
if (isPinSet) { errorText = binding.pinEditTextError.text.toString(),
if (enteredPin != currentPin) { callback = callback
binding.pinEditTextError.visibleWithText(R.string.pin_error_incorrect) )
binding.pinEditText.text = null } else {
isPinValid = false val enteredPin = binding.pinEditText.text.toString()
} else { callback.invoke(enteredPin)
binding.pinEditTextError.visibility = View.GONE
isPinValid = true
callback.invoke(enteredPin)
dialog.dismissSafe()
}
} else {
binding.pinEditTextError.visibility = View.GONE
isPinValid = true
}
} else if (isNewPin) {
binding.pinEditTextError.visibleWithText(R.string.pin_error_length)
isPinValid = false
} }
} }
}
override fun afterTextChanged(s: Editable?) {} val dialog = builder.create()
})
binding.pinEditText.doOnTextChanged { text, _, _, _ ->
val enteredPin = text.toString()
val isEnteredPinValid = enteredPin.length == 4
if (isEnteredPinValid) {
if (isPinSet) {
if (enteredPin != currentPin) {
binding.pinEditTextError.visibleWithText(R.string.pin_error_incorrect)
binding.pinEditText.text = null
isPinValid = false
} else {
binding.pinEditTextError.isVisible = false
isPinValid = true
callback.invoke(enteredPin)
dialog.dismissSafe()
}
} else {
binding.pinEditTextError.isVisible = false
isPinValid = true
}
} else if (isNewPin) {
binding.pinEditTextError.visibleWithText(R.string.pin_error_length)
isPinValid = false
}
}
// Detect IME_ACTION_DONE // Detect IME_ACTION_DONE
binding.pinEditText.setOnEditorActionListener { _, actionId, _ -> binding.pinEditText.setOnEditorActionListener { _, actionId, _ ->
@ -108,8 +126,7 @@ object AccountDialog {
// Auto focus on PIN input and show keyboard // Auto focus on PIN input and show keyboard
binding.pinEditText.requestFocus() binding.pinEditText.requestFocus()
binding.pinEditText.postDelayed({ binding.pinEditText.postDelayed({
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager showInputMethod(binding.pinEditText)
imm.showSoftInput(binding.pinEditText, InputMethodManager.SHOW_IMPLICIT)
}, 200) }, 200)
} }
} }